1 | #!/usr/bin/python |
---|
2 | |
---|
3 | from xen.xm.main import SERVER_LEGACY_XMLRPC, SERVER_XEN_API, parseServer |
---|
4 | from xen.xend import sxp |
---|
5 | |
---|
6 | serverType, serverURI = parseServer() |
---|
7 | |
---|
8 | if serverType == SERVER_XEN_API: |
---|
9 | from xen.xm import XenAPI |
---|
10 | server = XenAPI.Session(serverURI) |
---|
11 | username, password = parseAuthentication() |
---|
12 | server.login_with_password(username, password) |
---|
13 | def logout(): |
---|
14 | try: |
---|
15 | server.xenapi.session.logout() |
---|
16 | except: |
---|
17 | pass |
---|
18 | atexit.register(logout) |
---|
19 | else: |
---|
20 | from xen.util.xmlrpcclient import ServerProxy |
---|
21 | server = ServerProxy(serverURI) |
---|
22 | |
---|
23 | import sys |
---|
24 | |
---|
25 | if len(sys.argv) > 1: |
---|
26 | cmd = sys.argv[1] |
---|
27 | else: |
---|
28 | cmd = None |
---|
29 | |
---|
30 | def getDomains(): |
---|
31 | ret = {} |
---|
32 | if serverType == SERVER_XEN_API: |
---|
33 | domains = server.xenapu.VM.get_all_records() |
---|
34 | metrics = server.xenapi.VM_metrics.get_all_records() |
---|
35 | for d in domains.values(): |
---|
36 | ret[d['uuid']] = {'name': d['name_label'], |
---|
37 | 'cpu_time': sum(metrics[d['metrics']]['VCPUs_utilisation'].values()), |
---|
38 | 'domid': d['domid'], |
---|
39 | } |
---|
40 | return ret |
---|
41 | else: |
---|
42 | domains = server.xend.domains_with_state(True, 'all', True) |
---|
43 | for d in domains: |
---|
44 | ret[sxp.child_value(d, 'uuid', 'NONE')] = {'name': sxp.child_value(d, 'name', 'UNKNOWN'), |
---|
45 | 'cpu_time': sxp.child_value(d, 'cpu_time', 0.0), |
---|
46 | 'domid': sxp.child_value(d, 'domid', -1), |
---|
47 | } |
---|
48 | return ret |
---|
49 | |
---|
50 | if cmd == 'config': |
---|
51 | print """ |
---|
52 | graph_title Xen domain CPU usage |
---|
53 | graph_args --base 1000 -r --lower-limit 0 --upper-limit 800 |
---|
54 | graph_vlabel % |
---|
55 | graph_scale no |
---|
56 | graph_info This graph shows how CPU time is spent by Xen domains. |
---|
57 | graph_category system |
---|
58 | graph_period second""" |
---|
59 | domains = getDomains() |
---|
60 | for d in sorted(domains): |
---|
61 | name = domains[d]['name'] |
---|
62 | if name[0:2] == 'd_': |
---|
63 | name = 'user domain' |
---|
64 | print "%s.label %s" % (d, name) |
---|
65 | if domains[d]['domid'] == 0: |
---|
66 | print "%s.draw AREA" % d |
---|
67 | else: |
---|
68 | print "%s.draw STACK" % d |
---|
69 | print "%s.max 5000" % d |
---|
70 | print "%s.min 0" % d |
---|
71 | print "%s.type DERIVE" % d |
---|
72 | print "%s.info %s" % (d, name) |
---|
73 | sys.exit(0) |
---|
74 | |
---|
75 | domains = getDomains() |
---|
76 | for d in sorted(domains): |
---|
77 | print "%s.value %s" % (d, domains[d]['cpu_time']) |
---|