工具运行效果如下:
#iftop eth0 1 3
ifname in_bytes/s out_bytes/s total_bytes/s
eth0 446 418 864
eth0 70 182 252
eth0 70 182 252
脚本内容如下:
#!/bin/bash
# osdba 2009.10.22 monitor the interface's network traffic.
if [ $# -ne 3 ];then
echo example: $0 eth0 1 10
exit
fi
eth=$1
count=$3
interval=$2
inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
if [ -z "$inbytesfirst" ];then
echo The network interface $eth is not exits!
exit 1;
fi
outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
sleep $interval"s"
i=0
while [ "$i" -lt "$count" ]
do
inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
sumbytesin=$((($inbytesend-$inbytesfirst)/$interval))
sumbytesout=$((($outbytesend-$outbytesfirst)/$interval))
sumpacketsin=$((($inpacketsend-$inpacketsfirst)/$interval))
sumpacketsout=$((($outpacketsend-$outpacketsfirst)/$interval))
sumbytes=$(($sumbytesin+$sumbytesout))
sumpackets=$(($sumpacketsin+$sumpacketsout))
if [ $(($i%20)) -eq 0 ];then
#echo "ifname" "in_bytes/s" "out_bytes/s" "total_bytes/s" |awk '{printf("%10s %16s %16s %16s\n",$1,$2,$3,$4)}'
echo " ifname in_kbytes/s out_kbytes/s all_kbytes/s in_packets/s out_packets/s all_packets/s"
echo "--------- ----------- ------------ ------------ ------------ ------------- -------------"
fi
echo $eth $sumbytesin $sumbytesout $sumbytes $sumpacketsin $sumpacketsout $sumpackets |awk '{printf("%9s %11.1d %12.1d %12.1d %12s %13s %13s\n",$1,$2/1024,$3/1024,$4/1024,$5,$6,$7)}'
inbytesfirst=$inbytesend
outbytesfirst=$outbytesend
inpacketsfirst=$inpacketsend
outpacketsfirst=$outpacketsend
i=$(($i+1))
sleep $interval"s"
done
阅读(1851) | 评论(0) | 转发(0) |