diff --git a/check_lmsensors.py b/check_lmsensors.py new file mode 100644 index 0000000..0861561 --- /dev/null +++ b/check_lmsensors.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 +import json +import subprocess +import sys +import re +from pprint import pprint + +TEMP_RE = re.compile("temp\d_input") + +threshold_warning = 70 +threshold_critical = 80 + +exit_code=3 +message="Unknown problem occured during execution" + +try: + + def check_values(d): + warning = False + critical = False + for value in d.values(): + if value >= threshold_warning: + warning = True + if value >= threshold_critical: + critical = True + if critical: return 2 + if warning: return 1 + return 0 + + def format_dict(d): + result = "" + for key, value in d.items(): + result += f"'{key}'={value}°C;{threshold_warning};{threshold_critical}\n" + return result + + def get_data(): + json_data = subprocess.run(["sensors", "-Aj"], capture_output=True).stdout + parsed = json.loads(json_data) + + stats = {} + + for adapter, data in parsed.items(): + for sensor, data in data.items(): + temp_value = None + for key, value in data.items(): + if TEMP_RE.fullmatch(key): + temp_value = value + if temp_value is not None: + stats[f"{adapter}.{sensor}"] = temp_value + + return stats + + data = get_data() + exit_code = check_values(data) + if exit_code == 0: message = "SENSORS OK" + elif exit_code == 1: message = "SENSORS WARNING" + elif exit_code == 2: message = "SENSORS CRITICAL" + else: message = "SOMETHINGS WRONG" + message += "|"+format_dict(data) + +finally: + print(message) + sys.exit(exit_code)