awk监控与统计网卡流量的shell脚本,awk从proc/net/dev文件获取网卡总流量,取间隔时间段内的网止流量,完成网站流量监控,awk命令的完整例子。
shell脚本统计网卡流量
[chengmo@localhost ~]$ 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:1068205690 1288942839 0 0 0 0 0 0 1068205690 1288942839 0 0 0 0 0 0 eth0:91581844 334143895 0 0 0 0 0 145541676 4205113078 3435231517 0 0 0 0 0 0
说明:
awk 'BEGIN{
OFMT="%.3f"; devf="/proc/net/dev"; while(("cat "devf) | getline) { if($0 ~ /:/ && ($10+0) > 0) { split($1,tarr,":"); net[tarr[1]]=$10+tarr[2]; print tarr[1],$10+tarr[2]; } } close(devf); while((system("sleep 1 ")) >=0) { system("clear"); while( getline < devf ) { if($0 ~ /:/ && ($10+0) > 0) { split($1,tarr,":"); if(tarr[1] in net) { print tarr[1],":",($10+tarr[2]-net[tarr[1]])*8/1024,"kb/s"; net[tarr[1]]=$10+tarr[2]; } } } close(devf); } }'
说明:
注意:
运行结果: |