Development
This commit is contained in:
parent
4a9110cc4c
commit
094847966a
6 changed files with 197 additions and 57 deletions
|
|
@ -1,5 +1,8 @@
|
|||
import copy
|
||||
import gzip
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
from flask import Blueprint, request, redirect, flash, send_file, abort, jsonify
|
||||
from auth import require_level
|
||||
|
|
@ -73,23 +76,52 @@ def logging_save():
|
|||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/radius/logging_clear', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def logging_clear():
|
||||
try:
|
||||
open(RADIUS_LOG_FILE, 'w').close()
|
||||
flash('RADIUS log cleared.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not clear log: {ex}', 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/radius/logging_download', methods=['GET'])
|
||||
@require_level('administrator')
|
||||
def logging_download():
|
||||
if not os.path.isfile(RADIUS_LOG_FILE):
|
||||
log_dir = os.path.dirname(RADIUS_LOG_FILE)
|
||||
chunks = []
|
||||
|
||||
# Collect radius.log.N.gz files, sorted oldest-first (highest N first)
|
||||
gz_files = []
|
||||
for name in os.listdir(log_dir) if os.path.isdir(log_dir) else []:
|
||||
m = re.fullmatch(r'radius\.log\.(\d+)\.gz', name)
|
||||
if m:
|
||||
gz_files.append((int(m.group(1)), os.path.join(log_dir, name)))
|
||||
for _, path in sorted(gz_files, reverse=True):
|
||||
try:
|
||||
with gzip.open(path, 'rb') as f:
|
||||
chunks.append(f.read())
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# radius.log.1 (plain, older than current)
|
||||
rotated = RADIUS_LOG_FILE + '.1'
|
||||
if os.path.isfile(rotated):
|
||||
try:
|
||||
with open(rotated, 'rb') as f:
|
||||
chunks.append(f.read())
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# radius.log (current)
|
||||
if os.path.isfile(RADIUS_LOG_FILE):
|
||||
try:
|
||||
with open(RADIUS_LOG_FILE, 'rb') as f:
|
||||
chunks.append(f.read())
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
if not chunks:
|
||||
abort(404)
|
||||
return send_file(RADIUS_LOG_FILE, as_attachment=True, download_name='radius.log', mimetype='text/plain')
|
||||
|
||||
data = b''.join(chunks)
|
||||
return send_file(
|
||||
io.BytesIO(data),
|
||||
as_attachment=True,
|
||||
download_name='radius.log',
|
||||
mimetype='text/plain',
|
||||
)
|
||||
|
||||
|
||||
@bp.route('/api/radius/log-tail', methods=['GET'])
|
||||
|
|
@ -98,20 +130,43 @@ def api_log_tail():
|
|||
try:
|
||||
cfg = load_config()
|
||||
log_max_kb = cfg.get('radius', {}).get('general', {}).get('log_max_kb', 1024)
|
||||
size_kb = os.path.getsize(RADIUS_LOG_FILE) / 1024
|
||||
with open(RADIUS_LOG_FILE) as f:
|
||||
lines = f.readlines()
|
||||
|
||||
current = []
|
||||
try:
|
||||
with open(RADIUS_LOG_FILE) as f:
|
||||
current = f.readlines()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
prev = []
|
||||
if len(current) < 50:
|
||||
try:
|
||||
with open(RADIUS_LOG_FILE + '.1') as f:
|
||||
prev = f.readlines()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
need = max(0, 50 - len(current))
|
||||
lines = (prev[-need:] if need and prev else []) + current
|
||||
|
||||
if not lines:
|
||||
return jsonify({'log': '(log is empty)', 'summary': ''})
|
||||
total = len(lines)
|
||||
tail = lines[-50:]
|
||||
shown = len(tail)
|
||||
hidden = total - shown
|
||||
pct = min(100, round(size_kb / log_max_kb * 100)) if log_max_kb else 0
|
||||
left = f'Showing {shown} of {total} lines ({hidden} not shown)' if hidden > 0 else f'Showing {shown} of {total} lines'
|
||||
right = f'Log file size: {size_kb:.1f} KB ({pct}% of max)'
|
||||
return jsonify({'log': '(log is empty)', 'left': '', 'right': ''})
|
||||
|
||||
log_dir = os.path.dirname(RADIUS_LOG_FILE)
|
||||
try:
|
||||
size_kb = sum(
|
||||
os.path.getsize(os.path.join(log_dir, f))
|
||||
for f in os.listdir(log_dir)
|
||||
if os.path.isfile(os.path.join(log_dir, f))
|
||||
) / 1024
|
||||
except OSError:
|
||||
size_kb = 0.0
|
||||
|
||||
tail = lines[-50:]
|
||||
pct = min(100, round(size_kb / log_max_kb * 100)) if log_max_kb else 0
|
||||
note = ' (includes rotated log)' if (prev and need) else ''
|
||||
left = f'Showing {len(tail)} lines{note}'
|
||||
right = f'Total log size: {size_kb:.1f} KB ({pct}% of max)'
|
||||
return jsonify({'log': ''.join(tail).strip(), 'left': left, 'right': right})
|
||||
except FileNotFoundError:
|
||||
return jsonify({'log': '(log file not found)', 'left': '', 'right': ''})
|
||||
except Exception:
|
||||
return jsonify({'log': '(error reading log)', 'left': '', 'right': ''})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue