手机
当前位置:查字典教程网 >操作系统 >unix linux >shell awk实现实时监控网卡流量脚本(常见应用二)
shell awk实现实时监控网卡流量脚本(常见应用二)
摘要:实现原理:[chengmo@localhost~]$cat/proc/net/devInter-|Receive|Transmitface|...

实现原理:

[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

proc/net/dev 文件保存了网卡总流量信息,通过间隔一段间隔,将入网卡与出记录加起来。减去之前就得到实际速率。

程序代码:

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);

}

}'

说明:第一个while 是获得总的初始值,$1是网卡出流量,$10是网卡进流量。第2个while会间隔1秒钟启动一次。计算总流量差得到平均每秒流量。

注意:通过getline 逐行读取文件,需要close关闭 。否则在第2次while循环中不能获得数据。

运行结果:

shell awk实现实时监控网卡流量脚本(常见应用二)1

【shell awk实现实时监控网卡流量脚本(常见应用二)】相关文章:

混合使用Linux和Windows

linux格式化新硬盘并挂载并设置开机自动挂载

php创建带有效期的linux账户

linux下删除带-号的文件

Linux系统下如何查看已经登录用户

十招提高Linux系统安全性的设置方法

linux如何调节和维护内核详细介绍

linux shell 常用脚本语句语法收集 推荐

学习Linux之特性综述

shell后台定时任务时crontab的用法

精品推荐
分类导航