Cpu usage
Collection Algorithm
Collect and calculate the total CPU usage or single-core usage using the/proc/stat file. Taking cpu0 as an example, the algorithm is as follows:
1. cat/proc/stat | grep 'cpu0' to obtain 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 the cpu Information
6. cpuTotal2 = user + nice + system + idle + iowait + irq + softirq
7. cpuUsed2 = user + nice + system + irq + softirq
8. Obtain the single-core utilization rate of cpu0 in 30 seconds: (cpuUsed2-cpuUsed1) * 100/(cpuTotal2-cpuTotal1)
It is equivalent to using the top-d 30 command to add the usage of user, nice, system, irq, and softirq.
view sourceprint?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 30b=$(`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]})*100/(${b[0]}-${a[0]})))
Cpu load
Collection algorithm:
Read/proc/loadavg to get the average load of the machine in 1/5/15 minutes, multiplied by 100.
view sourceprint?cpuload=$(`cat /proc/loadavg | awk '{print $1,$2,$3}'`)load1=${cpuload[0]}load5=${cpuload[1]}load15=${cpuload[2]}
Memory collection
Application memory usage
Collection algorithm:
Read the/proc/meminfo file (MemTotal-MemFree-Buffers-Cached)/1024 to get the amount of memory used by the application.
view sourceprint?awk'/MemTotal/{total=$2}/MemFree/{free=$2}/Buffers/{buffers=$2}/^Cached/{cached=$2}END{print (total-free-buffers-cached)/1024}' /proc/meminfo
MEM usage
Collection algorithm:
Read the/proc/meminfo file and MemTotal-MemFree gets the MEM usage.
view sourceprint?awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo
SWAP usage size
Collection algorithm:
In the/proc/meminfo file, SwapTotal-SwapFree obtains the SWAP usage size.
Disk Information Collection disk io
1. IN: average volume of data read from hard disk to physical memory per second
Collection algorithm:
Read the/proc/vmstat file to get the increment of pgpgin In the last 240 seconds. Divide the increment of pgpgin by 240 to get the average increment per second.
It is equivalent to the output of the bi column of the vmstat 240 command.
view sourceprint?a=`awk '/pgpgin/{print $2}' /proc/vmstat`sleep 240b=`awk '/pgpgin/{print $2}' /proc/vmstat`ioin=$(((b-a)/240))
2. OUT: average volume of data written from the physical memory to the hard disk per second
Collection algorithm:
Read the/proc/vmstat file to get the increment of pgpgout In the last 240 seconds. Divide the increment of pgpgout by 240 to get the average increment per second.
It is equivalent to the output of the bo column of the vmstat 240 command.
view sourceprint?a=`awk '/pgpgout/{print $2}' /proc/vmstat`sleep 240b=`awk '/pgpgout/{print $2}' /proc/vmstat`ioout=$(((b-a)/240))
Network
Traffic
Taking http://www.centos.bz/as an example, eth0is an internal network, and eth1is an external network with a traffic volume of 60 seconds.
Average machine network card traffic per second
Collection algorithm:
Read the/proc/net/dev file and get the number of bytes (KB) sent and received in 60 seconds. multiply the number by 8 and divide it by 60 to get the average traffic per second.
view sourceprint?traffic_be=$(`awk 'BEGIN{ORS=" "}/eth0/{print $2,$10}/eth1/{print $2,$10}' /proc/net/dev`)sleep 60traffic_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 ))
Package volume
Average packets per second for machine NICs
Collection algorithm:
Read the/proc/net/dev file to obtain the number of packets sent and received in 60 seconds. Divide the number by 60 to obtain the average number of packets per second.
view sourceprint?packet_be=$(`awk 'BEGIN{ORS=" "}/eth0/{print $3,$11}/eth1/{print $3,$11}' /proc/net/dev`)sleep 60packet_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 ))