diff options
author | Guido Günther <agx@sigxcpu.org> | 2020-03-30 10:39:17 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2020-03-30 10:39:17 +0200 |
commit | 4180668edd5ddcb28adff6add951095063102091 (patch) | |
tree | 6ba22a9c1085d25d16956cbaabaa5995dcbaa0a0 | |
parent | 926869b73b950b1b0cc23156ed748fe32414c075 (diff) | |
parent | 6f85060c454e94fae7e7679521e8c0d8d6c0d365 (diff) |
Merge tag '0.0.7' into debian
munin-libvirt-plugins v0.0.7
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | README.md (renamed from README) | 32 | ||||
-rw-r--r-- | libvirt-blkstat | 64 | ||||
-rw-r--r-- | libvirt-cputime | 45 | ||||
-rw-r--r-- | libvirt-ifstat | 64 | ||||
-rw-r--r-- | libvirt-mem | 161 | ||||
-rw-r--r-- | munin-libvirt-plugins-detect.in | 36 | ||||
-rw-r--r-- | setup.cfg | 5 |
8 files changed, 260 insertions, 153 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) @@ -36,6 +36,6 @@ clean: rm -f *.py *.pyc $(DETECT) dist: clean check - git-archive --format=tar --prefix=$(PACKAGE)-$(VERSION)/ HEAD | gzip -c > ../$(PACKAGE)-$(VERSION).tar.gz + git archive --format=tar --prefix=$(PACKAGE)-$(VERSION)/ HEAD | gzip -c > ../$(PACKAGE)-$(VERSION).tar.gz .PHONY: clean check dist install build @@ -2,7 +2,8 @@ This is a set of munin plugins to monitor virtual machines managed via libvirt. See INSTALL for installation instructions. -== Setting things up == +Setting things up +================= Autoconfiguration is simple. Run: @@ -14,26 +15,27 @@ enable all plugins. If you prefer to configure things by hand: The default uri used to connect to libvirt is "qemu:///system". This can be changed in the plugin configuration: -cat <<EOF >/etc/munin/plugin-conf.d/libvirt -[libvirt-*] -env.uri xen:/// -EOF + cat <<EOF >/etc/munin/plugin-conf.d/libvirt + [libvirt-*] + env.uri xen:/// + EOF Enabling plugins by hand is the same as with any other munin plugin: - ln -s /usr/share/munin/plugins/libvirt-<plugin> /etc/munin/plugins/ - /etc/init.d/munin-node restart + ln -s /usr/share/munin/plugins/libvirt-<plugin> /etc/munin/plugins/ + /etc/init.d/munin-node restart -== Plugin Configuration == +Plugin Configuration +==================== -libvirt-mem and libvirt-cputime support limits[1] for critical and warning +libvirt-mem and libvirt-cputime support [limits][] for critical and warning values. These are set to 90% (warning) and 95% (critical) by default. However you can change them in /etc/munin/munin.conf: -[somehost.example.com] -libvirt_cputime.total_pc.warning 60 -libvirt_cputime.total_pc.critical 75 -libvirt_mem.total_pc.warning 25 -libvirt_mem.total_pc.critical 50 + [somehost.example.com] + libvirt_cputime.total_pc.warning 60 + libvirt_cputime.total_pc.critical 75 + libvirt_mem.total_pc.warning 25 + libvirt_mem.total_pc.critical 50 -[1]: http://munin.projects.linpro.no/wiki/HowToContact +[limits]: http://munin.projects.linpro.no/wiki/HowToContact 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 88bc2c2..a789326 100644 --- a/libvirt-mem +++ b/libvirt-mem @@ -1,39 +1,64 @@ -#!/usr/bin/python +#!/usr/bin/python3 # vim: set fileencoding=utf-8 : # # Munin plugin to show the amount of memory used by libvirt managed virtual # machines -# -# Copyright 2008 Guido Guenther <agx@sigxcpu.org> +# +# Copyright 2008,2012 Guido Guenther <agx@sigxcpu.org> # # License: GPLv2 # -# depends: python-libvirt +# depends: python-libvirt, python-libxml2 # #%# 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" + for id in ids: + try: + dom = conn.lookupByID(id) + name = dom.name() + 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)) + if draw == "AREA": + draw = "STACK" + + print(""" host_mem.label %(hostname)s (host) host_mem.type GAUGE host_mem.min 0 @@ -60,46 +85,90 @@ total_max_pc.type GAUGE total_max_pc.label total maximum memory percentage total_max_pc.graph no total_max_pc.info maximum memory in percent virtual machines can balloon to on host '%(hostname)s' -total_max_pc.min 0""" % dict(hostname=hostname) +total_max_pc.min 0 +total_min_guarantee.type GAUGE +total_min_guarantee.label total mem minimum guarantee +total_min_guarantee.info min_guarantee minimum amount of memory guaranteed to all virtual machines on host '%(hostname)s' +total_min_guarantee.min 0 +total_min_guarantee_pc.type GAUGE +total_min_guarantee_pc.label total mem minimum guarantee percentage +total_min_guarantee_pc.graph no +total_min_guarantee_pc.info min_guarantee minimum amount of memory in percent guaranteed to all virtual machines on host '%(hostname)s' +total_min_guarantee_pc.min 0 +total_hard_limit.type GAUGE +total_hard_limit.label total mem hard limit +total_hard_limit.info memory hard_limit of all virtual machines on host '%(hostname)s' +total_hard_limit.min 0 +total_hard_limit_pc.type GAUGE +total_hard_limit_pc.label total mem hard limit percentage +total_hard_limit_pc.graph no +total_hard_limit_pc.info memory hard_limit percentage of all virtual machines on host '%(hostname)s' +total_hard_limit_pc.min 0 +total_soft_limit.type GAUGE +total_soft_limit.label total mem soft limit +total_soft_limit.info memory soft_limit of all virtual machines on host '%(hostname)s' +total_soft_limit.min 0 +total_soft_limit_pc.type GAUGE +total_soft_limit_pc.label total mem soft limit percentage +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)) - ids = conn.listDomainsID() - draw = "AREA" - for id in ids: - try: - dom = conn.lookupByID(id) - name = dom.name() - except libvirt.libvirtError, err: - print >>sys.stderr, "Id: %s: %s" % (id, err) - 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) - if draw == "AREA": - draw = "STACK" return 0 +def get_memtune(dom): + memtune = {'min_guarantee': 0, 'soft_limit': 0, 'hard_limit': 0} + xml = dom.XMLDesc(0) + + try: + doc = libxml2.parseDoc(xml) + except Exception: + return [] + + ctx = doc.xpathNewContext() + try: + for key in memtune: + ret = ctx.xpathEval("/domain/memtune/%s" % key) + try: + for child in ret[0].children: + memtune[key] = int(child.content) + break + except IndexError: + # key not found in xml + pass + finally: + if ctx is not None: + ctx.xpathFreeContext() + if doc is not None: + doc.freeDoc() + return memtune + + def fetch_values(uri): + """Fetch values in the <memtune/> block""" total = 0 total_max = 0 + min_guarantee = 0 + hard_limit = 0 + soft_limit = 0 + 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 @@ -108,11 +177,22 @@ def fetch_values(uri): maxmem *= 1024 total += mem total_max += maxmem - print "%s_mem.value %d" % (canon(name), mem) - 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("%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))) return 0 @@ -120,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 |