4 sys.path.append('/usr/lib/xen-default/lib/python/')
5 from xen.lowlevel import xs
6 from collections import defaultdict
13 domids = set(xsc.ls('', '/local/domain'))
19 uuid, data = get_dom(int(domid))
20 except (xs.Error, TypeError, AttributeError):
21 continue # went down since we started
26 data = dict(domid=domid)
27 data['name'] = xsc.read('', '/local/domain/%d/name' % domid)
28 data['uuid'] = xsc.read('', '/local/domain/%d/vm' % domid)[4:]
29 if data['name'][0:2] == 'd_':
30 data['munin_name'] = 'db domid %d' % domid
32 data['munin_name'] = data['name']
33 data['munin_var'] = 'uuid_'+data['uuid'].replace('-', '_')
34 return data['munin_var'], data
37 """Parse /proc/net/dev to determine down/up counters for each interface.
39 N.B. Note that "down" and "up" are with respect to the host.
42 for line in open('/proc/net/dev', 'r'):
44 continue # skip header lines
45 iface, fields = line.split(':')
47 fields = fields.split()
48 usage[iface] = {'down': int(fields[0]),
52 IFACE_NAME_RE = re.compile(r'^(vif|tap)(\d+)\.(\d+)$')
55 """Group /proc/net/dev counters by domain.
57 N.B. Note that "down" and "up" are reversed - from the perspective of the guest.
59 by_dev = parse_net_dev()
60 by_domain = defaultdict(lambda: {'down': 0, 'up': 0})
62 m = IFACE_NAME_RE.match(iface)
64 viftype, domid, ifcount = m.groups()
66 by_domain[domid]['down'] += by_dev[iface]['up']
67 by_domain[domid]['up'] += by_dev[iface]['down']
70 if __name__ == '__main__':
80 graph_title Xen domain network usage
81 graph_args --base 1000
82 graph_vlabel bits in (-) / out (+) per ${graph_period}
83 graph_info This graph shows how network is utilized by Xen domains.
84 graph_category network
85 graph_period second"""
86 for d in sorted(domains):
87 for direction in ('down', 'up'):
88 key = "%s_%s" % (d, direction)
89 print "%s.label %s" % (key, domains[d]['munin_name'])
90 print "%s.draw AREASTACK" % key
91 print "%s.max 10000000000" % key
92 print "%s.min 0" % key
93 print "%s.type DERIVE" % key
94 print "%s.cdef %s,8,*" % (key, key)
95 if direction == 'down':
96 print "%s.graph no" % key
98 print "%s.negative %s_down" % (key, d)
99 print "%s.info uuid %s" % (key, domains[d]['uuid'])
102 net_usage = get_net_usage()
104 for d in sorted(domains):
105 for direction in ('down', 'up'):
106 print "%s_%s.value %d" % (d, direction, net_usage[domains[d]['domid']][direction])