feat(reporter): add uptime and fleet counters to state snapshot
This commit is contained in:
1 parent
7f323ed78b
commit
3b1e25d5cd
6 files changed
+55
-6
No files matched your search
@@ -30,6 +30,7 @@ The standard `crowdsec-firewall-bouncer` can occasionally experience "ghost" des
|
||||
### The Reporter (`reporter.py`)
|
||||
- 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`).
|
||||
- Default state path: `/root/threat-engine-agent/threat-agent-state.json`.
|
||||
|
||||
## 📂 Project Structure
|
||||
|
||||
+3
-3
@@ -118,7 +118,7 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
||||
extra_count=post_drift.get("extra_count", 0),
|
||||
metadata_drift_count=post_drift.get("metadata_drift_count", 0),
|
||||
last_drift_fingerprint=post_drift.get("fingerprint", ""),
|
||||
last_heal_result=heal_result,
|
||||
heal_result=heal_result,
|
||||
)
|
||||
|
||||
logging.warning(
|
||||
@@ -139,7 +139,7 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
||||
extra_count=0,
|
||||
metadata_drift_count=0,
|
||||
last_drift_fingerprint=drift.get("fingerprint", ""),
|
||||
last_heal_result=heal_result,
|
||||
heal_result=heal_result,
|
||||
)
|
||||
post_drift = {
|
||||
"reason": "verify_error",
|
||||
@@ -161,7 +161,7 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
||||
extra_count=drift.get("extra_count", 0),
|
||||
metadata_drift_count=drift.get("metadata_drift_count", 0),
|
||||
last_drift_fingerprint=drift.get("fingerprint", ""),
|
||||
last_heal_result=heal_result,
|
||||
heal_result=heal_result,
|
||||
)
|
||||
logging.error(
|
||||
f"Heal restart failed; skipping verification "
|
||||
|
||||
@@ -96,6 +96,7 @@ def main():
|
||||
extra_count=drift.get("extra_count", 0),
|
||||
metadata_drift_count=drift.get("metadata_drift_count", 0),
|
||||
last_drift_fingerprint=drift.get("fingerprint", ""),
|
||||
checks_increment=1,
|
||||
)
|
||||
|
||||
if is_synced:
|
||||
|
||||
+28
@@ -3,6 +3,7 @@
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
class LocalStateReporter:
|
||||
@@ -17,6 +18,7 @@ class LocalStateReporter:
|
||||
):
|
||||
self.state_file = state_file
|
||||
self.node_name = node_name
|
||||
self.started_at_epoch = time.time()
|
||||
# Ensure the directory is writable
|
||||
os.makedirs(os.path.dirname(os.path.abspath(self.state_file)), exist_ok=True)
|
||||
|
||||
@@ -28,6 +30,12 @@ class LocalStateReporter:
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
def _get_int(self, payload, key, default=0):
|
||||
try:
|
||||
return int(payload.get(key, default))
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
def update_status(
|
||||
self,
|
||||
is_synced,
|
||||
@@ -38,6 +46,8 @@ class LocalStateReporter:
|
||||
metadata_drift_count=0,
|
||||
last_drift_fingerprint="",
|
||||
last_heal_result=None,
|
||||
checks_increment=0,
|
||||
heal_result=None,
|
||||
):
|
||||
"""
|
||||
Writes the current health snapshot to a JSON file.
|
||||
@@ -48,6 +58,20 @@ class LocalStateReporter:
|
||||
if last_heal_result is not None
|
||||
else previous.get("last_heal_result")
|
||||
)
|
||||
if heal_result is not None:
|
||||
effective_heal_result = heal_result
|
||||
|
||||
checks_total = max(0, self._get_int(previous, "checks_total", 0))
|
||||
checks_total += max(0, int(checks_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))
|
||||
if heal_result is not None:
|
||||
heals_total += 1
|
||||
if heal_result != "success":
|
||||
heal_failures_total += 1
|
||||
|
||||
uptime_seconds = max(0, int(time.time() - self.started_at_epoch))
|
||||
status_data = {
|
||||
"node_name": self.node_name,
|
||||
"last_check": datetime.now().isoformat(),
|
||||
@@ -60,6 +84,10 @@ class LocalStateReporter:
|
||||
"metadata_drift_count": metadata_drift_count,
|
||||
"last_drift_fingerprint": last_drift_fingerprint,
|
||||
"last_heal_result": effective_heal_result,
|
||||
"uptime_seconds": uptime_seconds,
|
||||
"checks_total": checks_total,
|
||||
"heals_total": heals_total,
|
||||
"heal_failures_total": heal_failures_total,
|
||||
}
|
||||
|
||||
try:
|
||||
|
||||
+3
-3
@@ -101,7 +101,7 @@ class PostHealVerificationTests(unittest.TestCase):
|
||||
self.assertEqual(1, len(reporter.update_calls))
|
||||
self.assertTrue(reporter.update_calls[0]["is_synced"])
|
||||
self.assertEqual(12, reporter.update_calls[0]["ban_count"])
|
||||
self.assertEqual("success", reporter.update_calls[0]["last_heal_result"])
|
||||
self.assertEqual("success", reporter.update_calls[0]["heal_result"])
|
||||
self.assertEqual("", reporter.update_calls[0]["last_drift_fingerprint"])
|
||||
self.assertEqual({"fingerprint": "pre123"}, auditor.last_heal_drift)
|
||||
|
||||
@@ -124,7 +124,7 @@ class PostHealVerificationTests(unittest.TestCase):
|
||||
self.assertFalse(reporter.update_calls[0]["is_synced"])
|
||||
self.assertEqual(7, reporter.update_calls[0]["ban_count"])
|
||||
self.assertEqual("Bouncer restart failed", reporter.update_calls[0]["last_error"])
|
||||
self.assertEqual("restart_failed", reporter.update_calls[0]["last_heal_result"])
|
||||
self.assertEqual("restart_failed", reporter.update_calls[0]["heal_result"])
|
||||
|
||||
@patch("healing.time.sleep", return_value=None)
|
||||
def test_execute_heal_and_verify_verify_exception(self, _sleep):
|
||||
@@ -145,7 +145,7 @@ class PostHealVerificationTests(unittest.TestCase):
|
||||
self.assertFalse(reporter.update_calls[0]["is_synced"])
|
||||
self.assertEqual(7, reporter.update_calls[0]["ban_count"])
|
||||
self.assertIn("Post-heal verify error: boom", reporter.update_calls[0]["last_error"])
|
||||
self.assertEqual("verify_error", reporter.update_calls[0]["last_heal_result"])
|
||||
self.assertEqual("verify_error", reporter.update_calls[0]["heal_result"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -61,6 +61,25 @@ class ReporterStateTests(unittest.TestCase):
|
||||
self.assertEqual("success", payload["last_heal_result"])
|
||||
self.assertEqual("", payload["last_drift_fingerprint"])
|
||||
|
||||
def test_update_status_tracks_checks_and_heal_counters(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(True, 1, checks_increment=1)
|
||||
reporter.update_status(True, 1, checks_increment=1)
|
||||
reporter.update_status(False, 1, heal_result="failed")
|
||||
reporter.update_status(True, 1, heal_result="success")
|
||||
|
||||
with open(state_file, "r") as f:
|
||||
payload = json.load(f)
|
||||
|
||||
self.assertEqual(2, payload["checks_total"])
|
||||
self.assertEqual(2, payload["heals_total"])
|
||||
self.assertEqual(1, payload["heal_failures_total"])
|
||||
self.assertEqual("success", payload["last_heal_result"])
|
||||
self.assertGreaterEqual(payload["uptime_seconds"], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user