From 9f9464d13b297a5b84d0b40a2ba5c9c19b29448d Mon Sep 17 00:00:00 2001 From: Christian Neubauer Date: Fri, 3 Mar 2023 14:51:22 +0100 Subject: [PATCH 1/5] =?UTF-8?q?[add]=20#1=20=C3=9Cberwachung=20von=20Speic?= =?UTF-8?q?herplatzverbrauch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/test.hmdl.json | 19 ++++++++----------- source/localization/de.json | 3 ++- source/localization/en.json | 3 ++- source/logic/lib.py | 2 +- source/logic/main.py | 1 + tools/build | 1 + 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/test.hmdl.json b/examples/test.hmdl.json index d3e2051..37ac4e4 100644 --- a/examples/test.hmdl.json +++ b/examples/test.hmdl.json @@ -8,26 +8,23 @@ "annoy": false, "schedule": { "regular_interval": 15, - "attentive_interval": 5 + "attentive_interval": 1 }, "notifications": [ { "kind": "console", "parameters": { } - }, - { - "kind": "libnotify", - "parameters": { - } } ], - "kind": "file_state", + "kind": "generic_remote", "parameters": { - "path": "/tmp/test", - "exist": true, - "age_threshold": 60, - "size_threshold": 1 + "host" : "iks-vvd-cn.greenscale.lan", + "user" : "root", + "ssh_key" : "/home/christiann/.ssh/id_ed25519.pub", + "mount_point" : "/", + "threshold" : 95, + "strict" : false } } ] diff --git a/source/localization/de.json b/source/localization/de.json index 3dd8db2..ff9280c 100644 --- a/source/localization/de.json +++ b/source/localization/de.json @@ -21,5 +21,6 @@ "checks.http_request.header_value_mismatch": "Header-Wert für Schlüssel '{{key}}' '{{value_actual}}' stimmt nicht mit erwartetem Wert {{value_expected}} überein", "checks.http_request.body_misses_part": "Rumpf enthält nicht den erwarteten Teil '{{part}}'", "misc.state_file_path": "Pfad zur Zustands-Datei", - "misc.check_procedure_failed": "Prüfungs-Prozedur fehlgeschlagen" + "misc.check_procedure_failed": "Prüfungs-Prozedur fehlgeschlagen", + "checks.generic_remote.overflow": "Festplatte fast voll" } diff --git a/source/localization/en.json b/source/localization/en.json index 676273d..798c20d 100644 --- a/source/localization/en.json +++ b/source/localization/en.json @@ -21,5 +21,6 @@ "checks.http_request.header_value_mismatch": "actual header value for key '{{key}}' '{{value_actual}}' and does not match the expected value {{value_expected}}", "checks.http_request.body_misses_part": "body does not contain the expected part '{{part}}'", "misc.state_file_path": "state file path", - "misc.check_procedure_failed": "check procedure failed" + "misc.check_procedure_failed": "check procedure failed", + "checks.generic_remote.overflow": "harddisk full" } diff --git a/source/logic/lib.py b/source/logic/lib.py index 9ad64bb..9d741ac 100644 --- a/source/logic/lib.py +++ b/source/logic/lib.py @@ -14,7 +14,7 @@ def file_write(path, content): def string_coin(template, arguments): result = template for (key, value, ) in arguments.items(): - result = result.replace("{{%s}}" % key, value) + result = result.replace("{{%s}}" % key, str(value)) return result diff --git a/source/logic/main.py b/source/logic/main.py index 79e53fc..ac44b92 100644 --- a/source/logic/main.py +++ b/source/logic/main.py @@ -107,6 +107,7 @@ def main(): "script": implementation_check_kind_script(), "file_state": implementation_check_kind_file_state(), "http_request": implementation_check_kind_http_request(), + "generic_remote" : implementation_check_kind_generic_remote() } ### load notification channel implementations diff --git a/tools/build b/tools/build index d893ef2..511564f 100755 --- a/tools/build +++ b/tools/build @@ -44,6 +44,7 @@ def main(): _os.path.join(dir_source, "logic", "checks", "script.py"), _os.path.join(dir_source, "logic", "checks", "file_state.py"), _os.path.join(dir_source, "logic", "checks", "http_request.py"), + _os.path.join(dir_source, "logic", "checks", "generic_remote.py"), _os.path.join(dir_source, "logic", "channels", "_interface.py"), _os.path.join(dir_source, "logic", "channels", "console.py"), _os.path.join(dir_source, "logic", "channels", "email.py"), From f9cf26da7d951deb1271ff674f1ed2221864bba7 Mon Sep 17 00:00:00 2001 From: Christian Neubauer Date: Fri, 3 Mar 2023 14:58:00 +0100 Subject: [PATCH 2/5] =?UTF-8?q?[add]=20#1=20=C3=9Cberwachung=20von=20Speic?= =?UTF-8?q?herplatzverbrauch=20-=20vergessene=20Datei?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/logic/checks/generic_remote.py | 105 ++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 source/logic/checks/generic_remote.py diff --git a/source/logic/checks/generic_remote.py b/source/logic/checks/generic_remote.py new file mode 100644 index 0000000..b92b35b --- /dev/null +++ b/source/logic/checks/generic_remote.py @@ -0,0 +1,105 @@ +class implementation_check_kind_generic_remote(interface_check_kind): + + ''' + [implementation] + ''' + def parameters_schema(self): + return { + "type": "object", + "additionalProperties": False, + "properties": { + "host" : { + "type" : "string" + }, + "user" : { + "type" : "string" + }, + "ssh_key" : { + "type" : "string" + }, + "mount_point" : { + "type" : "string", + "default" : "/" + }, + "threshold" : { + "type" : "integer", + "default" : 95, + "description" : "maximaler Füllstand in Prozent" + }, + "strict" : { + "type" : "boolean", + "default" : False + } + }, + "required": [ + "host", "user", "ssh_key" + ] + } + + + ''' + [implementation] + ''' + def normalize_conf_node(self, node): + if not "host" in node \ + or not "user" in node \ + or not "ssh_key" in node: + raise ValueError("MISSING STUFF!") + if not "mount_point" in node: + node["mount_point"] = "/" + if not "threshold" in node: + node["threshold"] = 95 + if not "strict" in node: + node["strict"] = "/" + + return node + + ''' + [implementation] + ''' + def run(self, parameters): + SSH_COMMAND=string_coin("ssh -i {{ssh_key}} {{user}}@{{host}} \"df {{mount_point}} | tr -s ' '\"", parameters) + + retval=shell_command(SSH_COMMAND) + + if retval["return_code"] > 0: + return { + "condition" : enum_condition.unknown, + "info" : { + "error" : retval["stderr"] + } + } + else: + parts=retval["stdout"].split("\n")[-1].split(" ") + ret={ + "device" : parts[0], + "used" : parts[2], + "avail" : parts[3], + "perc" : int(parts[4][:-1]) + } + + if ret["perc"] > parameters["threshold"]: + return { + "condition": ( + enum_condition.critical + if parameters["strict"] else + enum_condition.warning + ), + "info": { + "mount_point": parameters["mount_point"], + "device": ret["device"], + "used": ret["used"], # ToDo: Humanlesbarkeit herstellen + "available": ret["avail"], # ToDo: Humanlesbarkeit herstellen + "percentage": str(ret["perc"]) + "%", + "host" : parameters["host"], + "faults": [ + translation_get("checks.generic_remote.overflow") + ] + } + } + + else: + return { + "condition": enum_condition.ok, + "info" : {} + } From 27b8ed017b626ca14ac207641a02a6a6c6367672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Fri, 3 Mar 2023 15:27:43 +0100 Subject: [PATCH 3/5] [fix] [#1] lib:shell_command, check:generic_remote --- source/logic/checks/generic_remote.py | 10 ++++++++-- source/logic/lib.py | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/source/logic/checks/generic_remote.py b/source/logic/checks/generic_remote.py index b92b35b..ad33581 100644 --- a/source/logic/checks/generic_remote.py +++ b/source/logic/checks/generic_remote.py @@ -11,6 +11,10 @@ class implementation_check_kind_generic_remote(interface_check_kind): "host" : { "type" : "string" }, + "port": { + "type": "integer", + "default": 22 + }, "user" : { "type" : "string" }, @@ -45,6 +49,8 @@ class implementation_check_kind_generic_remote(interface_check_kind): or not "user" in node \ or not "ssh_key" in node: raise ValueError("MISSING STUFF!") + if not "port" in node: + node["port"] = 22 if not "mount_point" in node: node["mount_point"] = "/" if not "threshold" in node: @@ -58,7 +64,7 @@ class implementation_check_kind_generic_remote(interface_check_kind): [implementation] ''' def run(self, parameters): - SSH_COMMAND=string_coin("ssh -i {{ssh_key}} {{user}}@{{host}} \"df {{mount_point}} | tr -s ' '\"", parameters) + SSH_COMMAND = string_coin("ssh -i {{ssh_key}} -p {{port}} {{user}}@{{host}} \"df {{mount_point}} | tr -s ' '\"", parameters) retval=shell_command(SSH_COMMAND) @@ -70,7 +76,7 @@ class implementation_check_kind_generic_remote(interface_check_kind): } } else: - parts=retval["stdout"].split("\n")[-1].split(" ") + parts=retval["stdout"].split("\n")[-2].split(" ") ret={ "device" : parts[0], "used" : parts[2], diff --git a/source/logic/lib.py b/source/logic/lib.py index 9d741ac..9854a3f 100644 --- a/source/logic/lib.py +++ b/source/logic/lib.py @@ -51,7 +51,8 @@ def env_get_language(): def shell_command(command): result = _subprocess.run( command, - capture_output = True + capture_output = True, + shell = True, ) return { "return_code": result.returncode, From aec13bdab4fd46b0fe62e04d36776da003b465d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Fri, 3 Mar 2023 15:31:57 +0100 Subject: [PATCH 4/5] [doc] [#1] updated conf json schema --- doc/hmdl.schema.json | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/doc/hmdl.schema.json b/doc/hmdl.schema.json index f6ab975..f606b5a 100644 --- a/doc/hmdl.schema.json +++ b/doc/hmdl.schema.json @@ -597,6 +597,60 @@ "kind", "parameters" ] + }, + { + "title": "check 'generic_remote'", + "type": "object", + "unevaluatedProperties": false, + "properties": { + "kind": { + "type": "string", + "enum": [ + "generic_remote" + ] + }, + "parameters": { + "type": "object", + "additionalProperties": false, + "properties": { + "host": { + "type": "string" + }, + "port": { + "type": "integer", + "default": 22 + }, + "user": { + "type": "string" + }, + "ssh_key": { + "type": "string" + }, + "mount_point": { + "type": "string", + "default": "/" + }, + "threshold": { + "type": "integer", + "default": 95, + "description": "maximaler F\u00fcllstand in Prozent" + }, + "strict": { + "type": "boolean", + "default": false + } + }, + "required": [ + "host", + "user", + "ssh_key" + ] + } + }, + "required": [ + "kind", + "parameters" + ] } ] } From e13f59450bf280f248233f4603741f7b8907e72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Sat, 4 Mar 2023 15:31:41 +0100 Subject: [PATCH 5/5] [fix] tools:build --- tools/build | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/build b/tools/build index 9cc49d7..621c89f 100755 --- a/tools/build +++ b/tools/build @@ -47,6 +47,7 @@ def main(): _os.path.join(dir_source, "logic", "checks", "script.py"), _os.path.join(dir_source, "logic", "checks", "file_state.py"), _os.path.join(dir_source, "logic", "checks", "http_request.py"), + _os.path.join(dir_source, "logic", "checks", "generic_remote.py"), _os.path.join(dir_source, "logic", "channels", "_interface.py"), _os.path.join(dir_source, "logic", "channels", "console.py"), _os.path.join(dir_source, "logic", "channels", "email.py"), @@ -66,6 +67,7 @@ def main(): _os.path.join(dir_source, "logic", "checks", "script.py"), _os.path.join(dir_source, "logic", "checks", "file_state.py"), _os.path.join(dir_source, "logic", "checks", "http_request.py"), + _os.path.join(dir_source, "logic", "checks", "generic_remote.py"), _os.path.join(dir_source, "logic", "channels", "_interface.py"), _os.path.join(dir_source, "logic", "channels", "console.py"), _os.path.join(dir_source, "logic", "channels", "email.py"),