shell采集系统cpu 内存 磁盘 网络信息
时间:2013-07-27 23:06来源:centos.bz 作者:感谢:“匿名”投稿 举报 点击: 153次
cpu使用率
采集算法
view source print? 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]})))
cpu负载
采集算法:
view source print? cpuload=$(`cat /proc/loadavg | awk '{print $1,$2,$3}'`) load1=${cpuload[0]} load5=${cpuload[1]} load15=${cpuload[2]}
内存采集
应用程序使用内存
采集算法:
view source print? awk'/MemTotal/{total=$2}/MemFree/{free=$2}/Buffers/{buffers=$2}/^Cached/{cached=$2}END{print (total-free-buffers-cached)/1024}' /proc/meminfo
MEM使用量
采集算法:
view source print? awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo
SWAP使用大小
采集算法: 磁盘信息采集disk io
1、IN:平均每秒把数据从硬盘读到物理内存的数据量
view source print? a=`awk '/pgpgin/{print $2}' /proc/vmstat` sleep 240 b=`awk '/pgpgin/{print $2}' /proc/vmstat` ioin=$(((b-a)/240))
2、OUT:平均每秒把数据从物理内存写到硬盘的数据量 view source print? a=`awk '/pgpgout/{print $2}' /proc/vmstat` sleep 240 b=`awk '/pgpgout/{print $2}' /proc/vmstat` ioout=$(((b-a)/240))
网络
流量
以http://www.centos.bz/为例,eth0是内网,eth1外网,获取60秒的流量。
view source
print?
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 ))
包量
机器网卡的平均每秒包量
view source print? 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 )) |