国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

監控linux流量python版

2019-11-14 16:54:44
字體:
來源:轉載
供稿:網友

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)

 



如有問題,請聯系362299908@QQ.com

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 揭东县| 浙江省| 甘孜| 渝中区| 平利县| 新乡市| 城市| 衢州市| 祁东县| 九龙城区| 大石桥市| 池州市| 漳州市| 远安县| 鄂伦春自治旗| 定西市| 泸溪县| 玛纳斯县| 湘潭市| 桐梓县| 招远市| 罗城| 罗田县| 龙门县| 綦江县| 疏勒县| 萍乡市| 上林县| 延津县| 宁晋县| 钟山县| 贺兰县| 武邑县| 隆子县| 读书| 大悟县| 麻栗坡县| 高邮市| 鄂尔多斯市| 库尔勒市| 子长县|