Files
FabledSteward/steward/core/notifications.py
T
bvandeusenandClaude Opus 5.5 ec55faf737
CI / lint (push) Successful in 4s
CI / integration (push) Successful in 1m22s
CI / unit (push) Successful in 1m49s
CI / publish (push) Successful in 1m13s
feat(email): alert and weekly-report emails read as designed messages (#5335)
#5335 "Emails Steward sends read as designed messages: an HTML alert email
and weekly report, with a plain-text twin".

- core/mail.py renders each email as multipart/alternative from
  templates/email/: a table-and-inline-style HTML part and a plain-text
  twin carrying the same facts. Colours are the design system's light
  theme, resolved from theme.css; nothing is fetched when a mail opens.
- Alert email: state badge, a headline sentence ("Disk used on web01 is
  94%."), rule, resource, reading, the condition in words, and the time in
  the zone Notifications was saved in with UTC beside it; "View in
  Steward" when Steward's address is set. Subject "[Steward] Firing: <rule>
  — <resource> 94%". Alerts now carry their operator.
- Weekly report: monitors down, active alerts, the week's alert events,
  uptime and busiest Traefik routers as small tables with links. Sent from
  the configured From address; the "Generated … UTC" line and the stale
  repo footer are gone.
- condition_text() states a condition from a rule's parts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 14:08:05 -04:00

127 lines
4.3 KiB
Python

from __future__ import annotations
import asyncio
import json
import logging
import smtplib
import ssl
import types
from email.message import EmailMessage
import httpx
from jinja2.sandbox import SandboxedEnvironment
logger = logging.getLogger(__name__)
async def dispatch_notifications(
smtp_cfg: dict,
webhook_cfg: dict,
alert_data: dict,
) -> dict:
"""Dispatch all configured notification channels. Returns results dict."""
results = {}
if smtp_cfg.get("host") and smtp_cfg.get("recipients"):
results["email"] = await _send_email(smtp_cfg, alert_data)
if webhook_cfg.get("url"):
results["webhook"] = await _send_webhook(webhook_cfg, alert_data)
return results
SMTP_TIMEOUT_S = 15
def smtp_mode(port, tls) -> str:
"""How to talk to the SMTP server: "ssl" (implicit TLS, port 465),
"starttls" (upgrade a plain connection, when TLS is ticked) or "plain"."""
if int(port or 0) == 465:
return "ssl"
return "starttls" if tls else "plain"
async def _send_email(cfg: dict, alert: dict) -> dict:
from steward.core.mail import alert_email, message
try:
subject, text, html = alert_email(cfg, alert)
msg = message(cfg, subject, text, html)
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, _smtp_send_sync, cfg, msg)
return {"sent": True, "error": None}
except Exception as exc:
logger.error(f"Email notification failed: {exc}")
return {"sent": False, "error": str(exc)}
def _smtp_send_sync(cfg: dict, msg: EmailMessage) -> None:
port = int(cfg.get("port") or 587)
mode = smtp_mode(port, cfg.get("tls", True))
ctx = ssl.create_default_context()
if mode == "ssl":
smtp = smtplib.SMTP_SSL(cfg["host"], port, timeout=SMTP_TIMEOUT_S, context=ctx)
else:
smtp = smtplib.SMTP(cfg["host"], port, timeout=SMTP_TIMEOUT_S)
with smtp:
if mode == "starttls":
smtp.starttls(context=ctx)
if cfg.get("username"):
smtp.login(cfg["username"], cfg.get("password", ""))
smtp.send_message(msg)
def _json_str(value) -> str:
"""A value as it may sit between the quotes of a JSON string."""
return json.dumps("" if value is None else str(value))[1:-1]
def render_webhook(template_str: str, alert: dict) -> str:
"""The webhook body for an alert, checked to be JSON.
In the template, `alert.<field>` is escaped to sit inside a JSON string
("{{ alert.rule_name }}" stays valid whatever the name holds), with a
missing value reading "no data"; `raw.<field>` is the plain value for use
with |tojson ({{ raw.value | tojson }} gives a number or null). The
template runs sandboxed. ValueError says why a body could not be made.
"""
shown = dict(alert)
if shown.get("value") is None:
shown["value"] = "no data"
env = SandboxedEnvironment()
try:
rendered = env.from_string(template_str).render(
alert=types.SimpleNamespace(**{k: _json_str(v) for k, v in shown.items()}),
raw=types.SimpleNamespace(**alert))
except Exception as exc:
raise ValueError(f"Template render failed: {exc}") from None
try:
json.loads(rendered)
except json.JSONDecodeError as exc:
raise ValueError(f"Rendered webhook template is not valid JSON: {exc}") from None
return rendered
async def _send_webhook(cfg: dict, alert: dict) -> dict:
template_str = cfg.get("template") or '{"content": "{{ alert.state }} — {{ alert.rule_name }}"}'
try:
rendered = render_webhook(template_str, alert)
except ValueError as exc:
logger.error(str(exc))
return {"sent": False, "error": str(exc)}
try:
async with httpx.AsyncClient(timeout=10) as client:
response = await client.post(
cfg["url"],
content=rendered,
headers={"Content-Type": "application/json"},
)
response.raise_for_status()
return {"sent": True, "error": None}
except Exception as exc:
# The URL is a secret (it usually carries its own token) and httpx
# puts it in its error messages.
error = str(exc).replace(cfg["url"], "<webhook URL>")
logger.error(f"Webhook notification failed: {error}")
return {"sent": False, "error": error}