52 lines
1.8 KiB
Python
52 lines
1.8 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="/home/patrick/threat-agent-state.json"):
|
|
self.state_file = state_file
|
|
# 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": "triton",
|
|
"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 log_heal_event(self, gap_count):
|
|
"""
|
|
Records a specific healing event in a persistent history log.
|
|
"""
|
|
history_file = self.state_file.replace(".json", ".history")
|
|
timestamp = datetime.now().isoformat()
|
|
|
|
try:
|
|
with open(history_file, 'a') as f:
|
|
f.write(f"{timestamp} - HEAL: Fixed desync of {gap_count} IPs\n")
|
|
except Exception as e:
|
|
logging.error(f"Reporter: Failed to write to history log: {e}")
|