Skip to content

健康监控

概述

系统提供多层级健康检查机制,用于前端展示系统状态和辅助运维排障。

健康检查 API 位于 api/routes/health.py,前端展示位于 ui/src/pages/HealthCheck.tsx

四层检查架构

┌─────────────────────────────────────────┐
│ GET /health  (综合检查)                  │
├─────────────────────────────────────────┤
│ Layer 1: Database                       │
│   repo.ping() + repo.table_status()     │
├─────────────────────────────────────────┤
│ Layer 2: Adapter                        │
│   detect_backend() → fjagepy/arlpy/...  │
├─────────────────────────────────────────┤
│ Layer 3: TCP Reachability               │
│   socket.create_connection() per node   │
├─────────────────────────────────────────┤
│ Layer 4: Deep Node Check (PHY)          │
│   connect → probe → check_phy → addr    │
└─────────────────────────────────────────┘

Layer 1: 数据库检查

  • 执行 SELECT 1 验证连接
  • 检查核心表是否存在(runs / run_summary / run_events)

Layer 2: 适配器检测

  • 调用 detect_backend() 确认可用的 UNET 适配器
  • 返回后端类型:fjagepy / arlpy / logonly / unknown

Layer 3: TCP 可达性

  • 对每个配置的节点端点发起 TCP 连接测试
  • 超时 1.5 秒
  • 返回每个端点的可达状态

Layer 4: 深度节点检查

对每个节点执行完整探测序列:

  1. adapter.connect(inst) — 建立网关连接
  2. adapter.probe(inst) — 获取 agent 数量和服务列表
  3. adapter.check_phy(inst) — 检查 PHY 层状态
  4. adapter.query_node_address(inst) — 查询 UNet 地址
  5. adapter.close(inst) — 关闭连接

API 端点

GET /health

综合健康检查,返回所有层级状态。

响应结构 (HealthResponse):

json
{
  "status": "healthy | degraded | unhealthy",
  "checks": {
    "api": { "ok": true },
    "database": { "ok": true, "detail": null }
  },
  "healthy_nodes": 3,
  "total_nodes": 5,
  "db": {
    "ok": true,
    "tables": { "runs": true, "run_summary": true, "run_events": true },
    "detail": null
  },
  "adapter": {
    "ok": true,
    "backend": "fjagepy",
    "detail": null,
    "unet_host": "127.0.0.1",
    "unet_port": 1100,
    "node_endpoints": [
      { "node": "A", "endpoint": "tcp://192.168.1.101:1100", "reachable": true }
    ]
  },
  "nodes": [
    {
      "node_id": 1,
      "name": "A",
      "ok": true,
      "phy_ok": true,
      "agents": 12,
      "services": ["phy", "mac", "link", "transport"],
      "unet_addr": 1,
      "detail": null
    }
  ]
}

GET /health/nodes

仅节点深度检查,轻量版本。

响应结构 (NodeHealthResponse):

json
{
  "status": "healthy | degraded | unhealthy",
  "nodes": [...],
  "healthy_count": 3,
  "total_count": 5
}

整体状态判定规则

条件状态
DB 故障 Adapter 不可用unhealthy
所有节点均不健康(total > 0)unhealthy
部分节点不健康degraded
全部正常healthy

前端 HealthCheck 页面

位于 ui/src/pages/HealthCheck.tsx

  • 使用 usePolling(fetchNodeHealth, 15_000) 每 15 秒轮询节点健康状态
  • 支持手动刷新和开关自动刷新
  • 状态颜色映射:
    • healthy → 绿色背景(#F0FDF4)
    • degraded → 黄色背景(#FEFCE8)
    • unhealthy → 红色背景(#FEF2F2)
  • 展示每个节点的 PHY 状态、agent 数量、服务列表、UNet 地址