From 1c4a4fb84bf470709541067665dbc232e2c12dd3 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 30 Mar 2020 10:08:06 +0200 Subject: flake8 cleanups --- libvirt-blkstat | 15 ++++++++++----- libvirt-cputime | 10 +++++++--- libvirt-ifstat | 15 ++++++++++----- libvirt-mem | 18 +++++++++++------- munin-libvirt-plugins-detect.in | 13 +++++++------ 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/libvirt-blkstat b/libvirt-blkstat index fc32c9d..37be14d 100644 --- a/libvirt-blkstat +++ b/libvirt-blkstat @@ -17,9 +17,11 @@ 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""" @@ -50,12 +52,13 @@ graph_info This graph shows the block device I/O of the virtual machines""") 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 = [] @@ -66,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() @@ -107,7 +111,7 @@ 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") return 0 @@ -120,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 d9f9e1b..e49d20e 100644 --- a/libvirt-cputime +++ b/libvirt-cputime @@ -17,9 +17,11 @@ 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""" @@ -37,7 +39,7 @@ total_pc.info cputime used by all virtual machines total_pc.warning 90 total_pc.critical 95""") - draw = [ "LINE1", "AREA"][stack] + draw = ["LINE1", "AREA"][stack] conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() for id in ids: @@ -57,6 +59,7 @@ total_pc.critical 95""") if draw == "AREA": draw = "STACK" + def fetch_values(uri): conn = libvirt.openReadOnly(uri) ids = conn.listDomainsID() @@ -81,10 +84,10 @@ def fetch_values(uri): 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") return 0 @@ -97,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 c9ff082..1561c6a 100644 --- a/libvirt-ifstat +++ b/libvirt-ifstat @@ -17,9 +17,11 @@ 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""" @@ -50,12 +52,13 @@ graph_info This graph shows the network I/O of the virtual machines""") 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 = [] @@ -66,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() @@ -106,7 +110,7 @@ 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") return 0 @@ -119,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 83dea77..a789326 100644 --- a/libvirt-mem +++ b/libvirt-mem @@ -18,9 +18,11 @@ 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: @@ -116,13 +118,14 @@ total_soft_limit_pc.min 0 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() @@ -134,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 @@ -197,7 +200,7 @@ 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") return 0 @@ -208,6 +211,7 @@ def main(sys): 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 397ccc2..7e86ff2 100644 --- a/munin-libvirt-plugins-detect.in +++ b/munin-libvirt-plugins-detect.in @@ -20,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 @@ -34,14 +35,14 @@ def check_uris(uris, force=False): libvirt.openReadOnly(uri) detected = uri break - except libvirt.libvirtError as err: + except libvirt.libvirtError: pass if detected: print("Hypervisor at %s detected." % detected) if not os.path.exists(PLUGIN_CONF) or force: try: - conf = file(PLUGIN_CONF, "w+") + conf = open(PLUGIN_CONF, "w+") print("[libvirt-*]", file=conf) print("env.uri %s""" % detected, file=conf) conf.close() @@ -73,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) -- cgit v1.2.3