diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..baf5917 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "nagios"] + path = nagios + url = https://git.mount-mockery.de/pylib/nagios.git diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a7a965e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "autopep8.args": ["--max-line-length=150"], + "[python]": { + "python.formatting.provider": "none", + "editor.defaultFormatter": "ms-python.autopep8", + "editor.formatOnSave": true + }, + "editor.fontSize": 14, + "emmet.includeLanguages": { "django-html": "html" }, + "python.testing.pytestArgs": ["-v", "--cov", "--cov-report=xml", "__test__"], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} diff --git a/check_shelly b/check_shelly new file mode 100755 index 0000000..7459c49 --- /dev/null +++ b/check_shelly @@ -0,0 +1,40 @@ +#!/bin/python3 +# +import argparse +import json +import nagios +import os +import urllib.request + +CHECKS = ['wifi'] +# +WIFI_QUALITY_ERROR = -30 +WIFI_QUALITY_WARNING = -50 + +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}") diff --git a/nagios b/nagios new file mode 160000 index 0000000..3d7f2ec --- /dev/null +++ b/nagios @@ -0,0 +1 @@ +Subproject commit 3d7f2eca05ebe5fdcf17f9e77e47cc62d326874b