| 关键词: nbsp speed ifconfig name echo result eth unit Count before |
原文:http://os.51cto.com/art/201202/318762.htm 根据原链接中“查看主机网卡流量”所写的实时流量监控脚本 监控linux主机流量的软件不少,之前一直用iptraf和dstat, 实际使用过程中dstat显示的速度与我cacti监测的数据不一致,(望知道原因的朋友不吝赐教) iptraf和cacti监测到的速度一致,但似乎只有kbit/s和kbyte/s两种单位显示 cacti数据: iptraf: dstat: 遂自己写了个脚本,根据ifconfig中RX和TX计数字段,执行两次ifconfig,用第二次的数减去第一次的数实现 netspeed.sh 01.#!/bin/bash 02.#network speed 03.#by [email protected] 04.#release 2012-11-13 05. 06.main() 07.{ 08.while : 09.do 10.time=`date +%m"-"%d" "%k":"%M":"%S` 11.rx_before=`ifconfig $eth_name|grep "RX bytes"|awk '{print $2}'|cut -d ':' -f 2` 12.tx_before=`ifconfig $eth_name|grep "TX bytes"|awk '{print $6}'|cut -d ':' -f 2` 13.sleep 2 14.rx_after=`ifconfig $eth_name|grep "RX bytes"|awk '{print $2}'|cut -d ':' -f 2` 15.tx_after=`ifconfig $eth_name|grep "TX bytes"|awk '{print $6}'|cut -d ':' -f 2` 16. 17.rx_result=$[(rx_after-rx_before)/256] 18.tx_result=$[(tx_after-tx_before)/256] 19.echo -e "$time In_Speed: \E[30;42m"$[rx_result/speed]"\E[0m $unit Out_Speed: \E[30;42m"$[tx_result/speed]"\E[0m $unit" #绿 20.#echo -e "$time In_Speed:`tput rev`"$[rx_result/speed]"`tput sgr0` $unit Out_Speed: `tput rev`"$[tx_result/speed]"`tput sgr0` $unit" #反向色 21. 22.sleep 2 23.done 24.} 25. 26.#default 27.eth_name="eth0" 28.unit="Kbps" 29.speed="1" 30. 31.while getopts hmMgGi: opt 32.do 33.case $opt in 34. i) 35. eth_name=$OPTARG 36. ;; 37. m|M) 38. speed="1024" 39. unit="Mbps" 40.# echo -e "\033[5m Count $eth_name use Mbps \033[0m" 41. ;; 42. g|G) 43. speed="1048576" 44. unit="Gbps" 45.# echo -e "\033[5m Count $eth_name use Gbps \033[0m" 46. ;; 47. h) 48. echo -e " Usage:\t netspeed {-m|-M|-g|-G} [-i [interface]]" 49. echo -e " Default:\t Count eth0 use kbps " 50. exit 0; 51. ;; 52. \?) 53. echo "Usage: netspeed {-m|-M|-g|-G} [-i [interface]]" 54. echo "Default : Count eth0 use kbps " 55. ;; 56.esac 57.done 58. 59.echo -e "\t\t \033[5m Count $eth_name speed ! \033[0m" 60.main |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|