免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2179 | 回复: 3

一个计算网络流量的脚本,请大家帮忙分析错在哪里 [复制链接]

论坛徽章:
0
发表于 2007-11-22 17:03 |显示全部楼层
一台机器作ftp,自己写了一个计算网络流量的脚本,通过查看结果感觉Transmit的速度是磁盘i/o的2倍还多一点
哪位达人给解释一下/proc/net/dev中Transmit的含义,按我理解在这种环境下网络i/o与磁盘i/o应该大致相当
是我理解有问题吗?
%cat /proc/net/dev
Inter-|   Receive                                             |  Transmit
face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:   45360     896    0    0    0     0          0         0    45360     896    0    0    0     0       0          0
bond0:602816058 3083480177    0 4827    0     4          0        57 2854378420 1285517131    0    0    0 0       0          0
  eth0:475837968 3082364828    0 4827    0     0          0        10 2901823013 2789924773    0    0    0 0       0          0
  eth1:126978090 1115349    0    0    0     4          0        47 4247522703 2790559654    0    0    0     0      0          0
  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0


脚本如下:

#!/bin/bash
echo -e "\tRecieve Rate\t\tTrans Rate"
echo -e "\tMBps/sec\t\tMBps/sec"
while true
do
RECV1=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $3}' /proc/net/dev`
SEND1=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $11}' /proc/net/dev`
sleep ${1}s
RECV2=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $3}' /proc/net/dev`
SEND2=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $11}' /proc/net/dev`
RECVRAT=`echo "scale=2;($RECV2-$RECV1)/2^20"|bc`
SENDRAT=`echo "scale=2;($SEND2-$SEND1)/2^20"|bc`
echo -e "\t$RECVRAT\t\t\t$SENDRAT"
done

论坛徽章:
0
发表于 2007-11-22 17:20 |显示全部楼层
COLSEP="|||"

getHostType(){
        hosttype=`uname -s`
        echo $hosttype
}

refreshlast(){

        netstat -i | sed -n '2,$p'
}


CURR_STAT=/tmp/netstatis.tmp


#add for table
TABLE_FILE="./sys_netfluxs.table"
if [ ! -f $TABLE_FILE ]
then
case `getHostType` in

        HP-UX)
  echo 'ID'$COLSEP'01.01.09.01'$COLSEP'01.01.09.02'$COLSEP'01.01.09.05'$COLSEP'01.01.09.06'$COLSEP'01.01.09.07' >> $TABLE_FILE
  ;;
  SunOS)
  echo 'ID'$COLSEP'01.02.09.01'$COLSEP'01.02.09.02'$COLSEP'01.02.09.05'$COLSEP'01.02.09.06'$COLSEP'01.02.09.07' >> $TABLE_FILE
  ;;
  
  AIX)
  echo 'ID'$COLSEP'01.07.09.01'$COLSEP'01.07.09.02'$COLSEP'01.07.09.05'$COLSEP'01.07.09.06'$COLSEP'01.07.09.07' >> $TABLE_FILE
  ;;
  SCO_SV)
  echo 'ID'$COLSEP'01.05.09.01'$COLSEP'01.05.09.02'$COLSEP'01.05.09.05'$COLSEP'01.05.09.06'$COLSEP'01.05.09.07' >> $TABLE_FILE
  ;;
  Linux)
  IO_RESULT=`sed '1,2d;s@^[^a-z]*@@;s@ @' /proc/net/dev | awk '{print $0}'`
  echo "$IO_RESULT" | awk  'BEGIN{print "ID'$COLSEP'01.03.04.01'$COLSEP'01.03.04.02'$COLSEP'01.03.04.03'$COLSEP'01.03.04.04'$COLSEP'01.03.04.05'$COLSEP'01.03.04.06'$COLSEP'01.03.04.07'$COLSEP'01.03.04.08"}{if(NR>0) print ""$1"'$COLSEP'"$2"'$COLSEP'"$3"'$COLSEP'"$4"'$COLSEP'"$5"'$COLSEP'"$10"'$COLSEP'"$11"'$COLSEP'"$12"'$COLSEP'"$13""}'
  exit
  ;;
  
esac
fi
#add end



refreshlast > $CURR_STAT
grep -v '^$' $CURR_STAT | while read line
do
       
   NAME=`echo $line | awk '{print $1}'`
   IN_S=`echo $line | awk '{print $5}'`
   OUT_S=`echo $line | awk '{print $7}'`
   IERR=`echo $line | awk '{print $6}'`
   OERR=`echo $line | awk '{print $8}'`
   OCOL=`echo $line | awk '{print $9}'`
   
   
   

         echo "$NAME$COLSEP$IN_S$COLSEP$OUT_S$COLSEP$IERR$COLSEP$OERR$COLSEP$OCOL" >> $TABLE_FILE
       
        #add end
done




#add for table
if [ -f $TABLE_FILE ]
then
cat $TABLE_FILE
rm $TABLE_FILE
else :
fi
#add end

论坛徽章:
0
发表于 2007-11-23 09:31 |显示全部楼层
没人解释我的疑问么?

论坛徽章:
0
发表于 2007-11-23 12:56 |显示全部楼层
不好意思,是脚本有错,应该改为:

#!/bin/bash
echo -e "\tRecieve Rate\t\tTrans Rate"
echo -e "\tMBps/sec\t\tMBps/sec"
while true
do
RECV1=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $3}' /proc/net/dev`
SEND1=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $11}' /proc/net/dev`
sleep ${1}s
RECV2=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $3}' /proc/net/dev`
SEND2=`awk 'BEGIN{FS="[ \t:]+"}$2~/bond0/{print $11}' /proc/net/dev`
RECVRAT=`echo "scale=2;($RECV2-$RECV1)/2^20/$1"|bc`
SENDRAT=`echo "scale=2;($SEND2-$SEND1)/2^20/$1"|bc`
echo -e "\t$RECVRAT\t\t\t$SENDRAT"
done
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP