#!/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""" โ›๏ธ Alpha MC โ€” Status

โ›๏ธ Alpha MC

Minecraft Bedrock Server ยท Auto-refreshes every 15s ยท Last updated: {now}

Server Status
{mc_status_text}
RAM Usage
{stats['mem_used']} / {stats['mem_total']}
Disk Usage
{stats['disk_used']} / {stats['disk_total']} ({stats['disk_pct']})
CPU Load
{stats['load']}
๐Ÿ”— Connect Address Minecraft โ†’ Add Server โ†’ paste below
{tunnel_text}

๐Ÿ“‹ Minecraft Server Log (last 40 lines)

{mc_log}
""" class StatusHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): html = render_html() self.send_response(200) self.send_header("Content-Type", "text/html; charset=utf-8") self.send_header("Content-Length", str(len(html.encode()))) self.end_headers() self.wfile.write(html.encode()) def log_message(self, format, *args): pass # suppress access logs if __name__ == "__main__": server = http.server.HTTPServer(("0.0.0.0", 7860), StatusHandler) print(f"[OK] Status dashboard running on http://0.0.0.0:7860") server.serve_forever()