"""Minimal sysmonstm gateway - standalone mode without dependencies.""" from fastapi import FastAPI, WebSocket, WebSocketDisconnect from fastapi.responses import HTMLResponse import json import asyncio from datetime import datetime app = FastAPI(title="sysmonstm") # Store connected websockets connections: list[WebSocket] = [] # Store latest metrics from collectors machines: dict = {} HTML = """ sysmonstm

sysmonstm

connecting...

No collectors connected

Start a collector to see metrics

""" @app.get("/", response_class=HTMLResponse) async def index(): return HTML @app.get("/health") async def health(): return {"status": "ok", "machines": len(machines)} @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() connections.append(websocket) try: # Send current state for machine_id, data in machines.items(): await websocket.send_json({"type": "metrics", "machine_id": machine_id, **data}) # Keep alive while True: try: msg = await asyncio.wait_for(websocket.receive_text(), timeout=30) data = json.loads(msg) if data.get("type") == "metrics": machine_id = data.get("machine_id", "unknown") machines[machine_id] = {**data, "timestamp": datetime.utcnow().isoformat()} # Broadcast to all for conn in connections: try: await conn.send_json({"type": "metrics", "machine_id": machine_id, **machines[machine_id]}) except: pass except asyncio.TimeoutError: await websocket.send_json({"type": "ping"}) except WebSocketDisconnect: pass finally: connections.remove(websocket) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8080)