Bumped number for precise
[invirt/packages/xvm-munin-config.git] / host / usr / share / xvm-munin-host-config / plugins / xen_cpu
index 41472db..06850cf 100755 (executable)
@@ -1,10 +1,16 @@
 #!/usr/bin/python
 
-from xen.xm.main import SERVER_LEGACY_XMLRPC, SERVER_XEN_API, parseServer
+from xen.xm.main import SERVER_LEGACY_XMLRPC, SERVER_XEN_API, parseServer, parseAuthentication
 from xen.xend import sxp
+import atexit
+import sys
 
 serverType, serverURI = parseServer()
 
+if serverType != SERVER_LEGACY_XMLRPC:
+    print >>sys.stderr, "xen_cpu is untested with this Xen server type"
+    sys.exit(1)
+
 if serverType == SERVER_XEN_API:
     from xen.xm import XenAPI
     server = XenAPI.Session(serverURI)
@@ -20,8 +26,6 @@ else:
     from xen.util.xmlrpcclient import ServerProxy
     server = ServerProxy(serverURI)
 
-import sys
-
 if len(sys.argv) > 1:
     cmd = sys.argv[1]
 else:
@@ -30,21 +34,30 @@ else:
 def getDomains():
     ret = {}
     if serverType == SERVER_XEN_API:
-        domains = server.xenapu.VM.get_all_records()
+        domains = server.xenapi.VM.get_all_records()
         metrics = server.xenapi.VM_metrics.get_all_records()
         for d in domains.values():
-            ret[d['uuid']] = {'name': d['name_label'],
-                              'cpu_time': sum(metrics[d['metrics']]['VCPUs_utilisation'].values()),
-                              'domid': d['domid'],
-                              }
+            ret['uuid_'+d['uuid'].replace('-', '_')] = {'name': d['name_label'],
+                                                        'cpu_time': sum(metrics[d['metrics']]['VCPUs_utilisation'].values()),
+                                                        'domid': d['domid'],
+                                                        'uuid': d['uuid'],
+                                                        # No equivalent
+                                                        }
         return ret
     else:
         domains = server.xend.domains_with_state(True, 'all', True)
         for d in domains:
-            ret[sxp.child_value(d, 'uuid', 'NONE')] = {'name': sxp.child_value(d, 'name', 'UNKNOWN'),
-                                                       'cpu_time': sxp.child_value(d, 'cpu_time', 0.0),
-                                                       'domid': sxp.child_value(d, 'domid', -1),
-                                                       }
+            data = {'name': sxp.child_value(d, 'name', 'UNKNOWN'),
+                    'cpu_time': sxp.child_value(d, 'cpu_time', 0.0),
+                    'domid': sxp.child_value(d, 'domid', -1),
+                    'uuid': sxp.child_value(d, 'uuid', 'NONE'),
+                    }
+            try:
+                sched = server.xend.domain.sched_credit_get(data['name'])
+                data['sched-credit'] = sched
+            except:
+                data['sched-credit'] = None
+            ret['uuid_'+sxp.child_value(d, 'uuid', 'NONE').replace('-', '_')] = data
         return ret
 
 if cmd == 'config':
@@ -60,18 +73,22 @@ graph_period second"""
     for d in sorted(domains):
         name = domains[d]['name']
         if name[0:2] == 'd_':
-            name = 'user domain'
+            name = 'db domid %d' % domains[d]['domid']
         print "%s.label %s" % (d, name)
         if domains[d]['domid'] == 0:
             print "%s.draw AREA" % d
         else:
             print "%s.draw STACK" % d
-        print "%s.max 5000" % d
+        print "%s.max 19200000000" % d # 64x 100% CPU usage
         print "%s.min 0" % d
         print "%s.type DERIVE" % d
-        print "%s.info %s" % (d, name)
+        if domains[d].get('sched-credit'):
+            print "%s.info uuid %s CPU weight %d cap %d%%" % (d, domains[d]['uuid'], domains[d]['sched-credit']['weight'], domains[d]['sched-credit']['cap'])
+        else:
+            print "%s.info uuid %s" % (d, domains[d]['uuid'])
+        print "%s.cdef %s,10000,/" % (d, d)
     sys.exit(0)
 
 domains = getDomains()
 for d in sorted(domains):
-    print "%s.value %s" % (d, domains[d]['cpu_time'])
+    print "%s.value %s" % (d, long(domains[d]['cpu_time']*1000000))