feat(alerts): silence is alertable — agent offline and no-data (#5062)
Agent offline is an ordinary rule now. The host_agent scheduler records host_agent/<host>/seconds_since_seen every minute (from last_seen_at, or registration time for an agent that never reported), replacing the task that only logged stale agents. The catalog offers the metric, and picking it on a new rule fills "> 180". No data: each rule's state tracks its last sample and its usual gap, smoothed so that an outage counts as at most three gaps. A core task, alert_silence_check, runs every 60s and marks a rule no_data_since once its series has had no sample for three usual gaps (at least 5 minutes; an hour while the cadence is unknown). A never-sampled rule counts from when it started watching. The mark sits beside the state, so a firing alert that goes silent stays firing. Each silence records one event and sends one notification (rule option notify_no_data, default on; muted in maintenance windows). The next sample clears it with a "data resumed" event and notification. Creating, changing, disabling or re-enabling a rule restarts its watch. Migration 0029 adds the columns and seeds each existing rule's last sample and gap from its last day of data, so an upgrade does not flag every rule. Shown on the Alerts page (a NO DATA badge with its start time), in MCP list_alerts, list_alert_rules and get_host (state "no_data", no_data_since), in get_overview findings (alert_no_data, or a firing alert marked silent) and in the weekly report. The rule form has the notify option, and its metric reference now matches what traefik records and lists host_agent. Emails say "no data" and carry the event's note. The metric-catalog guard also reads positional MetricBatch.add arguments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,48 +1,41 @@
|
||||
"""Pure-function tests for host_agent staleness filter."""
|
||||
"""Pure-function tests for the host_agent seconds_since_seen reconciler."""
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from plugins.host_agent.scheduler import _filter_stale
|
||||
from plugins.host_agent.scheduler import seconds_since_seen
|
||||
|
||||
|
||||
@dataclass
|
||||
class FakeReg:
|
||||
host_id: str
|
||||
last_seen_at: datetime | None
|
||||
created_at: datetime | None
|
||||
|
||||
|
||||
NOW = datetime(2026, 4, 15, 12, 0, 0, tzinfo=timezone.utc)
|
||||
NAMES = {"h1": "nas", "h2": "web", "h3": "db"}
|
||||
|
||||
|
||||
def test_filter_stale_returns_only_old_rows():
|
||||
regs = [
|
||||
FakeReg("fresh", NOW - timedelta(seconds=30)),
|
||||
FakeReg("stale", NOW - timedelta(seconds=600)),
|
||||
FakeReg("never", None),
|
||||
]
|
||||
stale = _filter_stale(regs, now=NOW, stale_after_seconds=180)
|
||||
ids = {r.host_id for r in stale}
|
||||
assert ids == {"stale"}
|
||||
def test_seconds_since_each_agent_last_reported():
|
||||
regs = [FakeReg("h1", NOW - timedelta(seconds=30), NOW - timedelta(days=9)),
|
||||
FakeReg("h2", NOW - timedelta(minutes=10), NOW - timedelta(days=9))]
|
||||
assert seconds_since_seen(regs, NAMES, NOW) == [("nas", 30.0), ("web", 600.0)]
|
||||
|
||||
|
||||
def test_filter_stale_empty_input():
|
||||
assert _filter_stale([], now=NOW, stale_after_seconds=180) == []
|
||||
def test_an_agent_that_never_reported_counts_from_registration():
|
||||
regs = [FakeReg("h3", None, NOW - timedelta(minutes=5))]
|
||||
assert seconds_since_seen(regs, NAMES, NOW) == [("db", 300.0)]
|
||||
|
||||
|
||||
def test_filter_stale_all_fresh():
|
||||
regs = [
|
||||
FakeReg("a", NOW - timedelta(seconds=10)),
|
||||
FakeReg("b", NOW - timedelta(seconds=30)),
|
||||
]
|
||||
assert _filter_stale(regs, now=NOW, stale_after_seconds=60) == []
|
||||
def test_a_registration_without_its_host_is_skipped():
|
||||
regs = [FakeReg("gone", NOW, NOW)]
|
||||
assert seconds_since_seen(regs, NAMES, NOW) == []
|
||||
|
||||
|
||||
def test_filter_stale_boundary_exact_cutoff_is_not_stale():
|
||||
# A row exactly at the cutoff should not be flagged (strict less-than).
|
||||
regs = [FakeReg("edge", NOW - timedelta(seconds=180))]
|
||||
assert _filter_stale(regs, now=NOW, stale_after_seconds=180) == []
|
||||
def test_a_clock_ahead_of_ours_reads_as_just_seen():
|
||||
regs = [FakeReg("h1", NOW + timedelta(seconds=5), NOW)]
|
||||
assert seconds_since_seen(regs, NAMES, NOW) == [("nas", 0.0)]
|
||||
|
||||
|
||||
def test_filter_stale_skips_none_last_seen():
|
||||
regs = [FakeReg("never", None)]
|
||||
assert _filter_stale(regs, now=NOW, stale_after_seconds=1) == []
|
||||
def test_empty_input():
|
||||
assert seconds_since_seen([], NAMES, NOW) == []
|
||||
|
||||
Reference in New Issue
Block a user