Skip to content

Commit 177d8b7

Browse files
committed
network: add new iface plugin
- threaded - gather all interfaces and all counters Signed-off-by: Sylvain Rabot <[email protected]>
1 parent 9063c09 commit 177d8b7

3 files changed

Lines changed: 369 additions & 0 deletions

File tree

network/iface/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
iface
2+
=====
3+
4+
5+
Install
6+
-------
7+
8+
Copy iface.py from python_modules to your python modules directory, e.g. :
9+
10+
- /usr/lib/ganglia/python_modules
11+
- /usr/lib64/ganglia/python_modules
12+
13+
Copy iface.pyconf to the gmond conf.d directory, e.g. :
14+
15+
- /etc/ganglia/conf.d/
16+
17+
Tune the iface.pyconf file to match your server interfaces and then restart gmond.
18+
19+
## AUTHOR
20+
21+
Author: Sylvain Rabot https://github.com/sylr

network/iface/conf.d/iface.pyconf

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# iface module
2+
modules {
3+
module {
4+
name = 'iface'
5+
language = 'python'
6+
}
7+
}
8+
9+
collection_group {
10+
collect_every = 15
11+
time_threshold = 45
12+
13+
# The plugin gathers metrics for all interfaces and for all the counters
14+
# of the /proc/net/dev file. if you want to enable more interfaces or more
15+
# counters add a metric and change the name to match the interface, way and
16+
# counter you want to monitor :
17+
#
18+
# metric_name ::= "iface_<iface>_<way>_<counter>"
19+
# iface ::= lo|ethX|bondX|tunX|...
20+
# way ::= rx|tx
21+
# counter ::= bytes|packets|errs|drop|fifo|frame|compressed|multicast
22+
#
23+
# e.g. :
24+
#
25+
# - iface_bond0_rx_frame
26+
# - iface_tun2_tx_fifo
27+
28+
# loopback
29+
metric {
30+
name = "iface_lo_rx_bytes"
31+
title = "loopback RX bytes"
32+
value_threshold = 1.0
33+
}
34+
35+
metric {
36+
name = "iface_lo_rx_packets"
37+
title = "loopback RX packets"
38+
value_threshold = 1.0
39+
}
40+
41+
metric {
42+
name = "iface_lo_tx_bytes"
43+
title = "loopback TX bytes"
44+
value_threshold = 1.0
45+
}
46+
47+
metric {
48+
name = "iface_lo_tx_packets"
49+
title = "loopback TX packets"
50+
value_threshold = 1.0
51+
}
52+
53+
# eth0
54+
metric {
55+
name = "iface_eth0_rx_bytes"
56+
title = "eth0 RX bytes"
57+
value_threshold = 1.0
58+
}
59+
60+
metric {
61+
name = "iface_eth0_rx_packets"
62+
title = "eth0 RX packets"
63+
value_threshold = 1.0
64+
}
65+
66+
metric {
67+
name = "iface_eth0_rx_errs"
68+
title = "eth0 RX errors"
69+
value_threshold = 1.0
70+
}
71+
72+
metric {
73+
name = "iface_eth0_rx_drop"
74+
title = "eth0 RX drop"
75+
value_threshold = 1.0
76+
}
77+
78+
metric {
79+
name = "iface_eth0_tx_bytes"
80+
title = "eth0 TX bytes"
81+
value_threshold = 1.0
82+
}
83+
84+
metric {
85+
name = "iface_eth0_tx_packets"
86+
title = "eth0 TX packets"
87+
value_threshold = 1.0
88+
}
89+
90+
metric {
91+
name = "iface_eth0_tx_errs"
92+
title = "eth0 TX errors"
93+
value_threshold = 1.0
94+
}
95+
96+
metric {
97+
name = "iface_eth0_tx_drop"
98+
title = "eth0 TX drop"
99+
value_threshold = 1.0
100+
}
101+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import re
5+
import sys
6+
import traceback
7+
import os
8+
import threading
9+
import time
10+
import socket
11+
import select
12+
13+
descriptors = list()
14+
Desc_Skel = {}
15+
_Worker_Thread = None
16+
_Lock = threading.Lock() # synchronization lock
17+
Debug = False
18+
19+
def dprint(f, *v):
20+
if Debug:
21+
print >>sys.stderr, "iface: " + f % v
22+
23+
def floatable(str):
24+
try:
25+
float(str)
26+
return True
27+
except:
28+
return False
29+
30+
class UpdateMetricThread(threading.Thread):
31+
def __init__(self, params):
32+
threading.Thread.__init__(self)
33+
34+
self.running = False
35+
self.shuttingdown = False
36+
self.refresh_rate = params["refresh_rate"]
37+
self.mp = params["metrix_prefix"]
38+
self.metric = {}
39+
self.last_metric = {}
40+
41+
def shutdown(self):
42+
self.shuttingdown = True
43+
44+
if not self.running:
45+
return
46+
47+
self.join()
48+
49+
def run(self):
50+
self.running = True
51+
52+
while not self.shuttingdown:
53+
_Lock.acquire()
54+
updated = self.update_metric()
55+
_Lock.release()
56+
57+
if not updated:
58+
time.sleep(0.2)
59+
else:
60+
if "time" in self.last_metric:
61+
dprint("metric delta period %.3f" % (self.metric['time'] - self.last_metric['time']))
62+
63+
64+
self.running = False
65+
66+
def update_metric(self):
67+
if "time" in self.metric:
68+
if (time.time() - self.metric['time']) < self.refresh_rate:
69+
return False
70+
71+
dprint("updating metrics")
72+
73+
self.last_metric = self.metric.copy()
74+
75+
try:
76+
f = open('/proc/net/dev', 'r')
77+
except IOError:
78+
dprint("unable to open /proc/net/dev")
79+
return False
80+
81+
for line in f:
82+
if re.search(':', line):
83+
tokens = re.split('\s+', line.strip())
84+
iface = tokens[0].strip(':')
85+
86+
self.metric.update({
87+
'time' : time.time(),
88+
'%s_%s_%s' % (self.mp, iface, 'rx_bytes') : int(tokens[1]),
89+
'%s_%s_%s' % (self.mp, iface, 'rx_packets') : int(tokens[2]),
90+
'%s_%s_%s' % (self.mp, iface, 'rx_errs') : int(tokens[3]),
91+
'%s_%s_%s' % (self.mp, iface, 'rx_drop') : int(tokens[4]),
92+
'%s_%s_%s' % (self.mp, iface, 'rx_fifo') : int(tokens[5]),
93+
'%s_%s_%s' % (self.mp, iface, 'rx_frame') : int(tokens[6]),
94+
'%s_%s_%s' % (self.mp, iface, 'rx_compressed') : int(tokens[7]),
95+
'%s_%s_%s' % (self.mp, iface, 'rx_multicast') : int(tokens[8]),
96+
'%s_%s_%s' % (self.mp, iface, 'tx_bytes') : int(tokens[9]),
97+
'%s_%s_%s' % (self.mp, iface, 'tx_packets') : int(tokens[10]),
98+
'%s_%s_%s' % (self.mp, iface, 'tx_errs') : int(tokens[11]),
99+
'%s_%s_%s' % (self.mp, iface, 'tx_drop') : int(tokens[12]),
100+
'%s_%s_%s' % (self.mp, iface, 'tx_fifo') : int(tokens[13]),
101+
'%s_%s_%s' % (self.mp, iface, 'tx_frame') : int(tokens[14]),
102+
'%s_%s_%s' % (self.mp, iface, 'tx_compressed') : int(tokens[15]),
103+
'%s_%s_%s' % (self.mp, iface, 'tx_multicast') : int(tokens[16]),
104+
})
105+
106+
return True
107+
108+
def metric_delta(self, name):
109+
val = 0
110+
111+
if name in self.metric and name in self.last_metric:
112+
_Lock.acquire()
113+
if self.metric['time'] - self.last_metric['time'] != 0:
114+
val = (self.metric[name] - self.last_metric[name]) / (self.metric['time'] - self.last_metric['time'])
115+
_Lock.release()
116+
117+
return float(val)
118+
119+
def metric_init(params):
120+
global descriptors, Desc_Skel, _Worker_Thread, Debug
121+
122+
# initialize skeleton of descriptors
123+
Desc_Skel = {
124+
'name' : 'XXX',
125+
'call_back' : metric_delta,
126+
'time_max' : 60,
127+
'value_type' : 'float',
128+
'format' : '%.0f',
129+
'units' : 'XXX',
130+
'slope' : 'XXX', # zero|positive|negative|both
131+
'description' : 'XXX',
132+
'groups' : 'network'
133+
}
134+
135+
136+
params["refresh_rate"] = params["refresh_rate"] if "refresh_rate" in params else 15
137+
params["metrix_prefix"] = params["metrix_prefix"] if "metrix_prefix" in params else "iface"
138+
Debug = params["debug"] if "debug" in params else False
139+
140+
dprint("debugging has been turned on")
141+
142+
_Worker_Thread = UpdateMetricThread(params)
143+
_Worker_Thread.start()
144+
145+
mp = params["metrix_prefix"]
146+
147+
try:
148+
f = open("/proc/net/dev", 'r')
149+
except IOError:
150+
return
151+
152+
for line in f:
153+
if re.search(':', line):
154+
tokens = re.split('\s+', line.strip())
155+
iface = tokens[0].strip(':')
156+
157+
for way in ('tx', 'rx'):
158+
descriptors.append(create_desc(Desc_Skel, {
159+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'bytes'),
160+
"units" : "bytes/s",
161+
"slope" : "both",
162+
"description": 'Interface %s %s bytes per seconds' % (iface, way.upper())
163+
}))
164+
165+
descriptors.append(create_desc(Desc_Skel, {
166+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'packets'),
167+
"units" : "packets/s",
168+
"slope" : "both",
169+
"description": 'Interface %s %s packets per seconds' % (iface, way.upper())
170+
}))
171+
172+
descriptors.append(create_desc(Desc_Skel, {
173+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'errs'),
174+
"units" : "errs/s",
175+
"slope" : "both",
176+
"description": 'Interface %s %s errors per seconds' % (iface, way.upper())
177+
}))
178+
179+
descriptors.append(create_desc(Desc_Skel, {
180+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'drop'),
181+
"units" : "drop/s",
182+
"slope" : "both",
183+
"description": 'Interface %s %s drop per seconds' % (iface, way.upper())
184+
}))
185+
186+
descriptors.append(create_desc(Desc_Skel, {
187+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'fifo'),
188+
"units" : "fifo/s",
189+
"slope" : "both",
190+
"description": 'Interface %s %s fifo per seconds' % (iface, way.upper())
191+
}))
192+
193+
descriptors.append(create_desc(Desc_Skel, {
194+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'frame'),
195+
"units" : "frame/s",
196+
"slope" : "both",
197+
"description": 'Interface %s %s frame per seconds' % (iface, way.upper())
198+
}))
199+
200+
descriptors.append(create_desc(Desc_Skel, {
201+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'compressed'),
202+
"units" : "compressed/s",
203+
"slope" : "both",
204+
"description": 'Interface %s %s compressed per seconds' % (iface, way.upper())
205+
}))
206+
207+
descriptors.append(create_desc(Desc_Skel, {
208+
"name" : '%s_%s_%s_%s' % (mp, iface, way, 'multicast'),
209+
"units" : "multicast/s",
210+
"slope" : "both",
211+
"description": 'Interface %s %s multicast per seconds' % (iface, way.upper())
212+
}))
213+
214+
return descriptors
215+
216+
def create_desc(skel, prop):
217+
d = skel.copy()
218+
for k,v in prop.iteritems():
219+
d[k] = v
220+
return d
221+
222+
def metric_delta(name):
223+
return _Worker_Thread.metric_delta(name)
224+
225+
def metric_cleanup():
226+
_Worker_Thread.shutdown()
227+
228+
if __name__ == '__main__':
229+
params = {
230+
"debug" : True,
231+
"refresh_rate" : 15
232+
}
233+
234+
try:
235+
metric_init(params)
236+
237+
while True:
238+
time.sleep(params['refresh_rate'])
239+
for d in descriptors:
240+
v = d['call_back'](d['name'])
241+
print ('value for %s is ' + d['format']) % (d['name'], v)
242+
except KeyboardInterrupt:
243+
time.sleep(0.2)
244+
os._exit(1)
245+
except:
246+
traceback.print_exc()
247+
os._exit(1)

0 commit comments

Comments
 (0)