Bumped number for precise
[invirt/packages/xvm-munin-config.git] / host / usr / share / xvm-munin-host-config / plugins / xen_cpu
1 #!/usr/bin/python
2
3 from xen.xm.main import SERVER_LEGACY_XMLRPC, SERVER_XEN_API, parseServer, parseAuthentication
4 from xen.xend import sxp
5 import atexit
6 import sys
7
8 serverType, serverURI = parseServer()
9
10 if serverType != SERVER_LEGACY_XMLRPC:
11     print >>sys.stderr, "xen_cpu is untested with this Xen server type"
12     sys.exit(1)
13
14 if serverType == SERVER_XEN_API:
15     from xen.xm import XenAPI
16     server = XenAPI.Session(serverURI)
17     username, password = parseAuthentication()
18     server.login_with_password(username, password)
19     def logout():
20         try:
21             server.xenapi.session.logout()
22         except:
23             pass
24     atexit.register(logout)
25 else:
26     from xen.util.xmlrpcclient import ServerProxy
27     server = ServerProxy(serverURI)
28
29 if len(sys.argv) > 1:
30     cmd = sys.argv[1]
31 else:
32     cmd = None
33
34 def getDomains():
35     ret = {}
36     if serverType == SERVER_XEN_API:
37         domains = server.xenapi.VM.get_all_records()
38         metrics = server.xenapi.VM_metrics.get_all_records()
39         for d in domains.values():
40             ret['uuid_'+d['uuid'].replace('-', '_')] = {'name': d['name_label'],
41                                                         'cpu_time': sum(metrics[d['metrics']]['VCPUs_utilisation'].values()),
42                                                         'domid': d['domid'],
43                                                         'uuid': d['uuid'],
44                                                         # No equivalent
45                                                         }
46         return ret
47     else:
48         domains = server.xend.domains_with_state(True, 'all', True)
49         for d in domains:
50             data = {'name': sxp.child_value(d, 'name', 'UNKNOWN'),
51                     'cpu_time': sxp.child_value(d, 'cpu_time', 0.0),
52                     'domid': sxp.child_value(d, 'domid', -1),
53                     'uuid': sxp.child_value(d, 'uuid', 'NONE'),
54                     }
55             try:
56                 sched = server.xend.domain.sched_credit_get(data['name'])
57                 data['sched-credit'] = sched
58             except:
59                 data['sched-credit'] = None
60             ret['uuid_'+sxp.child_value(d, 'uuid', 'NONE').replace('-', '_')] = data
61         return ret
62
63 if cmd == 'config':
64     print """
65 graph_title Xen domain CPU usage
66 graph_args --base 1000 -r --lower-limit 0 --upper-limit 800
67 graph_vlabel %
68 graph_scale no
69 graph_info This graph shows how CPU time is spent by Xen domains.
70 graph_category system
71 graph_period second"""
72     domains = getDomains()
73     for d in sorted(domains):
74         name = domains[d]['name']
75         if name[0:2] == 'd_':
76             name = 'db domid %d' % domains[d]['domid']
77         print "%s.label %s" % (d, name)
78         if domains[d]['domid'] == 0:
79             print "%s.draw AREA" % d
80         else:
81             print "%s.draw STACK" % d
82         print "%s.max 19200000000" % d # 64x 100% CPU usage
83         print "%s.min 0" % d
84         print "%s.type DERIVE" % d
85         if domains[d].get('sched-credit'):
86             print "%s.info uuid %s CPU weight %d cap %d%%" % (d, domains[d]['uuid'], domains[d]['sched-credit']['weight'], domains[d]['sched-credit']['cap'])
87         else:
88             print "%s.info uuid %s" % (d, domains[d]['uuid'])
89         print "%s.cdef %s,10000,/" % (d, d)
90     sys.exit(0)
91
92 domains = getDomains()
93 for d in sorted(domains):
94     print "%s.value %s" % (d, long(domains[d]['cpu_time']*1000000))