mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-21 18:41:03 +00:00
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
This commit is contained in:
parent
2157586c7f
commit
3dc47ad750
1 changed files with 14 additions and 9 deletions
|
@ -61,15 +61,20 @@ def fetch(vms):
|
||||||
res = {}
|
res = {}
|
||||||
for pid in vms:
|
for pid in vms:
|
||||||
try:
|
try:
|
||||||
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
with open(f"/proc/{pid}/cmdline", "rb") as f:
|
||||||
amount = re.sub(r"^.*-m\x00(.*)\x00-smp.*$",r"\1", cmdline.readline())
|
line = f.read().replace(b'\x00', b' ')
|
||||||
amount = int(amount) * 1024 * 1024
|
match = re.search(rb"-m\s+size=(\d+)k", line)
|
||||||
print("%s_mem.value %s" % (vms[pid], amount))
|
if match:
|
||||||
except:
|
amount = int(match.group(1)) * 1024
|
||||||
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
else:
|
||||||
amount = re.sub(r"^.*-m\x00(\d+).*$",r"\1", cmdline.readline())
|
match = re.search(rb"-m\s+size=(\d+)", line)
|
||||||
amount = int(amount) * 1024 * 1024
|
if match:
|
||||||
print("%s_mem.value %s" % (vms[pid], amount))
|
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():
|
def detect_kvm():
|
||||||
''' Check if kvm is installed
|
''' Check if kvm is installed
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue