From 18fe4fdcd6d5a382ae4914ad88b734f4c6220638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Fri, 3 Mar 2023 16:00:46 +0100 Subject: [PATCH 1/2] [mod] todo --- todo.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/todo.md b/todo.md index 22de17d..c40cea3 100644 --- a/todo.md +++ b/todo.md @@ -8,3 +8,5 @@ - Test-Routinen - neu schreiben in TypeScript (und plankton dafür nutzen)? - Benachrichtigungen versenden, wenn ein Zustand sich wieder normalisiert hat (aber vorher über dem Schwellwert oft nicht OK war) +- include-System + From a7a57fbcfaeabf697cf139f628f875c05a354c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Fri, 3 Mar 2023 18:50:21 +0100 Subject: [PATCH 2/2] =?UTF-8?q?[mod]=20includes=20erm=C3=B6glicht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/hmdl.schema.json | 13 +++-- examples/test-1.hmdl.json | 27 +++++++++++ examples/test-2.hmdl.json | 25 ++++++++++ examples/test.hmdl.json | 34 ++------------ source/logic/conf.py | 99 ++++++++++++++++++++++++++++++++++++--- source/logic/main.py | 6 +-- todo.md | 10 ++-- 7 files changed, 165 insertions(+), 49 deletions(-) create mode 100644 examples/test-1.hmdl.json create mode 100644 examples/test-2.hmdl.json diff --git a/doc/hmdl.schema.json b/doc/hmdl.schema.json index f6ab975..32c4f6c 100644 --- a/doc/hmdl.schema.json +++ b/doc/hmdl.schema.json @@ -205,6 +205,14 @@ }, "required": [] }, + "includes": { + "type": "array", + "items": { + "type": "string" + }, + "default": [], + "description": "list of relative or absolute paths to other hmdl files on the local machine, which shall be subsumed in the overall monitoring task" + }, "checks": { "type": "array", "items": { @@ -604,8 +612,5 @@ } } }, - "required": [ - "defaults", - "checks" - ] + "required": [] } diff --git a/examples/test-1.hmdl.json b/examples/test-1.hmdl.json new file mode 100644 index 0000000..365b2ce --- /dev/null +++ b/examples/test-1.hmdl.json @@ -0,0 +1,27 @@ +{ + "defaults": { + "schedule": { + "regular_interval": 15, + "attentive_interval": 5 + }, + "notifications": [ + { + "kind": "console", + "parameters": { + } + } + ] + }, + "checks": [ + { + "name": "test", + "kind": "file_state", + "parameters": { + "path": "/tmp/test", + "exist": true, + "age_threshold": 60, + "size_threshold": 1 + } + } + ] +} diff --git a/examples/test-2.hmdl.json b/examples/test-2.hmdl.json new file mode 100644 index 0000000..74c5ba3 --- /dev/null +++ b/examples/test-2.hmdl.json @@ -0,0 +1,25 @@ +{ + "defaults": { + "schedule": { + "regular_interval": 10, + "attentive_interval": 1 + }, + "notifications": [ + { + "kind": "libnotify", + "parameters": { + } + } + ] + }, + "checks": [ + { + "name": "test", + "kind": "script", + "parameters": { + "path": "/tmp/script", + "arguments": [] + } + } + ] +} diff --git a/examples/test.hmdl.json b/examples/test.hmdl.json index d3e2051..4c39d28 100644 --- a/examples/test.hmdl.json +++ b/examples/test.hmdl.json @@ -1,34 +1,6 @@ { - "defaults": { - }, - "checks": [ - { - "name": "test", - "threshold": 3, - "annoy": false, - "schedule": { - "regular_interval": 15, - "attentive_interval": 5 - }, - "notifications": [ - { - "kind": "console", - "parameters": { - } - }, - { - "kind": "libnotify", - "parameters": { - } - } - ], - "kind": "file_state", - "parameters": { - "path": "/tmp/test", - "exist": true, - "age_threshold": 60, - "size_threshold": 1 - } - } + "includes": [ + "test-1.hmdl.json", + "test-2.hmdl.json" ] } diff --git a/source/logic/conf.py b/source/logic/conf.py index 61dd9d8..f587f4c 100644 --- a/source/logic/conf.py +++ b/source/logic/conf.py @@ -114,6 +114,14 @@ def conf_schema_root(check_kind_implementations, notification_channel_implementa "required": [ ], }, + "includes": { + "type": "array", + "items": { + "type": "string" + }, + "default": [], + "description": "list of relative or absolute paths to other hmdl files on the local machine, which shall be subsumed in the overall monitoring task" + }, "checks": { "type": "array", "items": { @@ -167,8 +175,6 @@ def conf_schema_root(check_kind_implementations, notification_channel_implementa } }, "required": [ - "defaults", - "checks", ] } @@ -284,9 +290,18 @@ def conf_normalize_check(check_kind_implementations, notification_channel_implem } -def conf_normalize_root(check_kind_implementations, notification_channel_implementations, node): +def conf_normalize_root( + check_kind_implementations, + notification_channel_implementations, + node +): counts = {} - for node_ in node["checks"]: + checks_raw = ( + node["checks"] + if ("checks" in node) else + [] + ) + for node_ in checks_raw: if (node_["name"] not in counts): counts[node_["name"]] = 0 counts[node_["name"]] += 1 @@ -301,9 +316,22 @@ def conf_normalize_root(check_kind_implementations, notification_channel_impleme ) ) else: - defaults = conf_normalize_defaults(notification_channel_implementations, node["defaults"]) + defaults = conf_normalize_defaults( + notification_channel_implementations, + ( + node["defaults"] + if ("defaults" in node) else + {} + ) + ) + includes = ( + node["includes"] + if ("includes" in node) else + [] + ) return { "defaults": defaults, + "includes": includes, "checks": list( map( lambda node_: conf_normalize_check( @@ -312,8 +340,67 @@ def conf_normalize_root(check_kind_implementations, notification_channel_impleme defaults, node_ ), - node["checks"] + checks_raw ) ) } + +def conf_load( + check_kind_implementations, + notification_channel_implementations, + path, + already_included = None +): + if (already_included is None): + already_included = set([]) + if (path in already_included): + raise ValueError("circular dependency detected") + else: + already_included.add(path) + conf_raw = _json.loads(file_read(path)) + includes = ( + conf_raw["includes"] + if ("includes" in conf_raw) else + [] + ) + for index in range(len(includes)): + path_ = includes[index] + sub_conf = conf_load( + check_kind_implementations, + notification_channel_implementations, + ( + path_ + if _os.path.isabs(path_) else + _os.path.join(_os.path.dirname(path), path_) + ), + already_included + ) + if (not "checks" in conf_raw): + conf_raw["checks"] = [] + conf_raw["checks"].extend( + list( + map( + lambda check: dict_merge( + check, + { + "name": string_coin( + "x{{number}}.{{original_name}}", + { + "number": ("%u" % (index + 1)), + "original_name": check["name"], + } + ), + } + ), + sub_conf["checks"] + ) + ) + ) + conf_raw["includes"] = [] + return conf_normalize_root( + check_kind_implementations, + notification_channel_implementations, + conf_raw + ) + diff --git a/source/logic/main.py b/source/logic/main.py index 79e53fc..78841ca 100644 --- a/source/logic/main.py +++ b/source/logic/main.py @@ -140,13 +140,13 @@ def main(): ) ### get configuration data - conf = conf_normalize_root( + conf = conf_load( check_kind_implementations, notification_channel_implementations, - _json.loads(file_read(args.conf_path)) + _os.path.abspath(args.conf_path) ) if (args.expose_full_conf): - _sys.stdout.write(_json.dumps(checks, indent = "\t") + "\n") + _sys.stdout.write(_json.dumps(conf, indent = "\t") + "\n") _sys.exit(1) else: ### get state data diff --git a/todo.md b/todo.md index c40cea3..0766f5e 100644 --- a/todo.md +++ b/todo.md @@ -1,12 +1,12 @@ - parallele Zugriffe auf die Zustands-Datei verhindern -- fehlertolerantere Implementierung +- Benachrichtigungen versenden, wenn ein Zustand sich wieder normalisiert hat (aber vorher über dem Schwellwert oft nicht OK war) +- erneute Benachrichtigung über nicht-OK-Zustand nach einer Weile (siehe https://gitlab.greenscale.de/tools/heimdall/-/issues/3) +- längere Statistiken über Metriken führen um auch Anstiege/Abfälle auszuwerten (z.B. "Speicherplatzverbrauch innerhalb einer Woche um 5GB gestiegen") - Selbst-Test - Benachrichtigungs-Kanäle: - Matrix + - evtl. die Kanäle ganz auslagern und nur als Library anbinden - Möglichkeit dauerhaft laufen zulassen (evtl. als systemd-Dienst) - Versionierung - Test-Routinen -- neu schreiben in TypeScript (und plankton dafür nutzen)? -- Benachrichtigungen versenden, wenn ein Zustand sich wieder normalisiert hat (aber vorher über dem Schwellwert oft nicht OK war) -- include-System - +- neu schreiben in TypeScript (und plankton dafür nutzen?)