54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
#!/usr/bin/env python3.11
|
|
"""Nagios plugin: report current memory usage.
|
|
|
|
Outputs a one-line status and perfdata and uses Nagios exit codes:
|
|
0 OK, 1 WARNING, 2 CRITICAL, 3 UNKNOWN
|
|
|
|
Defaults: warning=80%%, critical=90%% (percent used).
|
|
"""
|
|
|
|
import argparse
|
|
import sys
|
|
import psutil
|
|
|
|
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()
|
|
|
|
if args.warning >= args.critical:
|
|
print("UNKNOWN - warning threshold must be less than critical")
|
|
sys.exit(3)
|
|
|
|
try:
|
|
cpu = psutil.cpu_percent(interval=5,percpu=True)
|
|
except Exception as e:
|
|
print(f"UNKNOWN - failed to read cpu's: {e}")
|
|
sys.exit(3)
|
|
|
|
status = 0
|
|
cpu_list = ""
|
|
cpu_print = ""
|
|
cpu_num = 0
|
|
status_str = "OK"
|
|
for percent_used in cpu:
|
|
if percent_used >= args.critical:
|
|
status = 2
|
|
status_str = "CRITICAL"
|
|
elif percent_used >= args.warning:
|
|
status = 1
|
|
status_str = "WARNING"
|
|
cpu_list += f"cpu{cpu_num}={percent_used:.1f};;;0; "
|
|
cpu_print += f"{percent_used:.1f}% "
|
|
cpu_num += 1
|
|
|
|
print(f"CPU {status_str} {cpu_print} | {cpu_list} ")
|
|
sys.exit(status)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|