最近在做虚拟化迁入评估,其中很重要的一项就是流量的问题。现在部署一个工具和脚本用来统计服务器的网络流量。
linux下监控流量的工具有很多,比如ifstat、iftop等。
个人还是喜欢ifstat,因为它统计起来比较容易。
先安装ifstat。
还是老步骤,先下载ifstat-1.1.tar.gz,然后解压缩。
接着老方法走:
./configure
make && make isntall
这样ifstat就按照完成了。
至于这个软件的其他功能这里就不讨论了,只用它的命令行。
例如“
ifstat 1 6”
得到如下的数据输出:
eth0 eth1
KB/s in KB/s out KB/s in KB/s out
36.04 115.09 0.06 0.23
37.55 159.04 0.12 0.18
26.72 93.16 0.21 0.22
82.34 247.28 0.12 0.18
96.46 243.81 0.06 0.18
124.16 238.18 0.06 0.18
KB/s in KB/s out KB/s in KB/s out
36.04 115.09 0.06 0.23
37.55 159.04 0.12 0.18
26.72 93.16 0.21 0.22
82.34 247.28 0.12 0.18
96.46 243.81 0.06 0.18
124.16 238.18 0.06 0.18
这条命令的意思就是1秒钟输出一次,总共输出6次。输出网卡的流量。这样一目了然。
接下来部署脚本分别用来记录和统计流量情况。
1、vi /home/tools/tongji/network/network_record.sh
#/bin/sh
DATE=`date +%Y%m%d`
DATE=`date +%Y%m%d`
/usr/local/bin/ifstat 6 10|sed '1,2d'|awk '{print $1"\t"$2}'>>/home/tools/tongji/network/$DATE.log
2、 vi /home/tools/tongji/network/network_flow_rate_statistics.sh
#!/bin/sh
DATE=`date +%Y%m%d`
HOME_DIR=/home/tools/tongji/network/
FILE=$HOME_DIR/$DATE.log
DATE=`date +%Y%m%d`
HOME_DIR=/home/tools/tongji/network/
FILE=$HOME_DIR/$DATE.log
STATIC_RESULT_FILE=$HOME_DIR/statistics_result.$DATE
cd /home/tools/tongji/network
awk '{print $1}' $FILE >$DATE.in.tmp
awk '{print $2}' $FILE >$DATE.out.tmp
awk '{print $1}' $FILE >$DATE.in.tmp
awk '{print $2}' $FILE >$DATE.out.tmp
echo "Everyday network flow rate statistics!!">>$STATIC_RESULT_FILE
echo "--------------------------------------------------------------------------------------">>$STATIC_RESULT_FILE
echo "The Inbound network flow rate statistics:">>$STATIC_RESULT_FILE
awk -f statistics.awk $DATE.in.tmp>>$STATIC_RESULT_FILE
echo "--------------------------------------------------------------------------------------">>$STATIC_RESULT_FILE
echo "The Outbound network flow rate statistics:">>$STATIC_RESULT_FILE
awk -f statistics.awk $DATE.out.tmp>>$STATIC_RESULT_FILE
echo "--------------------------------------------------------------------------------------">>$STATIC_RESULT_FILE
echo "The Inbound network flow rate statistics:">>$STATIC_RESULT_FILE
awk -f statistics.awk $DATE.in.tmp>>$STATIC_RESULT_FILE
echo "--------------------------------------------------------------------------------------">>$STATIC_RESULT_FILE
echo "The Outbound network flow rate statistics:">>$STATIC_RESULT_FILE
awk -f statistics.awk $DATE.out.tmp>>$STATIC_RESULT_FILE
rm -rf $DATE.in.tmp $DATE.out.tmp
3.vi statistics.awk
{
if( NR == 1) {
min = $1;
max = $1;
total = $1;
} else {
if( $1 < min ) { min = $1 }
if( $1 > max ) { max = $1 }
total += $1;
}
if( NR == 1) {
min = $1;
max = $1;
total = $1;
} else {
if( $1 < min ) { min = $1 }
if( $1 > max ) { max = $1 }
total += $1;
}
}
END {
avg = total * 1.0 / NR
print "Min:"min,"Avg:"avg,"Max:",max,"Num:",NR
}
avg = total * 1.0 / NR
print "Min:"min,"Avg:"avg,"Max:",max,"Num:",NR
}
我的计划任务是这样安排的:
#流量监控统计
*/1 1,5,8,9,13,16,17,20,22 * * * /home/tools/tongji/network/network_record.sh
10 23 * * * /home/tools/tongji/network/network_flow_rate_statistics.sh
这样,每天的23点10分就会生成一份关于每天流量的报告。类似于下面的内容:
more statistics_result.20100118
Everyday network flow rate statistics!!
--------------------------------------------------------------------------------------
The Inbound network flow rate statistics:
Min:2.15 Avg:28.2562 Max: 69.08 Num: 610
--------------------------------------------------------------------------------------
The Outbound network flow rate statistics:
Min:7.20 Avg:114.506 Max: 519.37 Num: 610
这样连续几天就可以得出本台服务器的流量大致情况。
本文出自 “锡叶的暗钵 ” 博客,请务必保留此出处http://skyeydemon.blog.51cto.com/315899/268270