Files
aiops/.hermes/plans/2026-06-18_monitoring_overhaul.md
T

239 lines
7.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 监控系统全面升级实施方案
> **For Hermes:** Execute phase by phase, each phase is independently deployable.
**Goal:** 构建 4 大监控模块:主机监控、中间件监控、SNMP 设备监控、站点拨测
**架构:** Agent 扩展采集器 → Backend 新增路由/处理器 → 前端新建监控面板
**Tech Stack:** Go (Agent/Backend), React+TypeScript (Frontend), MySQL+Redis (Storage), gosnmp (SNMP library)
---
## Phase 1: Agent 指标扩展 + 主机监控仪表盘 🖥️
### Task 1.1: Agent 新增采集器 - 网络指标
**Files:**
- Create: `agent/collector/network.go`
- Modify: `agent/model/model.go`
- Modify: `agent/main.go`
**网络指标采集:**
- 并发连接数 (ESTABLISHED via /proc/net/sockstat)
- TIME_WAIT 连接数
- 网卡 rx_bytes/s, tx_bytes/s(两次采集差值 / 间隔)
- 网卡名列表和速率
### Task 1.2: Agent 新增采集器 - 磁盘 IO
**Files:**
- Create: `agent/collector/diskio.go`
- Modify: `agent/model/model.go`
**磁盘 IO 指标:**
- 读取 BPS (bytes/s)
- 写入 BPS (bytes/s)
- 读取 IOPS (count/s)
- 写入 IOPS (count/s)
### Task 1.3: Agent 新增采集器 - 进程监控
**Files:**
- Create: `agent/collector/process.go`
- Modify: `agent/model/model.go`
**进程监控:**
- Top N 进程 CPU/内存使用
- 进程总数
- 僵尸进程数
### Task 1.4: Agent 新增采集器 - 系统信息(启动时一次)
**Files:**
- Create: `agent/collector/system.go`
- Modify: `agent/model/model.go`
**系统信息:**
- OS 版本 (cat /etc/os-release)
- 内核版本 (uname -r)
- 运行时间 (uptime)
- ulimit -n (nofile 上限)
- ulimit -u (nproc 上限)
### Task 1.5: Backend - 扩展 AgentReport 模型
**Files:**
- Modify: `backend/internal/models/agent.go`
- Modify: `backend/internal/api/handlers/agent.go` (ReceiveReport)
**扩展字段:**
- NetworkInfo JSON (net_connections, net_rx_bps, net_tx_bps, nic_name)
- DiskIOInfo JSON (read_bps, write_bps, read_iops, write_iops)
- ProcessInfo JSON (top_processes, total_proc, zombie_count)
- SystemInfo JSON (os_version, kernel, uptime, ulimit_nofile, ulimit_nproc)
### Task 1.6: Backend - 主机监控 API
**Files:**
- Create: `backend/internal/api/handlers/host_monitor.go`
- Modify: `backend/internal/api/routes/routes.go`
**API 端点:**
- `GET /api/v1/monitor/host/:hostname/overview` - 主机概览
- `GET /api/v1/monitor/host/:hostname/metrics?metric=cpu&from=&to=` - 历史指标
- `GET /api/v1/monitor/host/:hostname/processes` - 进程列表
- `GET /api/v1/monitor/host/:hostname/network` - 网络指标
### Task 1.7: Frontend - 主机监控面板
**Files:**
- Create: `frontend/src/pages/HostMonitor.tsx`
- Create: `frontend/src/components/monitor/HostCard.tsx`
- Create: `frontend/src/components/monitor/MetricChart.tsx`
- Modify: `frontend/src/App.tsx` (add route /monitor/host)
**展示内容:**
- 主机卡片:CPU/内存/磁盘使用率仪表盘
- 趋势图:CPU/内存/磁盘 5m/1h/6h/24h
- 网络:并发连接数、网卡带宽趋势
- 进程 TOP 10 表格
---
## Phase 2: 中间件监控 🔌
### Task 2.1: Agent 新增采集器 - Redis
**Files:**
- Create: `agent/collector/redis.go`
- Modify: `agent/model/model.go`
**Redis 指标(需要配置 host:port:password):**
- 连接数 (connected_clients)
- 内存使用 (used_memory)
- 命中率 (keyspace_hits / (hits + misses))
- Key 数量
- 慢查询数
- QPS (instantaneous_ops_per_sec)
### Task 2.2: Agent 新增采集器 - MySQL
**Files:**
- Create: `agent/collector/mysql.go`
**MySQL 指标:**
- 当前连接数 / 最大连接数
- QPS (Questions 差值)
- 慢查询数
- Buffer Pool 命中率
- 复制延迟(如有)
### Task 2.3: Backend - 中间件监控 API + 配置管理
**Files:**
- Create: `backend/internal/api/handlers/middleware_monitor.go`
- Create: `backend/internal/models/middleware_config.go` (RedisConfig, MySQLConfig)
- Modify: `backend/internal/api/routes/routes.go`
**API:**
- `POST /api/v1/monitor/middleware/config` - 配置中间件认证
- `GET /api/v1/monitor/middleware/redis/:hostname` - Redis 指标
- `GET /api/v1/monitor/middleware/mysql/:hostname` - MySQL 指标
### Task 2.4: Frontend - 中间件监控面板
**Files:**
- Create: `frontend/src/pages/MiddlewareMonitor.tsx`
---
## Phase 3: SNMP 设备监控 📡
### Task 3.1: Backend - SNMP 采集引擎 + OID 模板系统
**Files:**
- Create: `backend/internal/api/handlers/snmp_monitor.go`
- Create: `backend/internal/models/snmp_template.go`
- Create: `backend/pkg/snmp/collector.go` (uses gosnmp)
- Add dep: `github.com/gosnmp/gosnmp`
**SNMP 模板系统:**
- 模板包含一组 OID → 指标名映射
- 预置模板:Cisco 交换机、H3C 交换机、华为交换机、服务器 iLO/iDRAC
- 用户可自定义 OID 模板
### Task 3.2: Backend - SNMP 定时采集任务
**Files:**
- Create: `backend/internal/scheduler/snmp_scheduler.go`
**定时采集:**
- 每 5 分钟对配置的 SNMP 设备执行采集
- 存储到 agent_reports 表(type=snmp)
### Task 3.3: Frontend - SNMP 设备管理 + 监控面板
**Files:**
- Create: `frontend/src/pages/SNMPMonitor.tsx`
- Create: `frontend/src/components/monitor/SNMPDeviceCard.tsx`
---
## Phase 4: 站点拨测 🌐
### Task 4.1: Backend - 拨测引擎
**Files:**
- Create: `backend/internal/api/handlers/site_monitor.go`
- Create: `backend/internal/models/site_task.go`
- Create: `backend/pkg/probe/` (HTTP, PING, WebSocket, DNS, TCP, UDP, SMTP, POP3, FTP probes)
**8 种拨测类型实现:**
- HTTP(S): GET/POST,检查状态码 + 响应时间 + 正则匹配 body
- PING: ICMP echo
- WebSocket: 连接 + 心跳
- DNS: 解析时间 + 结果验证
- TCP: 端口连通性
- UDP: 端口连通性
- SMTP: 连接 + STARTTLS
- POP3: 连接 + 认证
- FTP: 连接 + 登录
### Task 4.2: Backend - 拨测定时任务
**Files:**
- Modify: `backend/internal/scheduler/` (add probe scheduler)
### Task 4.3: Frontend - 站点拨测管理面板
**Files:**
- Create: `frontend/src/pages/SiteMonitor.tsx`
- Create: `frontend/src/components/monitor/ProbeTaskForm.tsx`
- Create: `frontend/src/components/monitor/ProbeResultChart.tsx`
**展示内容:**
- 拨测任务列表(类型、URL、频率、状态)
- 响应时间趋势图
- 可用率统计
- 告警配置
---
## Phase 5: 导航重构 🧭
### Task 5.1: 导航结构重组
**Files:**
- Modify: `frontend/src/components/layout/MainLayout.tsx` 或 Sidebar
**新导航结构:**
```
📊 监控告警
├── 🖥️ 主机监控 /monitor/host
├── 🔌 中间件监控 /monitor/middleware
├── 📡 SNMP 监控 /monitor/snmp
├── 🌐 站点拨测 /monitor/site
├── 📈 Agent 监控 /agent-monitor (已有)
├── 🚨 告警中心 /alert-center (已有)
└── 📜 告警规则 /agent-alerts (已有)
```
---
## 执行顺序
1. **Phase 1**(最优先,用户当前最需要)
→ Agent 扩展网络/磁盘IO/进程/系统信息
→ 前端主机监控面板
2. **Phase 2** → 中间件监控
3. **Phase 3** → SNMP 设备监控(较复杂,需 gosnmp 依赖)
4. **Phase 4** → 站点拨测
5. **Phase 5** → 导航重构(收尾)