Shell capture system CPU memory disk Network information

Source: Internet
Author: User

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:

    1. 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} ")
    2. Sleep 30
    3. 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} ")
    4. 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:

    1. Cpuload= (' cat/proc/loadavg | awk ' {print $1,$2,$3} ')
    2. Load1=${cpuload[0]}
    3. LOAD5=${CPULOAD[1]}
    4. 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:

    1. 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:

    1. 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:

    1. 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:

    1. A= ' awk '/pgpgin/{print $ '/proc/vmstat '
    2. Sleep 240
    3. b= ' awk '/pgpgin/{print $ '/proc/vmstat '
    4. 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:

    1. A= ' awk '/pgpgout/{print $ '/proc/vmstat '
    2. Sleep 240
    3. b= ' awk '/pgpgout/{print $ '/proc/vmstat '
    4. 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:

    1. Traffic_be= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $3,$10}/eth1/{print $3,$11} '/proc/net/dev ')
    2. Sleep 60
    3. traffic_af= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $3,$10}/eth1/{print $3,$11} '/proc/net/dev ')
    4. Eth0_in=$ (((${traffic_af[0]}-${traffic_be[0)})/60)
    5. Eth0_out=$ (((${traffic_af[1]}-${traffic_be[1)})/60)
    6. Eth1_in=$ (((${traffic_af[2]}-${traffic_be[2)})/60)
    7. 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:

      1. Packet_be= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $4,$12}/eth1/{print $4,$12} '/proc/net/dev ')
      2. Sleep 60
      3. packet_af= (' awk-f ' [:]+ ' begin{ors= ' "}/eth0/{print $4,$12}/eth1/{print $4,$12} '/proc/net/dev ')
      4. Eth0_in=$ (((${packet_af[0]}-${packet_be[0)})/60)
      5. Eth0_out=$ (((${packet_af[1]}-${packet_be[1})/60))
      6. Eth1_in=$ (((${packet_af[2]}-${packet_be[2})/60))
      7. Eth1_out=$ (((${packet_af[3]}-${packet_be[3})/60))

Shell capture system CPU memory disk Network information

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.