From 7a273f5072d7e06ea28848a10692a0705b42265c Mon Sep 17 00:00:00 2001 From: HgO Date: Mon, 11 Jul 2022 23:33:35 +0200 Subject: [PATCH] send a message when alert were not fonud --- matrix_alertbot/command.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/matrix_alertbot/command.py b/matrix_alertbot/command.py index 6f5e570..af8971f 100644 --- a/matrix_alertbot/command.py +++ b/matrix_alertbot/command.py @@ -7,7 +7,11 @@ from nio import AsyncClient, MatrixRoom from matrix_alertbot.alertmanager import AlertmanagerClient from matrix_alertbot.chat_functions import send_text_to_room from matrix_alertbot.config import Config -from matrix_alertbot.errors import AlertmanagerError +from matrix_alertbot.errors import ( + AlertNotFoundError, + AlertmanagerError, + SilenceNotFoundError, +) from matrix_alertbot.matcher import AlertMatcher, AlertRegexMatcher logger = logging.getLogger(__name__) @@ -95,6 +99,7 @@ class Command: alert_fingerprints = self.cache[self.event_id] logger.debug(f"Found {len(alert_fingerprints)} in cache") + count_alert_not_found = 0 count_created_silences = 0 for alert_fingerprint in alert_fingerprints: logger.debug( @@ -108,9 +113,19 @@ class Command: matchers, ) count_created_silences += 1 + except AlertNotFoundError as e: + logger.warning(f"Unable to create silence: {e}") + count_alert_not_found += 1 except AlertmanagerError as e: logger.exception(f"Unable to create silence: {e}", exc_info=e) + if count_alert_not_found > 0: + await send_text_to_room( + self.client, + self.room.room_id, + f"Sorry, I couldn't find {count_alert_not_found} alerts, and thus I couldn't acknowledge them.", + ) + await send_text_to_room( self.client, self.room.room_id, @@ -140,6 +155,7 @@ class Command: alert_fingerprints = self.cache[self.event_id] logger.debug(f"Found {len(alert_fingerprints)} in cache") + count_alert_not_found = 0 count_removed_silences = 0 for alert_fingerprint in alert_fingerprints: logger.debug( @@ -150,9 +166,19 @@ class Command: alert_fingerprint, matchers ) count_removed_silences += len(removed_silences) + except (AlertNotFoundError, SilenceNotFoundError) as e: + logger.error(f"Unable to create silence: {e}") + count_alert_not_found += 1 except AlertmanagerError as e: logger.exception(f"Unable to delete silence: {e}", exc_info=e) + if count_alert_not_found > 0: + await send_text_to_room( + self.client, + self.room.room_id, + f"Sorry, I couldn't find {count_alert_not_found} alerts, and thus I couldn't unacknowledge them.", + ) + await send_text_to_room( self.client, self.room.room_id,