alertmanager: distinguish between critical and non critical alerts
This commit is contained in:
parent
1f6cbb411f
commit
69613721bf
3 changed files with 44 additions and 16 deletions
|
|
@ -2,9 +2,21 @@ from typing import Any, Dict, List, Tuple
|
|||
|
||||
from fastapi import Request
|
||||
|
||||
from ops_bot.common import COLOR_ALARM, COLOR_OK, COLOR_UNKNOWN
|
||||
from ops_bot.common import (
|
||||
COLOR_ALARM,
|
||||
COLOR_INFO,
|
||||
COLOR_OK,
|
||||
COLOR_UNKNOWN,
|
||||
COLOR_WARNING,
|
||||
)
|
||||
from ops_bot.config import RoutingKey
|
||||
|
||||
severity_colors = {
|
||||
"warning": COLOR_WARNING,
|
||||
"info": COLOR_INFO,
|
||||
"critical": COLOR_ALARM,
|
||||
}
|
||||
|
||||
|
||||
def prometheus_alert_to_markdown(
|
||||
alert_data: Dict, # type: ignore[type-arg]
|
||||
|
|
@ -13,33 +25,35 @@ def prometheus_alert_to_markdown(
|
|||
Converts a prometheus alert json to markdown
|
||||
"""
|
||||
messages = []
|
||||
ignore_labels = ["grafana_folder", "alertname"]
|
||||
ignore_labels = ["grafana_folder"]
|
||||
for alert in alert_data["alerts"]:
|
||||
title = (
|
||||
alert["annotations"]["description"]
|
||||
if hasattr(alert["annotations"], "description")
|
||||
else alert["annotations"]["summary"]
|
||||
)
|
||||
severity = alert.get("severity", "critical")
|
||||
if alert["status"] == "resolved":
|
||||
labels = alert.get("labels", {})
|
||||
severity = labels.get("severity", "unknown")
|
||||
status = alert.get("status", "unknown-status")
|
||||
if status == "resolved":
|
||||
color = COLOR_OK
|
||||
elif severity == "critical":
|
||||
color = COLOR_ALARM
|
||||
else:
|
||||
color = COLOR_UNKNOWN
|
||||
color = severity_colors.get(severity, COLOR_UNKNOWN)
|
||||
|
||||
status = alert["status"]
|
||||
generatorURL = alert.get("generatorURL")
|
||||
plain = f"{status}: {title}"
|
||||
header_str = f"{status.upper()}[{status}]"
|
||||
plain = f"{status.upper()}[{severity}]: {title}"
|
||||
header_str = f"{status.upper()}[{severity}]"
|
||||
formatted = f"<strong><font color={color}>{header_str}</font></strong>: [{title}]({generatorURL})"
|
||||
for label_name, label_value in alert["labels"].items():
|
||||
label_strings = []
|
||||
for label_name, label_value in labels.items():
|
||||
if label_name.startswith("__"):
|
||||
continue
|
||||
if label_name in ignore_labels:
|
||||
continue
|
||||
plain += "\n* **{0}**: {1}".format(label_name.capitalize(), label_value)
|
||||
formatted += "\n* **{0}**: {1}".format(label_name.capitalize(), label_value)
|
||||
label_strings.append((f"**{label_name.capitalize()}**: {label_value}"))
|
||||
labels_final = ", ".join(label_strings)
|
||||
plain += f" \n{labels_final}"
|
||||
formatted += f"<br/>{labels_final}"
|
||||
messages.append((plain, formatted))
|
||||
return messages
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
COLOR_OK = "#33cc33" # green
|
||||
COLOR_ALARM = "#dc3545" # red
|
||||
COLOR_UNKNOWN = "#ffc107" # orange
|
||||
COLOR_INFO = "#17a2b8" # blue
|
||||
COLOR_WARNING = "#ffc107" # orange
|
||||
COLOR_UNKNOWN = "#ffa500" # yellowish
|
||||
|
|
|
|||
|
|
@ -54,5 +54,18 @@ def test_alertmanager():
|
|||
assert len(r) == 1
|
||||
plain, formatted = r[0]
|
||||
|
||||
assert "firing" in plain and "Instance webserver.example.com down" in plain
|
||||
assert "firing" in formatted and "Instance webserver.example.com down" in formatted
|
||||
print(formatted)
|
||||
assert (
|
||||
"FIRING" in plain
|
||||
and "Instance" in plain
|
||||
and "webserver.example.com" in plain
|
||||
and "Instance webserver.example.com down" in plain
|
||||
and "critical" in plain
|
||||
)
|
||||
assert (
|
||||
"FIRING" in formatted
|
||||
and "Instance" in formatted
|
||||
and "webserver.example.com" in formatted
|
||||
and "Instance webserver.example.com down" in formatted
|
||||
and "critical" in formatted
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue