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