2022-07-04 01:03:24 +02:00
|
|
|
from __future__ import annotations
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
import logging
|
|
|
|
from typing import Dict
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2019-09-25 14:31:56 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2019-10-12 15:20:18 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
class Alert:
|
|
|
|
EMOJIS = {"critical": "🔥", "warning": "⚠️", "resolved": "🥦"}
|
|
|
|
COLORS = {"critical": "dc3545", "warning": "ffc107", "resolved": "33cc33"}
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
id: str,
|
|
|
|
url: str,
|
|
|
|
firing: bool = True,
|
|
|
|
labels: Dict[str, str] = None,
|
|
|
|
annotations: Dict[str, str] = None,
|
|
|
|
):
|
|
|
|
self.id = id
|
|
|
|
self.url = url
|
|
|
|
self.firing = firing
|
|
|
|
|
|
|
|
if labels is None:
|
|
|
|
self.labels = {}
|
|
|
|
else:
|
|
|
|
self.labels = labels
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
if annotations is None:
|
|
|
|
self.annotations = {}
|
|
|
|
else:
|
|
|
|
self.annotations = annotations
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
if self.firing:
|
|
|
|
self.status = self.labels["severity"]
|
|
|
|
else:
|
|
|
|
self.status = "resolved"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_dict(data: Dict) -> Alert:
|
|
|
|
return Alert(
|
|
|
|
id=data["fingerprint"],
|
|
|
|
url=data["generatorURL"],
|
|
|
|
firing=data["status"] == "firing",
|
|
|
|
labels=data["labels"],
|
|
|
|
annotations=data["annotations"],
|
2020-08-16 16:51:59 +02:00
|
|
|
)
|
2019-09-25 14:26:29 +02:00
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
@property
|
|
|
|
def emoji(self) -> str:
|
|
|
|
return self.EMOJIS[self.status]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color(self) -> str:
|
|
|
|
return self.COLORS[self.status]
|
|
|
|
|
|
|
|
def plaintext(self) -> str:
|
|
|
|
alertname = self.labels["alertname"]
|
|
|
|
description = self.annotations["description"]
|
|
|
|
return f"[{self.emoji} {self.status.upper()}] {alertname}: {description}"
|
|
|
|
|
|
|
|
def html(self) -> str:
|
|
|
|
alertname = self.labels["alertname"]
|
|
|
|
job = self.labels["job"]
|
|
|
|
description = self.annotations["description"]
|
|
|
|
return (
|
|
|
|
f"<font color='#{self.color}'><b>[{self.emoji} {self.status.upper()}]</b></font> "
|
|
|
|
f"<a href='{self.url}'>{alertname}</a> ({job})<br/>{description}"
|
2020-08-10 00:02:07 +02:00
|
|
|
)
|