A lot of friends do not know how to use shell scripts to collect information about the Linux system, including CPU, memory, disk, network and other information, here the script to do a small part of the explanation, we come together to see it.
First, CPU information collection
1), Collect CPU utilization
Acquisition algorithm: Collects and calculates the total CPU utilization or single core utilization through the/proc/stat file. Take cpu0 as an example, the algorithm is as follows:
1,cat/proc/stat | grep ' cpu0 ' gets cpu0 's message 2,cputotal1=user+nice+system+idle+iowait+irq+softirq3,cpuused1=user+nice+system+irq+ Softirq4,sleep 30 sec 5, Cat/proc/stat again | grep ' cpu0 ' gets CPU information 6,cputotal2=user+nice+system+idle+iowait+irq+softirq7,cpuused2=user+nice+system+irq+softirq8 , get cpu0 Single-core utilization within 30 seconds: (cpuused2–cpuused1) * (CPUTOTAL2–CPUTOTAL1)
The equivalent of using the TOP–D 30 command to add the usage of the user, nice, system, IRQ, SOFTIRQ five items.
Shell code:
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{print total,used}'') Sleep -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{print total,used}'') Cpu_usage= (((${b[1]}-${a[1]})* -/(${b[0]}-${a[0]})))
2), Collect CPU load
Acquisition algorithm: Read/PROC/LOADAVG get the 1/5/15 minute average load of the machine, multiplied by 100.
Shell code:
' {print $1,$2,$3} ' ') load1=${cpuload[0]}load5=${cpuload[1]}load15=${cpuload[ 2]}
Second, memory acquisition
1). Application uses memory
Acquisition algorithm: reads the/proc/meminfo file, (memtotal–memfree–buffers–cached)/1024 Gets the amount of memory used by the application.
Shell code:
' /memtotal/{total=$2}/memfree/{free=$2}/buffers/{buffers=$2}/^cached/{cached=$2}end{print ( total-free-buffers-cached)/1024}' /proc/meminfo
2). MEM Usage
Acquisition algorithm: Read/proc/meminfo file, Memtotal–memfree get mem usage.
Shell code:
' /memtotal/{total=$2}/memfree/{free=$2}end{print (total-free)/1024} ' /proc/meminfo
3). Swap use size
Acquisition algorithm: Through the/proc/meminfo file, Swaptotal–swapfree get swap use size.
Shell code:
' /swaptotal/{total=$2}/swapfree/{free=$2}end{print (total-free)/1024} ' /proc/meminfo
Third, disk information acquisition (disks io)
1, in: The average amount of data per second read from the hard disk to the physical memory
Acquisition algorithm: Read the/proc/vmstat file to derive the increment of Pgpgin in the last 240 seconds, and divide the increment of pgpgin by 240 to get the average increment per second.
Equivalent to the output of the Vmstat 240 command bi column.
Shell code:
' /pgpgin/{print $} ' /proc/b'/pgpgin/{print$' /proc/ Vmstat ' Ioin=$ (((b-a)/+))
2, out: The average amount of data written from physical memory to hard disk per second
Acquisition algorithm: Read the/proc/vmstat file to derive the increment of pgpgout in the last 240 seconds, and divide the increment of pgpgout by 240 to get the average increment per second.
Equivalent to the output of the Vmstat 240 command bo column.
Shell code:
' /pgpgout/{print $} ' /proc/b'/pgpgout/{print$' /proc/vmstat ' Ioout=$ (((b-a)/)
Iv. Collection of network traffic
1). Flow rate
Take http://www.jquerycn.cn/as an example, Eth0 is the intranet, eth1 external network, get 60 seconds of traffic.
Average flow per second for machine cards
Acquisition algorithm: Read the/proc/net/dev file, get the number of bytes sent and received within 60 seconds (KB), and then multiply by 8, divided by 60, to get the average traffic per second.
Shell code:
Traffic_be= (' awk'begin{ors= ""}/eth0/{print $2,$10}/eth1/{print $2,$10}'/proc/net/Dev ') sleep -Traffic_af= (' awk'begin{ors= ""}/eth0/{print $2,$10}/eth1/{print $2,$10}'/proc/net/Dev ') eth0_in=$ (((${traffic_af[0]}-${traffic_be[0]})/ -)) Eth0_out=$ (((${traffic_af[1]}-${traffic_be[1]})/ -)) eth1_in=$ (((${traffic_af[2]}-${traffic_be[2]})/ -)) Eth1_out=$ (((${traffic_af[3]}-${traffic_be[3]})/ -))
2). Package Volume
Average number of packets per second for machine cards
Acquisition algorithm: Read the/proc/net/dev file, get the amount of packets sent and received within 60 seconds, and divide by 60 to get the average packet per second.
Shell code:
Packet_be= (' awk'begin{ors= ""}/eth0/{print $3,$11}/eth1/{print $3,$11}'/proc/net/Dev ') sleep -Packet_af= (' awk'begin{ors= ""}/eth0/{print $3,$11}/eth1/{print $3,$11}'/proc/net/Dev ') eth0_in=$ (((${packet_af[0]}-${packet_be[0]})/ -)) Eth0_out=$ (((${packet_af[1]}-${packet_be[1]})/ -)) eth1_in=$ (((${packet_af[2]}-${packet_be[2]})/ -)) Eth1_out=$ (((${packet_af[3]}-${packet_be[3]})/ -))
Shell script collection system CPU, memory, disk, network information