使用shell脚本采集系统cpu、内存、磁盘、网络等信息
一、cpu信息采集
1).采集cpu使用率
采集算法:通过/proc/stat文件采集并计算cpu总使用率或者单个核使用率。以cpu0为例,算法如下:
1. cat /proc/stat | grep ‘cpu0'得到cpu0的信息
2. cputotal1=user+nice+system+idle+iowait+irq+softirq
3. cpuused1=user+nice+system+irq+softirq
4. sleep 30秒
5. 再次cat /proc/stat | grep 'cpu0' 得到cpu的信息
6. cputotal2=user+nice+system+idle+iowait+irq+softirq
7. cpuused2=user+nice+system+irq+softirq
8. 得到cpu0 在30秒内的单核利用率:(cpuused2 – cpuused1) * 100 / (cputotal2 – cputotal1)
相当于使用top –d 30命令,把user、nice、system、irq、softirq五项的使用率相加。
shell代码:
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 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{print total,used}'`)
cpu_usage=(((${b[1]}-${a[1]})*100/(${b[0]}-${a[0]})))
2).采集cpu负载
采集算法:读取/proc/loadavg得到机器的1/5/15分钟平均负载,再乘以100。
shell代码:
cpuload=(`cat /proc/loadavg | awk '{print $1,$2,$3}'`)
load1=${cpuload[0]}
load5=${cpuload[1]}
load15=${cpuload[2]}
二、内存采集
1).应用程序使用内存
采集算法:读取/proc/meminfo文件,(memtotal – memfree – buffers – cached)/1024得到应用程序使用内存数。
shell代码:
awk '/memtotal/{total=$2}/memfree/{free=$2}/buffers/{buffers=$2}/^cached/{cached=$2}end{print (total-free-buffers-cached)/1024}' /proc/meminfo
2).mem使用量
采集算法:读取/proc/meminfo文件,memtotal – memfree得到mem使用量。
shell代码:
awk '/memtotal/{total=$2}/memfree/{free=$2}end{print (total-free)/1024}' /proc/meminfo
3).swap使用大小
采集算法:通过/proc/meminfo文件,swaptotal – swapfree得到swap使用大小。
shell代码:
awk '/swaptotal/{total=$2}/swapfree/{free=$2}end{print (total-free)/1024}' /proc/meminfo
三、磁盘信息采集(disk io)
1、in:平均每秒把数据从硬盘读到物理内存的数据量
采集算法:读取/proc/vmstat文件得出最近240秒内pgpgin的增量,把pgpgin的增量再除以240得到每秒的平均增量。
相当于vmstat 240命令bi一列的输出。
shell代码:
a=`awk '/pgpgin/{print $2}' /proc/vmstat`
sleep 240
b=`awk '/pgpgin/{print $2}' /proc/vmstat`
ioin=$(((b-a)/240))
2、out:平均每秒把数据从物理内存写到硬盘的数据量
采集算法:读取/proc/vmstat文件得出最近240秒内pgpgout的增量,把pgpgout的增量再除以240得到每秒的平均增量。
相当于vmstat 240命令bo一列的输出。
shell代码:
a=`awk '/pgpgout/{print $2}' /proc/vmstat`
sleep 240
b=`awk '/pgpgout/{print $2}' /proc/vmstat`
ioout=$(((b-a)/240))
四、网络
1).流量
以//www.jb51.net/为例,eth0是内网,eth1外网,获取60秒的流量。
机器网卡的平均每秒流量
采集算法:读取/proc/net/dev文件,得到60秒内发送和接收的字节数(kb),然后乘以8,再除以60,得到每秒的平均流量。
shell代码:
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).包量
机器网卡的平均每秒包量
采集算法:读取/proc/net/dev文件,得到60秒内发送和接收的包量,然后除以60,得到每秒的平均包量。
shell代码:
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 ))
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
为Nginx动态添加模块的方法
写在前面很多时候,我们根据当时的项目情况和业务需求安装完nginx后,后续随着业务的发展,往往会给安装好的nginx添加其他的功能模块。在为nginx添加功能模...
[阅读全文]
-
-
-
-
-
UOS arm换源
清华源mv/etc/apt/sources.list /etc/apt/sources.list.bak备份原始...
[阅读全文]
-
-
IT基础与云计算
软路由器(IPtable)iptable是Linux上常用的防火墙软件。负载均衡负载均衡是网络基础构架的关键组件...
[阅读全文]
网友评论