From e0be677806683875a9d301c494c57e305472e94f Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sat, 14 Sep 2024 17:50:18 +0200 Subject: [PATCH] check_shelly improved + library update --- check_shelly | 92 -------------------------------- check_shelly_filesystem | 1 + check_shelly_memory | 1 + check_shelly_mqtt | 1 + check_shelly_temperature | 1 + check_shelly_wifi | 111 +++++++++++++++++++++++++++++++++++++++ nagios | 2 +- z_server/devdi | 2 +- z_server/report | 2 +- 9 files changed, 118 insertions(+), 95 deletions(-) delete mode 100755 check_shelly create mode 120000 check_shelly_filesystem create mode 120000 check_shelly_memory create mode 120000 check_shelly_mqtt create mode 120000 check_shelly_temperature create mode 100755 check_shelly_wifi diff --git a/check_shelly b/check_shelly deleted file mode 100755 index 71f8dee..0000000 --- a/check_shelly +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/python3 -# -import argparse -import json -import nagios -import os -import urllib.request - -CHECKS = ['wifi', 'mqtt', 'memory', 'filesystem', 'temperature'] -# -WIFI_QUALITY_ERROR = -92 -WIFI_QUALITY_WARNING = -91 -# -RAM_ERROR = .15 -RAM_WARNING = .30 -# -FS_ERROR = .15 -FS_WARNING = .30 -# -TMP_WARNING = 55 -TMP_ERROR = 60 -# - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - prog=os.path.basename(__file__), - description='Check shelly for nagios monitorin', - # epilog='Text at the bottom of help' - ) - parser.add_argument('-H', '--hostname', required=True) - parser.add_argument('-c', '--check', choices=CHECKS, required=True) - args = parser.parse_args() - # - n = nagios.Nagios() - status = n.UNKNOWN - # - with urllib.request.urlopen(f"http://{args.hostname}/status") as response: - data = json.load(response) - # - # - if args.check == 'wifi': - connected = data['wifi_sta']['connected'] - quality = data['wifi_sta']['rssi'] - if not connected or quality <= WIFI_QUALITY_ERROR: - status = n.ERROR - elif quality <= WIFI_QUALITY_WARNING: - status = n.WARNING - else: - status = n.OK - n.exit(status, f"connected: {connected} - quality: {quality} > {WIFI_QUALITY_WARNING} > {WIFI_QUALITY_ERROR}") - elif args.check == 'mqtt': - connected = data['mqtt']['connected'] - if not connected: - status = n.ERROR - else: - status = n.OK - n.exit(status, f"connected: {connected}") - elif args.check == 'memory': - ram_total = data['ram_total'] - ram_free = data['ram_free'] - ram_left = ram_free / ram_total - if ram_left <= RAM_ERROR: - status = n.ERROR - elif ram_left <= RAM_WARNING: - status = n.WARNING - else: - status = n.OK - n.exit(status, f"ram_left: {ram_left} ({ram_total}) > {RAM_WARNING} > {RAM_ERROR}") - elif args.check == 'filesystem': - fs_size = data['fs_size'] - fs_free = data['fs_free'] - fs_left = fs_free / fs_size - if fs_left <= FS_ERROR: - status = n.ERROR - elif fs_left <= FS_WARNING: - status = n.WARNING - else: - status = n.OK - n.exit(status, f"fs_left: {fs_left} ({fs_size}) > {FS_WARNING} > {FS_ERROR}") - elif args.check == 'temperature': - temperature = data.get('tmp', {}).get('tC') - if temperature is None: - if args.hostname.startswith('shelly1-'): - n.exit(n.OK, "Shelly1 has no temperature information") - status = n.UNKNOWN - elif temperature >= TMP_ERROR: - status = n.ERROR - elif temperature >= TMP_WARNING: - status = n.WARNING - else: - status = n.OK - n.exit(status, f"temperature: {temperature} C < {TMP_WARNING} < {TMP_ERROR}") diff --git a/check_shelly_filesystem b/check_shelly_filesystem new file mode 120000 index 0000000..0850084 --- /dev/null +++ b/check_shelly_filesystem @@ -0,0 +1 @@ +check_shelly_wifi \ No newline at end of file diff --git a/check_shelly_memory b/check_shelly_memory new file mode 120000 index 0000000..0850084 --- /dev/null +++ b/check_shelly_memory @@ -0,0 +1 @@ +check_shelly_wifi \ No newline at end of file diff --git a/check_shelly_mqtt b/check_shelly_mqtt new file mode 120000 index 0000000..0850084 --- /dev/null +++ b/check_shelly_mqtt @@ -0,0 +1 @@ +check_shelly_wifi \ No newline at end of file diff --git a/check_shelly_temperature b/check_shelly_temperature new file mode 120000 index 0000000..0850084 --- /dev/null +++ b/check_shelly_temperature @@ -0,0 +1 @@ +check_shelly_wifi \ No newline at end of file diff --git a/check_shelly_wifi b/check_shelly_wifi new file mode 100755 index 0000000..ca96d1c --- /dev/null +++ b/check_shelly_wifi @@ -0,0 +1,111 @@ +#!/bin/python3 +# +import argparse +import json +import nagios +import os +import urllib.request + +def get_shelly_data(hostname): + with urllib.request.urlopen(f"http://{hostname}/status") as response: + return json.load(response) + +def check_wifi(hostname, warn_lvl, crit_lvl): + data = get_shelly_data(hostname) + # + connected = data['wifi_sta']['connected'] + quality = data['wifi_sta']['rssi'] + if not connected or quality <= crit_lvl: + status = nagios.Nagios.ERROR + elif quality <= warn_lvl: + status = nagios.Nagios.WARNING + else: + status = nagios.Nagios.OK + return (status, f"shelly: connected={connected}; quality={quality} shall be > warn={warn_lvl} / crit={crit_lvl}") + +def check_mqtt(hostname, warn_lvl, crit_lvl): + data = get_shelly_data(hostname) + # + connected = data['mqtt']['connected'] + if not connected: + status = nagios.Nagios.ERROR + else: + status = nagios.Nagios.OK + return (status, f"shelly: connected={connected}") + +def check_memory(hostname, warn_lvl, crit_lvl): + data = get_shelly_data(hostname) + # + ram_total = data['ram_total'] + ram_free = data['ram_free'] + ram_left = ram_free / ram_total * 100 + if ram_left <= crit_lvl: + status = nagios.Nagios.ERROR + elif ram_left <= warn_lvl: + status = nagios.Nagios.WARNING + else: + status = nagios.Nagios.OK + return (status, f"shelly: ram_left={ram_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}") + +def check_filesystem(hostname, warn_lvl, crit_lvl): + data = get_shelly_data(hostname) + # + fs_size = data['fs_size'] + fs_free = data['fs_free'] + fs_left = fs_free / fs_size * 100 + if fs_left <= crit_lvl: + status = nagios.Nagios.ERROR + elif fs_left <= warn_lvl: + status = nagios.Nagios.WARNING + else: + status = nagios.Nagios.OK + return (status, f"shelly: fs_left={fs_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}") + +def check_temperature(hostname, warn_lvl, crit_lvl): + data = get_shelly_data(hostname) + # + temperature = data.get('tmp', {}).get('tC') + if temperature is None: + if args.hostname.startswith('shelly1-'): + return (nagios.Nagios.OK, "shelly: Shelly1 has no temperature information") + status = nagios.Nagios.UNKNOWN + elif temperature >= crit_lvl: + status = nagios.Nagios.ERROR + elif temperature >= warn_lvl: + status = nagios.Nagios.WARNING + else: + status = nagios.Nagios.OK + return (status, f"shelly: temperature:={temperature} C shall be < warn={warn_lvl} / crit={crit_lvl}") + + +if __name__ == "__main__": + # Parse arguments + parser = argparse.ArgumentParser( + prog=os.path.basename(__file__), + description='Check shelly for nagios monitorin', + ) + parser.add_argument('-H', '--hostname', required=True) + parser.add_argument('-w', '--warn_lvl', type=float, required=True) + parser.add_argument('-c', '--critical_lvl', type=float, required=True) + args = parser.parse_args() + + # Init nagios instance + n = nagios.Nagios() + + # Identify check + this_check = parser.prog[len("check_shelly_"):] + check_function = { + 'wifi': check_wifi, + 'mqtt': check_mqtt, + 'memory': check_memory, + 'filesystem': check_filesystem, + 'temperature': check_temperature + }.get(this_check) + + # process check and return feedback + if check_function is None: + status = n.UNKNOWN + message = f"Unknown check - {this_check}" + else: + status, message = check_function(args.hostname, args.warn_lvl, args.critical_lvl) + n.exit(status, message) diff --git a/nagios b/nagios index 407641b..8726b19 160000 --- a/nagios +++ b/nagios @@ -1 +1 @@ -Subproject commit 407641b1d5b157ada46694dd81183fb962dc4831 +Subproject commit 8726b19639832d3ce0653bb91bd74e2e2a64bc3f diff --git a/z_server/devdi b/z_server/devdi index 4bda94e..66a4719 160000 --- a/z_server/devdi +++ b/z_server/devdi @@ -1 +1 @@ -Subproject commit 4bda94e4f7e969e51e368ccfae9b25512a717171 +Subproject commit 66a471979d41bb6477b676dddd3f5959ba0326cc diff --git a/z_server/report b/z_server/report index b53dd30..7003c13 160000 --- a/z_server/report +++ b/z_server/report @@ -1 +1 @@ -Subproject commit b53dd30eae1d679b7eec4999bec50aed55bc105b +Subproject commit 7003c13ef8c7e7c3a55a545cbbad4039cc024a9f