send a message when alert were not fonud

This commit is contained in:
HgO 2022-07-11 23:33:35 +02:00
parent 2359f6ca77
commit 7a273f5072

View file

@ -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,