shell脚本如何监控程序占用带宽?

。当晚心里有种流泪的感觉,弄了一天,结果有简单现成的方法。。。

2.一个程序不仅仅只使用一个端口。

原以为程序仅仅监听一个端口进行通信,后来询问研发得知,当这个程序是服务端的时候,端口是固定的;当这个程序主动访问外面的时候,端口是随机的。所以要想监控的准确,必须找到这个程序打开的所有端口。解决方法是:用netstat所这个程序的所有端口找出来。

3.iftop输出的流量单位不一样,且没有调整一致的命令。

单位不一样,里面有Mb,Kb,b单位,需要进行换算。我的解决方法是:把Mb替换成*1000,把Kb替换成空,把b直接不要过滤掉。最后用bc一算直接得结果。

4.程序发送占用带宽好算,接收带宽不好算。

根据第2步找到的几个端口,过滤出发送出去的流量一加就可以。但是接收的怎么算?见上边图中第一条流量,有”<=”的则为接收流量,”<=”这些行都是未知的IP与端口,怎么把它过滤出来得出结果??我的解决方法是:把”=>”行和”<=”放两个临时文件中,图中有”=>”的行第一列都有序号,那么全部是”<=”行的都和它一一对应,如:发送”=>”中的是序号12,13,15。那么”<=”文件中的第12,13,15行就是对应的接收流量。。是不是理解了?

5.shell脚本代码如下#!/bin/sh#author:yangrong#mail:10286460@qq.com#date:2014-05-14file_name=”test.txt”temp_file1=”liuliang.txt”temp_file2=”liuliang2.txt”iftop -Pp -Nn -t -L 100 -s 1 >$temp_file1pragrom_list=(VueDaemon VueCenter VueAgent VueCache VueSERVER VUEConnector Myswitch Slirpvde)#pragrom_list=(VueSERVER VueCenter)>$file_namefor i in ${pragrom_list[@]}doport_list=`netstat -plnt|grep $i|awk ‘{print $4}’|awk -F: ‘{print $2}’`port_all=””for port in $port_listdoport_all=”${port}|${port_all}”port_all=`echo $port_all|sed ‘s/\(.*\)|$/\1/g’`doneif [[ $port_all == “” ]];thenecho “${i}sendflow=0” >> $file_nameecho “${i}receiveflow=0” >> $file_namecontinuefisend_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} == “” ]];thensend_flow=0fisend_num=`cat $temp_file1 |grep -E “${port_all}”|grep “=>”|awk ‘{print $1}’`echo “” > $temp_file2for num in $send_numdocat $temp_file1 |grep ‘<=’|sed -n ${num}p|grep -E ‘Mb|Kb’ >>$temp_file2donereceive_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 == “” ]];thenreceive_flow=0fiecho “${i}sendflow=${send_flow}” >>$file_nameecho “${i}receiveflow=${receive_flow}” >>$file_namedone

6.shell

-N don’t convertport numbers to services

-p run inpromiscuous mode (show traffic between other

hosts on the samenetwork segment)

-b don’t displaya bar graph of traffic

-B Displaybandwidth in bytes

-iinterface listen on namedinterface

-Gnet6/mask6 show traffic flowsin/out of IPv6 network

-l display andcount link-local IPv6 traffic (default: off)

-P show ports aswell as hosts

Sorting orders:

-o2s Sort by first column(2s traffic average)

-o10s Sort by second column(10s traffic average) [default]

-o40s Sort by third column(40s traffic average)

-osource Sort by source address

-odestination Sort by destinationaddress

The following options are only available in combination with -t

-snum print one single textoutput afer num seconds, then quit

iftop, version 1.0pre4 #版本信息。

文本输出方法:

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

直接查看输iftop 即可。

iftop详细用法见网上文档。

总结:

1、先尽可能的寻找已有方法。

2、基本功要杂实,对sed,awk,grep等命令要熟练使用。

3、思路要灵活多变,不能被一种方法束缚死。

本文出自 “此心安处是吾乡” 博客,,转载请与作者联系!

愚公因此敢移山,矢志不渝是前行,握紧拳头勇登攀,

shell脚本如何监控程序占用带宽?

相关文章:

你感兴趣的文章:

标签云: