python采集bandwidth信息

python脚本采集bandwidth

经常要做一些 linux 系统上的性能分析或者采集 cpu/mem/bandwidth 上报到监控系统。

分享一个我平常常用到的 bandwidth 采集脚本,原理是分析 /proc/net/dev 文件, 脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#脚本探测网卡流入带宽,循环输出

import os
import time
import pdb

filename = './bandwidth.log'

'''
[root@~]$ cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 1144779885672 14057281982 0 0 0 0 0 0 1144779885672 14057281982 0 0 0 0 0 0
eth0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
eth1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
eth2: 26686495240 203608963 0 0 0 0 0 1 78529414436 193724479 0 0 0 0 0 0
eth3: 10038847365 82467612 0 0 0 0 0 0 26209215795 64571217 0 0 0 0 0 0
bond0: 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 rsbytes
interface_dict = {}
for i in xrange(2,len(r),1): #从 lo 开始
interface_name = r[i].split()[0].split(':')[0]
interface_dict[interface_name] = i

if 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 rsbytes


def iftop_interface(interface = 'eth0'):
begin = int(time.time())
beginrs = get_rx(interface)
if not beginrs:
print 'error: can not find interface %s' % interface
return
while True:
time.sleep(2)
endrs = get_rx(interface)
end = int(time.time())
rxrate = float((endrs[0] - beginrs[0])) / (end - begin) * 8
sxrate = float((endrs[1] - beginrs[1])) / (end - begin) * 8
tl = 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

#重新赋值,进入再次循环
begin,beginrs = end,endrs


if __name__ == "__main__":
iftop_interface('ens33')

默认的网卡名字是 eth0,有些机器可能会不一样,只需要修改成你自己机器的网卡名称就行。

脚本可以直接在 我的github 进行下载。

其他

欢迎关注下我的其他脚本,平常可能会用到的一些脚本,整理了一下。

https://github.com/smaugx/dailytools

Blog:

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

buy me a cola!

欢迎关注我的其它发布渠道