2022-12-03 16:36:44 +01:00
|
|
|
def main():
|
|
|
|
|
## setup translation for the first time
|
|
|
|
|
translation_initialize("en", env_get_language())
|
|
|
|
|
|
|
|
|
|
## args
|
|
|
|
|
argumentparser = _argparse.ArgumentParser(
|
|
|
|
|
description = translation_get("help.title"),
|
|
|
|
|
formatter_class = _argparse.ArgumentDefaultsHelpFormatter
|
|
|
|
|
)
|
|
|
|
|
argumentparser.add_argument(
|
|
|
|
|
type = str,
|
|
|
|
|
default = "monitoring.hmdl.json",
|
2023-04-28 17:30:51 +02:00
|
|
|
dest = "order_path",
|
|
|
|
|
metavar = "<order-path>",
|
|
|
|
|
help = translation_get("help.args.order_path"),
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
|
|
|
|
argumentparser.add_argument(
|
2023-04-28 17:30:51 +02:00
|
|
|
"-x",
|
|
|
|
|
"--erase-state",
|
|
|
|
|
action = "store_true",
|
|
|
|
|
default = False,
|
|
|
|
|
dest = "erase_state",
|
|
|
|
|
help = translation_get("help.args.erase_state"),
|
|
|
|
|
)
|
|
|
|
|
argumentparser.add_argument(
|
|
|
|
|
"-s",
|
|
|
|
|
"--show-schema",
|
|
|
|
|
action = "store_true",
|
|
|
|
|
default = False,
|
|
|
|
|
dest = "show_schema",
|
|
|
|
|
help = translation_get("help.args.show_schema"),
|
|
|
|
|
)
|
|
|
|
|
argumentparser.add_argument(
|
|
|
|
|
"-e",
|
|
|
|
|
"--expose-full-order",
|
|
|
|
|
action = "store_true",
|
|
|
|
|
default = False,
|
|
|
|
|
dest = "expose_full_order",
|
|
|
|
|
help = translation_get("help.args.expose_full_order"),
|
|
|
|
|
)
|
|
|
|
|
### v conf stuff v
|
|
|
|
|
argumentparser.add_argument(
|
|
|
|
|
"-d",
|
|
|
|
|
"--database-path",
|
2022-12-03 16:36:44 +01:00
|
|
|
type = str,
|
|
|
|
|
default = None,
|
2023-04-28 17:30:51 +02:00
|
|
|
dest = "database_path",
|
|
|
|
|
metavar = "<database-path>",
|
|
|
|
|
help = translation_get("help.args.database_path"),
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
2023-03-22 11:06:32 +01:00
|
|
|
argumentparser.add_argument(
|
|
|
|
|
"-m",
|
|
|
|
|
"--mutex-path",
|
|
|
|
|
type = str,
|
2023-04-28 17:30:51 +02:00
|
|
|
default = "/tmp/heimdall.lock",
|
2023-03-22 11:06:32 +01:00
|
|
|
dest = "mutex_path",
|
|
|
|
|
metavar = "<mutex-path>",
|
|
|
|
|
help = translation_get("help.args.mutex_path"),
|
|
|
|
|
)
|
2022-12-03 16:36:44 +01:00
|
|
|
argumentparser.add_argument(
|
|
|
|
|
"-y",
|
|
|
|
|
"--send-ok-notifications",
|
|
|
|
|
action = "store_true",
|
|
|
|
|
default = False,
|
|
|
|
|
dest = "send_ok_notifications",
|
|
|
|
|
help = translation_get("help.args.send_ok_notifications", {"condition_name": translation_get("conditions.ok")}),
|
|
|
|
|
)
|
|
|
|
|
argumentparser.add_argument(
|
|
|
|
|
"-l",
|
|
|
|
|
"--language",
|
|
|
|
|
type = str,
|
|
|
|
|
choices = localization_data.keys(),
|
|
|
|
|
default = None,
|
|
|
|
|
dest = "language",
|
|
|
|
|
metavar = "<language>",
|
|
|
|
|
help = translation_get("help.args.language"),
|
|
|
|
|
)
|
|
|
|
|
argumentparser.add_argument(
|
2023-04-28 17:30:51 +02:00
|
|
|
"-t",
|
|
|
|
|
"--time-to-live",
|
|
|
|
|
type = int,
|
|
|
|
|
default = (60 * 60 * 24 * 7),
|
|
|
|
|
dest = "time_to_live",
|
|
|
|
|
metavar = "<time-to-live>",
|
|
|
|
|
help = translation_get("help.args.time_to_live"),
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
|
|
|
|
args = argumentparser.parse_args()
|
|
|
|
|
|
|
|
|
|
## vars
|
2023-04-28 17:30:51 +02:00
|
|
|
id_ = _hashlib.sha256(_os.path.abspath(args.order_path).encode("ascii")).hexdigest()[:8]
|
|
|
|
|
database_path = (
|
|
|
|
|
args.database_path
|
|
|
|
|
if (args.database_path is not None) else
|
2022-12-03 16:36:44 +01:00
|
|
|
_os.path.join(
|
|
|
|
|
_tempfile.gettempdir(),
|
2023-04-28 17:30:51 +02:00
|
|
|
string_coin("monitoring-state-{{id}}.sqlite", {"id": id_})
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
## exec
|
|
|
|
|
|
|
|
|
|
### setup translation for the second time
|
|
|
|
|
if (args.language is not None):
|
|
|
|
|
translation_initialize("en", args.language)
|
|
|
|
|
|
|
|
|
|
### load check kind implementations
|
|
|
|
|
check_kind_implementations = {
|
|
|
|
|
"script": implementation_check_kind_script(),
|
|
|
|
|
"file_state": implementation_check_kind_file_state(),
|
2023-03-22 14:22:54 +01:00
|
|
|
"tls_certificate": implementation_check_kind_tls_certificate(),
|
2022-12-03 16:36:44 +01:00
|
|
|
"http_request": implementation_check_kind_http_request(),
|
2023-03-04 15:19:44 +01:00
|
|
|
"generic_remote" : implementation_check_kind_generic_remote(),
|
2022-12-03 16:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
### load notification channel implementations
|
|
|
|
|
notification_channel_implementations = {
|
|
|
|
|
"console": implementation_notification_channel_console(),
|
|
|
|
|
"email": implementation_notification_channel_email(),
|
|
|
|
|
"libnotify": implementation_notification_channel_libnotify(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args.show_schema):
|
|
|
|
|
_sys.stdout.write(
|
|
|
|
|
_json.dumps(
|
2023-04-28 17:30:51 +02:00
|
|
|
order_schema_root(
|
2022-12-03 16:36:44 +01:00
|
|
|
check_kind_implementations,
|
|
|
|
|
notification_channel_implementations
|
|
|
|
|
),
|
|
|
|
|
indent = "\t"
|
|
|
|
|
)
|
|
|
|
|
+
|
|
|
|
|
"\n"
|
|
|
|
|
)
|
|
|
|
|
else:
|
2023-04-28 17:30:51 +02:00
|
|
|
### get order data
|
|
|
|
|
order = order_load(
|
2022-12-03 16:36:44 +01:00
|
|
|
check_kind_implementations,
|
|
|
|
|
notification_channel_implementations,
|
2023-04-28 17:30:51 +02:00
|
|
|
_os.path.abspath(args.order_path)
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
2023-03-04 15:51:26 +01:00
|
|
|
|
2023-04-28 17:30:51 +02:00
|
|
|
if (args.expose_full_order):
|
|
|
|
|
_sys.stdout.write(_json.dumps(order, indent = "\t") + "\n")
|
2022-12-03 16:36:44 +01:00
|
|
|
_sys.exit(1)
|
|
|
|
|
else:
|
2023-03-04 15:51:26 +01:00
|
|
|
_sys.stderr.write(
|
|
|
|
|
string_coin(
|
|
|
|
|
"[info] {{label}}: {{path}}\n",
|
|
|
|
|
{
|
|
|
|
|
"label": translation_get("misc.state_file_path"),
|
2023-04-28 17:30:51 +02:00
|
|
|
"path": database_path,
|
2023-03-04 15:51:26 +01:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2023-03-22 11:06:32 +01:00
|
|
|
### mutex check
|
|
|
|
|
if (_os.path.exists(args.mutex_path)):
|
|
|
|
|
_sys.stderr.write(
|
|
|
|
|
string_coin(
|
|
|
|
|
"[error] {{message}} ({{path}})\n",
|
|
|
|
|
{
|
|
|
|
|
"message": translation_get("misc.still_running"),
|
|
|
|
|
"path": args.mutex_path,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
_sys.exit(2)
|
2022-12-03 16:36:44 +01:00
|
|
|
else:
|
2023-04-28 17:30:51 +02:00
|
|
|
### setup database
|
2023-05-20 10:13:41 +02:00
|
|
|
repository_result_setup(database_path)
|
2023-03-22 11:06:32 +01:00
|
|
|
|
2023-04-28 17:30:51 +02:00
|
|
|
### clean database
|
2023-05-20 10:13:41 +02:00
|
|
|
count = repository_result_clean(database_path, args.time_to_live, args.erase_state)
|
2023-04-28 17:30:51 +02:00
|
|
|
_sys.stderr.write(
|
|
|
|
|
string_coin(
|
|
|
|
|
"[info] {{text}}\n",
|
|
|
|
|
{
|
|
|
|
|
"text": translation_get(
|
|
|
|
|
"misc.cleanup_info",
|
|
|
|
|
{
|
2023-05-20 10:13:41 +02:00
|
|
|
"count": ("%u" % count),
|
2023-04-28 17:30:51 +02:00
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
file_write(args.mutex_path, "", {"append": True})
|
2023-03-22 11:06:32 +01:00
|
|
|
|
|
|
|
|
### iterate through checks
|
2023-04-28 17:30:51 +02:00
|
|
|
for check_data in order["checks"]:
|
2023-03-22 11:06:32 +01:00
|
|
|
if (not check_data["active"]):
|
2022-12-03 16:36:44 +01:00
|
|
|
pass
|
|
|
|
|
else:
|
2023-03-22 11:06:32 +01:00
|
|
|
### get old state and examine whether the check shall be executed
|
2023-05-20 10:13:41 +02:00
|
|
|
old_item_state = repository_result_scan(
|
2023-04-28 17:30:51 +02:00
|
|
|
database_path,
|
2023-05-20 10:13:41 +02:00
|
|
|
check_data["name"],
|
|
|
|
|
check_data["threshold"]
|
2023-03-22 11:06:32 +01:00
|
|
|
)
|
2023-04-28 17:30:51 +02:00
|
|
|
|
2023-03-22 11:06:32 +01:00
|
|
|
timestamp = get_current_timestamp()
|
|
|
|
|
due = (
|
|
|
|
|
(old_item_state is None)
|
|
|
|
|
or
|
|
|
|
|
(old_item_state["condition"] != enum_condition.ok)
|
|
|
|
|
or
|
|
|
|
|
((timestamp - old_item_state["timestamp"]) >= check_data["schedule"]["regular_interval"])
|
|
|
|
|
or
|
|
|
|
|
(
|
|
|
|
|
(old_item_state["count"] is not None)
|
|
|
|
|
and
|
|
|
|
|
((timestamp - old_item_state["timestamp"]) >= check_data["schedule"]["attentive_interval"])
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
|
|
|
|
)
|
2023-03-22 11:06:32 +01:00
|
|
|
if (not due):
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
_sys.stderr.write(
|
|
|
|
|
string_coin(
|
|
|
|
|
"-- {{check_name}}\n",
|
|
|
|
|
{
|
|
|
|
|
"check_name": check_data["name"],
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
### execute check and set new state
|
|
|
|
|
try:
|
|
|
|
|
result = check_kind_implementations[check_data["kind"]].run(check_data["parameters"])
|
|
|
|
|
except Exception as error:
|
|
|
|
|
result = {
|
|
|
|
|
"condition": enum_condition.unknown,
|
|
|
|
|
"info": {
|
2023-04-28 17:30:51 +02:00
|
|
|
"cause": translation_get("misc.check_procedure_failed"),
|
2023-03-22 11:06:32 +01:00
|
|
|
"error": str(error),
|
|
|
|
|
},
|
|
|
|
|
}
|
2023-03-22 11:24:13 +01:00
|
|
|
count = (
|
|
|
|
|
1
|
|
|
|
|
if (
|
|
|
|
|
(old_item_state is None)
|
|
|
|
|
or
|
|
|
|
|
(old_item_state["condition"] != result["condition"])
|
|
|
|
|
) else
|
|
|
|
|
(
|
|
|
|
|
(old_item_state["count"] + 1)
|
2022-12-03 16:36:44 +01:00
|
|
|
if (
|
2023-03-22 11:24:13 +01:00
|
|
|
(old_item_state["count"] is not None)
|
|
|
|
|
and
|
|
|
|
|
((old_item_state["count"] + 1) <= check_data["threshold"])
|
2022-12-03 16:36:44 +01:00
|
|
|
) else
|
2023-03-22 11:24:13 +01:00
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
shall_send_notification = (
|
2022-12-03 16:36:44 +01:00
|
|
|
(
|
2023-03-22 11:06:32 +01:00
|
|
|
(
|
2023-03-22 11:24:13 +01:00
|
|
|
(count is not None)
|
2023-03-22 11:06:32 +01:00
|
|
|
and
|
2023-03-22 11:24:13 +01:00
|
|
|
(count == check_data["threshold"])
|
2023-03-22 11:06:32 +01:00
|
|
|
)
|
|
|
|
|
or
|
|
|
|
|
(
|
2023-03-22 11:24:13 +01:00
|
|
|
(count is None)
|
2023-03-22 11:06:32 +01:00
|
|
|
and
|
|
|
|
|
check_data["annoy"]
|
|
|
|
|
)
|
2023-03-22 11:40:28 +01:00
|
|
|
or
|
|
|
|
|
(
|
|
|
|
|
(count is None)
|
|
|
|
|
and
|
|
|
|
|
(
|
|
|
|
|
(old_item_state is not None)
|
|
|
|
|
and
|
|
|
|
|
(old_item_state["last_notification_timestamp"] is not None)
|
|
|
|
|
and
|
|
|
|
|
(check_data["schedule"]["reminding_interval"] is not None)
|
|
|
|
|
and
|
|
|
|
|
(
|
|
|
|
|
(timestamp - old_item_state["last_notification_timestamp"])
|
|
|
|
|
>=
|
|
|
|
|
check_data["schedule"]["reminding_interval"]
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
2023-03-22 11:06:32 +01:00
|
|
|
and
|
2022-12-03 16:36:44 +01:00
|
|
|
(
|
2023-03-22 11:24:13 +01:00
|
|
|
(result["condition"] != enum_condition.ok)
|
2023-03-22 11:06:32 +01:00
|
|
|
or
|
|
|
|
|
args.send_ok_notifications
|
2022-12-03 16:36:44 +01:00
|
|
|
)
|
2023-03-22 11:24:13 +01:00
|
|
|
)
|
|
|
|
|
new_item_state = {
|
|
|
|
|
"timestamp": timestamp,
|
|
|
|
|
"condition": result["condition"],
|
|
|
|
|
"count": count,
|
|
|
|
|
"last_notification_timestamp": (
|
|
|
|
|
timestamp
|
|
|
|
|
if shall_send_notification else
|
|
|
|
|
(
|
|
|
|
|
None
|
|
|
|
|
if (old_item_state is None) else
|
|
|
|
|
old_item_state["last_notification_timestamp"]
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
}
|
2023-05-20 10:13:41 +02:00
|
|
|
repository_result_add(
|
2023-04-28 17:30:51 +02:00
|
|
|
database_path,
|
|
|
|
|
{
|
|
|
|
|
"check_name": check_data["name"],
|
|
|
|
|
"timestamp": timestamp,
|
2023-05-20 10:13:41 +02:00
|
|
|
"condition": result["condition"],
|
2023-04-28 17:30:51 +02:00
|
|
|
"notification_sent": shall_send_notification,
|
2023-05-20 10:13:41 +02:00
|
|
|
"info": result["info"],
|
2023-04-28 17:30:51 +02:00
|
|
|
}
|
|
|
|
|
)
|
2023-03-22 11:24:13 +01:00
|
|
|
|
|
|
|
|
### send notifications
|
2023-04-28 17:30:51 +02:00
|
|
|
if (not shall_send_notification):
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2023-03-22 11:06:32 +01:00
|
|
|
for notification in check_data["notifications"]:
|
|
|
|
|
notification_channel_implementations[notification["kind"]].notify(
|
|
|
|
|
notification["parameters"],
|
|
|
|
|
check_data["name"],
|
|
|
|
|
check_data,
|
|
|
|
|
new_item_state,
|
2023-04-26 17:27:47 +02:00
|
|
|
dict_merge(
|
|
|
|
|
(
|
|
|
|
|
{}
|
|
|
|
|
if (check_data["custom"] is None) else
|
|
|
|
|
{"custom": check_data["custom"]}
|
|
|
|
|
),
|
|
|
|
|
result["info"]
|
|
|
|
|
)
|
2023-03-22 11:06:32 +01:00
|
|
|
)
|
|
|
|
|
|
2023-05-20 09:14:27 +02:00
|
|
|
if (not _os.exists(args.mutex_path)):
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
_os.remove(args.mutex_path)
|
2022-12-03 16:36:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
main()
|
|
|
|
|
|