linux流量监控shell脚本

原文:http://www.weiruoyu.cn/?p=349

最近在看流量监控,网上找了很多,感觉写的最好是这个

 
 
  1. [root@localhost ~]# vi count_net.sh

 
 
  1. #!/bin/bash

  2. #

  3. R1=`cat /sys/class/net/eth0/statistics/rx_bytes`

  4. T1=`cat /sys/class/net/eth0/statistics/tx_bytes`

  5. sleep 1

  6. R2=`cat /sys/class/net/eth0/statistics/rx_bytes`

  7. T2=`cat /sys/class/net/eth0/statistics/tx_bytes`

  8. TBPS=`expr $T2 - $T1`

  9. RBPS=`expr $R2 - $R1`

  10. TKBPS=`expr $TBPS / 1024`

  11. RKBPS=`expr $RBPS / 1024`

  12. echo "上传速率 eth0: $TKBPS kb/s 下载速率 eth0: $RKBPS kb/s at $(date +%Y%m%d%H:%M:%S)" >>/var/log/filecopy/count_net/network_10.0.0.1_$(date +%Y%m%d).log

添加到计划任务里面,每5分钟一次

 
 
  1. */5 * * * * /backup/bin/count_net.sh

设置自动回传(要添加免密钥验证)

 
 
  1. [root@localhost bin]# vi count_net_bak.sh


 
 
  1. scp /var/log/filecopy/count_net/network_211.xxx.xxx.xxx_$(date +%Y%m%d).log [email protected]:/backup/count_net

==================================


 
 
  1. #!/bin/bash

  2. #

  3. TX=0;

  4. RX=0;

  5. MAX_TX=0;

  6. MAX_RX=0;

  7. while read line

  8. do

  9. a=`echo $line | grep "eth0" |awk '{print $3}'`

  10. if [ $a -ge 0 ]

  11. then

  12. TX=$a

  13. if [ $TX -ge $MAX_TX ]

  14. then

  15. MAX_TX=$TX

  16. fi

  17. fi

  18. b=`echo $line | grep "eth0" |awk '{print $7}'`

  19. if [ $b -ge 0 ]

  20. then

  21. RX=$b

  22. if [ $RX -ge $MAX_RX ]

  23. then

  24. MAX_RX=$RX

  25. fi

  26. fi

  27. done < /var/log/filecopy/count_net/network_10.0.0.1_$(date +%Y%m%d).log

  28. echo "最高上传速度为 $MAX_TX kb/s at $(date +%Y%m%d)">>/var/log/filecopy/count_net/tongji.log

  29. echo "最高下载速度为 $MAX_RX kb/s at $(date +%Y%m%d)">>/var/log/filecopy/count_net/tongji.log

详细请参考:http://www.weiruoyu.cn/?p=349


你可能感兴趣的:(linux流量监控shell脚本)