From 88f4d79d5acc93263d1ad6d1b76e10dcf1b5ef33 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Wed, 27 Dec 2023 09:51:24 -0500 Subject: [PATCH] Handle invalid values on older Libvirt versions --- daemon-common/cluster.py | 102 +++++++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/daemon-common/cluster.py b/daemon-common/cluster.py index 224d6eef..6d637cd1 100644 --- a/daemon-common/cluster.py +++ b/daemon-common/cluster.py @@ -1106,30 +1106,6 @@ def get_resource_metrics(zkhandler): f"pvc_vm_memory_stats_actual{{vm=\"{vm['name']}\"}} {actual_memory}" ) - output_lines.append("# HELP pvc_vm_memory_stats_unused PVC VM unused memory") - output_lines.append("# TYPE pvc_vm_memory_stats_unused gauge") - for vm in vm_data: - unused_memory = vm["memory_stats"]["unused"] - output_lines.append( - f"pvc_vm_memory_stats_unused{{vm=\"{vm['name']}\"}} {unused_memory}" - ) - - output_lines.append("# HELP pvc_vm_memory_stats_available PVC VM available memory") - output_lines.append("# TYPE pvc_vm_memory_stats_available gauge") - for vm in vm_data: - available_memory = vm["memory_stats"]["available"] - output_lines.append( - f"pvc_vm_memory_stats_available{{vm=\"{vm['name']}\"}} {available_memory}" - ) - - output_lines.append("# HELP pvc_vm_memory_stats_usable PVC VM usable memory") - output_lines.append("# TYPE pvc_vm_memory_stats_usable gauge") - for vm in vm_data: - usable_memory = vm["memory_stats"]["usable"] - output_lines.append( - f"pvc_vm_memory_stats_usable{{vm=\"{vm['name']}\"}} {usable_memory}" - ) - output_lines.append("# HELP pvc_vm_memory_stats_rss PVC VM RSS memory") output_lines.append("# TYPE pvc_vm_memory_stats_rss gauge") for vm in vm_data: @@ -1138,12 +1114,52 @@ def get_resource_metrics(zkhandler): f"pvc_vm_memory_stats_rss{{vm=\"{vm['name']}\"}} {rss_memory}" ) + output_lines.append("# HELP pvc_vm_memory_stats_unused PVC VM unused memory") + output_lines.append("# TYPE pvc_vm_memory_stats_unused gauge") + for vm in vm_data: + unused_memory = ( + vm["memory_stats"]["unused"] + if vm["memory_stats"].get("unused") is not None + else 0 + ) + output_lines.append( + f"pvc_vm_memory_stats_unused{{vm=\"{vm['name']}\"}} {unused_memory}" + ) + + output_lines.append("# HELP pvc_vm_memory_stats_available PVC VM available memory") + output_lines.append("# TYPE pvc_vm_memory_stats_available gauge") + for vm in vm_data: + available_memory = ( + vm["memory_stats"]["available"] + if vm["memory_stats"].get("available") is not None + else 0 + ) + output_lines.append( + f"pvc_vm_memory_stats_available{{vm=\"{vm['name']}\"}} {available_memory}" + ) + + output_lines.append("# HELP pvc_vm_memory_stats_usable PVC VM usable memory") + output_lines.append("# TYPE pvc_vm_memory_stats_usable gauge") + for vm in vm_data: + usable_memory = ( + vm["memory_stats"]["usable"] + if vm["memory_stats"].get("usable") is not None + else 0 + ) + output_lines.append( + f"pvc_vm_memory_stats_usable{{vm=\"{vm['name']}\"}} {usable_memory}" + ) + output_lines.append( "# HELP pvc_vm_memory_stats_disk_caches PVC VM disk cache memory" ) output_lines.append("# TYPE pvc_vm_memory_stats_disk_caches gauge") for vm in vm_data: - disk_caches_memory = vm["memory_stats"]["disk_caches"] + disk_caches_memory = ( + vm["memory_stats"]["disk_caches"] + if vm["memory_stats"].get("disk_caches") is not None + else 0 + ) output_lines.append( f"pvc_vm_memory_stats_disk_caches{{vm=\"{vm['name']}\"}} {disk_caches_memory}" ) @@ -1151,7 +1167,11 @@ def get_resource_metrics(zkhandler): output_lines.append("# HELP pvc_vm_memory_swap_in PVC VM memory swap in") output_lines.append("# TYPE pvc_vm_memory_swap_in gauge") for vm in vm_data: - swap_in_memory = vm["memory_stats"]["swap_in"] + swap_in_memory = ( + vm["memory_stats"]["swap_in"] + if vm["memory_stats"].get("swap_in") is not None + else 0 + ) output_lines.append( f"pvc_vm_memory_stats_swap_in{{vm=\"{vm['name']}\"}} {swap_in_memory}" ) @@ -1159,7 +1179,11 @@ def get_resource_metrics(zkhandler): output_lines.append("# HELP pvc_vm_memory_swap_out PVC VM memory swap out") output_lines.append("# TYPE pvc_vm_memory_swap_out gauge") for vm in vm_data: - swap_out_memory = vm["memory_stats"]["swap_out"] + swap_out_memory = ( + vm["memory_stats"]["swap_out"] + if vm["memory_stats"].get("swap_out") is not None + else 0 + ) output_lines.append( f"pvc_vm_memory_stats_swap_out{{vm=\"{vm['name']}\"}} {swap_out_memory}" ) @@ -1167,7 +1191,11 @@ def get_resource_metrics(zkhandler): output_lines.append("# HELP pvc_vm_memory_major_fault PVC VM memory major faults") output_lines.append("# TYPE pvc_vm_memory_major_fault gauge") for vm in vm_data: - major_fault_memory = vm["memory_stats"]["major_fault"] + major_fault_memory = ( + vm["memory_stats"]["major_fault"] + if vm["memory_stats"].get("major_fault") is not None + else 0 + ) output_lines.append( f"pvc_vm_memory_stats_major_fault{{vm=\"{vm['name']}\"}} {major_fault_memory}" ) @@ -1175,7 +1203,11 @@ def get_resource_metrics(zkhandler): output_lines.append("# HELP pvc_vm_memory_minor_fault PVC VM memory minor faults") output_lines.append("# TYPE pvc_vm_memory_minor_fault gauge") for vm in vm_data: - minor_fault_memory = vm["memory_stats"]["minor_fault"] + minor_fault_memory = ( + vm["memory_stats"]["minor_fault"] + if vm["memory_stats"].get("minor_fault") is not None + else 0 + ) output_lines.append( f"pvc_vm_memory_stats_minor_fault{{vm=\"{vm['name']}\"}} {minor_fault_memory}" ) @@ -1185,7 +1217,11 @@ def get_resource_metrics(zkhandler): ) output_lines.append("# TYPE pvc_vm_memory_hugetlb_pgalloc gauge") for vm in vm_data: - hugetlb_pgalloc_memory = vm["memory_stats"]["hugetlb_pgalloc"] + hugetlb_pgalloc_memory = ( + vm["memory_stats"]["hugetlb_pgalloc"] + if vm["memory_stats"].get("hugetlb_pgalloc") is not None + else 0 + ) output_lines.append( f"pvc_vm_memory_stats_hugetlb_pgalloc{{vm=\"{vm['name']}\"}} {hugetlb_pgalloc_memory}" ) @@ -1195,7 +1231,11 @@ def get_resource_metrics(zkhandler): ) output_lines.append("# TYPE pvc_vm_memory_hugetlb_pgfail gauge") for vm in vm_data: - hugetlb_pgfail_memory = vm["memory_stats"]["hugetlb_pgfail"] + hugetlb_pgfail_memory = ( + vm["memory_stats"]["hugetlb_pgfail"] + if vm["memory_stats"].get("hugetlb_pgfail") is not None + else 0 + ) output_lines.append( f"pvc_vm_memory_stats_hugetlb_pgfail{{vm=\"{vm['name']}\"}} {hugetlb_pgfail_memory}" )