自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Shell 腳本如何監(jiān)控程序占用帶寬?

運維 系統(tǒng)運維
眾所周知,使用iftop能監(jiān)控所有程序占用的網(wǎng)絡(luò)帶寬,一般情況下,手動執(zhí)行iftop就可查看。但現(xiàn)在需要使用腳本來監(jiān)控程序占用的帶寬,遇到的問題真不是一點半點,現(xiàn)記錄如下,希望能給其它運維人帶來更多的幫助。

眾所周知,使用iftop能監(jiān)控所有程序占用的網(wǎng)絡(luò)帶寬,一般情況下,手動執(zhí)行iftop就可查看。但現(xiàn)在需要使用腳本來監(jiān)控程序占用的帶寬,遇到的問題真不是一點半點,現(xiàn)記錄如下,希望能給其它運維人帶來更多的幫助。

中途所遇到的難點:


1.iftop把結(jié)果重定向到文本中,是圖形格式的

重定向到文本中的內(nèi)容,全部是一行,根本無法用腳本取值。最開始我使用python讀取這個文件,得到所有特殊符號,找到規(guī)律,然后使用sed替換成規(guī)范的格式。終于在自己測試機(jī)上完成,能展示出正常的格式。當(dāng)放到線上機(jī)器時,特殊符號變了…又變成亂糟糟的了。網(wǎng)上找了很久的資料,終于找到了解決方法:iftop 1.0-pre之后的版本都能輸出文本格式,之前用的是iftop 0.7版本。當(dāng)晚心里有種流淚的感覺,弄了一天,結(jié)果有簡單現(xiàn)成的方法。。。

2.一個程序不僅僅只使用一個端口

 原以為程序僅僅監(jiān)聽一個端口進(jìn)行通信,后來詢問研發(fā)得知,當(dāng)這個程序是服務(wù)端的時候,端口是固定的;當(dāng)這個程序主動訪問外面的時候,端口是隨機(jī)的。所以要想監(jiān)控的準(zhǔn)確,必須找到這個程序打開的所有端口。解決方法是:用netstat所這個程序的所有端口找出來。

3.iftop輸出的流量單位不一樣,且沒有調(diào)整一致的命令

單位不一樣,里面有Mb,Kb,b單位,需要進(jìn)行換算。我的解決方法是:把Mb替換成*1000,把Kb替換成空,把b直接不要過濾掉。最后用bc一算直接得結(jié)果。

wKioL1NzkHfAk4YzAAL8FIXqbyA454.jpg

4.程序發(fā)送占用帶寬好算,接收帶寬不好算

  根據(jù)第2步找到的幾個端口,過濾出發(fā)送出去的流量一加就可以。但是接收的怎么算?見上邊圖中第一條流量,有"<="的則為接收流量,"<="這些行都是未知的IP與端口,怎么把它過濾出來得出結(jié)果??我的解決方法是:把"=>"行和"<="放兩個臨時文件中,圖中有"=>"的行第一列都有序號,那么全部是"<="行的都和它一一對應(yīng),如:發(fā)送"=>"中的是序號12,13,15。那么"<="文件中的第12,13,15行就是對應(yīng)的接收流量。。是不是理解了?

5.shell腳本代碼如下

  1. #!/bin/sh  
  2. #author:yangrong  
  3. #mail:10286460@qq.com  
  4. #date:2014-05-14  
  5. file_name="test.txt"  
  6. temp_file1="liuliang.txt"  
  7. temp_file2="liuliang2.txt"  
  8. iftop  -Pp -Nn -t -L 100 -s 1 >$temp_file1  
  9. pragrom_list=(VueDaemon VueCenter VueAgent VueCache VueSERVER VUEConnector Myswitch Slirpvde)  
  10. #pragrom_list=(VueSERVER VueCenter)  
  11. >$file_name  
  12. for i in ${pragrom_list[@]}  
  13. do  
  14.         port_list=`netstat -plnt|grep $i|awk '{print $4}'|awk -F: '{print $2}'`  
  15.         port_all=""  
  16.         for port in $port_list  
  17.         do  
  18.                 port_all="${port}|${port_all}"  
  19.                 port_all=`echo $port_all|sed 's/\(.*\)|$/\1/g'`  
  20.         done  
  21.         if [[ $port_all == "" ]];then  
  22.                 echo "${i}sendflow=0>> $file_name  
  23.                 echo "${i}receiveflow=0>> $file_name  
  24.                 continue  
  25.         fi  
  26.         send_flow=`cat $temp_file1 |grep -E "${port_all}"|grep -E 'Mb|Kb'|grep '=>'|awk '{print $4}'|\  
  27.         tr '\n' '+' |sed -e s/Mb/*1000/g |sed s/Kb//g |sed  's/\(.*\)+$/\1\n/g'|bc`  
  28.         #echo "cat liuliang.txt |grep -E "${port_all}"|grep -E 'Mb|Kb'|grep '=>'|awk '{print $4}'|\  
  29.         #tr '\n' '+' |sed -e s/Mb/*1000/g |sed s/Kb//g |sed  's/\(.*\)+$/\1\n/g'|bc"  
  30.         if [[ ${send_flow} == "" ]];then  
  31.                 send_flow=0  
  32.         fi  
  33.         send_num=`cat $temp_file1 |grep -E "${port_all}"|grep "=>"|awk '{print $1}'`  
  34.         echo "" > $temp_file2  
  35.         for num in $send_num  
  36.         do  
  37.           cat $temp_file1 |grep  '<='|sed -n ${num}p|grep -E 'Mb|Kb' >>$temp_file2  
  38.         done  
  39.         receive_flow=`cat $temp_file2 |grep -E 'Mb|Kb'|awk '{print $4}'|\  
  40.         tr '\n' '+' |sed -e s/Mb/*1000/g |sed s/Kb//g |sed  's/\(.*\)+$/\1\n/g'|bc`  
  41.         if [[ $receive_flow == "" ]];then  
  42.                 receive_flow=0  
  43.         fi  
  44.         echo "${i}sendflow=${send_flow}" >>$file_name  
  45.         echo "${i}receiveflow=${receive_flow}" >>$file_name  
  46. done  

