diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | libvirt-blkstat | 64 | ||||
-rw-r--r-- | libvirt-cputime | 45 | ||||
-rw-r--r-- | libvirt-ifstat | 64 | ||||
-rw-r--r-- | libvirt-mem | 83 | ||||
-rw-r--r-- | munin-libvirt-plugins-detect.in | 36 | ||||
-rw-r--r-- | setup.cfg | 5 |
7 files changed, 165 insertions, 136 deletions
@@ -1,4 +1,4 @@ -VERSION=0.0.6 +VERSION=0.0.7 PACKAGE=munin-libvirt-plugins SBINDIR=/usr/sbin PLUGINDIR=/usr/share/munin/plugins @@ -28,7 +28,7 @@ $(DETECT): $(DETECT).in %.py: % ln -s $< $@ - pychecker -q -e Error $@ + flake8 $@ check: $(DETECT) $(PYFILES) diff --git a/libvirt-blkstat b/libvirt-blkstat index 42ec1b4..37be14d 100644 --- a/libvirt-blkstat +++ b/libvirt-blkstat @@ -1,8 +1,8 @@ -#!/usr/bin/python +#!/usr/bin/python3 # vim: set fileencoding=utf-8 : # # Munin plugin to show the I/O load of libvirt managed virtual machines -# +# # Copyright 2008 Guido Guenther <agx@sigxcpu.org> # # Licesnse: GPLv2 @@ -12,20 +12,23 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libvirt import libxml2 + def canon(name): return re.sub(r"[^a-zA-Z0-9_]", "_", name) + def print_config(uri): """print the plugin config, determine the domains""" - print """graph_title Virtual Domain Block Device I/O + print("""graph_title Virtual Domain Block Device I/O graph_vlabel Bytes read (-)/ written (+) per ${graph_period} graph_category Virtual Machines -graph_info This graph shows the block device I/O of the virtual machines""" +graph_info This graph shows the block device I/O of the virtual machines""") conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() @@ -33,28 +36,29 @@ graph_info This graph shows the block device I/O of the virtual machines""" 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_read.label %s" % (canon(name), name) - print "%s_read.type DERIVE" % canon(name) - print "%s_read.min 0" % canon(name) - print "%s_read.graph no" % canon(name) - print "%s_read.draw LINE1" % canon(name) - print "%s_write.label %s" % (canon(name), name) - print "%s_write.type DERIVE" % canon(name) - print "%s_write.min 0" % canon(name) - print "%s_write.negative %s_read" % (canon(name), canon(name)) - print "%s_write.draw LINE1" % canon(name) + print("%s_read.label %s" % (canon(name), name)) + print("%s_read.type DERIVE" % canon(name)) + print("%s_read.min 0" % canon(name)) + print("%s_read.graph no" % canon(name)) + print("%s_read.draw LINE1" % canon(name)) + print("%s_write.label %s" % (canon(name), name)) + print("%s_write.type DERIVE" % canon(name)) + print("%s_write.min 0" % canon(name)) + print("%s_write.negative %s_read" % (canon(name), canon(name))) + print("%s_write.draw LINE1" % canon(name)) + def get_disks(dom): xml = dom.XMLDesc(0) doc = None try: doc = libxml2.parseDoc(xml) - except: + except Exception: return [] ctx = doc.xpathNewContext() disks = [] @@ -65,16 +69,17 @@ def get_disks(dom): for child in node.children: if child.name == "target": devdst = child.prop("dev") - if devdst == None: + if devdst is None: continue disks.append(devdst) finally: - if ctx != None: + if ctx is not None: ctx.xpathFreeContext() - if doc != None: + if doc is not None: doc.freeDoc() return disks - + + def fetch_values(uri): conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() @@ -84,8 +89,8 @@ def fetch_values(uri): 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 @@ -97,21 +102,21 @@ def fetch_values(uri): rd += rd_bytes wr += wr_bytes except TypeError: - print >>sys.stderr, "Cannot get blockstats for '%s' on '%s'" % (disk, name) - print "%s_read.value %d" % (canon(name), rd) - print "%s_write.value %d" % (canon(name), wr) + print("Cannot get blockstats for '%s' on '%s'" % (disk, name), file=sys.stderr) + print("%s_read.value %d" % (canon(name), rd)) + print("%s_write.value %d" % (canon(name), wr)) 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': print_config(uri) @@ -119,6 +124,7 @@ def main(sys): fetch_values(uri) return 0 + if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/libvirt-cputime b/libvirt-cputime index f2d658d..e49d20e 100644 --- a/libvirt-cputime +++ b/libvirt-cputime @@ -1,9 +1,9 @@ -#!/usr/bin/python +#!/usr/bin/python3 # vim: set fileencoding=utf-8 : # # Munin plugin to show the percent of cputime of libvirt managed virtual # machines -# +# # Copyright 2008 Guido Guenther <agx@sigxcpu.org> # # License: GPLv2 @@ -13,16 +13,19 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libvirt + def canon(name): return re.sub(r"[^a-zA-Z0-9_]", "_", name) + def print_config(uri, stack): """print the plugin config, determine the domains""" - print """graph_title Virtual Domain Cpu Time + print("""graph_title Virtual Domain Cpu Time graph_vlabel CPU Time percentage graph_category Virtual Machines graph_info This graph shows the cpu time percentage of each virtual machine @@ -34,28 +37,29 @@ total_pc.max 100 total_pc.label total total_pc.info cputime used by all virtual machines total_pc.warning 90 -total_pc.critical 95""" +total_pc.critical 95""") - draw = [ "LINE1", "AREA"][stack] + draw = ["LINE1", "AREA"][stack] conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() 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 - print "%s_cputime.label %s" % (canon(name), name) - print "%s_cputime.type DERIVE" % canon(name) - print "%s_cputime.min 0" % canon(name) - print "%s_cputime.draw %s" % (canon(name), draw) - print "%s_cputime.info percent of cputime used by virtual machine '%s'" % (canon(name), name) + print("%s_cputime.label %s" % (canon(name), name)) + print("%s_cputime.type DERIVE" % canon(name)) + print("%s_cputime.min 0" % canon(name)) + print("%s_cputime.draw %s" % (canon(name), draw)) + print("%s_cputime.info percent of cputime used by virtual machine '%s'" % (canon(name), name)) if draw == "AREA": draw = "STACK" + def fetch_values(uri): conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() @@ -66,29 +70,29 @@ def fetch_values(uri): 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 cputime = float(dom.info()[4]) cputime_percentage = 1.0e-7 * cputime / processors total += cputime_percentage - print "%s_cputime.value %.0f" % (canon(name), cputime_percentage) - print "total_pc.value %.0f" % total + print("%s_cputime.value %.0f" % (canon(name), cputime_percentage)) + print("total_pc.value %.0f" % total) def main(sys): uri = os.getenv("uri", "qemu:///system") - stack = [ False, True ][os.getenv("stack") == "1"] + stack = [False, True][os.getenv("stack") == "1"] 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': print_config(uri, stack) @@ -96,6 +100,7 @@ def main(sys): fetch_values(uri) return 0 + if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/libvirt-ifstat b/libvirt-ifstat index 4fd2f93..1561c6a 100644 --- a/libvirt-ifstat +++ b/libvirt-ifstat @@ -1,8 +1,8 @@ -#!/usr/bin/python +#!/usr/bin/python3 # vim: set fileencoding=utf-8 : # # Munin plugin to show the network traffic of libvirt managed virtual machines -# +# # Copyright 2008 Guido Guenther <agx@sigxcpu.org> # # License GPLv2 @@ -12,20 +12,23 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libvirt import libxml2 + def canon(name): return re.sub(r"[^a-zA-Z0-9_]", "_", name) + def print_config(uri): """print the plugin config, determine the domains""" - print """graph_title Virtual Domain Network I/O + print("""graph_title Virtual Domain Network I/O graph_vlabel Bytes rx (-)/ tx (+) per ${graph_period} graph_category Virtual Machines -graph_info This graph shows the network I/O of the virtual machines""" +graph_info This graph shows the network I/O of the virtual machines""") conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() @@ -33,28 +36,29 @@ graph_info This graph shows the network I/O of the virtual machines""" 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_rx.label %s" % (canon(name), name) - print "%s_rx.type DERIVE" % canon(name) - print "%s_rx.min 0" % canon(name) - print "%s_rx.graph no" % canon(name) - print "%s_rx.draw LINE1" % canon(name) - print "%s_tx.label %s" % (canon(name), name) - print "%s_tx.type DERIVE" % canon(name) - print "%s_tx.min 0" % canon(name) - print "%s_tx.negative %s_rx" % (canon(name), canon(name)) - print "%s_tx.draw LINE1" % canon(name) + print("%s_rx.label %s" % (canon(name), name)) + print("%s_rx.type DERIVE" % canon(name)) + print("%s_rx.min 0" % canon(name)) + print("%s_rx.graph no" % canon(name)) + print("%s_rx.draw LINE1" % canon(name)) + print("%s_tx.label %s" % (canon(name), name)) + print("%s_tx.type DERIVE" % canon(name)) + print("%s_tx.min 0" % canon(name)) + print("%s_tx.negative %s_rx" % (canon(name), canon(name))) + print("%s_tx.draw LINE1" % canon(name)) + def get_ifaces(dom): xml = dom.XMLDesc(0) doc = None try: doc = libxml2.parseDoc(xml) - except: + except Exception: return [] ctx = doc.xpathNewContext() ifaces = [] @@ -65,16 +69,17 @@ def get_ifaces(dom): for child in node.children: if child.name == "target": devdst = child.prop("dev") - if devdst == None: + if devdst is None: continue ifaces.append(devdst) finally: - if ctx != None: + if ctx is not None: ctx.xpathFreeContext() - if doc != None: + if doc is not None: doc.freeDoc() return ifaces - + + def fetch_values(uri): conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() @@ -84,8 +89,8 @@ def fetch_values(uri): 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 @@ -96,21 +101,21 @@ def fetch_values(uri): rd += stats[0] wr += stats[4] except TypeError: - print >>sys.stderr, "Cannot get ifstats for '%s' on '%s'" % (iface, name) - print "%s_rx.value %d" % (canon(name), rd) - print "%s_tx.value %d" % (canon(name), wr) + print("Cannot get ifstats for '%s' on '%s'" % (iface, name), file=sys.stderr) + print("%s_rx.value %d" % (canon(name), rd)) + print("%s_tx.value %d" % (canon(name), wr)) 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': print_config(uri) @@ -118,6 +123,7 @@ def main(sys): fetch_values(uri) return 0 + if __name__ == "__main__": sys.exit(main(sys.argv)) 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)) diff --git a/munin-libvirt-plugins-detect.in b/munin-libvirt-plugins-detect.in index 275c987..7e86ff2 100644 --- a/munin-libvirt-plugins-detect.in +++ b/munin-libvirt-plugins-detect.in @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # vim: set fileencoding=utf-8 : # # Configure and enable munin libvirt plugins @@ -9,6 +9,7 @@ # # Detect connection uri and enable plugins +from __future__ import print_function import os import sys from optparse import OptionParser @@ -19,12 +20,13 @@ try: except ImportError: has_libvirt = False -URIS = [ "xen:///", "qemu:///system" ] +URIS = ["xen:///", "qemu:///system"] PLUGIN_CONF = "::MUNINCONFDIR::/plugin-conf.d/libvirt" -PLUGINS = [ "blkstat", "cputime", "ifstat", "mem" ] +PLUGINS = ["blkstat", "cputime", "ifstat", "mem"] PLUGIN_SRC = "::PLUGINDIR::" PLUGIN_DST = "::MUNINCONFDIR::/plugins/" + def check_uris(uris, force=False): detected = None @@ -33,27 +35,27 @@ def check_uris(uris, force=False): libvirt.openReadOnly(uri) detected = uri break - except libvirt.libvirtError, err: + except libvirt.libvirtError: pass if detected: - print "Hypervisor at %s detected." % detected + print("Hypervisor at %s detected." % detected) if not os.path.exists(PLUGIN_CONF) or force: try: - conf = file(PLUGIN_CONF, "w+") - print >>conf, "[libvirt-*]" - print >>conf, "env.uri %s""" % detected + conf = open(PLUGIN_CONF, "w+") + print("[libvirt-*]", file=conf) + print("env.uri %s""" % detected, file=conf) conf.close() - except IOError, err: - print >>sys.stderr, err + except IOError as err: + print(err, file=sys.stderr) return 1 else: - print >>sys.stderr, "Plugin configuration '%s' already exists - doing nothing" % PLUGIN_CONF + print("Plugin configuration '%s' already exists - doing nothing" % PLUGIN_CONF, file=sys.stderr) return 0 def enable_plugins(force=False): - for plugin in map(lambda x: "libvirt-" + x, PLUGINS): + for plugin in ["libvirt-" + x for x in PLUGINS]: src = os.path.join(PLUGIN_SRC, plugin) dst = os.path.join(PLUGIN_DST, plugin) if force: @@ -62,9 +64,9 @@ def enable_plugins(force=False): except OSError: pass if os.path.exists(dst): - print >>sys.stderr, "Plugin '%s' already enabled - doing nothing" % plugin + print("Plugin '%s' already enabled - doing nothing" % plugin, file=sys.stderr) else: - print >>sys.stderr, "Enabling plugin '%s'" % plugin + print("Enabling plugin '%s'" % plugin, file=sys.stderr) os.symlink(src, dst) return 0 @@ -72,8 +74,8 @@ def enable_plugins(force=False): def restart_munin_node(): act = dict(service='munin-node', action='restart') - for path in [ '/usr/sbin/invoke-rc.d', - '/sbin/service' ]: + for path in ['/usr/sbin/invoke-rc.d', + '/sbin/service']: if os.path.exists(path): act['exec'] = path ret = os.system('%(exec)s %(service)s %(action)s' % act) @@ -99,7 +101,7 @@ def main(args): uris = URIS if not has_libvirt: - print >>sys.stderr, "Libvirt not available." + print("Libvirt not available.", file=sys.stderr) return 1 ret = check_uris(uris, options.force) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..f141a94 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,5 @@ +[flake8] +# E501: ignore line length +# E265: block comment should start with '# ' +# E401: multiple imports on one line +ignore=E501,E265,E401 |