From 6ee90eabeacc699f3b42d346fd9d5d107db78cf7 Mon Sep 17 00:00:00 2001 From: danny Date: Thu, 4 Dec 2025 08:02:02 +0100 Subject: [PATCH] Update roles/pips/files/memory_usage.py --- roles/pips/files/memory_usage.py | 71 ++++++++++++++++---------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/roles/pips/files/memory_usage.py b/roles/pips/files/memory_usage.py index 55f032f..3b62b03 100644 --- a/roles/pips/files/memory_usage.py +++ b/roles/pips/files/memory_usage.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3.11 """Nagios plugin: report current memory usage. Outputs a one-line status and perfdata and uses Nagios exit codes: @@ -13,50 +13,49 @@ import psutil def human_mb(bytes_val): - return bytes_val / (1024 * 1024) + return bytes_val / (1024 * 1024) def main(): - parser = argparse.ArgumentParser(description="Nagios memory usage check") - parser.add_argument("-w", "--warning", type=int, default=80, - help="warning threshold (percent used)") - parser.add_argument("-c", "--critical", type=int, default=90, - help="critical threshold (percent used)") - args = parser.parse_args() + parser = argparse.ArgumentParser(description="Nagios memory usage check") + parser.add_argument("-w", "--warning", type=int, default=80, + help="warning threshold (percent used)") + parser.add_argument("-c", "--critical", type=int, default=90, + help="critical threshold (percent used)") + args = parser.parse_args() - if args.warning >= args.critical: - print("UNKNOWN - warning threshold must be less than critical") - sys.exit(3) + if args.warning >= args.critical: + print("UNKNOWN - warning threshold must be less than critical") + sys.exit(3) - try: - vm = psutil.virtual_memory() - except Exception as e: - print(f"UNKNOWN - failed to read memory: {e}") - sys.exit(3) + try: + vm = psutil.virtual_memory() + except Exception as e: + print(f"UNKNOWN - failed to read memory: {e}") + sys.exit(3) - total_mb = human_mb(vm.total) - used_mb = human_mb(vm.total - vm.available) - percent_used = vm.percent + total_mb = human_mb(vm.total) + used_mb = human_mb(vm.total - vm.available) + percent_used = vm.percent - status = 0 - status_str = "OK" - if percent_used >= args.critical: - status = 2 - status_str = "CRITICAL" - elif percent_used >= args.warning: - status = 1 - status_str = "WARNING" + status = 0 + status_str = "OK" + if percent_used >= args.critical: + status = 2 + status_str = "CRITICAL" + elif percent_used >= args.warning: + status = 1 + status_str = "WARNING" - # Perfdata: used and total in MiB, and percent - perf = ( - f"used={used_mb:.1f}MiB;{args.warning};{args.critical};0;{total_mb:.1f}" - f" total={total_mb:.1f}MiB" - ) + # Perfdata: used and total in MiB, and percent + perf = ( + f"used={used_mb:.1f}MiB;;;0;{total_mb:.1f}" + f" total={total_mb:.1f}MiB" + ) - print(f"MEMORY {status_str} - {used_mb:.1f}MiB used of {total_mb:.1f}MiB ({percent_used:.1f}%) | {perf}") - sys.exit(status) + print(f"MEMORY {status_str} - {used_mb:.1f}MiB used of {total_mb:.1f}MiB ({percent_used:.1f}%) | {perf}") + sys.exit(status) if __name__ == "__main__": - main() - + main()