CPU Information Acquisition CPU Usage
Acquisition algorithm
Collect and calculate total CPU usage or single core utilization with/proc/stat files. 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+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. 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{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]}))
CPU Load
Acquisition algorithm:
Reading the/proc/loadavg gets the 1/5/15 minute average load of the machine, multiplied by 100.
Shell code:
- Cpuload= (' cat/proc/loadavg | awk ' {print $1,$2,$3} ')
- Load1=${cpuload[0]}
- LOAD5=${CPULOAD[1]}
- LOAD15=${CPULOAD[2]}
Memory capture Applications use memory
Acquisition algorithm:
Read the/proc/meminfo file, (memtotal–memfree–buffers–cached)/1024 to get the amount of memory used by the application.
Shell code:
- awk '/memtotal/{total=$2}/memfree/{free=$2}/buffers/{buffers=$2}/^cached/{cached=$2}end{print ( total-free-buffers-cached)/1024} '/proc/meminfo
MEM Usage
Acquisition algorithm:
Read the/proc/meminfo file and Memtotal–memfree get the mem usage.
Shell code:
- awk '/memtotal/{total=$2}/memfree/{free=$2}end{print (total-free)/1024} '/proc/meminfo
Swap use size
Acquisition algorithm:
With the/proc/meminfo file, Swaptotal–swapfree gets the swap size.
Shell code:
- awk '/swaptotal/{total=$2}/swapfree/{free=$2}end{print (total-free)/1024} '/proc/meminfo
Disk Information Acquisition Disk 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:
- A= ' awk '/pgpgin/{print $ '/proc/vmstat '
- Sleep 240
- b= ' awk '/pgpgin/{print $ '/proc/vmstat '
- Ioin=$ (((b-a)/240))
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:
- A= ' awk '/pgpgout/{print $ '/proc/vmstat '
- Sleep 240
- b= ' awk '/pgpgout/{print $ '/proc/vmstat '
- Ioout=$ (((b-a)/240))
Internet Flow
Take http://www.centos.bz/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), then multiply by 8 and divide by 60 to get the average traffic per second.
Shell code:
- Traffic_be= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $3,$10}/eth1/{print $3,$11} '/proc/net/dev ')
- Sleep 60
- traffic_af= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $3,$10}/eth1/{print $3,$11} '/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 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, then divide by 60 to get the average packet per second.
Shell code:
- Packet_be= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $4,$12}/eth1/{print $4,$12} '/proc/net/dev ')
- Sleep 60
- packet_af= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $4,$12}/eth1/{print $4,$12} '/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))
Shell capture system CPU memory disk Network information