2019-12-04 19:28:06 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-04 18:36:14 +00:00
|
|
|
from restic_compose_backup.alerts.smtp import SMTPAlert
|
|
|
|
from restic_compose_backup.alerts.discord import DiscordWebhookAlert
|
|
|
|
from restic_compose_backup.config import Config
|
|
|
|
|
2019-12-04 19:28:06 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-12-04 02:58:27 +00:00
|
|
|
ALERT_INFO = 'INFO',
|
|
|
|
ALERT_ERROR = 'ERROR'
|
|
|
|
ALERT_TYPES = [ALERT_INFO, ALERT_ERROR]
|
2019-12-04 18:36:14 +00:00
|
|
|
BACKENDS = [SMTPAlert, DiscordWebhookAlert]
|
2019-12-04 02:58:27 +00:00
|
|
|
|
|
|
|
|
2019-12-04 19:55:01 +00:00
|
|
|
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():
|
2019-12-04 18:36:14 +00:00
|
|
|
"""Returns a list of configured alert class instances"""
|
2019-12-04 19:28:06 +00:00
|
|
|
logger.debug('Getting alert backends')
|
2019-12-04 18:36:14 +00:00
|
|
|
entires = []
|
|
|
|
|
|
|
|
for cls in BACKENDS:
|
|
|
|
instance = cls.create_from_env()
|
2019-12-04 19:28:06 +00:00
|
|
|
logger.debug("Alert backend '%s' configured: %s", cls.name, instance != None)
|
2019-12-04 18:36:14 +00:00
|
|
|
if instance:
|
|
|
|
entires.append(instance)
|
|
|
|
|
|
|
|
return entires
|