From d9a082d044e228c3ac6fb5a23968d580a9c2e08b Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Wed, 4 Dec 2019 20:55:01 +0100 Subject: [PATCH] Generic alert send --- restic_compose_backup/alerts/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/restic_compose_backup/alerts/__init__.py b/restic_compose_backup/alerts/__init__.py index 785d892..4c3c046 100644 --- a/restic_compose_backup/alerts/__init__.py +++ b/restic_compose_backup/alerts/__init__.py @@ -12,7 +12,24 @@ ALERT_TYPES = [ALERT_INFO, ALERT_ERROR] BACKENDS = [SMTPAlert, DiscordWebhookAlert] -def configured_alert_classes(): +def send(subject: str = None, body: str = None, alert_type: str = 'INFO'): + """Send alert to all configured backends""" + alert_classes = configured_alert_types() + for instance in alert_classes: + logger.info('Configured: %s', instance.name) + try: + instance.send( + subject=f'[{alert_type}] {subject}', + body=body, + ) + except Exception as ex: + logger.error("Exception raised when sending alert [%s]: %s", instance.name, ex) + + if len(alert_classes) == 0: + logger.info("No alerts configured") + + +def configured_alert_types(): """Returns a list of configured alert class instances""" logger.debug('Getting alert backends') entires = []