From 8cb47cbfbbe6780d8772236a438e948225c47806 Mon Sep 17 00:00:00 2001 From: Bianco Veigel Date: Sun, 30 Aug 2020 20:57:50 +0200 Subject: [PATCH 1/3] remove empty line from config --- plugins/libvirt/kvm_mem | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/libvirt/kvm_mem b/plugins/libvirt/kvm_mem index eaa86fda..437e59a1 100755 --- a/plugins/libvirt/kvm_mem +++ b/plugins/libvirt/kvm_mem @@ -25,8 +25,7 @@ def config(vm_names): graph_vlabel Bytes graph_category virtualization graph_info This graph shows the current amount of memory used by virtual machines -graph_args --base 1024 - """ +graph_args --base 1024""" print base_config draw = "AREA" for vm in vm_names: From 2385a6f570e1a58fe11941ea911ed3c1360d8759 Mon Sep 17 00:00:00 2001 From: Bianco Veigel Date: Mon, 7 Sep 2020 19:13:39 +0200 Subject: [PATCH 2/3] refactoring and python3 compatibility --- plugins/libvirt/kvm_mem | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/plugins/libvirt/kvm_mem b/plugins/libvirt/kvm_mem index 437e59a1..fb63f784 100755 --- a/plugins/libvirt/kvm_mem +++ b/plugins/libvirt/kvm_mem @@ -25,17 +25,13 @@ def config(vm_names): graph_vlabel Bytes graph_category virtualization graph_info This graph shows the current amount of memory used by virtual machines -graph_args --base 1024""" - print base_config - draw = "AREA" +graph_args --base 1024 -l 0""" + print(base_config) for vm in vm_names: - print "%s_mem.label %s" % (vm, vm) - print "%s_mem.type GAUGE" % vm - if draw == 'AREA': - print "%s_mem.min 0" % vm - print "%s_mem.draw %s" % (vm, draw) - print "%s_mem.info memory used by virtual machine %s" % (vm, vm) - draw = "STACK" + print("%s_mem.label %s" % (vm, vm)) + print("%s_mem.type GAUGE" % vm) + print("%s_mem.draw %s" % (vm, "AREASTACK")) + print("%s_mem.info memory used by virtual machine %s" % (vm, vm)) def clean_vm_name(vm_name): @@ -68,12 +64,12 @@ def fetch(vms): cmdline = open("/proc/%s/cmdline" % pid, "r") amount = re.sub(r"^.*-m\x00(.*)\x00-smp.*$",r"\1", cmdline.readline()) amount = int(amount) * 1024 * 1024 - print "%s_mem.value %s" % (vms[pid], amount) + print("%s_mem.value %s" % (vms[pid], amount)) except: cmdline = open("/proc/%s/cmdline" % pid, "r") amount = re.sub(r"^.*-m\x00(\d+).*$",r"\1", cmdline.readline()) amount = int(amount) * 1024 * 1024 - print "%s_mem.value %s" % (vms[pid], amount) + print("%s_mem.value %s" % (vms[pid], amount)) def detect_kvm(): ''' Check if kvm is installed @@ -103,9 +99,9 @@ if __name__ == "__main__": if len(sys.argv) > 1: if sys.argv[1] in ['autoconf', 'detect']: if detect_kvm(): - print "yes" + print("yes") else: - print "no" + print("no") elif sys.argv[1] == "config": config(find_vm_names(list_pids()).values()) else: From 6b4a3124c7ae2f7941eace58d4403f522ad69820 Mon Sep 17 00:00:00 2001 From: Bianco Veigel Date: Fri, 11 Sep 2020 13:15:43 +0200 Subject: [PATCH 3/3] switch to python3 --- plugins/libvirt/kvm_mem | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/libvirt/kvm_mem b/plugins/libvirt/kvm_mem index fb63f784..2ada1da1 100755 --- a/plugins/libvirt/kvm_mem +++ b/plugins/libvirt/kvm_mem @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -*- coding: utf-8 -*- # vim: set fileencoding=utf-8 # @@ -92,7 +92,7 @@ def list_pids(): ''' Find the pid of kvm processes @return a list of pids from running kvm ''' - pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE) + pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE, text=True) return pid.communicate()[0].split() if __name__ == "__main__":