From acae25af33bbc7f397bd979d4944bcfb568e522d Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 30 Mar 2020 09:49:11 +0200 Subject: Run python3-futurize --- libvirt-blkstat | 43 ++++++++++++++-------------- libvirt-cputime | 31 ++++++++++---------- libvirt-ifstat | 43 ++++++++++++++-------------- libvirt-mem | 63 +++++++++++++++++++++-------------------- munin-libvirt-plugins-detect.in | 23 ++++++++------- 5 files changed, 104 insertions(+), 99 deletions(-) diff --git a/libvirt-blkstat b/libvirt-blkstat index 42ec1b4..5e236c0 100644 --- a/libvirt-blkstat +++ b/libvirt-blkstat @@ -12,6 +12,7 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libvirt import libxml2 @@ -22,10 +23,10 @@ def canon(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,21 +34,21 @@ 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) @@ -84,8 +85,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,9 +98,9 @@ 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): @@ -108,10 +109,10 @@ def main(sys): if len(sys) > 1: 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) diff --git a/libvirt-cputime b/libvirt-cputime index f2d658d..a0af52c 100644 --- a/libvirt-cputime +++ b/libvirt-cputime @@ -13,6 +13,7 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libvirt @@ -22,7 +23,7 @@ def canon(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,7 +35,7 @@ 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] conn = libvirt.openReadOnly(uri) @@ -43,16 +44,16 @@ total_pc.critical 95""" 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" @@ -66,16 +67,16 @@ 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): @@ -85,10 +86,10 @@ def main(sys): if len(sys) > 1: 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) diff --git a/libvirt-ifstat b/libvirt-ifstat index 4fd2f93..c3b73fd 100644 --- a/libvirt-ifstat +++ b/libvirt-ifstat @@ -12,6 +12,7 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libvirt import libxml2 @@ -22,10 +23,10 @@ def canon(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,21 +34,21 @@ 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) @@ -84,8 +85,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,9 +97,9 @@ 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): @@ -107,10 +108,10 @@ def main(sys): if len(sys) > 1: 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) diff --git a/libvirt-mem b/libvirt-mem index 80e1d3a..deef1dd 100644 --- a/libvirt-mem +++ b/libvirt-mem @@ -13,6 +13,7 @@ #%# capabilities=autoconf #%# family=contrib +from __future__ import print_function import re, sys, os import libxml2 import libvirt @@ -24,17 +25,17 @@ 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 +43,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,7 +112,7 @@ 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 @@ -153,18 +154,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 +174,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 @@ -198,10 +199,10 @@ def main(sys): if len(sys) > 1: 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) diff --git a/munin-libvirt-plugins-detect.in b/munin-libvirt-plugins-detect.in index 275c987..57eeff1 100644 --- a/munin-libvirt-plugins-detect.in +++ b/munin-libvirt-plugins-detect.in @@ -9,6 +9,7 @@ # # Detect connection uri and enable plugins +from __future__ import print_function import os import sys from optparse import OptionParser @@ -33,27 +34,27 @@ def check_uris(uris, force=False): libvirt.openReadOnly(uri) detected = uri break - except libvirt.libvirtError, err: + except libvirt.libvirtError as err: 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 + 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 +63,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 @@ -99,7 +100,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) -- cgit v1.2.3