[mod] includes ermöglicht

This commit is contained in:
Christian Fraß 2023-03-03 18:50:21 +01:00
parent 18fe4fdcd6
commit a7a57fbcfa
7 changed files with 165 additions and 49 deletions

View file

@ -205,6 +205,14 @@
}, },
"required": [] "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": { "checks": {
"type": "array", "type": "array",
"items": { "items": {
@ -604,8 +612,5 @@
} }
} }
}, },
"required": [ "required": []
"defaults",
"checks"
]
} }

27
examples/test-1.hmdl.json Normal file
View file

@ -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
}
}
]
}

25
examples/test-2.hmdl.json Normal file
View file

@ -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": []
}
}
]
}

View file

@ -1,34 +1,6 @@
{ {
"defaults": { "includes": [
}, "test-1.hmdl.json",
"checks": [ "test-2.hmdl.json"
{
"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
}
}
] ]
} }

View file

@ -114,6 +114,14 @@ def conf_schema_root(check_kind_implementations, notification_channel_implementa
"required": [ "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": { "checks": {
"type": "array", "type": "array",
"items": { "items": {
@ -167,8 +175,6 @@ def conf_schema_root(check_kind_implementations, notification_channel_implementa
} }
}, },
"required": [ "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 = {} 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): if (node_["name"] not in counts):
counts[node_["name"]] = 0 counts[node_["name"]] = 0
counts[node_["name"]] += 1 counts[node_["name"]] += 1
@ -301,9 +316,22 @@ def conf_normalize_root(check_kind_implementations, notification_channel_impleme
) )
) )
else: 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 { return {
"defaults": defaults, "defaults": defaults,
"includes": includes,
"checks": list( "checks": list(
map( map(
lambda node_: conf_normalize_check( lambda node_: conf_normalize_check(
@ -312,8 +340,67 @@ def conf_normalize_root(check_kind_implementations, notification_channel_impleme
defaults, defaults,
node_ 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
)

View file

@ -140,13 +140,13 @@ def main():
) )
### get configuration data ### get configuration data
conf = conf_normalize_root( conf = conf_load(
check_kind_implementations, check_kind_implementations,
notification_channel_implementations, notification_channel_implementations,
_json.loads(file_read(args.conf_path)) _os.path.abspath(args.conf_path)
) )
if (args.expose_full_conf): 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) _sys.exit(1)
else: else:
### get state data ### get state data

10
todo.md
View file

@ -1,12 +1,12 @@
- parallele Zugriffe auf die Zustands-Datei verhindern - 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 - Selbst-Test
- Benachrichtigungs-Kanäle: - Benachrichtigungs-Kanäle:
- Matrix - Matrix
- evtl. die Kanäle ganz auslagern und nur als Library anbinden
- Möglichkeit dauerhaft laufen zulassen (evtl. als systemd-Dienst) - Möglichkeit dauerhaft laufen zulassen (evtl. als systemd-Dienst)
- Versionierung - Versionierung
- Test-Routinen - Test-Routinen
- neu schreiben in TypeScript (und plankton dafür nutzen)? - 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