#!/usr/bin/env python3 """ Live status dashboard for Alpha MC โ serves on port 7860. Reads /tmp/mc_status.json and log files to show real-time info. """ import http.server import json import os import subprocess import time from datetime import datetime STATUS_FILE = "/tmp/mc_status.json" MC_LOG = "/data/logs/minecraft.log" PLAYIT_LOG = "/data/playit_gg/playit.log" MONITOR_LOG = "/data/logs/monitor.log" # kept for compat def read_status(): try: with open(STATUS_FILE) as f: return json.load(f) except Exception: return { "mc_online": False, "playit_online": False, "tunnel_address": "", "started_at": "", "uptime": "unknown" } def tail_file(path, lines=30): try: result = subprocess.run( ["tail", "-n", str(lines), path], capture_output=True, text=True, timeout=3 ) return result.stdout.strip() or "(no logs yet)" except Exception: return "(log unavailable)" def get_system_stats(): try: mem = subprocess.run(["free", "-h"], capture_output=True, text=True).stdout mem_line = [l for l in mem.splitlines() if l.startswith("Mem:")][0].split() mem_used, mem_total = mem_line[2], mem_line[1] except Exception: mem_used, mem_total = "?", "?" try: disk = subprocess.run(["df", "-h", "/data"], capture_output=True, text=True).stdout disk_line = disk.splitlines()[1].split() disk_used, disk_total, disk_pct = disk_line[2], disk_line[1], disk_line[4] except Exception: disk_used, disk_total, disk_pct = "?", "?", "?" try: load = subprocess.run(["uptime"], capture_output=True, text=True).stdout load = load.split("load average:")[-1].strip() except Exception: load = "?" return { "mem_used": mem_used, "mem_total": mem_total, "disk_used": disk_used, "disk_total": disk_total, "disk_pct": disk_pct, "load": load } def get_tunnel_address(): """Try to extract the live tunnel address from playit log.""" try: result = subprocess.run( ["grep", "-oE", r"[a-z0-9-]+\.playit\.gg(:[0-9]+)?", PLAYIT_LOG], capture_output=True, text=True, timeout=3 ) addr = result.stdout.strip().splitlines() return addr[0] if addr else "" except Exception: return "" def render_html(): status = read_status() stats = get_system_stats() tunnel = get_tunnel_address() or status.get("tunnel_address", "") mc_log = tail_file(MC_LOG, 40) now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") mc_status_color = "#4ade80" if status.get("mc_online") else "#f87171" mc_status_text = "๐ข Online" if status.get("mc_online") else "๐ด Offline" tunnel_color = "#4ade80" if tunnel else "#facc15" tunnel_text = tunnel if tunnel else "Waiting for tunnel..." return f"""
Minecraft Bedrock Server ยท Auto-refreshes every 15s ยท Last updated: {now}
{mc_log}