新增 oss/core/security/ 模块(852行): - jwt_auth.py: JWT签发/验证(HMAC-SHA256,零外部依赖) - csrf.py: CSRF Token生成与校验 - input_validator.py: JSON Schema校验+类型强制 - tls.py: 自签名证书生成+SSL上下文 新增 oss/core/ops/ 模块: - health.py: 增强版/health端点(CPU/内存/磁盘/运行时间) - metrics.py: Prometheus兼容/metrics端点 对接改造: - engine.py: 导出新模块 - manager.py: 注册/api/login /health /metrics路由 - middleware.py: CSRF+InputValidation中间件 - config.py: JWT_SECRET/CSRF_SECRET等配置项 - security.py→security/__init__.py: 合并插件沙箱与HTTP安全
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
"""健康检查 — 增强版 /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",
|
|
},
|
|
}
|