diff options
Diffstat (limited to 'libvirt-mem')
-rw-r--r-- | libvirt-mem | 83 |
1 files changed, 44 insertions, 39 deletions
diff --git a/libvirt-mem b/libvirt-mem index 80e1d3a..a789326 100644 --- a/libvirt-mem +++ b/libvirt-mem @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # vim: set fileencoding=utf-8 : # # Munin plugin to show the amount of memory used by libvirt managed virtual @@ -13,28 +13,31 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libxml2 import libvirt + def canon(name): return re.sub(r"[^a-zA-Z0-9_]", "_", name) + def print_config(uri): """print the plugin config, determine the domains""" try: conn = libvirt.openReadOnly(uri) - except libvirt.libvirtError, err: - print >>sys.stderr, "Error opening to %s connection: %s" % (uri, err) + except libvirt.libvirtError as err: + print("Error opening to %s connection: %s" % (uri, err), file=sys.stderr) return 1 hostname = conn.getHostname() - print """graph_title Virtual Domain Memory Usage + print("""graph_title Virtual Domain Memory Usage graph_vlabel Memory Usage / Bytes graph_category Virtual Machines graph_info This graph shows the current amount of memory used by each virtual machine -graph_args --base 1024 -l 0""" +graph_args --base 1024 -l 0""") ids = conn.listDomainsID() draw = "AREA" @@ -42,20 +45,20 @@ graph_args --base 1024 -l 0""" try: dom = conn.lookupByID(id) name = dom.name() - except libvirt.libvirtError, err: - print >>sys.stderr, "Id: %s: %s" % (id, err) + except libvirt.libvirtError as err: + print("Id: %s: %s" % (id, err), file=sys.stderr) continue if name == "Domain-0": continue - print "%s_mem.label %s" % (canon(name), name) - print "%s_mem.type GAUGE" % canon(name) - print "%s_mem.min 0" % canon(name) - print "%s_mem.draw %s" % (canon(name), draw) - print "%s_mem.info memory used by virtual machine '%s'" % (canon(name), name) + print("%s_mem.label %s" % (canon(name), name)) + print("%s_mem.type GAUGE" % canon(name)) + print("%s_mem.min 0" % canon(name)) + print("%s_mem.draw %s" % (canon(name), draw)) + print("%s_mem.info memory used by virtual machine '%s'" % (canon(name), name)) if draw == "AREA": draw = "STACK" - print """ + print(""" host_mem.label %(hostname)s (host) host_mem.type GAUGE host_mem.min 0 @@ -111,17 +114,18 @@ total_soft_limit_pc.graph no total_soft_limit_pc.info memory soft_limit percentage of all virtual machines on host '%(hostname)s' total_soft_limit_pc.min 0 -""" % dict(hostname=hostname) +""" % dict(hostname=hostname)) return 0 + def get_memtune(dom): - memtune = { 'min_guarantee': 0, 'soft_limit': 0, 'hard_limit': 0 } + memtune = {'min_guarantee': 0, 'soft_limit': 0, 'hard_limit': 0} xml = dom.XMLDesc(0) try: doc = libxml2.parseDoc(xml) - except: + except Exception: return [] ctx = doc.xpathNewContext() @@ -133,12 +137,12 @@ def get_memtune(dom): memtune[key] = int(child.content) break except IndexError: - # key not found in xml - pass + # key not found in xml + pass finally: - if ctx != None: + if ctx is not None: ctx.xpathFreeContext() - if doc != None: + if doc is not None: doc.freeDoc() return memtune @@ -153,18 +157,18 @@ def fetch_values(uri): try: conn = libvirt.openReadOnly(uri) - except libvirt.libvirtError, err: - print >>sys.stderr, "Error opening to %s connection: %s" % (uri, err) + except libvirt.libvirtError as err: + print("Error opening to %s connection: %s" % (uri, err), file=sys.stderr) return 1 ids = conn.listDomainsID() hostmem = conn.getInfo()[1] * 1024 * 1024 - print "host_mem.value %d" % hostmem + print("host_mem.value %d" % hostmem) for id in ids: try: dom = conn.lookupByID(id) name = dom.name() - except libvirt.libvirtError, err: - print >>sys.stderr, "Id: %s: %s" % (id, err) + except libvirt.libvirtError as err: + print("Id: %s: %s" % (id, err), file=sys.stderr) continue if name == "Domain-0": continue @@ -173,22 +177,22 @@ def fetch_values(uri): maxmem *= 1024 total += mem total_max += maxmem - print "%s_mem.value %d" % (canon(name), mem) + print("%s_mem.value %d" % (canon(name), mem)) memtune = get_memtune(dom) min_guarantee += memtune['min_guarantee'] * 1024 hard_limit += memtune['hard_limit'] * 1024 soft_limit += memtune['soft_limit'] * 1024 - print "total.value %d" % total - print "total_pc.value %.0f" % (100.0 * total / float(hostmem)) - print "total_max.value %d" % total_max - print "total_max_pc.value %.0f" % (100.0 * total_max / float(hostmem)) - print "total_min_guarantee.value %d" % min_guarantee - print "total_min_guarantee_pc.value %.0f" % (100.0 * min_guarantee / float(hostmem)) - print "total_soft_limit.value %d" % soft_limit - print "total_soft_limit_pc.value %.0f" % (100.0 * soft_limit / float(hostmem)) - print "total_hard_limit.value %d" % hard_limit - print "total_hard_limit_pc.value %.0f" % (100.0 * hard_limit / float(hostmem)) + print("total.value %d" % total) + print("total_pc.value %.0f" % (100.0 * total / float(hostmem))) + print("total_max.value %d" % total_max) + print("total_max_pc.value %.0f" % (100.0 * total_max / float(hostmem))) + print("total_min_guarantee.value %d" % min_guarantee) + print("total_min_guarantee_pc.value %.0f" % (100.0 * min_guarantee / float(hostmem))) + print("total_soft_limit.value %d" % soft_limit) + print("total_soft_limit_pc.value %.0f" % (100.0 * soft_limit / float(hostmem))) + print("total_hard_limit.value %d" % hard_limit) + print("total_hard_limit_pc.value %.0f" % (100.0 * hard_limit / float(hostmem))) return 0 @@ -196,17 +200,18 @@ def main(sys): uri = os.getenv("uri", "qemu:///system") if len(sys) > 1: - if sys[1] in [ 'autoconf', 'detect' ]: + if sys[1] in ['autoconf', 'detect']: if libvirt.openReadOnly(uri): - print "yes" + print("yes") return 0 else: - print "no" + print("no") return 1 elif sys[1] == 'config': return print_config(uri) return fetch_values(uri) + if __name__ == "__main__": sys.exit(main(sys.argv)) |