Shell 腳本如何監(jiān)控程序占用帶寬?
眾所周知,使用iftop能監(jiān)控所有程序占用的網(wǎng)絡帶寬,一般情況下,手動執(zhí)行iftop就可查看。但現(xiàn)在需要使用腳本來監(jiān)控程序占用的帶寬,遇到的問題真不是一點半點,現(xiàn)記錄如下,希望能給其它運維人帶來更多的幫助。
中途所遇到的難點:
1.iftop把結(jié)果重定向到文本中,是圖形格式的
重定向到文本中的內(nèi)容,全部是一行,根本無法用腳本取值。最開始我使用python讀取這個文件,得到所有特殊符號,找到規(guī)律,然后使用sed替換成規(guī)范的格式。終于在自己測試機上完成,能展示出正常的格式。當放到線上機器時,特殊符號變了…又變成亂糟糟的了。網(wǎng)上找了很久的資料,終于找到了解決方法:iftop 1.0-pre之后的版本都能輸出文本格式,之前用的是iftop 0.7版本。當晚心里有種流淚的感覺,弄了一天,結(jié)果有簡單現(xiàn)成的方法。。。
2.一個程序不僅僅只使用一個端口
原以為程序僅僅監(jiān)聽一個端口進行通信,后來詢問研發(fā)得知,當這個程序是服務端的時候,端口是固定的;當這個程序主動訪問外面的時候,端口是隨機的。所以要想監(jiān)控的準確,必須找到這個程序打開的所有端口。解決方法是:用netstat所這個程序的所有端口找出來。
3.iftop輸出的流量單位不一樣,且沒有調(diào)整一致的命令
單位不一樣,里面有Mb,Kb,b單位,需要進行換算。我的解決方法是:把Mb替換成*1000,把Kb替換成空,把b直接不要過濾掉。最后用bc一算直接得結(jié)果。
4.程序發(fā)送占用帶寬好算,接收帶寬不好算
根據(jù)第2步找到的幾個端口,過濾出發(fā)送出去的流量一加就可以。但是接收的怎么算?見上邊圖中第一條流量,有"<="的則為接收流量,"<="這些行都是未知的IP與端口,怎么把它過濾出來得出結(jié)果??我的解決方法是:把"=>"行和"<="放兩個臨時文件中,圖中有"=>"的行第一列都有序號,那么全部是"<="行的都和它一一對應,如:發(fā)送"=>"中的是序號12,13,15。那么"<="文件中的第12,13,15行就是對應的接收流量。。是不是理解了?
5.shell腳本代碼如下
- #!/bin/sh
 - #author:yangrong
 - #mail:10286460@qq.com
 - #date:2014-05-14
 - file_name="test.txt"
 - temp_file1="liuliang.txt"
 - temp_file2="liuliang2.txt"
 - iftop -Pp -Nn -t -L 100 -s 1 >$temp_file1
 - pragrom_list=(VueDaemon VueCenter VueAgent VueCache VueSERVER VUEConnector Myswitch Slirpvde)
 - #pragrom_list=(VueSERVER VueCenter)
 - >$file_name
 - for i in ${pragrom_list[@]}
 - do
 - port_list=`netstat -plnt|grep $i|awk '{print $4}'|awk -F: '{print $2}'`
 - port_all=""
 - for port in $port_list
 - do
 - port_all="${port}|${port_all}"
 - port_all=`echo $port_all|sed 's/\(.*\)|$/\1/g'`
 - done
 - if [[ $port_all == "" ]];then
 - echo "${i}sendflow=0" >> $file_name
 - echo "${i}receiveflow=0" >> $file_name
 - continue
 - fi
 - send_flow=`cat $temp_file1 |grep -E "${port_all}"|grep -E 'Mb|Kb'|grep '=>'|awk '{print $4}'|\
 - tr '\n' '+' |sed -e s/Mb/*1000/g |sed s/Kb//g |sed 's/\(.*\)+$/\1\n/g'|bc`
 - #echo "cat liuliang.txt |grep -E "${port_all}"|grep -E 'Mb|Kb'|grep '=>'|awk '{print $4}'|\
 - #tr '\n' '+' |sed -e s/Mb/*1000/g |sed s/Kb//g |sed 's/\(.*\)+$/\1\n/g'|bc"
 - if [[ ${send_flow} == "" ]];then
 - send_flow=0
 - fi
 - send_num=`cat $temp_file1 |grep -E "${port_all}"|grep "=>"|awk '{print $1}'`
 - echo "" > $temp_file2
 - for num in $send_num
 - do
 - cat $temp_file1 |grep '<='|sed -n ${num}p|grep -E 'Mb|Kb' >>$temp_file2
 - done
 - receive_flow=`cat $temp_file2 |grep -E 'Mb|Kb'|awk '{print $4}'|\
 - tr '\n' '+' |sed -e s/Mb/*1000/g |sed s/Kb//g |sed 's/\(.*\)+$/\1\n/g'|bc`
 - if [[ $receive_flow == "" ]];then
 - receive_flow=0
 - fi
 - echo "${i}sendflow=${send_flow}" >>$file_name
 - echo "${i}receiveflow=${receive_flow}" >>$file_name
 - done
 
