From c4a08bfa53ee326df91d64b668e7b98105f659d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20S?= Date: Thu, 11 Jul 2019 11:43:32 +0200 Subject: [PATCH] libvirt/kvm_*: Simple regex to match VM name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Debian Stretch the cmdline is something like: qemu-system-x86_64-enable-kvm-nameguest=vmname,debug-threads=on-S-[…] Without null characters: qemu-system-x86_64 -enable-kvm -name guest=vmname,debug-threads=on[…] We need to match only guest=vmname, so the regex "^.*guest=([a-zA-Z0-9.-_-]*).*$" is simpler and match the VM name. The precedent regex "^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$" was not matching it. BTW, It seems that \x00 does not match correctly null characters so I removed it. --- plugins/libvirt/kvm_cpu | 2 +- plugins/libvirt/kvm_io | 2 +- plugins/libvirt/kvm_mem | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/libvirt/kvm_cpu b/plugins/libvirt/kvm_cpu index c9cad306..ff696fc7 100755 --- a/plugins/libvirt/kvm_cpu +++ b/plugins/libvirt/kvm_cpu @@ -68,7 +68,7 @@ def find_vm_names(pids): result = {} for pid in pids: cmdline = open("/proc/%s/cmdline" % pid, "r") - result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$",r"\1", cmdline.readline())) + result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$",r"\1", cmdline.readline())) return result def list_pids(): diff --git a/plugins/libvirt/kvm_io b/plugins/libvirt/kvm_io index 972f4983..0e0cc386 100755 --- a/plugins/libvirt/kvm_io +++ b/plugins/libvirt/kvm_io @@ -85,7 +85,7 @@ def find_vm_names(pids): result = {} for pid in pids: cmdline = open("/proc/%s/cmdline" % pid, "r") - result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$",r"\1", cmdline.readline())) + result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$",r"\1", cmdline.readline())) return result def list_pids(): diff --git a/plugins/libvirt/kvm_mem b/plugins/libvirt/kvm_mem index 7e8d2793..aeb1af7e 100755 --- a/plugins/libvirt/kvm_mem +++ b/plugins/libvirt/kvm_mem @@ -82,7 +82,7 @@ def find_vm_names(pids): result = {} for pid in pids: cmdline = open("/proc/%s/cmdline" % pid, "r") - result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$",r"\1", cmdline.readline())) + result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$",r"\1", cmdline.readline())) return result def list_pids():