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 # /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
33 data['munin_name'] = data['name']
34 data['munin_var'] = 'uuid_'+data['uuid'].replace('-', '_')
35 return data['munin_var'], data
38 """Parse /proc/net/dev to determine down/up counters for each interface.
40 N.B. Note that "down" and "up" are with respect to the host.
43 for line in open('/proc/net/dev', 'r'):
45 continue # skip header lines
46 iface, fields = line.split(':')
48 fields = fields.split()
49 usage[iface] = {'down': int(fields[0]),
53 IFACE_NAME_RE = re.compile(r'^(vif|tap)(\d+)\.(\d+)$')
56 """Group /proc/net/dev counters by domain.
58 N.B. Note that "down" and "up" are reversed - from the perspective of the guest.
60 by_dev = parse_net_dev()
61 by_domain = defaultdict(lambda: {'down': 0, 'up': 0})
63 m = IFACE_NAME_RE.match(iface)
65 viftype, domid, ifcount = m.groups()
67 by_domain[domid]['down'] += by_dev[iface]['up']
68 by_domain[domid]['up'] += by_dev[iface]['down']
71 if __name__ == '__main__':
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
99 print "%s.negative %s_down" % (key, d)
100 print "%s.info uuid %s" % (key, domains[d]['uuid'])
103 net_usage = get_net_usage()
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])