forked from innogames/igcollect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_network.py
executable file
·72 lines (60 loc) · 3.16 KB
/
linux_network.py
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
#
# igcollect - Linux network
#
# Copyright (c) 2016, InnoGames GmbH
#
from __future__ import print_function
import socket, time, sys
def resolve_to_vserver(vifname=False):
''' returns the vserver name for a given virtual network interface'''
def get_netdev_dict():
''' returns a dictionary made from /proc/net/dev '''
try:
nd = open('/proc/net/dev','r')
netdev_data = nd.readlines(1024)
nd.close()
except:
sys.exit(1)
netdev_dict = {}
header=[]
for line in netdev_data:
if line.find('Inter') != -1:
''' header 1 '''
elif line.find(' face |bytes') != -1:
''' header 2 '''
a,rx_header,tx_header = line.split('|')
for i in rx_header.split():
header.append('rx_'+i)
for i in tx_header.split():
header.append('tx_'+i)
else:
''' here we have to handle some kind of interface
first the interface name than the counters as mentioned
in the header'''
x = line.strip().split()
if_name = x.pop(0).strip(' :')
netdev_dict[if_name]={}
for i in header:
netdev_dict[if_name][i] = x.pop(0)
return(netdev_dict)
graphite_data=''
hostname = socket.gethostname().replace('.','_')
now = str(int(time.time()))
nd = get_netdev_dict()
for interface in nd:
if not interface.startswith('vif'):
graphite_data += 'servers.%s.system.network.%s.bytesIn %s %s\n' % (hostname,interface, str(nd[interface]['rx_bytes']), now )
graphite_data += 'servers.%s.system.network.%s.bytesOut %s %s\n' % (hostname,interface, str(nd[interface]['tx_bytes']), now )
graphite_data += 'servers.%s.system.network.%s.pktsIn %s %s\n' % (hostname,interface, str(nd[interface]['rx_packets']), now )
graphite_data += 'servers.%s.system.network.%s.pktsOut %s %s\n' % (hostname,interface, str(nd[interface]['tx_packets']), now )
graphite_data += 'servers.%s.system.network.%s.errsIn %s %s\n' % (hostname,interface, str(nd[interface]['rx_errs']), now )
graphite_data += 'servers.%s.system.network.%s.errsOut %s %s\n' % (hostname,interface, str(nd[interface]['tx_errs']), now )
graphite_data += 'servers.%s.system.network.%s.dropIn %s %s\n' % (hostname,interface, str(nd[interface]['rx_drop']), now )
graphite_data += 'servers.%s.system.network.%s.dropOut %s %s\n' % (hostname,interface, str(nd[interface]['tx_drop']), now )
graphite_data += 'servers.%s.system.network.%s.fifoIn %s %s\n' % (hostname,interface, str(nd[interface]['rx_fifo']), now )
graphite_data += 'servers.%s.system.network.%s.fifoOut %s %s\n' % (hostname,interface, str(nd[interface]['tx_fifo']), now )
graphite_data += 'servers.%s.system.network.%s.frameIn %s %s\n' % (hostname,interface, str(nd[interface]['rx_frame']), now )
graphite_data += 'servers.%s.system.network.%s.collsOut %s %s\n' % (hostname,interface, str(nd[interface]['tx_colls']), now )
graphite_data += 'servers.%s.system.network.%s.carrierOut %s %s\n' % (hostname,interface, str(nd[interface]['tx_carrier']), now )
print(graphite_data)