Handle SNS cloudwatch alarm states with colors

This commit is contained in:
Abel Luck 2022-11-30 16:05:39 +00:00
parent 5ecb68f2e7
commit 08f5b49b16
3 changed files with 21 additions and 11 deletions

View file

@ -2,7 +2,7 @@ import json
import logging import logging
from typing import Any, Tuple from typing import Any, Tuple
from ops_bot.common import urgency_color from ops_bot.common import COLOR_ALARM, COLOR_OK, COLOR_UNKNOWN
def handle_subscribe_confirm(payload: Any) -> Tuple[str, str]: def handle_subscribe_confirm(payload: Any) -> Tuple[str, str]:
@ -17,9 +17,8 @@ def handle_notification(payload: Any) -> Tuple[str, str]:
subject = payload.get("Subject") subject = payload.get("Subject")
plain = f"{subject}\n{message}" plain = f"{subject}\n{message}"
color = urgency_color("high")
formatted = ( formatted = (
f"<strong><font color={color}>{subject}</font></strong>\n<p>{message}</p>" f"<strong><font color={COLOR_ALARM}>{subject}</font></strong>\n<p>{message}</p>"
) )
return plain, formatted return plain, formatted
@ -33,9 +32,15 @@ def handle_json_notification(payload: Any, body: Any) -> Tuple[str, str]:
description = body.get("AlarmDescription") description = body.get("AlarmDescription")
subject = payload.get("Subject") subject = payload.get("Subject")
state_value = payload.get("NewStateValue", "unknown")
plain = f"{subject}\n{description}" plain = f"{subject}\n{description}"
color = urgency_color("high") if state_value == "ALARM":
color = COLOR_ALARM
elif state_value == "OK":
color = COLOR_OK
else:
color = COLOR_UNKNOWN
formatted = ( formatted = (
f"<strong><font color={color}>{subject}</font></strong>\n<p>{description}</p>" f"<strong><font color={color}>{subject}</font></strong>\n<p>{description}</p>"
) )

View file

@ -1,6 +1,4 @@
def urgency_color(urgency: str) -> str: COLOR_OK = "#33cc33" # green
if urgency == "high": COLOR_ALARM = "#dc3545" # red
return "#dc3545" # red COLOR_UNKNOWN = "#ffc107" # orange
else: COLOR_INFO = "#17a2b8" # blue
return "#ffc107" # orange
# return "#17a2b8" # blue

View file

@ -1,7 +1,14 @@
import json import json
from typing import Any, Tuple from typing import Any, Tuple
from ops_bot.common import urgency_color from ops_bot.common import COLOR_ALARM, COLOR_UNKNOWN
def urgency_color(urgency: str) -> str:
if urgency == "high":
return COLOR_ALARM
else:
return COLOR_UNKNOWN
def parse_pagerduty_event(payload: Any) -> Tuple[str, str]: def parse_pagerduty_event(payload: Any) -> Tuple[str, str]: