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
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()
return response.json()
@ -40,8 +40,8 @@ class AlertmanagerClient:
silence = {
"matchers": matchers,
"startsAt": start_time,
"endsAt": end_time,
"startsAt": start_time.isoformat(),
"endsAt": end_time.isoformat(),
"createdBy": user,
"comment": "Acknowledge alert from Matrix",
}

View file

@ -73,19 +73,23 @@ class Command:
except KeyError:
logger.debug("Unable to find the event ID of the alert")
return
logger.debug(f"Read alert fingerprint for event {alert_event_id} from cache")
alert_fingerprint = self.cache[alert_event_id]
logger.debug(
f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}"
)
silence_id = self.alertmanager.create_silence(
alert_fingerprint, duration, self.room.user_name(self.event.sender)
)
logger.debug(f"Read alert fingerprints for event {alert_event_id} from cache")
silence_ids = []
alert_fingerprints = self.cache[alert_event_id]
for alert_fingerprint in alert_fingerprints:
logger.debug(
f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}"
)
silence_id = self.alertmanager.create_silence(
alert_fingerprint, duration, self.room.user_name(self.event.sender)
)
silence_ids.append(silence_id)
silences = ", ".join(silence_ids)
await send_text_to_room(
self.client,
self.room.room_id,
f"Created silence {silence_id} for {duration}",
reply_to_event_id=alert_event_id,
f"Created silences {silences} for a duration of {duration}",
)
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()
logger.info(f"Received alert: {data}")
client = request.app["client"]
cache = request.app['cache']
cache = request.app["cache"]
plaintext = ""
html = ""
@ -33,13 +33,14 @@ async def create_alert(request: web_request.Request) -> web.Response:
html += alert.html()
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:
logger.error(e)
return web.Response(status=500)
cache[event.event_id] = tuple(alert["fingerprint"] for alert in data["alerts"])
return web.Response(status=200)