Shell脚本实现的单机流量统计功能
程序员文章站
2022-06-20 20:14:02
在网上看到这个单机流量的脚本,挺不错的。
复制代码 代码如下:
#!/bin/sh
usage(){
echo “usage: $0 [-i interface]...
在网上看到这个单机流量的脚本,挺不错的。
复制代码 代码如下:
#!/bin/sh
usage(){
echo “usage: $0 [-i interface] [-s interval] [-c count]”
echo
echo “-i interface”
echo “ the interface to monitor, default is eth0.”
echo “-s interval”
echo “ the time to wait in seconds between measurements, default is 3 seconds.”
echo “-c count”
echo “ the number of times to measure, default is 10 times.”
exit 3
}
readargs(){
while [ "$#" -gt 0 ] ; do
case “$1″ in
-i)
if [ "$2" ] ; then
interface=”$2″
shift ; shift
else
echo “missing a value for $1.”
echo
shift
usage
fi
;;
-s)
if [ "$2" ] ; then
sleep=”$2″
shift ; shift
else
echo “missing a value for $1.”
echo
shift
usage
fi
;;
-c)
if [ "$2" ] ; then
counter=”$2″
shift ; shift
else
echo “missing a value for $1.”
echo
shift
usage
fi
;;
*)
echo “unknown option $1.”
echo
shift
usage
;;
esac
done
}
checkargs(){
if [ ! "$interface" ] ; then
interface=”eth0″
fi
if [ ! "$sleep" ] ; then
sleep=”3″
fi
if [ ! "$counter" ] ; then
counter=”10″
fi
}
printrxbytes(){
/sbin/ifconfig “$interface” | grep “rx bytes” | cut -d: -f2 | awk ‘{ print $1 }'
}
printtxbytes(){
/sbin/ifconfig “$interface” | grep “tx bytes” | cut -d: -f3 | awk ‘{ print $1 }'
}
bytestohumanreadable(){
multiplier=”0″
number=”$1″
while [ "$number" -ge 1024 ] ; do
multiplier=$(($multiplier+1))
number=$(($number/1024))
done
case “$multiplier” in
1)
echo “$number kb”
;;
2)
echo “$number mb”
;;
3)
echo “$number gb”
;;
4)
echo “$number tb”
;;
*)
echo “$1 b”
;;
esac
}
printresults(){
while [ "$counter" -ge 0 ] ; do
counter=$(($counter – 1))
if [ "$rxbytes" ] ; then
oldrxbytes=”$rxbytes”
oldtxbytes=”$txbytes”
fi
rxbytes=$(printrxbytes)
txbytes=$(printtxbytes)
if [ "$oldrxbytes" -a "$rxbytes" -a "$oldtxbytes" -a "$txbytes" ] ; then
echo “$(/bin/date +%y%m%d-%h%m%s) rxbytes = $(bytestohumanreadable $(($rxbytes – $oldrxbytes))) txbytes = $(bytestohumanreadable $(($txbytes – $oldtxbytes)))”
else
echo “monitoring $interface every $sleep seconds. (rxbyte total = $(bytestohumanreadable $rxbytes) txbytes total = $(bytestohumanreadable $txbytes))”
fi
sleep “$sleep”
done
}
readargs “$@”
checkargs
printresults
测试如下:
每三秒的流量,总输出999行,可以输出到文件里,其中:前面为时间,rx packets 是接收数据包,即下载,tx packets 是发送数据包,即上传.
复制代码 代码如下:
[root@host]#sh t.sh -c 999 monitoring eth0 every 3 seconds. (rxbyte total = 6 tb txbytes total = 5 tb)
20101105-201539 rxbytes = 126 kb txbytes = 658 kb
20101105-201542 rxbytes = 87 kb txbytes = 487 kb
20101105-201545 rxbytes = 159 kb txbytes = 668 kb
20101105-201548 rxbytes = 107 kb txbytes = 725 kb
20101105-201551 rxbytes = 110 kb txbytes = 704 kb
20101105-201554 rxbytes = 90 kb txbytes = 726 kb
20101105-201558 rxbytes = 100 kb txbytes = 850 kb
20101105-201601 rxbytes = 102 kb txbytes = 703 kb
20101105-201604 rxbytes = 168 kb txbytes = 693 kb
20101105-201607 rxbytes = 105 kb txbytes = 730 kb
20101105-201610 rxbytes = 133 kb txbytes = 711 kb
20101105-201613 rxbytes = 431 kb txbytes = 703 kb
20101105-201616 rxbytes = 84 kb txbytes = 527 kb
20101105-201619 rxbytes = 239 kb txbytes = 825 kb
20101105-201622 rxbytes = 117 kb txbytes = 801 kb
20101105-201625 rxbytes = 99 kb txbytes = 913 kb
20101105-201628 rxbytes = 89 kb txbytes = 322 kb
20101105-201631 rxbytes = 63 kb txbytes = 73 kb
20101105-201634 rxbytes = 84 kb txbytes = 191 kb
20101105-201637 rxbytes = 174 kb txbytes = 481 kb
20101105-201640 rxbytes = 120 kb txbytes = 383 kb
20101105-201643 rxbytes = 94 kb txbytes = 496 kb
20101105-201646 rxbytes = 108 kb txbytes = 340 kb
20101105-201649 rxbytes = 91 kb txbytes = 639 kb
20101105-201652 rxbytes = 106 kb txbytes = 629 kb
20101105-201655 rxbytes = 125 kb txbytes = 496 kb
20101105-201658 rxbytes = 90 kb txbytes = 537 kb
20101105-201701 rxbytes = 114 kb txbytes = 641 kb