feat(healing): add post-restart verification and heal outcome telemetry

This commit is contained in:
patrick committed 2026-02-10 21:55:43 -05:00
1 parent b2c489602a
commit 234224a5c6
3 files changed
+46 -2

No files matched your search

+39 -1
View File
@@ -74,6 +74,7 @@ def main():
node_name = os.getenv("SERVER_NAME", "Unknown Node")
CHECK_INTERVAL = 600
HEAL_VERIFY_DELAY = 5
print(f"[*] Agent started. Check interval: {CHECK_INTERVAL}s")
logging.info(f"Threat Engine Agent initialized on {node_name}.")
@@ -103,7 +104,42 @@ def main():
if should_heal(history_file, drift.get("fingerprint", "")):
logging.warning("Healing approved.")
auditor.heal()
heal_ok = auditor.heal()
heal_result = "restart_failed"
post_drift = {
"reason": "not_verified",
"missing_count": 0,
"extra_count": 0,
"metadata_drift_count": 0,
"fingerprint": "",
}
if heal_ok:
logging.info(
f"Post-heal verification in {HEAL_VERIFY_DELAY}s "
f"(pre_fingerprint={drift.get('fingerprint', '')})."
)
time.sleep(HEAL_VERIFY_DELAY)
post_is_synced, post_count, post_drift = auditor.reconcile()
heal_result = "success" if post_is_synced else "failed"
verify_error = None if post_is_synced else "Post-heal verification failed"
reporter.update_status(post_is_synced, post_count, last_error=verify_error)
logging.warning(
"Post-heal verification result "
f"result={heal_result} pre={drift.get('fingerprint', '')} "
f"post={post_drift.get('fingerprint', '')} "
f"missing={post_drift.get('missing_count', 0)} "
f"extra={post_drift.get('extra_count', 0)} "
f"metadata={post_drift.get('metadata_drift_count', 0)}"
)
else:
reporter.update_status(False, count, last_error="Bouncer restart failed")
logging.error(
f"Heal restart failed; skipping verification "
f"(pre_fingerprint={drift.get('fingerprint', '')})."
)
reporter.log_heal_event(
count,
drift.get("missing_count", 0),
@@ -111,6 +147,8 @@ def main():
drift.get("metadata_drift_count", 0),
drift.get("fingerprint", ""),
drift.get("reason", "unknown"),
heal_result,
post_drift.get("fingerprint", ""),
)
else:
logging.warning("Healing skipped to prevent flapping.")
+2
View File
@@ -121,8 +121,10 @@ class FirewallAuditor:
try:
subprocess.run(["systemctl", "restart", "crowdsec-firewall-bouncer"], check=True)
logging.info("Heal: Bouncer restarted successfully.")
return True
except subprocess.CalledProcessError as e:
logging.error(f"Heal Failed: Could not restart bouncer: {e}")
return False
def reconcile(self):
truth_map = self.get_remote_lapi_truth()
+5 -1
View File
@@ -45,6 +45,8 @@ class LocalStateReporter:
metadata_drift_count,
drift_fingerprint,
reason,
heal_result,
post_drift_fingerprint,
):
"""
Records a specific healing event in a persistent history log.
@@ -58,7 +60,9 @@ class LocalStateReporter:
f"{timestamp} - HEAL: reason={reason} "
f"kernel={kernel_count} missing={missing_count} "
f"extra={extra_count} metadata={metadata_drift_count} "
f"drift_fingerprint={drift_fingerprint}\n"
f"drift_fingerprint={drift_fingerprint} "
f"heal_result={heal_result} "
f"post_drift_fingerprint={post_drift_fingerprint}\n"
)
except Exception as e:
logging.error(f"Reporter: Failed to write to history log: {e}")