6.shell腳本執(zhí)行效果
腳本中定義的進程列表為:pragrom_list=(VueDaemonVueCenter VueAgent VueCache VueSERVER VUEConnector Myswitch Slirpvde)
執(zhí)行腳本的輸出單位是Kb。
7.附:iftop命令用法
- [root@center230 python]# iftop --help
 - iftop: unknown option --
 - iftop: display bandwidth usage on aninterface by host
 - Synopsis: iftop -h | [-npblNBP] [-iinterface] [-f filter code]
 - [-F net/mask][-G net6/mask6]
 - -h display thismessage #幫助信息
 - -n don't do hostname lookups #禁用主機解析,即不會出現(xiàn)IP顯示域名
 - -N don't convertport numbers to services #以數(shù)字為示端口號,如21端口不會顯示成ftp
 - -p run inpromiscuous mode (show traffic between other
 - hosts on the samenetwork segment)
 - -b don't displaya bar graph of traffic #以b單位顯示
 - -B Displaybandwidth in bytes #以B單位顯示
 - -iinterface listen on namedinterface #指定監(jiān)聽的網(wǎng)口
 - -ffilter code use filter code toselect packets to count
 - (default: none, but onlyIP packets are counted)
 - -Fnet/mask show traffic flowsin/out of IPv4 network #顯示指定Ipv4段流量
 - -Gnet6/mask6 show traffic flowsin/out of IPv6 network #顯示指定Ipv6段流量
 - -l display andcount link-local IPv6 traffic (default: off) #顯示Ipv6的流量
 - -P show ports aswell as hosts #顯示端口信息
 - -mlimit sets the upper limit forthe bandwidth scale
 - -cconfig file specifies an alternativeconfiguration file
 - -t use textinterface without ncurses #使用文本模式輸出
 - Sorting orders:
 - -o2s Sort by first column(2s traffic average) #按2s平均流量列排序
 - -o10s Sort by second column(10s traffic average) [default] #按10s平均流量列排序
 - -o40s Sort by third column(40s traffic average) #按50s平均流量列排序
 - -osource Sort by source address #按源IP列排序
 - -odestination Sort by destinationaddress #按目的IP列排序
 - The following options are only available in combination with -t
 - -snum print one single textoutput afer num seconds, then quit #指定刷新幾次。
 - -Lnum number of lines to print #顯示多少行數(shù)據(jù)。當程序多流量大時,則要顯示行數(shù)多些才行。
 - iftop, version 1.0pre4 #版本信息。
 
文本輸出方法:
- iftop -Pp -Nn -t -L 100 -s 1 >temp_file
 
直接查看輸iftop 即可。
iftop詳細用法見網(wǎng)上文檔。
http://www.vpser.net/manage/iftop.html
總結(jié):
1、先盡可能的尋找已有方法。
2、基本功要雜實,對sed,awk,grep等命令要熟練使用。
3、思路要靈活多變,不能被一種方法束縛死。

















 
 
 





 
 
 
 