Files
threat-engine-agent/reporter.py
T

78 lines
2.5 KiB
Python

# threat_engine_agent/reporter.py
import json
import os
import logging
from datetime import datetime
class LocalStateReporter:
"""
Manages the health manifest on Triton.
Acts as the 'Black Box' recorder for Argus to query.
"""
def __init__(
self,
state_file="/root/threat-engine-agent/threat-agent-state.json",
node_name="unknown",
):
self.state_file = state_file
self.node_name = node_name
# Ensure the directory is writable
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):
"""
Writes the current health snapshot to a JSON file.
"""
status_data = {
"node_name": self.node_name,
"last_check": datetime.now().isoformat(),
"status": "HEALTHY" if is_synced else "DESYNC_DETECTED",
"is_synced": is_synced,
"kernel_ban_count": ban_count,
"last_error": last_error
}
try:
# Atomic write: Write to a temp file then rename to avoid corruption
temp_file = f"{self.state_file}.tmp"
with open(temp_file, 'w') as f:
json.dump(status_data, f, indent=4)
os.replace(temp_file, self.state_file)
except Exception as e:
logging.error(f"Reporter: Failed to write state file: {e}")
def get_history_file(self):
base, _ = os.path.splitext(self.state_file)
return f"{base}.history"
def log_heal_event(
self,
kernel_count,
missing_count,
extra_count,
metadata_drift_count,
drift_fingerprint,
reason,
heal_result,
post_drift_fingerprint,
):
"""
Records a specific healing event in a persistent history log.
"""
history_file = self.get_history_file()
timestamp = datetime.now().isoformat()
try:
with open(history_file, 'a') as f:
f.write(
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} "
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}")