added Readme.md and some debug logs
This commit is contained in:
@@ -210,13 +210,15 @@ def analyze_logs(lines):
|
|||||||
|
|
||||||
def load_offender_state():
|
def load_offender_state():
|
||||||
if not os.path.exists(STATE_FILE):
|
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 {}
|
return {}
|
||||||
try:
|
try:
|
||||||
with open(STATE_FILE, "r") as f:
|
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:
|
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 {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
@@ -225,8 +227,32 @@ def save_offender_state(state):
|
|||||||
os.makedirs(os.path.dirname(STATE_FILE), exist_ok=True)
|
os.makedirs(os.path.dirname(STATE_FILE), exist_ok=True)
|
||||||
with open(STATE_FILE, "w") as f:
|
with open(STATE_FILE, "w") as f:
|
||||||
json.dump(state, f, indent=2, sort_keys=True)
|
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:
|
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):
|
def update_offender_state(state, suspicious_ips_today, report_date_str):
|
||||||
@@ -383,6 +409,8 @@ if __name__ == "__main__":
|
|||||||
if DEBUG:
|
if DEBUG:
|
||||||
print("[DEBUG] Debug mode is ENABLED.")
|
print("[DEBUG] Debug mode is ENABLED.")
|
||||||
|
|
||||||
|
check_state_persistence()
|
||||||
|
|
||||||
# 1. Register the permanent daily schedule first
|
# 1. Register the permanent daily schedule first
|
||||||
schedule.every().day.at(REPORT_TIME).do(job)
|
schedule.every().day.at(REPORT_TIME).do(job)
|
||||||
print(f"Job permanently scheduled to run daily at {REPORT_TIME}.")
|
print(f"Job permanently scheduled to run daily at {REPORT_TIME}.")
|
||||||
|
|||||||
Reference in New Issue
Block a user