Use shell script to capture system CPU, memory, disk, network information _linux Shell

Source: Internet
Author: User
Tags sleep cpu usage

First, CPU information collection


1). Collect CPU Usage rate

Acquisition algorithm: collecting and calculating CPU total utilization or single kernel utilization through/proc/stat file. Taking cpu0 as an example, the algorithm is as follows:
1. Cat/proc/stat | grep ' cpu0 ' gets cpu0 information
2. Cputotal1=user+nice+system+idle+iowait+irq+softirq
3. Cpuused1=user+nice+system+irq+softirq
4. Sleep 30 seconds
5. Cat/proc/stat again | grep ' cpu0 ' to get CPU information
6. Cputotal2=user+nice+system+idle+iowait+irq+softirq
7. Cpuused2=user+nice+system+irq+softirq
8. Single-core utilization of cpu0 in 30 seconds: (cpuused2–cpuused1) * (CPUTOTAL2–CPUTOTAL1)
It is equivalent to using the TOP–D 30 command to add user, nice, system, IRQ, SOFTIRQ five usage rates.
Shell code:

Copy Code code as follows:
A= (' cat/proc/stat | grep-e "cpu\b" | awk-v total=0 ' {$1= ' "; for (i=2;i<=nf;i++) {total+= $i};used=$2+$3+$4+$7+$8}END{p Rint total,used} "")
Sleep 30
b= (' cat/proc/stat | grep-e "cpu\b" | awk-v total=0 ' {$1= ' "; for (i=2;i<=nf;i++) {total+= $i};used=$2+$3+$4+$7+$8}END{p Rint total,used} "")
Cpu_usage= ((${b[1]}-${a[1]}) *100/(${b[0]}-${a[0]}))

2). Collect CPU Load

Acquisition algorithm: Read/PROC/LOADAVG get the machine's 1/5/15 minute average load, and then multiply by 100.
Shell code:

Copy Code code as follows:
Cpuload= (' cat/proc/loadavg | awk ' {print $1,$2,$3} ')
Load1=${cpuload[0]}
LOAD5=${CPULOAD[1]}
LOAD15=${CPULOAD[2]}

Second, memory acquisition

1). Application uses memory
Acquisition algorithm: Read/proc/meminfo file, (memtotal–memfree–buffers–cached)/1024 to get the number of applications using memory.
Shell code:

Copy Code code as follows:
awk '/memtotal/{total=$2}/memfree/{free=$2}/buffers/{buffers=$2}/^cached/{cached=$2}end{print ( total-free-buffers-cached)/1024} '/proc/meminfo

2). Amount of Mem used
Acquisition algorithm: Read/proc/meminfo file, Memtotal–memfree get mem usage.
Shell code:
Copy Code code as follows:
awk '/memtotal/{total=$2}/memfree/{free=$2}end{print (total-free)/1024} '/proc/meminfo

3). Swap usage Size
Acquisition algorithm: Through/proc/meminfo file, Swaptotal–swapfree get swap use size.
Shell code:
Copy Code code as follows:
awk '/swaptotal/{total=$2}/swapfree/{free=$2}end{print (total-free)/1024} '/proc/meminfo

Third, disk information acquisition (diskette IO)

1. In: The average amount of data from the hard disk to the physical memory per second
Acquisition algorithm: Read the/proc/vmstat file in the last 240 seconds of the pgpgin increment, the pgpgin increment divided by 240 to get the average increment per second.
Equivalent to the output of the Vmstat 240 command bi column.
Shell code:

Copy Code code as follows:
A= ' awk '/pgpgin/{print $} '/proc/vmstat '
Sleep 240
b= ' awk '/pgpgin/{print $} '/proc/vmstat '
Ioin=$ (((b-a)/240))

2. Out: Average data from physical memory to hard disk per second

Acquisition algorithm: Read the/proc/vmstat file in the last 240 seconds of the pgpgout increment, the pgpgout increment divided by 240 to get the average increment per second.
Equivalent to the output of a column of Vmstat 240 command bo.
Shell code:

Copy Code code as follows:
A= ' awk '/pgpgout/{print $} '/proc/vmstat '
Sleep 240
b= ' awk '/pgpgout/{print $} '/proc/vmstat '
Ioout=$ (((b-a)/240))

Four, the network

1). Flow rate
Take http://www.jb51.net/as an example, Eth0 is the intranet, eth1 extranet, get 60 seconds of traffic.
Average traffic per second of the machine card
Acquisition algorithm: Read the/proc/net/dev file, the number of bytes sent and received within 60 seconds (KB), then multiplied by 8, then divided by 60, to get the average flow per second.
Shell code:

Copy Code code as follows:
Traffic_be= (' awk ' begin{ors= ' "}/eth0/{print $2,$10}/eth1/{print $2,$10} '/proc/net/dev ')
Sleep 60
traffic_af= (' awk ' begin{ors= ' "}/eth0/{print $2,$10}/eth1/{print $2,$10} '/proc/net/dev ')
Eth0_in=$ (((${traffic_af[0]}-${traffic_be[0]})/60)
Eth0_out=$ (((${traffic_af[1]}-${traffic_be[1]})/60)
Eth1_in=$ (((${traffic_af[2]}-${traffic_be[2]})/60)
Eth1_out=$ (((${traffic_af[3]}-${traffic_be[3]})/60)

2). Package Volume
Average number of packets per second of the machine card
Acquisition algorithm: Read the/proc/net/dev file, get 60 seconds to send and receive packets, and then divided by 60, to get the average packet amount per second.
Shell code:
Copy Code code as follows:
Packet_be= (' awk ' begin{ors= ' "}/eth0/{print $3,$11}/eth1/{print $3,$11} '/proc/net/dev ')
Sleep 60
packet_af= (' awk ' begin{ors= ' "}/eth0/{print $3,$11}/eth1/{print $3,$11} '/proc/net/dev ')
Eth0_in=$ (((${packet_af[0]}-${packet_be[0]})/60)
eth0_out=$ ((${packet_af[1]}-${packet_be[1]})/60)
eth1_in=$ ((${packet_af[2]}-${packet_be[2]})/60)
eth1_out=$ ((${packet_af[3]}-${packet_be[3]})/60)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.