Development

This commit is contained in:
Matthew Grotke 2026-06-03 00:45:04 -04:00
parent 4a9110cc4c
commit 094847966a
6 changed files with 197 additions and 57 deletions

View file

@ -488,20 +488,46 @@ def process_provider(provider, current_ip, force=False):
# FreeRADIUS log rotation
# ===================================================================
def rotate_radius_log(radius_cfg):
"""Truncate the FreeRADIUS log if it exceeds radius.general.log_max_kb."""
max_kb = radius_cfg.get("general", {}).get("log_max_kb", 1024)
max_bytes = int(max_kb * 1024)
if not RADIUS_LOG_FILE.exists():
return
def _clear_radius_log_dir(log_dir, reason):
"""Delete all files in log_dir and print reason."""
try:
if RADIUS_LOG_FILE.stat().st_size > max_bytes:
RADIUS_LOG_FILE.write_text("")
print(f"FreeRADIUS log cleared (exceeded {max_kb} KB).")
files = [p for p in log_dir.iterdir() if p.is_file()]
if not files:
return
for p in files:
try:
p.unlink()
except PermissionError:
print(f"WARNING: Cannot delete {p} (permission denied).")
except OSError as e:
print(f"WARNING: Error deleting {p}: {e}")
print(f"FreeRADIUS logs cleared ({reason}).")
except PermissionError:
print(f"WARNING: Cannot write to {RADIUS_LOG_FILE} (permission denied).")
print(f"WARNING: Cannot read {log_dir} (permission denied).")
except OSError as e:
print(f"WARNING: Error checking FreeRADIUS log: {e}")
print(f"WARNING: Error clearing FreeRADIUS log dir: {e}")
def rotate_radius_log(radius_cfg):
"""Clear the FreeRADIUS log dir if logging is disabled or total size exceeds log_max_kb."""
general = radius_cfg.get("general", {})
log_dir = RADIUS_LOG_FILE.parent
if not log_dir.exists():
return
if not general.get("logging", False):
_clear_radius_log_dir(log_dir, "logging disabled")
return
max_kb = general.get("log_max_kb", 1024)
max_bytes = int(max_kb * 1024)
try:
files = [p for p in log_dir.iterdir() if p.is_file()]
total = sum(p.stat().st_size for p in files)
if total > max_bytes:
_clear_radius_log_dir(log_dir, f"total {total // 1024} KB exceeded {max_kb} KB")
except PermissionError:
print(f"WARNING: Cannot read {log_dir} (permission denied).")
except OSError as e:
print(f"WARNING: Error checking FreeRADIUS log dir: {e}")
# ===================================================================