826ba8bb5930498df30c4c20bb7fa37eb04b1978
[invirt/packages/xvm-munin-config.git] / host / usr / share / xvm-munin-host-config / plugins / xen_net
1 #!/usr/bin/python
2
3 import sys
4 sys.path.append('/usr/lib/xen-default/lib/python/')
5 from xen.lowlevel import xs
6 from collections import defaultdict
7 import os
8 import re
9
10 xsc = xs.xs()
11
12 def live_vms():
13     domids = set(xsc.ls('', '/local/domain'))
14
15     vms = dict()
16
17     for domid in domids:
18         try:
19             uuid, data = get_dom(int(domid))
20         except (xs.Error, TypeError, AttributeError):
21             continue # went down since we started
22         vms[uuid] = data
23     return vms
24
25 def get_dom(domid):
26     data = dict(domid=domid)
27     data['name'] = xsc.read('', '/local/domain/%d/name' % domid)
28     # /vm contains a path like "/vm/UUID"; strip the "/vm/"
29     data['uuid'] = xsc.read('', '/local/domain/%d/vm' % domid)[4:]
30     if data['name'].startswith('d_'):
31         data['munin_name'] = 'db domid %d' % domid
32     else:
33         data['munin_name'] = data['name']
34     data['munin_var'] = 'uuid_'+data['uuid'].replace('-', '_')
35     return data['munin_var'], data
36
37 def parse_net_dev():
38     """Parse /proc/net/dev to determine down/up counters for each interface.
39
40     N.B. Note that "down" and "up" are with respect to the host.
41     """
42     usage = {}
43     for line in open('/proc/net/dev', 'r'):
44         if ':' not in line:
45             continue # skip header lines
46         iface, fields = line.split(':')
47         iface = iface.strip()
48         fields = fields.split()
49         usage[iface] = {'down': int(fields[0]),
50                         'up': int(fields[8])}
51     return usage
52
53 IFACE_NAME_RE = re.compile(r'^(vif|tap)(\d+)\.(\d+)$')
54
55 def get_net_usage():
56     """Group /proc/net/dev counters by domain.
57
58     N.B. Note that "down" and "up" are reversed - from the perspective of the guest.
59     """
60     by_dev = parse_net_dev()
61     by_domain = defaultdict(lambda: {'down': 0, 'up': 0})
62     for iface in by_dev:
63         m = IFACE_NAME_RE.match(iface)
64         if m:
65             viftype, domid, ifcount = m.groups()
66             domid = int(domid)
67             by_domain[domid]['down'] += by_dev[iface]['up']
68             by_domain[domid]['up'] += by_dev[iface]['down']
69     return by_domain
70
71 if __name__ == '__main__':
72     if len(sys.argv) > 1:
73         cmd = sys.argv[1]
74     else:
75         cmd = None
76
77     domains = live_vms()
78
79     if cmd == 'config':
80         print """
81 graph_title Xen domain network usage
82 graph_args --base 1000
83 graph_vlabel bits in (-) / out (+) per ${graph_period}
84 graph_info This graph shows how network is utilized by Xen domains.
85 graph_category network
86 graph_period second"""
87         for d in sorted(domains):
88             for direction in ('down', 'up'):
89                 key = "%s_%s" % (d, direction)
90                 print "%s.label %s" % (key, domains[d]['munin_name'])
91                 print "%s.draw AREASTACK" % key
92                 print "%s.max 10000000000" % key
93                 print "%s.min 0" % key
94                 print "%s.type DERIVE" % key
95                 print "%s.cdef %s,8,*" % (key, key)
96                 if direction == 'down':
97                     print "%s.graph no" % key
98                 else:
99                     print "%s.negative %s_down" % (key, d)
100                 print "%s.info uuid %s" % (key, domains[d]['uuid'])
101         sys.exit(0)
102
103     net_usage = get_net_usage()
104
105     for d in sorted(domains):
106         for direction in ('down', 'up'):
107             print "%s_%s.value %d" % (d, direction, net_usage[domains[d]['domid']][direction])