Make use of new classification structure

This commit is contained in:
Julian-Samuel Gebühr 2022-09-10 01:49:12 +02:00
parent 2ff5a6d9ce
commit e12938b695

View File

@ -56,36 +56,50 @@ def get_alert_type(data):
return "not-found" return "not-found"
def get_alert_messages(alertmanager_data: dict, raw_mode=False) -> list: def get_alert_messages(alert_data: dict, raw_mode=False) -> list:
""" """
Returns a list of messages in markdown format Returns a list of messages in markdown format
:param alert_data: The data send to the bot as dict
:param raw_mode: Toggles a mode where the data is not parsed but simply returned as code block in a message :param raw_mode: Toggles a mode where the data is not parsed but simply returned as code block in a message
:param alertmanager_data:
:return: List of alert messages in markdown format :return: List of alert messages in markdown format
""" """
if raw_mode:
return ["**Data received**\n```\n" + str(alertmanager_data).strip("\n").strip() + "\n```"] alert_type = get_alert_type(alert_data)
messages = []
for alert in alertmanager_data["alerts"]: if raw_mode or alert_type == "not-found":
messages.append(alert_to_markdown(alert)) return ["**Data received**\n```\n" + str(alert_data).strip("\n").strip() + "\n```"]
else:
if alert_type == "grafana-alert":
messages = grafana_alert_to_markdown(alert_data)
elif alert_type == "grafana-resolved":
messages = grafana_alert_to_markdown(alert_data)
elif alert_type == "prometheus-alert":
messages = prometheus_alert_to_markdown(alert_data)
elif alert_type == "uptime-kuma-alert":
messages = uptime_kuma_alert_to_markdown(alert_data)
elif alert_type == "uptime-kuma-resolved":
messages = uptime_kuma_resolved_to_markdown(alert_data)
return messages return messages
def alert_to_markdown(alert: dict) -> str: def uptime_kuma_alert_to_markdown(alert_data: dict):
if alert["fingerprint"]: return ["**Uptime Kuma Alert Data:**\n```\n" + str(alert_data).strip("\n").strip() + "\n```"]
return grafana_alert_to_markdown(alert)
else:
return prometheus_alert_to_markdown(alert)
def grafana_alert_to_markdown(alert: dict) -> str: def uptime_kuma_resolved_to_markdown(alert_data: dict):
return ["**Uptime Kuma Resolved Data:**\n```\n" + str(alert_data).strip("\n").strip() + "\n```"]
def grafana_alert_to_markdown(alert_data: dict) -> list:
""" """
Converts a grafana alert json to markdown Converts a grafana alert json to markdown
:param alert: :param alert_data:
:return: Alert as fomatted markdown :return: Alerts as formatted markdown string list
""" """
messages = []
for alert in alert_data["alerts"]:
datetime_format = "%Y-%m-%dT%H:%M:%S%z" datetime_format = "%Y-%m-%dT%H:%M:%S%z"
if alert['status'] == "firing": if alert['status'] == "firing":
message = ( message = (
@ -107,17 +121,19 @@ f"""**Firing 🔥**: {alert['labels']['alertname']}
* **Fingerprint:** {alert['fingerprint']} * **Fingerprint:** {alert['fingerprint']}
""" """
) )
return message messages.append(message)
return messages
def prometheus_alert_to_markdown(alert: dict) -> str: def prometheus_alert_to_markdown(alert_data: dict) -> str:
""" """
Converts a prometheus alert json to markdown Converts a prometheus alert json to markdown
:param alert: :param alert_data:
:return: Alert as fomatted markdown :return: Alert as fomatted markdown
""" """
messages = []
for alert in alert_data["alerts"]:
message = ( message = (
f"""**{alert['status']}**: {alert['annotations']['description']} f"""**{alert['status']}**: {alert['annotations']['description']}
@ -126,7 +142,8 @@ def prometheus_alert_to_markdown(alert: dict) -> str:
* **Job:** {alert["labels"]['job']} * **Job:** {alert["labels"]['job']}
""" """
) )
return message messages.append(message)
return messages
class AlertBot(Plugin): class AlertBot(Plugin):