cme.sh网络流量监控使用

林继 VPS 知识 2,535 共写了30个字 (2011-11-13 13:59:44) 没有评论 打印 扫描二维码 百度已收录
  1.   [root@china213 ~]# cd /bin
  2.  
  3.   [root@china213 bin]# ./cme.sh
  4.  
  5.   cpu 99
  6.  
  7.   disk 39
  8.  
  9.   mem 38
  10.  
  11.   nic 19855 33 0
  12.  
  13.   [root@china213 bin]#
  14.  
  15.   CPU剩余率,磁盘剩余率,内存剩余率,以及网卡接收的bps,pps和dps(丢包)
  16.  
  17.   把下面内容用vi命令写入到cme.sh中[root@china213 bin]# vi cme.sh,同时用[root@china213 bin]# chmod +x cme.sh增加可执行权限
  18.  
  19.   注意:shell文件开头必须以#!/bin/sh开始,在window中保存成cme.sh上传的时候会执行不了,可能是文件头的问题。
  20.  
  21.   ==============================不要把这行分各符放到cme.sh文件中=========================================
  22.  
  23.   #!/bin/sh
  24.  
  25.   eth="eth0 eth1 "
  26.  
  27.   function get_eth_info()
  28.  
  29.   {
  30.  
  31.   name=$1
  32.  
  33.   cat /proc/net/dev | awk -F ':' '{if(NR>2)print $1" "$2" "$9" "$10}' | awk '{print$1" "$2" "$3" "$5}' | while \
  34.  
  35.   read line
  36.  
  37.   do
  38.  
  39.   # echo $line
  40.  
  41.   data=`echo $name" "$line | awk '{if($1==$2)print $3" "$4" "$5}'`
  42.  
  43.   if ! [ -z "$data" ]
  44.  
  45.   then
  46.  
  47.   echo $data
  48.  
  49.   fi
  50.  
  51.   done
  52.  
  53.   }
  54.  
  55.   function sum_eth()
  56.  
  57.   {
  58.  
  59.   rm -fr 12321.txt
  60.  
  61.   for name in $eth
  62.  
  63.   do
  64.  
  65.   ret=`get_eth_info $name`
  66.  
  67.   #echo $ret | awk '{bps+=$0;pps+$1;dps+=$2;print bps" "pps" "dps}'
  68.  
  69.   echo $ret >> 12321.txt
  70.  
  71.   done
  72.  
  73.   cat 12321.txt | awk 'BEGIN{bps=0;pps=0;dps=0}{bps+=$1;pps+=$2;dps+=$3}END{print bps" "pps" "dps}'
  74.  
  75.   }
  76.  
  77.   flow=`sum_eth`
  78.  
  79.   disk=`df | awk 'BEGIN{total=0;avl=0;used=0;}NR > 1{total+=$2;used+=$3;avl+=$4;}END{printf"%d", avl/total*100}'`
  80.  
  81.   #top -b -n 1 | grep -w Mem | awk '{print"tot_mem: "$2" used_mem: " $4 " free_mem: "$6" left:"$6/$2"%"}'
  82.  
  83.   mem=`top -b -n 1 | grep -w Mem | awk '{printf"%d",$6/$2*100}'`
  84.  
  85.   #top -b -n 1 | grep -w Cpu | awk '{print"cpu: "$5}' | awk -F '%' {print$1}
  86.  
  87.   cpu=`top -b -n 1 | grep -w Cpu | awk '{print$5}' | awk -F '%' '{printf"%d",$1}'`
  88.  
  89.   tm=`date +%s`
  90.  
  91.   if ! [ -f "flow.txt" ]
  92.  
  93.   then
  94.  
  95.   echo $tm" "$flow >flow.txt
  96.  
  97.   sleep 1
  98.  
  99.   tm=`date +%s`
  100.  
  101.   flow=`sum_eth`
  102.  
  103.   fi
  104.  
  105.   old_flow=`cat flow.txt`
  106.  
  107.   new_flow=`echo $tm $flow`
  108.  
  109.   echo $new_flow >flow.txt
  110.  
  111.   #echo $old_flow
  112.  
  113.   #echo $new_flow
  114.  
  115.   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}'`
  116.  
  117.   #final_flow=`echo $old_flow $new_flow | awk '{print$0}'`
  118.  
  119.   echo "cpu "$cpu
  120.  
  121.   echo "disk "$disk
  122.  
  123.   echo "mem "$mem
  124.  
  125.   echo "nic "$final_flow

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

< >