This commit is contained in:
Christian Fraß 2023-07-03 10:37:31 +02:00
commit a2cc43dffb
2 changed files with 278 additions and 312 deletions

View file

@ -28,7 +28,7 @@
"checks.http_request.request_failed": "HTTP request failed",
"checks.http_request.status_code_mismatch": "actual status code {{status_code_actual}} does not match expected value {{status_code_expected}}",
"checks.http_request.header_missing": "header '{{key}}' is unset and hence does not match the expected value '{{value_expected}}'",
"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.header_value_mismatch": "actual header value for key '{{key}}' '{{value_actual}}' 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",

View file

@ -1,7 +1,4 @@
def main():
## const
version = "0.8"
## setup translation for the first time
translation_initialize("en", env_get_language())
@ -11,7 +8,6 @@ def main():
formatter_class = _argparse.ArgumentDefaultsHelpFormatter
)
argumentparser.add_argument(
nargs = "?",
type = str,
default = "monitoring.hmdl.json",
dest = "order_path",
@ -19,12 +15,12 @@ def main():
help = translation_get("help.args.order_path"),
)
argumentparser.add_argument(
"-v",
"--version",
"-x",
"--erase-state",
action = "store_true",
default = False,
dest = "show_version",
help = translation_get("help.args.show_version"),
dest = "erase_state",
help = translation_get("help.args.erase_state"),
)
argumentparser.add_argument(
"-s",
@ -42,14 +38,6 @@ def main():
dest = "expose_full_order",
help = translation_get("help.args.expose_full_order"),
)
argumentparser.add_argument(
"-x",
"--erase-state",
action = "store_true",
default = False,
dest = "erase_state",
help = translation_get("help.args.erase_state"),
)
### v conf stuff v
argumentparser.add_argument(
"-d",
@ -98,10 +86,19 @@ def main():
)
args = argumentparser.parse_args()
## vars
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
_os.path.join(
_tempfile.gettempdir(),
string_coin("monitoring-state-{{id}}.sqlite", {"id": id_})
)
)
## exec
if (args.show_version):
_sys.stdout.write(version + "\n")
else:
### setup translation for the second time
if (args.language is not None):
translation_initialize("en", args.language)
@ -135,37 +132,6 @@ def main():
"\n"
)
else:
### vars
if (not _os.path.exists(args.order_path)):
_sys.stderr.write(
string_coin(
"{{message}}\n",
{
"message": translation_get(
"misc.order_file_not_found",
{
"path": args.order_path,
}
),
}
)
)
_sys.exit(1)
else:
database_path = (
args.database_path
if (args.database_path is not None) else
_os.path.join(
_tempfile.gettempdir(),
string_coin(
"monitoring-state-{{id}}.sqlite",
{
"id": _hashlib.sha256(_os.path.abspath(args.order_path).encode("ascii")).hexdigest()[:8]
}
)
)
)
### get order data
order = order_load(
check_kind_implementations,
@ -238,7 +204,14 @@ def main():
pass
else:
### get old state and examine whether the check shall be executed
rows = sqlite_query_get(
rows1 = sqlite_query_get(
database_path,
"SELECT MAX(timestamp) FROM results WHERE (check_name = :check_name) AND (notification_sent = TRUE);",
{
"check_name": check_data["name"],
}
)
rows2 = sqlite_query_get(
database_path,
"SELECT timestamp, condition, notification_sent FROM results WHERE (check_name = :check_name) ORDER BY timestamp DESC LIMIT :limit;",
{
@ -246,13 +219,12 @@ def main():
"limit": (check_data["threshold"] + 1),
}
)
if (len(rows) <= 0):
if (len(rows2) <= 0):
old_item_state = None
else:
last_notification_timestamp = None
count = 1
for row in rows[1:]:
if (row[1] == rows[0][1]):
for row in rows2[1:]:
if (row[1] == rows2[0][1]):
count += 1
else:
break
@ -260,17 +232,11 @@ def main():
count = None
else:
pass
for row in rows:
if (row[2]):
last_notification_timestamp = row[0]
break
else:
pass
old_item_state = {
"timestamp": rows[0][0],
"condition": condition_decode(rows[0][1]),
"timestamp": rows2[0][0],
"condition": condition_decode(rows2[0][1]),
"count": count,
"last_notification_timestamp": last_notification_timestamp,
"last_notification_timestamp": rows1[0][0],
}
timestamp = get_current_timestamp()
@ -412,7 +378,7 @@ def main():
)
)
if (not _os.exists(args.mutex_path)):
if (not _os.path.exists(args.mutex_path)):
pass
else:
_os.remove(args.mutex_path)