开发者

ifstat logging bandwidth usage

i just learnt that ifstat display network usage, and below command gives what i want on the console screen

开发者_JS百科
/usr/bin/ifstat -i eth0 -b -n | awk 'NR>2 {print "Download" $1 "upload" $2}'

i want to output that into a file so that i can use javascript library to plot those values..

but i cannot seem to output the logs into a file

/usr/bin/ifstat -i eth0 -b -n | awk 'NR>2 {print "Download" $1 "upload" $2}' > bandwidth.txt

bandwidth.txt is empty

can someone advise how to log network bandwidth usage into a file (collectl seems to be interesting but cannot figure out to get the uplaod and download stats)


awk is buffering its output. Normally, most programs use line buffering when stdout is a terminal but switch to larger buffer sizes when stdout is not a terminal, and awk is no exception. If you ran your command for long enough, you'd eventually see the file size grow in chunks of size 4096 or so (possibly more or less) as the buffer fills up and gets flushed.

To force awk to flush its buffer after every line regardless of whether or not stdout is a terminal, add an fflush() command:

/usr/bin/ifstat -i eth0 -b -n | awk 'NR>2 {print "Download" $1 "upload" $2; fflush()}' > bandwidth.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