fix acknowledge multiple alerts

This commit is contained in:
HgO 2022-07-05 23:35:19 +02:00
parent 96ee7f068a
commit 17bf73f22b
3 changed files with 21 additions and 16 deletions

View file

@ -14,7 +14,7 @@ class AlertmanagerClient:
self.cache = cache self.cache = cache
def get_alerts(self) -> List[Dict]: def get_alerts(self) -> List[Dict]:
response = requests.get(f"{self.api_url}/alert") response = requests.get(f"{self.api_url}/alerts")
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
@ -40,8 +40,8 @@ class AlertmanagerClient:
silence = { silence = {
"matchers": matchers, "matchers": matchers,
"startsAt": start_time, "startsAt": start_time.isoformat(),
"endsAt": end_time, "endsAt": end_time.isoformat(),
"createdBy": user, "createdBy": user,
"comment": "Acknowledge alert from Matrix", "comment": "Acknowledge alert from Matrix",
} }

View file

@ -73,19 +73,23 @@ class Command:
except KeyError: except KeyError:
logger.debug("Unable to find the event ID of the alert") logger.debug("Unable to find the event ID of the alert")
return return
logger.debug(f"Read alert fingerprint for event {alert_event_id} from cache") logger.debug(f"Read alert fingerprints for event {alert_event_id} from cache")
alert_fingerprint = self.cache[alert_event_id]
silence_ids = []
alert_fingerprints = self.cache[alert_event_id]
for alert_fingerprint in alert_fingerprints:
logger.debug( logger.debug(
f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}" f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}"
) )
silence_id = self.alertmanager.create_silence( silence_id = self.alertmanager.create_silence(
alert_fingerprint, duration, self.room.user_name(self.event.sender) alert_fingerprint, duration, self.room.user_name(self.event.sender)
) )
silence_ids.append(silence_id)
silences = ", ".join(silence_ids)
await send_text_to_room( await send_text_to_room(
self.client, self.client,
self.room.room_id, self.room.room_id,
f"Created silence {silence_id} for {duration}", f"Created silences {silences} for a duration of {duration}",
reply_to_event_id=alert_event_id,
) )
async def _react(self) -> None: async def _react(self) -> None:

View file

@ -19,7 +19,7 @@ async def create_alert(request: web_request.Request) -> web.Response:
data = await request.json() data = await request.json()
logger.info(f"Received alert: {data}") logger.info(f"Received alert: {data}")
client = request.app["client"] client = request.app["client"]
cache = request.app['cache'] cache = request.app["cache"]
plaintext = "" plaintext = ""
html = "" html = ""
@ -33,13 +33,14 @@ async def create_alert(request: web_request.Request) -> web.Response:
html += alert.html() html += alert.html()
try: try:
event = await send_text_to_room(client, request.app["room_id"], plaintext, html) event = await send_text_to_room(
client, request.app["room_id"], plaintext, html, notice=False
)
except SendRetryError as e: except SendRetryError as e:
logger.error(e) logger.error(e)
return web.Response(status=500) return web.Response(status=500)
cache[event.event_id] = tuple(alert["fingerprint"] for alert in data["alerts"]) cache[event.event_id] = tuple(alert["fingerprint"] for alert in data["alerts"])
return web.Response(status=200) return web.Response(status=200)