feat(healing): add post-restart verification and heal outcome telemetry
This commit is contained in:
1 parent
b2c489602a
commit
234224a5c6
3 files changed
+46
-2
No files matched your search
@@ -74,6 +74,7 @@ def main():
|
|||||||
node_name = os.getenv("SERVER_NAME", "Unknown Node")
|
node_name = os.getenv("SERVER_NAME", "Unknown Node")
|
||||||
|
|
||||||
CHECK_INTERVAL = 600
|
CHECK_INTERVAL = 600
|
||||||
|
HEAL_VERIFY_DELAY = 5
|
||||||
print(f"[*] Agent started. Check interval: {CHECK_INTERVAL}s")
|
print(f"[*] Agent started. Check interval: {CHECK_INTERVAL}s")
|
||||||
logging.info(f"Threat Engine Agent initialized on {node_name}.")
|
logging.info(f"Threat Engine Agent initialized on {node_name}.")
|
||||||
|
|
||||||
@@ -103,7 +104,42 @@ def main():
|
|||||||
|
|
||||||
if should_heal(history_file, drift.get("fingerprint", "")):
|
if should_heal(history_file, drift.get("fingerprint", "")):
|
||||||
logging.warning("Healing approved.")
|
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(
|
reporter.log_heal_event(
|
||||||
count,
|
count,
|
||||||
drift.get("missing_count", 0),
|
drift.get("missing_count", 0),
|
||||||
@@ -111,6 +147,8 @@ def main():
|
|||||||
drift.get("metadata_drift_count", 0),
|
drift.get("metadata_drift_count", 0),
|
||||||
drift.get("fingerprint", ""),
|
drift.get("fingerprint", ""),
|
||||||
drift.get("reason", "unknown"),
|
drift.get("reason", "unknown"),
|
||||||
|
heal_result,
|
||||||
|
post_drift.get("fingerprint", ""),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
logging.warning("Healing skipped to prevent flapping.")
|
logging.warning("Healing skipped to prevent flapping.")
|
||||||
|
|||||||
@@ -121,8 +121,10 @@ class FirewallAuditor:
|
|||||||
try:
|
try:
|
||||||
subprocess.run(["systemctl", "restart", "crowdsec-firewall-bouncer"], check=True)
|
subprocess.run(["systemctl", "restart", "crowdsec-firewall-bouncer"], check=True)
|
||||||
logging.info("Heal: Bouncer restarted successfully.")
|
logging.info("Heal: Bouncer restarted successfully.")
|
||||||
|
return True
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
logging.error(f"Heal Failed: Could not restart bouncer: {e}")
|
logging.error(f"Heal Failed: Could not restart bouncer: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
def reconcile(self):
|
def reconcile(self):
|
||||||
truth_map = self.get_remote_lapi_truth()
|
truth_map = self.get_remote_lapi_truth()
|
||||||
|
|||||||
+5
-1
@@ -45,6 +45,8 @@ class LocalStateReporter:
|
|||||||
metadata_drift_count,
|
metadata_drift_count,
|
||||||
drift_fingerprint,
|
drift_fingerprint,
|
||||||
reason,
|
reason,
|
||||||
|
heal_result,
|
||||||
|
post_drift_fingerprint,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Records a specific healing event in a persistent history log.
|
Records a specific healing event in a persistent history log.
|
||||||
@@ -58,7 +60,9 @@ class LocalStateReporter:
|
|||||||
f"{timestamp} - HEAL: reason={reason} "
|
f"{timestamp} - HEAL: reason={reason} "
|
||||||
f"kernel={kernel_count} missing={missing_count} "
|
f"kernel={kernel_count} missing={missing_count} "
|
||||||
f"extra={extra_count} metadata={metadata_drift_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:
|
except Exception as e:
|
||||||
logging.error(f"Reporter: Failed to write to history log: {e}")
|
logging.error(f"Reporter: Failed to write to history log: {e}")
|
||||||
Reference in new issue
Block a user