feat(alerts): Send test on a rule checks that its alerts reach someone (#5466)
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 1m9s
CI / unit (push) Successful in 1m36s
CI / publish (push) Successful in 1m6s

#5466 "Send test: check a rule's delivery from the rule". A rule's page gets
a Send test button (operator role), and MCP gets send_test_alert. Each sends
a TEST alert for that rule now, by email to everyone who gets alerts and by
webhook. The alert carries the rule's name and condition, at the latest
reading of what it watches; a rule covering hosts uses its first host. The
result for each channel shows under the button, or "Nothing is set up to
send to". Nothing fires, no event is written, and the test is audited as
alert_rule.tested. An alert with no reading now says "has no reading yet"
instead of "is None".

Docs: alerting.md also gains the burst email from #5465 "One email per burst
of alerts".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 08:33:32 -04:00
co-authored by Claude Opus 5.5
parent 997aa50939
commit 5ef18c992c
13 changed files with 299 additions and 7 deletions
+15
View File
@@ -420,6 +420,21 @@ async def toggle_rule(rule_id: str):
return redirect(url_for("alerts.list_alerts"))
@alerts_bp.post("/rules/<rule_id>/test")
@require_role(UserRole.operator)
async def test_rule(rule_id: str):
"""Send test (#5466): a TEST notification for this rule, now, by every
channel its alerts use; the result shows under the button."""
try:
sent = await alert_svc.send_test(current_app, rule_id)
except LookupError:
return "Not found", 404
await log_audit(current_app, session.get("user_id"), session.get("username", ""),
"alert_rule.tested", entity_type="alert_rule", entity_id=sent["rule"],
detail={"rule_id": rule_id, "ok": sent["ok"], "lines": sent["lines"]})
return await render_template("alerts/_test_result.html", ok=sent["ok"], lines=sent["lines"])
@alerts_bp.post("/rules/<rule_id>/delete")
@require_role(UserRole.admin)
async def delete_rule(rule_id: str):
+53
View File
@@ -602,6 +602,59 @@ def part_of(rule: AlertRule, rules) -> AlertRule | None:
return next((r for r in rules if r.id == rule.parent_id), None) if rule.parent_id else None
async def test_alert(db, rule: AlertRule) -> dict:
"""What a Send test carries for `rule` (#5466): a TEST alert with the
rule's name and condition, on its resource at the latest reading. A rule
covering hosts tests on its first host."""
target = rule
if rule.covers != "resource":
target = (await db.execute(
select(AlertRule).where(AlertRule.parent_id == rule.id)
.order_by(AlertRule.resource_name).limit(1))).scalar_one_or_none() or rule
return {"rule_name": rule.name, "state": "TEST", "metric": rule.metric_name,
"value": await latest_value(db, target) if target.resource_name else None,
"operator": rule.operator.value, "threshold": rule.threshold,
"resource": target.resource_name or covers_text(rule),
"source_module": rule.source_module,
"note": "A test of this rule's notifications. Nothing has fired.",
"timestamp": datetime.now(timezone.utc).isoformat()}
async def send_test(app, rule_id: str) -> dict:
"""Send a TEST notification for a rule now, by every channel a real alert
of it would use: email to whoever gets alerts, each in their own zone, and
the webhook. Not held for a burst, and not recorded in the rule's history.
Returns {"rule", "ok", "lines"}: ok when something was sent and nothing
failed; lines say what happened to each channel. LookupError for no rule.
"""
from urllib.parse import urlsplit
from steward.core.mail import recipients
from steward.core.notifications import dispatch_notifications
smtp, webhook = app.config.get("SMTP", {}), app.config.get("WEBHOOK", {})
async with app.db_sessionmaker() as db:
rule = await db.get(AlertRule, rule_id)
if rule is None:
raise LookupError(rule_id)
alert = await test_alert(db, rule)
to = await recipients(db, "alerts", smtp) if smtp.get("host") else []
results = await dispatch_notifications(smtp, webhook, alert, to)
lines = []
if email := results.get("email"):
who = ", ".join(r.address for r in to)
lines.append(f"Email sent to {who}." if email["sent"]
else f"Email to {who} failed: {email['error']}")
if hook := results.get("webhook"):
host = urlsplit(webhook["url"]).hostname or webhook["url"]
lines.append(f"Webhook to {host} delivered." if hook["sent"]
else f"Webhook to {host} failed: {hook['error']}")
if not lines:
lines.append("Nothing is set up to send to: no email server with someone to send to, "
"and no webhook.")
return {"rule": rule.name, "ok": bool(results) and all(r["sent"] for r in results.values()),
"lines": lines, "email": results.get("email"), "webhook": results.get("webhook")}
class PartOfCoveringRule(ValueError):
"""A child of a covering rule is changed through its parent, never alone."""
+1
View File
@@ -149,6 +149,7 @@ ACTIONS = {
"alert_rule.disabled": "turned off alert rule",
"alert_rule.deleted": "deleted alert rule",
"alert_rule.acknowledged": "acknowledged the alert from",
"alert_rule.tested": "sent a test alert for rule",
"alert_rule.action_set": "set the playbook run by alert rule",
"alert_rule.action_cleared": "removed the playbook from alert rule",
"maintenance_window.created": "scheduled maintenance",
+2
View File
@@ -222,6 +222,8 @@ def alert_facts(alert: dict, tz: tzinfo, show_utc: bool = True) -> dict:
elif state == "resolved":
headline = f"{metric_label(metric)} on {resource} is back within its limit" + (
f", at {reading}." if reading else ".")
elif reading is None:
headline = f"{metric_label(metric)} on {resource} has no reading yet."
else:
headline = f"{metric_label(metric)} on {resource} is {reading}."
at = datetime.fromisoformat(alert["timestamp"])
+21 -2
View File
@@ -6,7 +6,7 @@ from datetime import datetime, timedelta, timezone
from sqlalchemy import select
from steward.alerts import service as alert_svc
from steward.mcp._context import current_caller
from steward.mcp._context import current_caller, mcp_app
from steward.models.alerts import AlertOperator, AlertRule, AlertState, MaintenanceWindow
from steward.models.users import UserRole
@@ -391,6 +391,25 @@ async def acknowledge_alert(rule: str) -> dict:
"acknowledged_at": iso(state.acknowledged_at)}
async def send_test_alert(rule: str) -> dict:
"""Send a TEST notification for a rule now, to check it reaches someone:
email to everyone who gets alerts (each in their own zone) and the
webhook, the same channels a real alert of this rule would use. Nothing
fires and the rule's history is unchanged.
rule: the rule's name or id (list_alert_rules). Returns `ok` (something
was sent and nothing failed) and one line per channel saying what
happened. Recorded in the audit log.
"""
async with db_session() as db:
r = await _rule(db, rule)
sent = await alert_svc.send_test(mcp_app(), r.id)
await audit_mcp("alert_rule.tested", "alert_rule", r.name,
{"rule_id": r.id, "ok": sent["ok"], "lines": sent["lines"]})
return {"rule_id": r.id, "rule_name": r.name, "ok": sent["ok"], "result": sent["lines"],
"email": sent["email"], "webhook": sent["webhook"]}
def _when(value: str, what: str) -> datetime:
try:
dt = datetime.fromisoformat(value.strip().replace("Z", "+00:00"))
@@ -467,7 +486,7 @@ def register(register_tool) -> None:
for fn in (list_alerts, get_alert_history, list_alert_rules, list_maintenance_windows,
get_alert_rule_options):
register_tool(fn, role=UserRole.viewer, read_only=True)
for fn in (create_alert_rule, update_alert_rule, acknowledge_alert,
for fn in (create_alert_rule, update_alert_rule, acknowledge_alert, send_test_alert,
create_maintenance_window, end_maintenance_window):
register_tool(fn, role=UserRole.operator, read_only=False)
register_tool(delete_alert_rule, role=UserRole.admin, read_only=False)
@@ -0,0 +1,4 @@
{# alerts/_test_result.html — what a rule's Send test did (#5466), one line per channel #}
<div class="alert {% if ok %}alert-success{% else %}alert-error{% endif %}" style="margin:0.75rem 0 0;">
{% for line in lines %}<div>{{ line }}</div>{% endfor %}
</div>
+9
View File
@@ -141,6 +141,15 @@
{%- else %}<span class="tone-warn">It notifies nobody yet:</span> no email server with recipients and no webhook is set
{%- if session.user_role == 'admin' %} (<a href="/settings/notifications/">Settings → Notifications</a>){% endif %}.{% endif -%}
</p>
{% if rule %}
<div class="form-group">
<button type="button" class="btn btn-sm btn-ghost" hx-post="/alerts/rules/{{ rule.id }}/test"
hx-target="#rule-test-result" hx-swap="innerHTML">Send test</button>
<p class="field-hint">Sends a TEST alert for this rule as saved, now, to everywhere above.
Nothing fires, and the rule's history doesn't change.</p>
<div id="rule-test-result" aria-live="polite"></div>
</div>
{% endif %}
<div class="form-group">
<label style="display:flex;align-items:center;gap:0.5rem;font-weight:normal;">
<input type="checkbox" name="notify_no_data"