From 69f6695253170fa4e09738c6ca0b31b0a95b7aae Mon Sep 17 00:00:00 2001 From: Soumik Dutta Date: Sun, 5 Mar 2023 19:27:32 +0530 Subject: [PATCH] Add support for healthchecks "log" feature #628 Signed-off-by: Soumik Dutta --- borgmatic/commands/borgmatic.py | 18 ++++++++++++++++++ borgmatic/config/schema.yaml | 1 + borgmatic/hooks/cronhub.py | 1 + borgmatic/hooks/cronitor.py | 1 + borgmatic/hooks/healthchecks.py | 3 ++- borgmatic/hooks/monitor.py | 1 + borgmatic/hooks/ntfy.py | 1 + 7 files changed, 25 insertions(+), 1 deletion(-) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index e08df97..b5e8664 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -152,6 +152,24 @@ def run_configuration(config_filename, config, arguments): encountered_error = error error_repository = repository_path + try: + # send logs irrespective of error + dispatch.call_hooks( + 'ping_monitor', + hooks, + config_filename, + monitor.MONITOR_HOOK_NAMES, + monitor.State.LOG, + monitoring_log_level, + global_arguments.dry_run, + ) + except (OSError, CalledProcessError) as error: + if command.considered_soft_failure(config_filename, error): + return + + encountered_error = error + yield from log_error_records('{}: Error pinging monitor'.format(config_filename), error) + if not encountered_error: try: if using_primary_action: diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 2f873b7..0b2a378 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -1199,6 +1199,7 @@ properties: - start - finish - fail + - log uniqueItems: true description: | List of one or more monitoring states to ping for: diff --git a/borgmatic/hooks/cronhub.py b/borgmatic/hooks/cronhub.py index b93788e..b3a3521 100644 --- a/borgmatic/hooks/cronhub.py +++ b/borgmatic/hooks/cronhub.py @@ -10,6 +10,7 @@ MONITOR_STATE_TO_CRONHUB = { monitor.State.START: 'start', monitor.State.FINISH: 'finish', monitor.State.FAIL: 'fail', + monitor.State.LOG: 'log', } diff --git a/borgmatic/hooks/cronitor.py b/borgmatic/hooks/cronitor.py index 8866a6a..c01d5f6 100644 --- a/borgmatic/hooks/cronitor.py +++ b/borgmatic/hooks/cronitor.py @@ -10,6 +10,7 @@ MONITOR_STATE_TO_CRONITOR = { monitor.State.START: 'run', monitor.State.FINISH: 'complete', monitor.State.FAIL: 'fail', + monitor.State.LOG: 'log', } diff --git a/borgmatic/hooks/healthchecks.py b/borgmatic/hooks/healthchecks.py index 03d012a..6ad8449 100644 --- a/borgmatic/hooks/healthchecks.py +++ b/borgmatic/hooks/healthchecks.py @@ -10,6 +10,7 @@ MONITOR_STATE_TO_HEALTHCHECKS = { monitor.State.START: 'start', monitor.State.FINISH: None, # Healthchecks doesn't append to the URL for the finished state. monitor.State.FAIL: 'fail', + monitor.State.LOG: 'log', } PAYLOAD_TRUNCATION_INDICATOR = '...\n' @@ -117,7 +118,7 @@ def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_ ) logger.debug('{}: Using Healthchecks ping URL {}'.format(config_filename, ping_url)) - if state in (monitor.State.FINISH, monitor.State.FAIL): + if state in (monitor.State.FINISH, monitor.State.FAIL, monitor.State.LOG): payload = format_buffered_logs_for_payload() else: payload = '' diff --git a/borgmatic/hooks/monitor.py b/borgmatic/hooks/monitor.py index 846fca1..c016817 100644 --- a/borgmatic/hooks/monitor.py +++ b/borgmatic/hooks/monitor.py @@ -7,3 +7,4 @@ class State(Enum): START = 1 FINISH = 2 FAIL = 3 + LOG = 4 diff --git a/borgmatic/hooks/ntfy.py b/borgmatic/hooks/ntfy.py index 99ed254..fda912c 100644 --- a/borgmatic/hooks/ntfy.py +++ b/borgmatic/hooks/ntfy.py @@ -10,6 +10,7 @@ MONITOR_STATE_TO_NTFY = { monitor.State.START: None, monitor.State.FINISH: None, monitor.State.FAIL: None, + monitor.State.LOG: None, }