亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

shell awk實現(xiàn)實時監(jiān)控網(wǎng)卡流量腳本(常見應用二)

  發(fā)布時間:2013-11-19 21:50:42   作者:佚名   我要評論
通過第3方工具獲得網(wǎng)卡流量,這個大家一定很清楚。其實通過腳本一樣可以實現(xiàn)效果。下面是我個人工作中整理的數(shù)據(jù)。以下是shell腳本統(tǒng)計網(wǎng)卡流量

實現(xiàn)原理:
[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 文件保存了網(wǎng)卡總流量信息,通過間隔一段間隔,將入網(wǎng)卡與出記錄加起來。減去之前就得到實際速率。

程序代碼:


復制代碼
代碼如下:

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是網(wǎng)卡出流量,$10是網(wǎng)卡進流量。第2個while會間隔1秒鐘啟動一次。計算總流量差得到平均每秒流量。

注意:通過getline 逐行讀取文件,需要close關(guān)閉 。否則在第2次while循環(huán)中不能獲得數(shù)據(jù)。

運行結(jié)果:

相關(guān)文章

最新評論