integrations: Add AlertManager webhook.

This commit is contained in:
Chris Heald 2020-02-07 02:04:22 -07:00 committed by Tim Abbott
parent cbdfef28a8
commit 18e3982acd
9 changed files with 212 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 852 B

View File

@ -227,6 +227,12 @@ EMBEDDED_BOTS = [
WEBHOOK_INTEGRATIONS = [ WEBHOOK_INTEGRATIONS = [
WebhookIntegration('airbrake', ['monitoring']), WebhookIntegration('airbrake', ['monitoring']),
WebhookIntegration(
'alertmanager',
['monitoring'],
display_name='Prometheus AlertManager',
logo='images/integrations/logos/prometheus.svg'
),
WebhookIntegration('ansibletower', ['deployment'], display_name='Ansible Tower'), WebhookIntegration('ansibletower', ['deployment'], display_name='Ansible Tower'),
WebhookIntegration('appfollow', ['customer-support'], display_name='AppFollow'), WebhookIntegration('appfollow', ['customer-support'], display_name='AppFollow'),
WebhookIntegration('appveyor', ['continuous-integration'], display_name='AppVeyor'), WebhookIntegration('appveyor', ['continuous-integration'], display_name='AppVeyor'),

View File

View File

@ -0,0 +1,23 @@
Get Zulip notifications from AlertManager!
1. {!create-stream.md!}
1. {!create-bot-construct-url-indented.md!}
Additionally, you may specify URL parameters named `name` and `desc` to specify which labels
or annotations will be used to construct the alert message. This allows you to use arbitrary labels
and annotations defined in your alerting rules.
{{ api_url }}{{ integration_url }}?api_key=abcdefgh&stream=stream%20name&name=host&desc=alertname
1. In your AlertManager config, set up a new webhook receiver, like so:
```
- name: ops-zulip
webhook_configs:
- url: "<the url constructed above>"
```
{!congrats.md!}
![](/static/images/integrations/alertmanager/001.png)

View File

@ -0,0 +1,51 @@
{
"receiver": "ops-zulip",
"status": "firing",
"groupLabels": {},
"commonLabels": {
"alertname": "High CPU Temperature",
"severity": "critical"
},
"commonAnnotations": {
"summary": "High CPU core temperature"
},
"externalURL": "http://ops1:9093",
"version": "4",
"groupKey": "{}/{}:{}",
"alerts": [
{
"status": "firing",
"labels": {
"alertname": "High CPU Temperature",
"host": "andromeda",
"severity": "critical"
},
"annotations": {
"description": "CPU core temperature is 34.75C",
"topic": "andromeda",
"summary": "High CPU core temperature"
},
"startsAt": "2020-02-06T00:17:58.432911368Z",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://cobalt:9090/graph?g0.expr=avg+by%28host%29+%28sensors_temp_input%7Bfeature%3D~%22core_%5B0-9%5D%2B%22%7D%29+%3E+15&g0.tab=1",
"fingerprint": "a1bc48475b20e73d"
},
{
"status": "firing",
"labels": {
"alertname": "High CPU Temperature",
"host": "andromeda",
"severity": "critical"
},
"annotations": {
"description": "CPU core temperature is 17.625C",
"topic": "andromeda",
"summary": "High CPU core temperature"
},
"startsAt": "2020-02-06T00:17:58.432911368Z",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://cobalt:9090/graph?g0.expr=avg+by%28host%29+%28sensors_temp_input%7Bfeature%3D~%22core_%5B0-9%5D%2B%22%7D%29+%3E+15&g0.tab=1",
"fingerprint": "c2ea85182f28cbd9"
}
]
}

View File

@ -0,0 +1,34 @@
{
"receiver": "ops-zulip",
"status": "resolved",
"groupLabels": {},
"commonLabels": {
"alertname": "High CPU Temperature",
"severity": "critical"
},
"commonAnnotations": {
"summary": "High CPU core temperature"
},
"externalURL": "http://ops1:9093",
"version": "4",
"groupKey": "{}/{}:{}",
"alerts": [
{
"status": "resolved",
"labels": {
"alertname": "High CPU Temperature",
"host": "andromeda",
"severity": "critical"
},
"annotations": {
"description": "CPU core temperature is 34.75C",
"topic": "andromeda",
"summary": "High CPU core temperature"
},
"startsAt": "2020-02-06T00:17:58.432911368Z",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://cobalt:9090/graph?g0.expr=avg+by%28host%29+%28sensors_temp_input%7Bfeature%3D~%22core_%5B0-9%5D%2B%22%7D%29+%3E+15&g0.tab=1",
"fingerprint": "a1bc48475b20e73d"
}
]
}

View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
from zerver.lib.test_classes import WebhookTestCase
class AlertmanagerHookTests(WebhookTestCase):
STREAM_NAME = 'alertmanager'
URL_TEMPLATE = "/api/v1/external/alertmanager?&api_key={api_key}&stream={stream}&name=topic&desc=description"
FIXTURE_DIR_NAME = 'alertmanager'
def test_error_issue_message(self) -> None:
expected_topic = "andromeda"
expected_message = """
:alert: **FIRING**
* CPU core temperature is 34.75C ([graph](http://cobalt:9090/graph?g0.expr=avg+by%28host%29+%28sensors_temp_input%7Bfeature%3D~%22core_%5B0-9%5D%2B%22%7D%29+%3E+15&g0.tab=0))
* CPU core temperature is 17.625C ([graph](http://cobalt:9090/graph?g0.expr=avg+by%28host%29+%28sensors_temp_input%7Bfeature%3D~%22core_%5B0-9%5D%2B%22%7D%29+%3E+15&g0.tab=0))
""".strip()
self.send_and_test_stream_message(
'alert',
expected_topic,
expected_message,
"application/json"
)
def test_single_error_issue_message(self) -> None:
expected_topic = "andromeda"
expected_message = """
:squared_ok: **Resolved** CPU core temperature is 34.75C ([graph](http://cobalt:9090/graph?g0.expr=avg+by%28host%29+%28sensors_temp_input%7Bfeature%3D~%22core_%5B0-9%5D%2B%22%7D%29+%3E+15&g0.tab=0))
""".strip()
self.send_and_test_stream_message(
'single_alert',
expected_topic,
expected_message,
"application/json"
)

View File

@ -0,0 +1,62 @@
# Webhooks for external integrations.
from typing import Any, Dict, List
from django.http import HttpRequest, HttpResponse
from zerver.decorator import api_key_only_webhook_view
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
@api_key_only_webhook_view('AlertManager')
@has_request_variables
def api_alertmanager_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any] = REQ(argument_type='body')) -> HttpResponse:
name_field = request.GET.get("name", "instance")
desc_field = request.GET.get("desc", "alertname")
topics = {} # type: Dict[str, Dict[str, List[str]]]
for alert in payload["alerts"]:
labels = alert.get("labels", {})
annotations = alert.get("annotations", {})
name = labels.get(
name_field, annotations.get(name_field, "(unknown)"))
desc = labels.get(
desc_field, annotations.get(desc_field, "<missing field: {}>".format(desc_field)))
url = alert.get("generatorURL").replace("tab=1", "tab=0")
body = "{description} ([graph]({url}))".format(description=desc, url=url)
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:
body = "{icon} **{title}** {message}".format(
icon=icon,
title=title,
message=messages[0])
else:
message_list = "\n".join(["* {}".format(m) for m in messages])
body = "{icon} **{title}**\n{messages}".format(
icon=icon,
title=title,
messages=message_list)
check_send_webhook_message(request, user_profile, topic, body)
return json_success()