From 3dc47ad750dc8835a64530760c672e58ab4171d5 Mon Sep 17 00:00:00 2001 From: Rowan Wookey Date: Tue, 17 Jun 2025 12:20:00 +0100 Subject: [PATCH] Updated kvm_mem to better handle extrating values Improved the regex parsing to handle cmdline with `k` suffix on memory and to output the command line to stderr if it can't be parsed --- plugins/libvirt/kvm_mem | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/plugins/libvirt/kvm_mem b/plugins/libvirt/kvm_mem index c6bfd97d..95277425 100755 --- a/plugins/libvirt/kvm_mem +++ b/plugins/libvirt/kvm_mem @@ -61,15 +61,20 @@ def fetch(vms): res = {} for pid in vms: try: - 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)) - 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)) + with open(f"/proc/{pid}/cmdline", "rb") as f: + line = f.read().replace(b'\x00', b' ') + match = re.search(rb"-m\s+size=(\d+)k", line) + if match: + amount = int(match.group(1)) * 1024 + else: + match = re.search(rb"-m\s+size=(\d+)", line) + if match: + amount = int(match.group(1)) * 1024 * 1024 + else: + raise ValueError(f"Memory size not found in {line}") + print(f"{vms[pid]}_mem.value {amount}") + except Exception as e: + print(f"Error extracting memory for PID {pid}: {e}", file=sys.stderr) def detect_kvm(): ''' Check if kvm is installed