2020-02-07 10:04:22 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-02-07 10:04:22 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Alertmanager")
|
2020-02-07 10:04:22 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_alertmanager_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2021-07-29 19:13:14 +02:00
|
|
|
name_field: str = REQ("name", default="instance"),
|
|
|
|
desc_field: str = REQ("desc", default="alertname"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
topics: Dict[str, Dict[str, List[str]]] = {}
|
2020-02-07 10:04:22 +01:00
|
|
|
|
|
|
|
for alert in payload["alerts"]:
|
|
|
|
labels = alert.get("labels", {})
|
|
|
|
annotations = alert.get("annotations", {})
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
name = labels.get(name_field, annotations.get(name_field, "(unknown)"))
|
|
|
|
desc = labels.get(desc_field, annotations.get(desc_field, f"<missing field: {desc_field}>"))
|
2020-02-07 10:04:22 +01:00
|
|
|
|
|
|
|
url = alert.get("generatorURL").replace("tab=1", "tab=0")
|
|
|
|
|
2020-06-09 00:25:09 +02:00
|
|
|
body = f"{desc} ([graph]({url}))"
|
2020-02-07 10:04:22 +01:00
|
|
|
if name not in topics:
|
|
|
|
topics[name] = {"firing": [], "resolved": []}
|
|
|
|
topics[name][alert["status"]].append(body)
|
|
|
|
|
|
|
|
for topic, statuses in topics.items():
|
|
|
|
for status, messages in statuses.items():
|
|
|
|
if len(messages) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if status == "firing":
|
|
|
|
icon = ":alert:"
|
|
|
|
title = "FIRING"
|
|
|
|
else:
|
|
|
|
title = "Resolved"
|
|
|
|
icon = ":squared_ok:"
|
|
|
|
|
|
|
|
if len(messages) == 1:
|
2020-06-10 06:40:53 +02:00
|
|
|
body = f"{icon} **{title}** {messages[0]}"
|
2020-02-07 10:04:22 +01:00
|
|
|
else:
|
2020-09-02 06:20:26 +02:00
|
|
|
message_list = "\n".join(f"* {m}" for m in messages)
|
2020-06-10 06:40:53 +02:00
|
|
|
body = f"{icon} **{title}**\n{message_list}"
|
2020-02-07 10:04:22 +01:00
|
|
|
|
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
|
|
|
|
|
|
|
return json_success()
|