import os from config_utils import collect_layout_tokens, CONFIGS_DIR RADIUS_LOG_MAX = 50 RADIUS_LOG_FILE = '/var/log/freeradius/radius.log' def radius_log_tail(cfg): try: log_max_kb = cfg.get('radius', {}).get('general', {}).get('log_max_kb', 1024) current = [] try: with open(RADIUS_LOG_FILE) as f: current = f.readlines() except FileNotFoundError: pass prev = [] if len(current) < RADIUS_LOG_MAX: try: with open(RADIUS_LOG_FILE + '.1') as f: prev = f.readlines() except FileNotFoundError: pass need = max(0, RADIUS_LOG_MAX - len(current)) lines = (prev[-need:] if need and prev else []) + current if not lines: return '(log is empty)', '' 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[-RADIUS_LOG_MAX:] 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)' summary = ( '