6.shell腳本執(zhí)行效果

腳本中定義的進(jìn)程列表為:pragrom_list=(VueDaemonVueCenter VueAgent VueCache VueSERVER VUEConnector Myswitch Slirpvde)

執(zhí)行腳本的輸出單位是Kb。

wKioL1NzkHiDVYQtAAIVY8-h-SU791.jpg

7.附:iftop命令用法

  1. [root@center230 python]# iftop --help 
  2. iftop: unknown option -- 
  3. iftop: display bandwidth usage on aninterface by host 
  4. Synopsis: iftop -h | [-npblNBP] [-iinterface] [-f filter code] 
  5.                               [-F net/mask][-G net6/mask6] 
  6.  -h                  display thismessage #幫助信息 
  7.  -n                  don't do hostname lookups  #禁用主機(jī)解析,即不會出現(xiàn)IP顯示域名 
  8.  -N                  don't convertport numbers to services #以數(shù)字為示端口號,如21端口不會顯示成ftp 
  9.  -p                  run inpromiscuous mode (show traffic between other 
  10.                       hosts on the samenetwork segment) 
  11.  -b                  don't displaya bar graph of traffic   #以b單位顯示 
  12.  -B                  Displaybandwidth in bytes    #以B單位顯示 
  13.   -iinterface        listen on namedinterface   #指定監(jiān)聽的網(wǎng)口 
  14.   -ffilter code      use filter code toselect packets to count     
  15.                       (default: none, but onlyIP packets are counted) 
  16.   -Fnet/mask         show traffic flowsin/out of IPv4 network  #顯示指定Ipv4段流量 
  17.   -Gnet6/mask6       show traffic flowsin/out of IPv6 network  #顯示指定Ipv6段流量 
  18.  -l                  display andcount link-local IPv6 traffic (default: off) #顯示Ipv6的流量 
  19.  -P                  show ports aswell as hosts  #顯示端口信息 
  20.   -mlimit            sets the upper limit forthe bandwidth scale   
  21.   -cconfig file      specifies an alternativeconfiguration file 
  22.  -t                  use textinterface without ncurses #使用文本模式輸出 
  23.  Sorting orders: 
  24.   -o2s                Sort by first column(2s traffic average) #按2s平均流量列排序 
  25.   -o10s               Sort by second column(10s traffic average) [default] #按10s平均流量列排序 
  26.   -o40s               Sort by third column(40s traffic average) #按50s平均流量列排序 
  27.   -osource            Sort by source address #按源IP列排序 
  28.   -odestination       Sort by destinationaddress #按目的IP列排序 
  29.  The following options are only available in combination with -t 
  30.   -snum              print one single textoutput afer num seconds, then quit #指定刷新幾次。 
  31.   -Lnum              number of lines to print #顯示多少行數(shù)據(jù)。當(dāng)程序多流量大時,則要顯示行數(shù)多些才行。 
  32. iftop, version 1.0pre4  #版本信息。 

文本輸出方法:

  1. iftop -Pp -Nn -t -L 100 -s 1 >temp_file 

直接查看輸iftop 即可。

iftop詳細(xì)用法見網(wǎng)上文檔。

http://www.vpser.net/manage/iftop.html

總結(jié):

1、先盡可能的尋找已有方法。

2、基本功要雜實,對sed,awk,grep等命令要熟練使用。

3、思路要靈活多變,不能被一種方法束縛死。

責(zé)任編輯:黃丹 來源: 51TCO博客
相關(guān)推薦

2014-05-04 09:37:51

2014-04-28 09:11:31

應(yīng)用帶寬WLAN建設(shè)

2015-05-29 09:44:03

Trickle應(yīng)用程序

2022-05-02 18:29:35

bashshellLinux

2013-08-30 10:25:22

Shell主機(jī)監(jiān)控

2013-09-04 09:59:49

監(jiān)控 Shell 腳本

2020-11-02 08:23:36

shell腳本Linux

2021-07-02 06:54:44

Shell腳本 Linux

2014-08-13 14:48:01

LinuxShell腳本

2023-05-20 17:45:25

LinuxShell

2020-12-02 13:19:47

Shell監(jiān)控文件Linux

2021-08-09 11:36:53

Linux網(wǎng)絡(luò)帶寬命令

2009-07-20 15:42:34

監(jiān)控JRubyJProfiler

2020-09-23 06:00:04

ShellLinux郵件監(jiān)控

2020-04-01 15:11:36

Shell命令Linux

2019-05-24 08:19:59

2024-11-27 09:19:25

2019-08-09 13:50:08

shellLinux

2021-04-21 08:03:34

腳本Shell讀取

2021-08-20 10:46:25

Shell腳本文件Linux
點贊
收藏

51CTO技術(shù)棧公眾號