From c335ff26152ee42574e43a5cd4f1d7e6eb6d95ae Mon Sep 17 00:00:00 2001 From: cpu Date: Sat, 11 Jul 2026 17:17:55 +0000 Subject: [PATCH] added Readme.md and some debug logs --- daily_summary.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/daily_summary.py b/daily_summary.py index 22d0810..7e5d72a 100644 --- a/daily_summary.py +++ b/daily_summary.py @@ -210,13 +210,15 @@ def analyze_logs(lines): def load_offender_state(): if not os.path.exists(STATE_FILE): - debug_print(f"No existing state file at {STATE_FILE}, starting fresh.") + print(f"[STATE] No existing state file at {STATE_FILE} yet -- starting fresh (expected on first run).") return {} try: with open(STATE_FILE, "r") as f: - return json.load(f) + state = json.load(f) + debug_print(f"[STATE] Loaded {len(state)} tracked IP(s) from {STATE_FILE}.") + return state except (json.JSONDecodeError, OSError) as e: - debug_print(f"Could not read state file ({e}), starting fresh.") + print(f"[STATE] WARNING: could not read state file ({e}). Starting fresh -- repeat-offender history may be lost.") return {} @@ -225,8 +227,32 @@ def save_offender_state(state): os.makedirs(os.path.dirname(STATE_FILE), exist_ok=True) with open(STATE_FILE, "w") as f: json.dump(state, f, indent=2, sort_keys=True) + debug_print(f"[STATE] Wrote {len(state)} tracked IP(s) to {STATE_FILE}.") except OSError as e: - debug_print(f"Could not write state file: {e}") + print(f"[STATE] ERROR: could not write state file at {STATE_FILE}: {e}. " + f"Repeat-offender tracking will NOT persist across runs until this is fixed " + f"(check that the /data mount exists and is writable).") + + +def check_state_persistence(): + """Run once at container startup so a broken mount is obvious in `docker logs` + immediately, rather than discovered days later when offender counts never grow.""" + state_dir = os.path.dirname(STATE_FILE) or "." + print(f"[STARTUP] State file configured at: {STATE_FILE}") + if not os.path.isdir(state_dir): + print(f"[STARTUP] WARNING: directory '{state_dir}' does not exist inside the container. " + f"Is the /data bind mount configured in the systemd unit, and did you " + f"`systemctl daemon-reload && systemctl restart` after editing it?") + return + probe = os.path.join(state_dir, ".write_test") + try: + with open(probe, "w") as f: + f.write("ok") + os.remove(probe) + print(f"[STARTUP] '{state_dir}' is writable -- repeat-offender tracking will persist correctly.") + except OSError as e: + print(f"[STARTUP] WARNING: '{state_dir}' is NOT writable ({e}). " + f"Repeat-offender tracking will silently reset every run until this is fixed.") def update_offender_state(state, suspicious_ips_today, report_date_str): @@ -383,6 +409,8 @@ if __name__ == "__main__": if DEBUG: print("[DEBUG] Debug mode is ENABLED.") + check_state_persistence() + # 1. Register the permanent daily schedule first schedule.every().day.at(REPORT_TIME).do(job) print(f"Job permanently scheduled to run daily at {REPORT_TIME}.")