feat(reporter): persist drift metrics and last heal result in state snapshot
This commit is contained in:
1 parent
c698d01c40
commit
b34ac69ac1
5 files changed
+160
-14
No files matched your search
+32
-4
@@ -86,8 +86,9 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
|||||||
"""
|
"""
|
||||||
Executes heal + post-heal verification and persists reporter state.
|
Executes heal + post-heal verification and persists reporter state.
|
||||||
"""
|
"""
|
||||||
|
drift = drift or {}
|
||||||
if hasattr(auditor, "heal_with_drift"):
|
if hasattr(auditor, "heal_with_drift"):
|
||||||
heal_ok = auditor.heal_with_drift(drift or {})
|
heal_ok = auditor.heal_with_drift(drift)
|
||||||
else:
|
else:
|
||||||
heal_ok = auditor.heal()
|
heal_ok = auditor.heal()
|
||||||
heal_result = "restart_failed"
|
heal_result = "restart_failed"
|
||||||
@@ -109,7 +110,16 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
|||||||
post_is_synced, post_count, post_drift = auditor.reconcile()
|
post_is_synced, post_count, post_drift = auditor.reconcile()
|
||||||
heal_result = "success" if post_is_synced else "failed"
|
heal_result = "success" if post_is_synced else "failed"
|
||||||
verify_error = None if post_is_synced else "Post-heal verification 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)
|
reporter.update_status(
|
||||||
|
post_is_synced,
|
||||||
|
post_count,
|
||||||
|
last_error=verify_error,
|
||||||
|
missing_count=post_drift.get("missing_count", 0),
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
logging.warning(
|
logging.warning(
|
||||||
"Post-heal verification result "
|
"Post-heal verification result "
|
||||||
@@ -121,7 +131,16 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
heal_result = "verify_error"
|
heal_result = "verify_error"
|
||||||
reporter.update_status(False, count, last_error=f"Post-heal verify error: {e}")
|
reporter.update_status(
|
||||||
|
False,
|
||||||
|
count,
|
||||||
|
last_error=f"Post-heal verify error: {e}",
|
||||||
|
missing_count=0,
|
||||||
|
extra_count=0,
|
||||||
|
metadata_drift_count=0,
|
||||||
|
last_drift_fingerprint=drift.get("fingerprint", ""),
|
||||||
|
last_heal_result=heal_result,
|
||||||
|
)
|
||||||
post_drift = {
|
post_drift = {
|
||||||
"reason": "verify_error",
|
"reason": "verify_error",
|
||||||
"missing_count": 0,
|
"missing_count": 0,
|
||||||
@@ -134,7 +153,16 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
|||||||
f"(pre_fingerprint={drift.get('fingerprint', '')}): {e}"
|
f"(pre_fingerprint={drift.get('fingerprint', '')}): {e}"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
reporter.update_status(False, count, last_error="Bouncer restart failed")
|
reporter.update_status(
|
||||||
|
False,
|
||||||
|
count,
|
||||||
|
last_error="Bouncer restart failed",
|
||||||
|
missing_count=drift.get("missing_count", 0),
|
||||||
|
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,
|
||||||
|
)
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Heal restart failed; skipping verification "
|
f"Heal restart failed; skipping verification "
|
||||||
f"(pre_fingerprint={drift.get('fingerprint', '')})."
|
f"(pre_fingerprint={drift.get('fingerprint', '')})."
|
||||||
|
|||||||
@@ -51,7 +51,14 @@ def main():
|
|||||||
while not stop_event.is_set():
|
while not stop_event.is_set():
|
||||||
try:
|
try:
|
||||||
is_synced, count, drift = auditor.reconcile()
|
is_synced, count, drift = auditor.reconcile()
|
||||||
reporter.update_status(is_synced, count)
|
reporter.update_status(
|
||||||
|
is_synced,
|
||||||
|
count,
|
||||||
|
missing_count=drift.get("missing_count", 0),
|
||||||
|
extra_count=drift.get("extra_count", 0),
|
||||||
|
metadata_drift_count=drift.get("metadata_drift_count", 0),
|
||||||
|
last_drift_fingerprint=drift.get("fingerprint", ""),
|
||||||
|
)
|
||||||
|
|
||||||
if is_synced:
|
if is_synced:
|
||||||
status_msg = f"Audit Success: {count} IPs in sync."
|
status_msg = f"Audit Success: {count} IPs in sync."
|
||||||
|
|||||||
+31
-2
@@ -20,17 +20,46 @@ class LocalStateReporter:
|
|||||||
# Ensure the directory is writable
|
# Ensure the directory is writable
|
||||||
os.makedirs(os.path.dirname(os.path.abspath(self.state_file)), exist_ok=True)
|
os.makedirs(os.path.dirname(os.path.abspath(self.state_file)), exist_ok=True)
|
||||||
|
|
||||||
def update_status(self, is_synced, ban_count, last_error=None):
|
def _load_previous_state(self):
|
||||||
|
try:
|
||||||
|
with open(self.state_file, "r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
return data if isinstance(data, dict) else {}
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def update_status(
|
||||||
|
self,
|
||||||
|
is_synced,
|
||||||
|
ban_count,
|
||||||
|
last_error=None,
|
||||||
|
missing_count=0,
|
||||||
|
extra_count=0,
|
||||||
|
metadata_drift_count=0,
|
||||||
|
last_drift_fingerprint="",
|
||||||
|
last_heal_result=None,
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Writes the current health snapshot to a JSON file.
|
Writes the current health snapshot to a JSON file.
|
||||||
"""
|
"""
|
||||||
|
previous = self._load_previous_state()
|
||||||
|
effective_heal_result = (
|
||||||
|
last_heal_result
|
||||||
|
if last_heal_result is not None
|
||||||
|
else previous.get("last_heal_result")
|
||||||
|
)
|
||||||
status_data = {
|
status_data = {
|
||||||
"node_name": self.node_name,
|
"node_name": self.node_name,
|
||||||
"last_check": datetime.now().isoformat(),
|
"last_check": datetime.now().isoformat(),
|
||||||
"status": "HEALTHY" if is_synced else "DESYNC_DETECTED",
|
"status": "HEALTHY" if is_synced else "DESYNC_DETECTED",
|
||||||
"is_synced": is_synced,
|
"is_synced": is_synced,
|
||||||
"kernel_ban_count": ban_count,
|
"kernel_ban_count": ban_count,
|
||||||
"last_error": last_error
|
"last_error": last_error,
|
||||||
|
"missing_count": missing_count,
|
||||||
|
"extra_count": extra_count,
|
||||||
|
"metadata_drift_count": metadata_drift_count,
|
||||||
|
"last_drift_fingerprint": last_drift_fingerprint,
|
||||||
|
"last_heal_result": effective_heal_result,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
+23
-7
@@ -10,8 +10,15 @@ class FakeReporter:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.update_calls = []
|
self.update_calls = []
|
||||||
|
|
||||||
def update_status(self, is_synced, ban_count, last_error=None):
|
def update_status(self, is_synced, ban_count, last_error=None, **kwargs):
|
||||||
self.update_calls.append((is_synced, ban_count, last_error))
|
self.update_calls.append(
|
||||||
|
{
|
||||||
|
"is_synced": is_synced,
|
||||||
|
"ban_count": ban_count,
|
||||||
|
"last_error": last_error,
|
||||||
|
**kwargs,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class FakeAuditor:
|
class FakeAuditor:
|
||||||
@@ -91,7 +98,11 @@ class PostHealVerificationTests(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual("success", heal_result)
|
self.assertEqual("success", heal_result)
|
||||||
self.assertEqual(post_drift, returned_post_drift)
|
self.assertEqual(post_drift, returned_post_drift)
|
||||||
self.assertEqual([(True, 12, None)], reporter.update_calls)
|
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("", reporter.update_calls[0]["last_drift_fingerprint"])
|
||||||
self.assertEqual({"fingerprint": "pre123"}, auditor.last_heal_drift)
|
self.assertEqual({"fingerprint": "pre123"}, auditor.last_heal_drift)
|
||||||
|
|
||||||
@patch("healing.time.sleep", return_value=None)
|
@patch("healing.time.sleep", return_value=None)
|
||||||
@@ -109,7 +120,11 @@ class PostHealVerificationTests(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual("restart_failed", heal_result)
|
self.assertEqual("restart_failed", heal_result)
|
||||||
self.assertEqual("not_verified", returned_post_drift["reason"])
|
self.assertEqual("not_verified", returned_post_drift["reason"])
|
||||||
self.assertEqual([(False, 7, "Bouncer restart failed")], reporter.update_calls)
|
self.assertEqual(1, len(reporter.update_calls))
|
||||||
|
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"])
|
||||||
|
|
||||||
@patch("healing.time.sleep", return_value=None)
|
@patch("healing.time.sleep", return_value=None)
|
||||||
def test_execute_heal_and_verify_verify_exception(self, _sleep):
|
def test_execute_heal_and_verify_verify_exception(self, _sleep):
|
||||||
@@ -127,9 +142,10 @@ class PostHealVerificationTests(unittest.TestCase):
|
|||||||
self.assertEqual("verify_error", heal_result)
|
self.assertEqual("verify_error", heal_result)
|
||||||
self.assertEqual("verify_error", returned_post_drift["reason"])
|
self.assertEqual("verify_error", returned_post_drift["reason"])
|
||||||
self.assertEqual(1, len(reporter.update_calls))
|
self.assertEqual(1, len(reporter.update_calls))
|
||||||
self.assertFalse(reporter.update_calls[0][0])
|
self.assertFalse(reporter.update_calls[0]["is_synced"])
|
||||||
self.assertEqual(7, reporter.update_calls[0][1])
|
self.assertEqual(7, reporter.update_calls[0]["ban_count"])
|
||||||
self.assertIn("Post-heal verify error: boom", reporter.update_calls[0][2])
|
self.assertIn("Post-heal verify error: boom", reporter.update_calls[0]["last_error"])
|
||||||
|
self.assertEqual("verify_error", reporter.update_calls[0]["last_heal_result"])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from reporter import LocalStateReporter
|
||||||
|
|
||||||
|
|
||||||
|
class ReporterStateTests(unittest.TestCase):
|
||||||
|
def test_update_status_writes_drift_and_fingerprint_fields(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,
|
||||||
|
10,
|
||||||
|
last_error="boom",
|
||||||
|
missing_count=2,
|
||||||
|
extra_count=3,
|
||||||
|
metadata_drift_count=1,
|
||||||
|
last_drift_fingerprint="abc123",
|
||||||
|
last_heal_result="failed",
|
||||||
|
)
|
||||||
|
|
||||||
|
with open(state_file, "r") as f:
|
||||||
|
payload = json.load(f)
|
||||||
|
|
||||||
|
self.assertEqual("n1", payload["node_name"])
|
||||||
|
self.assertEqual("DESYNC_DETECTED", payload["status"])
|
||||||
|
self.assertEqual(10, payload["kernel_ban_count"])
|
||||||
|
self.assertEqual(2, payload["missing_count"])
|
||||||
|
self.assertEqual(3, payload["extra_count"])
|
||||||
|
self.assertEqual(1, payload["metadata_drift_count"])
|
||||||
|
self.assertEqual("abc123", payload["last_drift_fingerprint"])
|
||||||
|
self.assertEqual("failed", payload["last_heal_result"])
|
||||||
|
|
||||||
|
def test_update_status_preserves_last_heal_result_when_omitted(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,
|
||||||
|
7,
|
||||||
|
last_heal_result="success",
|
||||||
|
last_drift_fingerprint="oldfp",
|
||||||
|
)
|
||||||
|
reporter.update_status(
|
||||||
|
True,
|
||||||
|
7,
|
||||||
|
missing_count=0,
|
||||||
|
extra_count=0,
|
||||||
|
metadata_drift_count=0,
|
||||||
|
last_drift_fingerprint="",
|
||||||
|
)
|
||||||
|
|
||||||
|
with open(state_file, "r") as f:
|
||||||
|
payload = json.load(f)
|
||||||
|
|
||||||
|
self.assertEqual("success", payload["last_heal_result"])
|
||||||
|
self.assertEqual("", payload["last_drift_fingerprint"])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user