"""健康检查 — 增强版 /health 端点""" import json import time import os from typing import Optional from oss.config import get_config from oss.logger.logger import Log _start_time = time.time() class HealthChecker: """系统健康检查""" @staticmethod def get_uptime() -> float: return time.time() - _start_time @staticmethod def get_system_stats() -> dict: """获取系统资源状态""" stats = {"cpu": "unknown", "memory": "unknown", "disk": "unknown"} try: import psutil stats["cpu"] = psutil.cpu_percent(interval=0.1) mem = psutil.virtual_memory() stats["memory"] = { "total": mem.total, "available": mem.available, "percent": mem.percent, } disk = psutil.disk_usage("/") stats["disk"] = { "total": disk.total, "free": disk.free, "percent": disk.percent, } except ImportError: pass return stats @staticmethod def check() -> dict: """执行健康检查,返回完整报告""" config = get_config() stats = HealthChecker.get_system_stats() plugins_total = 5 # 实际应从 PluginManager 读取 return { "status": "ok", "version": config.get("VERSION", "1.2.0"), "uptime": HealthChecker.get_uptime(), "plugins": { "total": plugins_total, "active": plugins_total, "degraded": [], }, "system": { "cpu_percent": stats.get("cpu", "unknown"), "memory_percent": stats["memory"]["percent"] if isinstance(stats.get("memory"), dict) else "unknown", "disk_percent": stats["disk"]["percent"] if isinstance(stats.get("disk"), dict) else "unknown", "disk_free_gb": round(stats["disk"]["free"] / (1024**3), 1) if isinstance(stats.get("disk"), dict) else "unknown", }, }