python版监控linux流量
直接上代码,使用OptionParser来传入参数
#coding:utf-8#-------------#Author:Hu#Data:20150520#-------------from __future__ import divisionimport reimport timefrom optparse import OptionParserdef getbandwidth(eth='eth0',intevel=1): a=open('/proc/net/dev') data=a.read() patten=eth + '.*' if not re.search(patten,data): print "The ETHname not have" exit(1) Rev_old=re.search(patten,data).group().replace(':',' ').split()[1] Send_old=re.search(patten,data).group().replace(':',' ').split()[9] a.close() while True: #print intevel time.sleep(int(intevel)) a=open('/proc/net/dev') data=a.read() Rev=re.search(patten,data).group().replace(':',' ').split()[1] Send=re.search(patten,data).group().replace(':',' ').split()[9] diff_Rev=int(Rev)-int(Rev_old) diff_Sen=int(Send)-int(Send_old) diff_M=diff_Rev*8/1024/1024/int(intevel) diff_S=diff_Sen*8/1024/1024/int(intevel) print time.strftime("%Y%m%d %H:%M:%S") + ' The Recevie is %6.2f Mbps(byte is %d)' % (diff_M,diff_Rev) + ' The Send is %6.2f Mbps(byte is %d)' % (diff_S,diff_Sen) Rev_old=Rev Send_old=Send a.close()if __name__=='__main__': import sys usage='''%prog [-i ethname] [-t interveltime] Example:%prog -i eth0 -t 1''' parser=OptionParser(usage=usage,version='2.0_20150602') parser.add_option('-i','--interface',dest='interface',default='eth0',help='Wann to interface') parser.add_option('-t','--time',dest='intevel',type='int',default='1',help='The intevel time') (options,args)=parser.parse_args() print "The interafce is %s and the intevel time is %d" % (options.interface,options.intevel) getbandwidth(options.interface,options.intevel)
使用方法:
'''%prog [-i ethname] [-t interveltime] Example:%prog -i eth0 -t 1''' 默认是eth0 ,时间间隔是1 效果如下:
[root@iZ94nv1rj5tZ tools]# ./bandwidth3.py -versionUsage: bandwidth3.py [-i ethname] [-t interveltime] Example:bandwidth3.py -i eth0 -t 1bandwidth3.py: error: no such option: -v[root@iZ94nv1rj5tZ tools]# ./bandwidth3.py -i eth1 -t 1The interafce is eth1 and the intevel time is 120151113 16:51:43 The Recevie is 0.01 Mbps(byte is 1876) The Send is 0.00 Mbps(byte is 154)20151113 16:51:44 The Recevie is 0.02 Mbps(byte is 2156) The Send is 0.00 Mbps(byte is 202)20151113 16:51:45 The Recevie is 0.08 Mbps(byte is 10482) The Send is 0.00 Mbps(byte is 202)20151113 16:51:46 The Recevie is 0.02 Mbps(byte is 2014) The Send is 0.00 Mbps(byte is 202)20151113 16:51:47 The Recevie is 0.02 Mbps(byte is 2612) The Send is 0.00 Mbps(byte is 202)20151113 16:51:48 The Recevie is 0.09 Mbps(byte is 11394) The Send is 0.00 Mbps(byte is 202)
2016-01-08 更新:
最新在centos 7中测试时,发现centos的网卡与6的有点不一样,如下:
[root@localhost tools]# cat /proc/net/devInter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed eno3: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 eno4: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 eno5: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0enp0s29u1u1u5: 59304 2097 0 0 0 0 0 0 0 0 0 0 0 0 0 0 eth2: 534252412 394444 0 56 0 0 0 0 117712356 277855 0 0 0 0 0 0 lo: 2808 27 0 0 0 0 0 0 2808 27 0 0 0 0 0 0virbr0-nic: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0virbr0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
centos 6如下:
[root@localhost tools]# cat /proc/net/devInter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed lo:940478306 9822773 0 0 0 0 0 0 940478306 9822773 0 0 0 0 0 0 eth0:892682633544 786583176 0 0 0 0 0 7086639 138419689066 361042275 0 0 0 0 0 0 eth1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 usb0:88358010 1359354 0 0 0 0 0 0 438 5 0 0 0 0 0 0
与老版的linux有区别,centos 7网卡名后有空格,但centos 6以下的网卡名后没空格。
所以,如果该脚步要在7中使用,要将
Send_old=re.search(patten,data).group().replace(':',' ').split()[9] 改成:
Send_old=re.search(patten,data).group().replace(':',' ').split()[10] 即可。
如有问题,请联系362299908@qq.com