feat(reporter): add drift event and heal-attempt counters to state snapshot

This commit is contained in:
patrick committed 2026-03-05 21:28:25 -05:00
1 parent a2030a71b7
commit 8c7b0768b0
4 files changed
+29

No files matched your search

+1
View File
@@ -31,6 +31,7 @@ The standard `crowdsec-firewall-bouncer` can occasionally experience "ghost" des
- Writes local health state JSON atomically.
- Persists structured heal history, including pre/post fingerprints and heal result.
- Exposes lightweight node metrics in state (`uptime_seconds`, `checks_total`, `heals_total`, `heal_failures_total`).
- Tracks drift activity counters (`drift_events_total`, `drift_heal_attempts_total`) for suppression/load visibility.
- Default state path: `/root/threat-engine-agent/threat-agent-state.json`.
## 📂 Project Structure
+1
View File
@@ -97,6 +97,7 @@ def main():
metadata_drift_count=drift.get("metadata_drift_count", 0),
last_drift_fingerprint=drift.get("fingerprint", ""),
checks_increment=1,
drift_events_increment=0 if is_synced else 1,
)
if is_synced:
+9
View File
@@ -48,6 +48,7 @@ class LocalStateReporter:
last_heal_result=None,
last_heal_timestamp=None,
checks_increment=0,
drift_events_increment=0,
heal_result=None,
):
"""
@@ -71,11 +72,17 @@ class LocalStateReporter:
checks_total = max(0, self._get_int(previous, "checks_total", 0))
checks_total += max(0, int(checks_increment))
drift_events_total = max(0, self._get_int(previous, "drift_events_total", 0))
drift_events_total += max(0, int(drift_events_increment))
heals_total = max(0, self._get_int(previous, "heals_total", 0))
heal_failures_total = max(0, self._get_int(previous, "heal_failures_total", 0))
drift_heal_attempts_total = max(
0, self._get_int(previous, "drift_heal_attempts_total", 0)
)
if heal_result is not None:
heals_total += 1
drift_heal_attempts_total += 1
if heal_result != "success":
heal_failures_total += 1
@@ -95,6 +102,8 @@ class LocalStateReporter:
"last_heal_timestamp": effective_last_heal_timestamp,
"uptime_seconds": uptime_seconds,
"checks_total": checks_total,
"drift_events_total": drift_events_total,
"drift_heal_attempts_total": drift_heal_attempts_total,
"heals_total": heals_total,
"heal_failures_total": heal_failures_total,
}
+18
View File
@@ -69,6 +69,7 @@ class ReporterStateTests(unittest.TestCase):
reporter.update_status(True, 1, checks_increment=1)
reporter.update_status(True, 1, checks_increment=1)
reporter.update_status(False, 1, drift_events_increment=1)
reporter.update_status(False, 1, heal_result="failed")
reporter.update_status(True, 1, heal_result="success")
@@ -76,6 +77,8 @@ class ReporterStateTests(unittest.TestCase):
payload = json.load(f)
self.assertEqual(2, payload["checks_total"])
self.assertEqual(1, payload["drift_events_total"])
self.assertEqual(2, payload["drift_heal_attempts_total"])
self.assertEqual(2, payload["heals_total"])
self.assertEqual(1, payload["heal_failures_total"])
self.assertEqual("success", payload["last_heal_result"])
@@ -83,6 +86,21 @@ class ReporterStateTests(unittest.TestCase):
datetime.fromisoformat(payload["last_heal_timestamp"])
self.assertGreaterEqual(payload["uptime_seconds"], 0)
def test_update_status_can_show_suppressed_heal_gap(self):
with tempfile.TemporaryDirectory() as tmp_dir:
state_file = os.path.join(tmp_dir, "state.json")
reporter = LocalStateReporter(state_file=state_file, node_name="n1")
reporter.update_status(False, 5, checks_increment=1, drift_events_increment=1)
reporter.update_status(False, 5, checks_increment=1, drift_events_increment=1)
reporter.update_status(False, 5, heal_result="failed")
with open(state_file, "r") as f:
payload = json.load(f)
self.assertEqual(2, payload["drift_events_total"])
self.assertEqual(1, payload["drift_heal_attempts_total"])
def test_update_status_preserves_last_heal_timestamp_when_no_new_heal(self):
with tempfile.TemporaryDirectory() as tmp_dir:
state_file = os.path.join(tmp_dir, "state.json")