cme.sh網絡流量監控使用
[root@china213 ~]# cd /bin
[root@china213 bin]# ./cme.sh
cpu 99
disk 39
mem 38
nic 19855 33 0
[root@china213 bin]#
CPU剩余率,磁盤剩余率,內存剩余率,以及網卡接收的bps,pps和dps(丟包)
把下面內容用vi命令寫入到cme.sh中[root@china213 bin]# vi cme.sh,同時用[root@china213 bin]# chmod +x cme.sh增加可執行權限
注意:shell文件開頭必須以#!/bin/sh開始,在window中保存成cme.sh上傳的時候會執行不了,可能是文件頭的問題。
==============================不要把這行分各符放到cme.sh文件中=========================================
#!/bin/sh
eth="eth0 eth1 "
function get_eth_info()
{
name=$1
cat /proc/net/dev | awk -F ':' '{if(NR>2)print $1" "$2" "$9" "$10}' | awk '{print$1" "$2" "$3" "$5}' | while \
read line
do
# echo $line
data=`echo $name" "$line | awk '{if($1==$2)print $3" "$4" "$5}'`
if ! [ -z "$data" ]
then
echo $data
fi
done
}
function sum_eth()
{
rm -fr 12321.txt
for name in $eth
do
ret=`get_eth_info $name`
#echo $ret | awk '{bps+=$0;pps+$1;dps+=$2;print bps" "pps" "dps}'
echo $ret >> 12321.txt
done
cat 12321.txt | awk 'BEGIN{bps=0;pps=0;dps=0}{bps+=$1;pps+=$2;dps+=$3}END{print bps" "pps" "dps}'
}
flow=`sum_eth`
disk=`df | awk 'BEGIN{total=0;avl=0;used=0;}NR > 1{total+=$2;used+=$3;avl+=$4;}END{printf"%d", avl/total*100}'`
#top -b -n 1 | grep -w Mem | awk '{print"tot_mem: "$2" used_mem: " $4 " free_mem: "$6" left:"$6/$2"%"}'
mem=`top -b -n 1 | grep -w Mem | awk '{printf"%d",$6/$2*100}'`
#top -b -n 1 | grep -w Cpu | awk '{print"cpu: "$5}' | awk -F '%' {print$1}
cpu=`top -b -n 1 | grep -w Cpu | awk '{print$5}' | awk -F '%' '{printf"%d",$1}'`
tm=`date +%s`
if ! [ -f "flow.txt" ]
then
echo $tm" "$flow >flow.txt
sleep 1
tm=`date +%s`
flow=`sum_eth`
fi
old_flow=`cat flow.txt`
new_flow=`echo $tm $flow`
echo $new_flow >flow.txt
#echo $old_flow
#echo $new_flow
final_flow=`echo $old_flow $new_flow | awk '{dif_tm=$5-$1;dif_byte=$6-$2;dif_pkt=$7-$3;dif_dpkt=$8-$4;printf"%d %d %d",dif_byte*8/dif_tm, dif_pkt/dif_tm, dif_dpkt/dif_tm}'`
#final_flow=`echo $old_flow $new_flow | awk '{print$0}'`
echo "cpu "$cpu
echo "disk "$disk
echo "mem "$mem
echo "nic "$final_flow