歡迎您光臨本站 註冊首頁

linux下網路流量監控統計

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
最近在做虛擬化遷入評估,其中很重要的一項就是流量的問題.現在部署一個工具和腳本用來統計伺服器的網路流量. 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 這條命令的意思就是1秒鐘輸出一次,總共輸出6次.輸出網卡的流量.這樣一目了然. 接下來部署腳本分別用來記錄和統計流量情況. 1、vi /home/tools/tongji/network/network_record.sh #/bin/sh
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
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 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 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;
} } END {
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 這樣連續幾天就可以得出本台伺服器的流量大致情況.


[火星人 ] linux下網路流量監控統計已經有432次圍觀

http://coctec.com/docs/linux/show-post-52086.html