2020-04-15 06:06:49 +02:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-04-15 06:06:49 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2020-04-15 06:06:49 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
GRAFANA_TOPIC_TEMPLATE = "{alert_title}"
|
2020-04-15 06:06:49 +02:00
|
|
|
|
2021-05-24 23:25:33 +02:00
|
|
|
GRAFANA_ALERT_STATUS_TEMPLATE = "{alert_icon} **{alert_state}**\n\n"
|
|
|
|
|
|
|
|
GRAFANA_MESSAGE_TEMPLATE = (
|
|
|
|
"{alert_status}[{rule_name}]({rule_url})\n\n{alert_message}{eval_matches}"
|
|
|
|
)
|
2020-04-15 06:06:49 +02:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = ["ok", "pending", "alerting", "paused"]
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("Grafana", all_event_types=ALL_EVENT_TYPES)
|
2020-04-15 06:06:49 +02:00
|
|
|
@has_request_variables
|
|
|
|
def api_grafana_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2020-04-15 06:06:49 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = GRAFANA_TOPIC_TEMPLATE.format(alert_title=payload["title"])
|
2020-04-15 06:06:49 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
eval_matches_text = ""
|
|
|
|
eval_matches = payload.get("evalMatches")
|
2020-04-15 06:06:49 +02:00
|
|
|
if eval_matches is not None:
|
|
|
|
for match in eval_matches:
|
2021-02-12 08:20:45 +01:00
|
|
|
eval_matches_text += "**{}:** {}\n".format(match["metric"], match["value"])
|
2020-04-15 06:06:49 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message_text = ""
|
|
|
|
if payload.get("message") is not None:
|
|
|
|
message_text = payload["message"] + "\n\n"
|
2020-04-15 06:06:49 +02:00
|
|
|
|
2021-05-24 23:25:33 +02:00
|
|
|
if payload.get("state") is not None:
|
|
|
|
if payload.get("state") == "alerting":
|
|
|
|
alert_status = GRAFANA_ALERT_STATUS_TEMPLATE.format(
|
|
|
|
alert_icon=":alert:", alert_state=payload["state"].upper()
|
|
|
|
)
|
|
|
|
elif payload.get("state") == "ok":
|
|
|
|
alert_status = GRAFANA_ALERT_STATUS_TEMPLATE.format(
|
|
|
|
alert_icon=":squared_ok:", alert_state=payload["state"].upper()
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
alert_status = GRAFANA_ALERT_STATUS_TEMPLATE.format(
|
|
|
|
alert_icon=":info:", alert_state=payload["state"].upper()
|
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
body = GRAFANA_MESSAGE_TEMPLATE.format(
|
|
|
|
alert_message=message_text,
|
2021-05-24 23:25:33 +02:00
|
|
|
alert_status=alert_status,
|
2021-02-12 08:20:45 +01:00
|
|
|
rule_name=payload["ruleName"],
|
|
|
|
rule_url=payload["ruleUrl"],
|
2021-02-12 08:19:30 +01:00
|
|
|
eval_matches=eval_matches_text,
|
|
|
|
)
|
2020-04-15 06:06:49 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if payload.get("imageUrl") is not None:
|
2021-02-12 08:19:30 +01:00
|
|
|
body += "\n[Click to view visualization]({visualization})".format(
|
2021-02-12 08:20:45 +01:00
|
|
|
visualization=payload["imageUrl"]
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-04-15 06:06:49 +02:00
|
|
|
|
|
|
|
body = body.strip()
|
|
|
|
|
|
|
|
# send the message
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, payload.get("state"))
|
2020-04-15 06:06:49 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|