题目要求
写一个脚本,检测你的网络流量,并记录到一个日志里。须要按照以下格式,而且一分钟统计一次(只须要统>计外网网卡,假设网卡名字为eth0): 2017-08-04 01:11 eth0 input: 1000bps eth0 output : 200000bpsbash
2017-08-04 01:12 eth0 input: 1000bps eth0 output : 200000bps 提示:使用sar -n DEV 1 59 这样能够统计一分钟的平均网卡流量,只须要最后面的平均值。另外,注意换>算一下,1Byte=8bit网络
#!/bin/bash logdir=/tmp/sar_log file=$logdir/`date +%d%H`.log t=`date +"%F %H:%M"` [ -d $logdir ] || mkdir -p $logdir LANG=en sar -n DEV 1 5 |grep eth0 |grep "Average" > /tmp/sar.tmp exec >>$file echo "$t" awk '{print "eth0 input:",$5*8000"bps""\n""eth0 output:",$6*8000"bps"}' /tmp/sar.tmp echo "#### ###################"