source: trunk/packages/xvm-munin-config/host/usr/share/xvm-munin-host-config/plugins/xen_cpu @ 1783

Last change on this file since 1783 was 1783, checked in by quentin, 15 years ago

Block xen_cpu from running with XenAPI until we switch all of XVM

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1#!/usr/bin/python
2
3from xen.xm.main import SERVER_LEGACY_XMLRPC, SERVER_XEN_API, parseServer, parseAuthentication
4from xen.xend import sxp
5import atexit
6import sys
7
8serverType, serverURI = parseServer()
9
10if serverType != SERVER_LEGACY_XMLRPC:
11    print >>sys.stderr, "xen_cpu is untested with this Xen server type"
12    sys.exit(1)
13
14if 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)
25else:
26    from xen.util.xmlrpcclient import ServerProxy
27    server = ServerProxy(serverURI)
28
29if len(sys.argv) > 1:
30    cmd = sys.argv[1]
31else:
32    cmd = None
33
34def 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[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[sxp.child_value(d, 'uuid', 'NONE').replace('-', '_')] = data
61        return ret
62
63if cmd == 'config':
64    print """
65graph_title Xen domain CPU usage
66graph_args --base 1000 -r --lower-limit 0 --upper-limit 800
67graph_vlabel %
68graph_scale no
69graph_info This graph shows how CPU time is spent by Xen domains.
70graph_category system
71graph_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
92domains = getDomains()
93for d in sorted(domains):
94    print "%s.value %s" % (d, long(domains[d]['cpu_time']*1000000))
Note: See TracBrowser for help on using the repository browser.