免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2485 | 回复: 6
打印 上一主题 下一主题

检查网卡流量脚本大家帮忙分析分析 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-20 09:31 |只看该作者 |倒序浏览
这个脚本中的4294967295是什么值?是怎么得来的
#!/bin/bash
#  osdba 2008.10.22 monitor the interface's network traffic.
#  Zeuslion 2009.08.29.
if [ $# -ne 3 ];then
   echo Useage : $0 interface interval count
   echo Example: $0 eth0 2 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}')

   bytesin=$(($inbytesend-$inbytesfirst))
   bytesout=$(($outbytesend-$outbytesfirst))
   packetsin=$(($inpacketsend-$inpacketsfirst))
   packetsout=$(($outpacketsend-$outpacketsfirst))

   if [ "$bytesin" -lt "0" ];then
      bytesin=$((4294967295-$inbytesfirst+$inbytesend))
      #echo bytesin $bytesin $inbytesfirst $inbytesend
   fi
   if [ "$bytesout" -lt "0" ];then
      bytesout=$((4294967295-$outbytesfirst+$outbytesend))
      #echo bytesout $bytesout $outbytesfirst $outbytesend
   fi
   if [ "$packetsin" -lt "0" ];then
      packetsin=$((4294967295-$inpacketsfirst+$inpacketsend))
      #echo packetsin $packetsin $inpacketsfirst $inpacketsend
   fi
   if [ "$packetsout" -lt "0" ];then
      packetsout=$((4294967295-$outpacketsfirst+$outpacketsend))
      #echo packetsout $packetsout $outpacketsfirst $outpacketsend
   fi

   bytesin=$(($bytesin/$interval))
   bytesout=$(($bytesout/$interval))
   packetsin=$(($packetsin/$interval))
   packetsout=$(($packetsout/$interval))

   sumbytesin=$(($sumbytesin+$bytesin))
   sumbytesout=$(($sumbytesout+$bytesout))
   sumpacketsin=$(($sumpacketsin+$packetsin))
   sumpacketsout=$(($sumpacketsout+$packetsout))

   if [ $(($i%20)) -eq 0 ];then
      echo " ifname   | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s"
      echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
   fi
   echo $eth $bytesin $bytesout $packetsin $packetsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'
   inbytesfirst=$inbytesend
   outbytesfirst=$outbytesend
   inpacketsfirst=$inpacketsend
   outpacketsfirst=$outpacketsend
   
   i=$(($i+1))
   sleep $interval"s"
done

sumbytesin=$(($sumbytesin/$i))
sumbytesout=$(($sumbytesout/$i))
sumpacketsin=$(($sumpacketsin/$i))
sumpacketsout=$(($sumpacketsout/$i))

echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
echo Average $sumbytesin $sumbytesout $sumpacketsin $sumpacketsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'

论坛徽章:
0
2 [报告]
发表于 2010-04-20 09:51 |只看该作者
回复 1# ybbdnvjfd


    这是为了计算unsigned int(4字节)整数计数发生溢出的时候
4294967295是32位无符号整数所能表达的最大整数
假设当前数据包流量离4294967295 很近时,当下一次再去查看流量时,计数就有可能发生回绕
后来的流量减去前面的流量就会是个负值
所以为了计算增加的流量,用整个区间的值+他们之间的负值,即最大值-|他们两个的距离值|=增加的流量

论坛徽章:
0
3 [报告]
发表于 2010-04-20 10:03 |只看该作者
..LZ什么系统,我在bash上能计算出的最大整数值好像比这个大不少

论坛徽章:
0
4 [报告]
发表于 2010-04-20 10:15 |只看该作者
redhat 4 这个是我在网上搜到的感觉很有用,不过有的地方不明白所以发出来请大家帮忙看看

论坛徽章:
0
5 [报告]
发表于 2010-04-20 10:16 |只看该作者
本帖最后由 ybbdnvjfd 于 2010-04-20 10:18 编辑
回复  ybbdnvjfd


    这是为了计算unsigned int(4字节)整数计数发生溢出的时候
4294967295是32位无符 ...
ghp268 发表于 2010-04-20 09:51



    能在说的明白点吗?我没有理解通。。。能举个例子吗?

论坛徽章:
0
6 [报告]
发表于 2010-04-20 10:44 |只看该作者
回复 5# ybbdnvjfd


    用unsigned char 为例,unsigned char 能表示的最大值为255
假设当前计数为250,过了interval秒后,这期间又有100个数据包过来
计数值是350么?显然不是,因为能表示的最大数为255也就是只能表示8位,这时250+6=256=1 0000 0000(二进制)
然后1 0000 0000(二进制) +94(十进制)=0+94=94
这时94-250=-156
所以inteerval秒内进来的数据包为255+(-156)=99
这个算起来其实最后应该再加1

论坛徽章:
0
7 [报告]
发表于 2010-04-20 10:54 |只看该作者
哦,有点明白了,我在想想。谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP