Add ntfy warning

This commit is contained in:
Paul Hoffmann 2023-03-30 16:02:19 +02:00
parent 89500df429
commit 89cc4e824a
6 changed files with 63 additions and 0 deletions

View File

@ -30,6 +30,7 @@ from borgmatic.borg import rlist as borg_rlist
from borgmatic.borg import transfer as borg_transfer
from borgmatic.borg import umount as borg_umount
from borgmatic.borg import version as borg_version
from borgmatic.commands import warning
from borgmatic.commands.arguments import parse_arguments
from borgmatic.config import checks, collect, convert, validate
from borgmatic.hooks import command, dispatch, dump, monitor
@ -153,6 +154,16 @@ def run_configuration(config_filename, config, arguments):
encountered_error = error
error_repository = repository_path
except warning.BorgmaticWarning:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.WARN,
monitoring_log_level,
global_arguments.dry_run,
)
if not encountered_error:
try:
if using_primary_action:

View File

@ -0,0 +1,3 @@
class BorgmaticWarning(Warning):
pass

View File

@ -1004,6 +1004,29 @@ properties:
description: |
Tags to attach to the message.
example: incoming_envelope
warn:
type: object
properties:
title:
type: string
description: |
The title of the message.
example: Ping!
message:
type: string
description: |
The message body to publish.
example: Your backups ended with warnings
priority:
type: string
description: |
The priority to set.
example: urgent
tags:
type: string
description: |
Tags to attach to the message.
example: incoming_envelope
states:
type: array
items:
@ -1012,6 +1035,7 @@ properties:
- start
- finish
- fail
- warn
uniqueItems: true
description: |
List of one or more monitoring states to ping for:

View File

@ -3,12 +3,14 @@ import logging
import os
import select
import subprocess
from borgmatic.commands import warning
logger = logging.getLogger(__name__)
ERROR_OUTPUT_MAX_LINE_COUNT = 25
BORG_ERROR_EXIT_CODE = 2
BORG_WARNING_EXIT_CODE = 1
def exit_code_indicates_error(process, exit_code, borg_local_path=None):
@ -27,6 +29,19 @@ def exit_code_indicates_error(process, exit_code, borg_local_path=None):
return bool(exit_code != 0)
def exit_code_indicates_borg_warning(process, exit_code, borg_local_path=None):
'''
Return True if the given exit code from running a borg command corresponds to an borg warning.
'''
if exit_code is None:
return False
command = process.args.split(' ') if isinstance(process.args, str) else process.args
if borg_local_path and command[0] == borg_local_path:
return bool(exit_code == BORG_WARNING_EXIT_CODE)
return False
def command_for_process(process):
'''
@ -120,6 +135,9 @@ def log_outputs(processes, exclude_stdouts, output_log_level, borg_local_path):
if exit_code is None:
still_running = True
if exit_code_indicates_borg_warning(process, exit_code, borg_local_path):
raise warning.BorgmaticWarning()
# If any process errors, then raise accordingly.
if exit_code_indicates_error(process, exit_code, borg_local_path):
# If an error occurs, include its output in the raised exception so that we don't
@ -194,6 +212,7 @@ def execute_command(
do_not_capture = bool(output_file is DO_NOT_CAPTURE)
command = ' '.join(full_command) if shell else full_command
""" logging.warning(f"{do_not_capture, output_file, shell=}") """
process = subprocess.Popen(
command,
stdin=input_file,
@ -203,6 +222,10 @@ def execute_command(
env=environment,
cwd=working_directory,
)
""" logging.warning(f"{process.args}") """
""" logging.warning(f"{process.wait()}") """
if not run_to_completion:
return process

View File

@ -7,3 +7,4 @@ class State(Enum):
START = 1
FINISH = 2
FAIL = 3
WARN = 4

View File

@ -10,6 +10,7 @@ MONITOR_STATE_TO_NTFY = {
monitor.State.START: None,
monitor.State.FINISH: None,
monitor.State.FAIL: None,
monitor.State.WARN: None,
}