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 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
|
from ops_bot.config import RoutingKey
|
||||||
|
|
||||||
|
severity_colors = {
|
||||||
|
"warning": COLOR_WARNING,
|
||||||
|
"info": COLOR_INFO,
|
||||||
|
"critical": COLOR_ALARM,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def prometheus_alert_to_markdown(
|
def prometheus_alert_to_markdown(
|
||||||
alert_data: Dict, # type: ignore[type-arg]
|
alert_data: Dict, # type: ignore[type-arg]
|
||||||
|
|
@ -13,33 +25,35 @@ def prometheus_alert_to_markdown(
|
||||||
Converts a prometheus alert json to markdown
|
Converts a prometheus alert json to markdown
|
||||||
"""
|
"""
|
||||||
messages = []
|
messages = []
|
||||||
ignore_labels = ["grafana_folder", "alertname"]
|
ignore_labels = ["grafana_folder"]
|
||||||
for alert in alert_data["alerts"]:
|
for alert in alert_data["alerts"]:
|
||||||
title = (
|
title = (
|
||||||
alert["annotations"]["description"]
|
alert["annotations"]["description"]
|
||||||
if hasattr(alert["annotations"], "description")
|
if hasattr(alert["annotations"], "description")
|
||||||
else alert["annotations"]["summary"]
|
else alert["annotations"]["summary"]
|
||||||
)
|
)
|
||||||
severity = alert.get("severity", "critical")
|
labels = alert.get("labels", {})
|
||||||
if alert["status"] == "resolved":
|
severity = labels.get("severity", "unknown")
|
||||||
|
status = alert.get("status", "unknown-status")
|
||||||
|
if status == "resolved":
|
||||||
color = COLOR_OK
|
color = COLOR_OK
|
||||||
elif severity == "critical":
|
|
||||||
color = COLOR_ALARM
|
|
||||||
else:
|
else:
|
||||||
color = COLOR_UNKNOWN
|
color = severity_colors.get(severity, COLOR_UNKNOWN)
|
||||||
|
|
||||||
status = alert["status"]
|
|
||||||
generatorURL = alert.get("generatorURL")
|
generatorURL = alert.get("generatorURL")
|
||||||
plain = f"{status}: {title}"
|
plain = f"{status.upper()}[{severity}]: {title}"
|
||||||
header_str = f"{status.upper()}[{status}]"
|
header_str = f"{status.upper()}[{severity}]"
|
||||||
formatted = f"<strong><font color={color}>{header_str}</font></strong>: [{title}]({generatorURL})"
|
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("__"):
|
if label_name.startswith("__"):
|
||||||
continue
|
continue
|
||||||
if label_name in ignore_labels:
|
if label_name in ignore_labels:
|
||||||
continue
|
continue
|
||||||
plain += "\n* **{0}**: {1}".format(label_name.capitalize(), label_value)
|
label_strings.append((f"**{label_name.capitalize()}**: {label_value}"))
|
||||||
formatted += "\n* **{0}**: {1}".format(label_name.capitalize(), label_value)
|
labels_final = ", ".join(label_strings)
|
||||||
|
plain += f" \n{labels_final}"
|
||||||
|
formatted += f"<br/>{labels_final}"
|
||||||
messages.append((plain, formatted))
|
messages.append((plain, formatted))
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
COLOR_OK = "#33cc33" # green
|
COLOR_OK = "#33cc33" # green
|
||||||
COLOR_ALARM = "#dc3545" # red
|
COLOR_ALARM = "#dc3545" # red
|
||||||
COLOR_UNKNOWN = "#ffc107" # orange
|
|
||||||
COLOR_INFO = "#17a2b8" # blue
|
COLOR_INFO = "#17a2b8" # blue
|
||||||
|
COLOR_WARNING = "#ffc107" # orange
|
||||||
|
COLOR_UNKNOWN = "#ffa500" # yellowish
|
||||||
|
|
|
||||||
|
|
@ -54,5 +54,18 @@ def test_alertmanager():
|
||||||
assert len(r) == 1
|
assert len(r) == 1
|
||||||
plain, formatted = r[0]
|
plain, formatted = r[0]
|
||||||
|
|
||||||
assert "firing" in plain and "Instance webserver.example.com down" in plain
|
print(formatted)
|
||||||
assert "firing" in formatted and "Instance webserver.example.com down" in 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