From 9438fe32d7d99161681ef93c5e813573235964cf Mon Sep 17 00:00:00 2001 From: Khue Doan Date: Thu, 28 Mar 2024 17:07:25 +0700 Subject: [PATCH] feat(alertmanager): add more info in notifications Map status, priority, tags and runbook. --- .../alertmanager-to-ntfy.jsonnet | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/system/monitoring-system/files/webhook-transformer/alertmanager-to-ntfy.jsonnet b/system/monitoring-system/files/webhook-transformer/alertmanager-to-ntfy.jsonnet index 4b74b08c..c0698739 100644 --- a/system/monitoring-system/files/webhook-transformer/alertmanager-to-ntfy.jsonnet +++ b/system/monitoring-system/files/webhook-transformer/alertmanager-to-ntfy.jsonnet @@ -1,8 +1,44 @@ +local get_tags(status, severity) = + // https://docs.ntfy.sh/emojis + if status == "resolved" then + ["tada"] + else + std.get({ + critical: ["rotating_light"], + warning: ["warning"], + info: ["newspaper"], + }, severity, ["question"]); + +local get_priority(status, severity) = + // https://docs.ntfy.sh/publish/#message-priority + if status == "resolved" then + 2 + else + std.get({ + critical: 5, + warning: 3, + info: 1, + }, severity, 3); + +local get_actions(status, annotations) = + // https://docs.ntfy.sh/publish/#action-buttons + if status == "resolved" || !("runbook_url" in annotations) then + [] + else + [ + { + action: "view", + label: "Open runbook", + url: annotations.runbook_url, + }, + ]; + +// TODO support multiple alerts { - "topic": env.NTFY_TOPIC, - "title": body.alerts[0].labels.alertname, // TODO support multiple alerts - "message": body.alerts[0].annotations.description, - "tags": [], - "priority": 3, - "actions": [] + topic: env.NTFY_TOPIC, + title: "[" + std.asciiUpper(body.status) + "] " + body.alerts[0].labels.alertname, + message: body.alerts[0].annotations.description, + tags: get_tags(body.status, body.alerts[0].labels.severity), + priority: get_priority(body.status, body.alerts[0].labels.severity), + actions: get_actions(body.status, body.alerts[0].annotations), }