Files
nas-tools/dashboard/backend/routers/system.py
T
Gan, Jimmy 28149b991e
Deploy Dashboard / deploy (push) Failing after 1m52s
Add OpenClaw, backup automation, health checks, Telegram notifications
Phase 6: OpenClaw docker-compose (port 3100) with health check
Phase 9: Enhanced backup.sh with full DB/config coverage + Telegram alerts
Phase 10: Health checks on all compose files, /api/system/notify endpoint
2026-02-19 21:40:34 +08:00

58 lines
1.7 KiB
Python

import os
import shutil
import psutil
import platform
import time
import httpx
from fastapi import APIRouter
router = APIRouter()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "")
TELEGRAM_CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID", "")
@router.post("/notify")
async def send_notification(body: dict):
"""Send a Telegram notification. Body: {"message": "text"}"""
msg = body.get("message", "")
if not msg or not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID:
return {"ok": False, "error": "missing message or Telegram config"}
async with httpx.AsyncClient() as client:
r = await client.post(
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
data={"chat_id": TELEGRAM_CHAT_ID, "text": msg, "parse_mode": "Markdown"},
)
return {"ok": r.status_code == 200}
@router.get("/stats")
def system_stats():
disk = shutil.disk_usage("/volume1")
mem = psutil.virtual_memory()
cpu_pct = psutil.cpu_percent(interval=0.5)
load_1, load_5, load_15 = psutil.getloadavg()
uptime_s = time.time() - psutil.boot_time()
days = int(uptime_s // 86400)
hours = int((uptime_s % 86400) // 3600)
return {
"cpu_percent": cpu_pct,
"cpu_count": psutil.cpu_count(),
"load_avg": [round(load_1, 2), round(load_5, 2), round(load_15, 2)],
"memory": {
"total": mem.total,
"used": mem.used,
"percent": mem.percent,
},
"disk": {
"total": disk.total,
"used": disk.used,
"free": disk.free,
"percent": round(disk.used / disk.total * 100, 1),
},
"uptime": f"{days}d {hours}h",
"platform": platform.platform(),
}