added Readme.md and some debug logs

This commit is contained in:
cpu
2026-07-11 17:17:55 +00:00
parent abf67d8664
commit c335ff2615

View File

@@ -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}.")