日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

python采集bandwidth信息

發(fā)布時(shí)間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python采集bandwidth信息 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

舊博文,搬到 csdn
原文:http://rebootcat.com/2018/05/21/analyze_bandwidth/

python腳本采集bandwidth

經(jīng)常要做一些 linux 系統(tǒng)上的性能分析或者采集 cpu/mem/bandwidth 上報(bào)到監(jiān)控系統(tǒng)。

分享一個(gè)我平常常用到的 bandwidth 采集腳本,原理是分析 /proc/net/dev 文件, 腳本如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#腳本探測(cè)網(wǎng)卡流入帶寬,循環(huán)輸出import os
import time
import pdbfilename = './bandwidth.log''''
[root@~]$ cat /proc/net/dev
Inter-|   Receive                                                |  Transmitface |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressedlo: 1144779885672 14057281982    0    0    0     0          0         0 1144779885672 14057281982    0    0    0     0       0          0eth0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0eth1:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0eth2: 26686495240 203608963    0    0    0     0          0         1 78529414436 193724479    0    0    0     0       0          0eth3: 10038847365 82467612    0    0    0     0          0         0 26209215795 64571217    0    0    0     0       0          0bond0: 36725342605 286076575    0    0    0     0          0         1 104738630231 258295696    0    0    0     0       0          0
'''def get_rx(interface = 'eth0'):rsbytes = []cmd = 'cat /proc/net/dev'r = os.popen(cmd).readlines()if len(r) < 4:print "error: can't find eth interface"return rsbytesinterface_dict = {}for i in xrange(2,len(r),1): #從 lo 開始interface_name = r[i].split()[0].split(':')[0]interface_dict[interface_name] = iif interface in interface_dict:position = interface_dict.get(interface)recvbytes = r[position].split()[1]sendbytes = r[position].split()[9]rsbytes.append(int(recvbytes))rsbytes.append(int(sendbytes))return rsbytesdef iftop_interface(interface = 'eth0'):begin = int(time.time())beginrs = get_rx(interface)if not beginrs:print 'error: can not find interface %s' % interfacereturnwhile True:time.sleep(2)endrs = get_rx(interface)end = int(time.time())rxrate = float((endrs[0] - beginrs[0])) / (end - begin)  * 8sxrate = float((endrs[1] - beginrs[1])) / (end - begin)  * 8tl = time.localtime(end)date = time.strftime('%m-%d %H:%M:%S', tl)cout = "%s  [recv(rate) = %s Mbps] [send(rate) = %s Mbps] \n" % (date,rxrate / 1000000,sxrate / 1000000)fout = open(filename,'a')fout.write(cout)fout.close()print cout#重新賦值,進(jìn)入再次循環(huán)begin,beginrs = end,endrsif __name__ == "__main__":iftop_interface('ens33')    

默認(rèn)的網(wǎng)卡名字是 eth0,有些機(jī)器可能會(huì)不一樣,只需要修改成你自己機(jī)器的網(wǎng)卡名稱就行。

腳本可以直接在 我的github 進(jìn)行下載。

其他

歡迎關(guān)注下我的其他腳本,平常可能會(huì)用到的一些腳本,整理了一下。

https://github.com/smaugx/dailytools

Blog:

  • rebootcat.com

  • email: linuxcode2niki@gmail.com

2018-05-21 于杭州
By 史矛革

總結(jié)

以上是生活随笔為你收集整理的python采集bandwidth信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。