原文: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
- #!/bin/bash
- #network speed
- #by wyyj12@163.com
- #release 2012-11-13
- main()
- {
- while :
- do
- time=`date +%m"-"%d" "%k":"%M":"%S`
- rx_before=`ifconfig $eth_name|grep "RX bytes"|awk '{print $2}'|cut -d ':' -f 2`
- tx_before=`ifconfig $eth_name|grep "TX bytes"|awk '{print $6}'|cut -d ':' -f 2`
- sleep 2
- rx_after=`ifconfig $eth_name|grep "RX bytes"|awk '{print $2}'|cut -d ':' -f 2`
- tx_after=`ifconfig $eth_name|grep "TX bytes"|awk '{print $6}'|cut -d ':' -f 2`
- rx_result=$[(rx_after-rx_before)/256]
- tx_result=$[(tx_after-tx_before)/256]
- 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" #绿
- #echo -e "$time In_Speed:`tput rev`"$[rx_result/speed]"`tput sgr0` $unit Out_Speed: `tput rev`"$[tx_result/speed]"`tput sgr0` $unit" #反向色
- sleep 2
- done
- }
- #default
- eth_name="eth0"
- unit="Kbps"
- speed="1"
- while getopts hmMgGi: opt
- do
- case $opt in
- i)
- eth_name=$OPTARG
- ;;
- m|M)
- speed="1024"
- unit="Mbps"
- # echo -e "\033[5m Count $eth_name use Mbps \033[0m"
- ;;
- g|G)
- speed="1048576"
- unit="Gbps"
- # echo -e "\033[5m Count $eth_name use Gbps \033[0m"
- ;;
- h)
- echo -e " Usage:\t netspeed {-m|-M|-g|-G} [-i [interface]]"
- echo -e " Default:\t Count eth0 use kbps "
- exit 0;
- ;;
- \?)
- echo "Usage: netspeed {-m|-M|-g|-G} [-i [interface]]"
- echo "Default : Count eth0 use kbps "
- ;;
- esac
- done
- echo -e "\t\t \033[5m Count $eth_name speed ! \033[0m"
- main
转载于:https://blog.51cto.com/ianblog/1064880