本文共 2536 字,大约阅读时间需要 8 分钟。
cat /proc/net/dev 通过这个文件可以看到网络流量
[@more@]查看主机流量,ok,以KB为单位,一个nohup命令让他跑这么几天, 取出来分析便是。/proc/net/dev,就是分析状态。
零、嘛是/proc/net/dev:
/proc的那些文件的操作函数很特殊,不需要直接对/proc文件的内容进行写操作来添加内容,就能用充满魔力的show函数来给用户提供信息。听不懂,就当神话来听,知道怎么用这个东东就行。
# 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:1219485788609 620698175 0 0 0 0 0 0 1219485788609 620698175 0 0 0 0 0 0 eth0:182025224125 332789797 0 0 0 0 0 1946 226393677809 315770454 0 0 0 0 0 0 eth1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 tunl0:18186156911 209305063 0 0 0 0 0 0 0 0 0 0 0 0 0 0ok,你要用的就是第1,2,9,10列。
一、一般服务器就一个网卡,bash版的够用了:
#! /bin/bash
while true do receive1=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'` send1=`cat /proc/net/dev|grep eth0 | awk '{print$9}'` sleep 300 receive2=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'` receive_cnt=`expr $receive2 - $receive1` receive_cnt=`expr $receive_cnt / 1024` send2=`cat /proc/net/dev|grep eth0 | awk '{print$9}'` send_cnt=`expr $send2 - $send1` send_cnt=`expr $send_cnt / 1024` echo `date` 'eth0 Receive Bytes:'$receive_cnt >>ok.txt echo `date` 'eth0 Send Bytes:' $send_cnt >>ok.txt done
二、如果你多网卡,又想有个万能脚本,ok,老游给你一个perl版本:
#!/usr/bin/perl
$count=0;my $sec=60;while(1){ my ($RX_bytes1,$TX_bytes1)=getnet(); sleep($sec); my ($RX_bytes2,$TX_bytes2)=getnet(); my $net_RX_rate=int(($RX_bytes2-$RX_bytes1)/$sec); my $net_TX_rate=int(($TX_bytes2-$TX_bytes1)/$sec); my $net_rate=$net_RX_rate+$net_TX_rate; my $localt=localtime(); $count++; printlog("$countt$localtt$net_ratet$net_RX_ratet$net_TX_raten");}#-------------------------------------------------------------------------sub getnet{ system("rm -fr net_use.tmp") if(-e "net_use.tmp"); system("cp /proc/net/dev net_use.tmp"); open(IN,"net_use.tmp")||die "$!"; my @array=; close IN; system("rm -fr net_use.tmp"); my ($RX_bytes,$TX_bytes)=(0,0); foreach my $line(@array){ if($line=~/ethd+:(.+)/){ my $tmp=$1; my @array2=split(/s+/,$tmp); $RX_bytes+=$array2[0]; $TX_bytes+=$array2[8]; } } return ($RX_bytes,$TX_bytes);}#-------------------------------------------------------------------------sub printlog{ my $str=shift; open(LOGOUT,">>net_stat.log"); print LOGOUT "$str"; close LOGOUT; }ref: http://51runaway.blog.163.com/blog/static/240286882009102665632660/
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7916042/viewspace-1057003/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7916042/viewspace-1057003/