root / snf-cyclades-gtools / collectd / plugins / ganeti-netstats.py @ 2439c9d6
History | View | Annotate | Download (1.3 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
import os |
4 |
import collectd |
5 |
|
6 |
from glob import glob |
7 |
|
8 |
|
9 |
def read_int(file): |
10 |
f = open(file, "r") |
11 |
try:
|
12 |
val = int(f.read())
|
13 |
except ValueError: |
14 |
val = None
|
15 |
finally:
|
16 |
f.close() |
17 |
|
18 |
return val
|
19 |
|
20 |
|
21 |
def netstats(data=None): |
22 |
for dir in glob("/var/run/ganeti/kvm-hypervisor/nic/*"): |
23 |
if not os.path.isdir(dir): |
24 |
continue
|
25 |
|
26 |
hostname = os.path.basename(dir)
|
27 |
|
28 |
for nic in glob(os.path.join(dir, "*")): |
29 |
idx = int(os.path.basename(nic))
|
30 |
with open(nic) as nicfile: |
31 |
try:
|
32 |
iface = nicfile.readline().strip() |
33 |
except EnvironmentError: |
34 |
continue
|
35 |
|
36 |
if not os.path.isdir("/sys/class/net/%s" % iface): |
37 |
continue
|
38 |
|
39 |
bytes_in = read_int("/sys/class/net/%s/statistics/rx_bytes"
|
40 |
% iface) |
41 |
bytes_out = read_int("/sys/class/net/%s/statistics/tx_bytes"
|
42 |
% iface) |
43 |
|
44 |
vl = collectd.Values(type="derive")
|
45 |
vl.host = hostname |
46 |
vl.plugin = "interface"
|
47 |
vl.type = "if_octets"
|
48 |
vl.type_instance = "eth%d" % idx
|
49 |
vl.dispatch(values=[bytes_out, bytes_in]) |
50 |
|
51 |
collectd.register_read(netstats) |
52 |
|
53 |
# vim: set ts=4 sts=4 et sw=4 :
|