智能监控技术方案 链接到标题
概述 链接到标题
本文档描述基于开源组件构建的智能监控告警体系,实现从指标采集到飞书通知的完整告警流程。
架构概览 链接到标题
告警处理流程
flowchart TD
A["node-exporter<br/>:9100"]
B["Prometheus<br/>:9090"]
C["Alertmanager<br/>:9093"]
D["alert-transformer<br/>:9091"]
E["OpenClaw<br/>:18789"]
F["飞书 Webhook"]
G["告警规则<br/>alerts.yml"]
A -->|指标采集| B
B -->|告警评估| C
B -.->|加载| G
C -->|Webhook| D
D -->|格式转换| E
E -->|飞书通知| F
指标采集流程
flowchart LR
subgraph 指标
H["node-exporter<br/>:9100"]
I["Prometheus<br/>:9090"]
H -->|"Prometheus 抓取 (15s)"| I
end
subgraph 日志
J["Docker 日志"]
K["Alloy<br/>:12345"]
L["Loki<br/>:3100"]
J -->|采集| K
K -->|发送| L
end
组件职责 链接到标题
| 组件 | 职责 | 端口 | 部署位置 |
|---|---|---|---|
| node-exporter | 系统指标采集 (CPU/内存/磁盘/网络) | 9100 | 每台被监控主机 |
| Prometheus | 指标抓取、存储、告警规则评估 | 9090 | 监控服务器 |
| Alertmanager | 告警去重、分组、路由、抑制 | 9093 | 监控服务器 |
| alert-transformer | 告警格式转换为 OpenClaw API | 9091 | 监控服务器 |
| OpenClaw | AI 决策、自然语言处理、飞书通知 | 18789 | OpenClaw 服务器 |
| Loki | 日志存储和查询 | 3100 | 监控服务器 |
| Alloy | Docker 日志采集 | 12345 | 监控服务器 |
部署配置 链接到标题
node-exporter 部署 链接到标题
每台被监控主机部署 node-exporter,建议使用 Docker Compose:
services:
node-exporter:
image: prom/node-exporter:v1.8.2
container_name: node-exporter
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
- '--path.rootfs=/rootfs'
- '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
- '--collector.netclass.ignored-devices=^(veth|docker|br-|flannel|cali)`
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
ports:
- "9100:9100"
restart: unless-stopped
network_mode: host
常用部署命令:
# 创建目录
ssh <hostname> "sudo mkdir -p <部署路径> && sudo chown $(id -u):$(id -g) <部署路径>"
# 上传 docker-compose.yaml
scp docker-compose.yaml <hostname>:<部署路径>/
# 启动
ssh <hostname> "cd /opt/monitor && docker compose up -d"
# 验证
ssh <hostname> "curl -s localhost:9100/metrics | head -20"
Prometheus 配置 链接到标题
主配置文件 prometheus.yml:
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093
rule_files:
- /etc/prometheus/alerts.yml
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
- job_name: robin
static_configs:
- targets:
- node-exporter:9100
labels:
hostname: robin
- job_name: node
static_configs:
- targets:
- <节点IP>:9100
labels:
hostname: <hostname>
Alertmanager 配置 链接到标题
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'openclaw'
receivers:
- name: 'openclaw'
webhook_configs:
- url: 'http://<alert-transformer地址>:9091/alertmanager'
send_resolved: true
alert-transformer 配置 链接到标题
环境变量配置:
| 变量 | 默认值 | 说明 |
|---|---|---|
ALERT_TRANSFORMER_PORT |
9091 | 服务监听端口 |
OPENCLAW_URL |
http://openclaw-gateway:18789 | OpenClaw API 地址 |
OPENCLAW_TOKEN |
- | OpenClaw 认证 Token |
QUEUE_CONCURRENCY |
3 | 并发处理数量 |
SKIP_SEVERITY |
- | 跳过低于该级别的告警 |
IGNORE_ALERTS |
- | 忽略的告警名称列表 |
AGENT_ROUTE_TABLE |
{} | 告警到 Agent 的路由映射 |
OpenClaw 配置 链接到标题
services:
openclaw-gateway:
image: ghcr.nju.edu.cn/openclaw/openclaw:2026.3.28
environment:
OPENCLAW_GATEWAY_TOKEN: ${OPENCLAW_GATEWAY_TOKEN}
HTTP_PROXY: http://<代理服务器IP>:7890
HTTPS_PROXY: http://<代理服务器IP>:7890
NO_PROXY: localhost,127.0.0.1,<数据库主机名>,<数据库IP>
volumes:
- ./.openclaw:/home/node/.openclaw
- <用户目录>/.ssh:/home/node/.ssh:ro
ports:
- "18789:18789"
- "18790:18790"
extra_hosts:
- "<数据库主机名>:<数据库IP>"
restart: unless-stopped
告警规则 链接到标题
告警分级 链接到标题
| 级别 | 标签 | 处理时效 | 说明 |
|---|---|---|---|
| Critical | severity: critical |
立即处理 | 服务宕机或严重资源耗尽 |
| Warning | severity: warning |
关注处理 | 资源使用率较高但未到临界 |
| Info | severity: info |
可延迟 | 信息性告警,无需立即处理 |
告警规则示例 链接到标题
groups:
- name: system_alerts
rules:
# CPU 告警
- alert: HighCPUUsage
expr: 100 - (avg by(hostname) (rate(node_cpu_seconds_total{mode="idle"}[5m]))) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "CPU 使用率超过 80%"
- alert: CriticalCPUUsage
expr: 100 - (avg by(hostname) (rate(node_cpu_seconds_total{mode="idle"}[5m]))) * 100 > 90
for: 2m
labels:
severity: critical
annotations:
summary: "CPU 使用率超过 90%"
# 内存告警
- alert: HighMemoryUsage
expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 85
for: 5m
labels:
severity: warning
annotations:
summary: "内存使用率超过 85%"
# 磁盘告警
- alert: HighDiskUsage
expr: (1 - node_filesystem_avail_bytes{fstype!~"tmpfs|fuse.lxcfs"} / node_filesystem_size_bytes{fstype!~"tmpfs|fuse.lxcfs"}) * 100 > 90
for: 5m
labels:
severity: critical
annotations:
summary: "磁盘使用率超过 90%"
- name: container_alerts
rules:
# 节点宕机
- alert: ContainerDown
expr: up{job="node"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "节点宕机"
告警消息格式 链接到标题
alert-transformer 发送的消息格式:
告警触发 (firing)
告警信息
名称: HighCPUUsage
级别: warning
主机: tank
开始时间: 2024-01-15 10:30:00
摘要: CPU 使用率超过 80%
描述: 主机 tank 当前 CPU: 85.3%
请关注处理。
告警恢复 (resolved)
告警恢复
名称: HighCPUUsage
主机: tank
恢复时间: 2024-01-15 10:35:00
数据流详解 链接到标题
指标采集流程 链接到标题
- Prometheus 主动抓取:Prometheus 每 15s 从各 node-exporter 拉取指标
- 指标存储:指标存储在 TSDB,支持 PromQL 查询
- 规则评估:每 15s 评估一次告警规则,满足条件则触发告警
- 告警发送:将 firing/resolved 状态的告警发送给 Alertmanager
告警处理流程 链接到标题
- Alertmanager 接收:接收 Prometheus 发来的告警
- 去重分组:根据
group_by配置去重和分组 - 发送 webhook:将告警以 HTTP POST 发送到 alert-transformer
- 格式转换:alert-transformer 将格式转换为 OpenClaw API 格式
- AI 处理:OpenClaw 接收消息,AI Agent 处理并决定如何响应
- 飞书通知:OpenClaw 通过飞书 Webhook 发送通知给用户
关键配置点 链接到标题
| 环节 | 配置项 | 说明 |
|---|---|---|
| Prometheus 抓取间隔 | scrape_interval: 15s |
影响告警响应速度 |
| 告警触发延迟 | for: 5m |
避免瞬时波动触发告警 |
| Alertmanager 分组 | group_by: ['alertname'] |
同名告警合并 |
| Alertmanager 抑制 | inhibit_rules |
避免告警风暴 |
| 告警转发间隔 | group_interval: 10s |
连续告警的转发频率 |
| 告警重复间隔 | repeat_interval: 1h |
重复告警的发送间隔 |
运维指南 链接到标题
常用命令 链接到标题
# 查看容器状态 (<监控服务器>)
ssh <监控服务器> "docker ps --format 'table {{.Names}}\t{{.Status}}'"
# 查看 Prometheus targets
ssh <监控服务器> "curl -s http://localhost:9090/api/v1/targets | python3 -c \"import sys,json; d=json.load(sys.stdin); [print(t['labels']['job'], t['health']) for t in d['data']['activeTargets']]\""
# 查看告警状态
ssh <监控服务器> "curl -s http://localhost:9090/api/v1/alerts | python3 -c 'import sys,json; d=json.load(sys.stdin); [print(a[\"labels\"][\"alertname\"], a[\"state\"]) for a in d[\"data\"]]'"
# 查看 Alertmanager 状态
ssh <监控服务器> "curl -s http://localhost:9093/api/v1/status | python3 -m json.tool"
# 重启 Prometheus
ssh <监控服务器> "cd <部署路径> && sudo docker compose restart prometheus"
# 查看 alert-transformer 日志
ssh <监控服务器> "docker logs -f alert-transformer"
# 查看 OpenClaw 日志
ssh <OpenClaw服务器> "docker logs -f openclaw-gateway"
# 测试节点 exporter
curl -s <节点IP>:9100/metrics | grep node_cpu
# 查看告警规则
ssh <监控服务器> "curl -s http://localhost:9090/api/v1/rules | python3 -c 'import sys,json; d=json.load(sys.stdin); [print(r[\"name\"], r[\"rules\"][0][\"health\"]) for r in d[\"data\"]]'"
添加新节点监控 链接到标题
- 编辑 prometheus.yml,在
job_name: node下添加新节点:
- targets:
- <新节点IP>:9100
labels:
hostname: <主机名>
- 同步到监控服务器:
scp monitor/prometheus/prometheus.yml <监控服务器>:/tmp/prometheus.yml
ssh <监控服务器> "sudo cp /tmp/prometheus.yml <部署路径>/prometheus/prometheus.yml"
ssh <监控服务器> "cd <部署路径> && sudo docker compose restart prometheus"
- 部署 node-exporter:
scp monitor/node-exporter/docker-compose.yaml <hostname>:<部署路径>/
ssh <hostname> "cd <部署路径> && docker compose up -d"
- 验证:
ssh <监控服务器> "curl -s http://localhost:9090/api/v1/targets | python3 -c \"import sys,json; d=json.load(sys.stdin); [print(t['labels']['job'], t['labels']['hostname'], t['health']) for t in d['data']['activeTargets']]\""
故障排查 链接到标题
| 问题现象 | 可能原因 | 排查命令 |
|---|---|---|
| 告警未触发 | Prometheus 无法连接 node-exporter | curl -s <node-ip>:9100/metrics |
| 告警未发送到飞书 | alert-transformer 异常 | docker logs alert-transformer |
| OpenClaw 未响应 | OpenClaw 服务异常 | docker ps --filter name=openclaw |
| Alertmanager 无日志 | webhook 配置错误 | curl -s http://localhost:9093/api/v1/status |
| 指标缺失 | node-exporter 未启动 | `ssh “docker ps |
扩展方向 链接到标题
应用层监控 链接到标题
- MySQL/PostgreSQL Exporter:数据库指标
- Redis Exporter:缓存监控
- Nginx Exporter:Web 服务器指标
日志聚合 链接到标题
- Grafana Loki:替代 Prometheus 存储日志
- Grafana:可视化仪表盘
告警增强 链接到标题
- 钉钉/企微集成:多渠道通知
- 告警升级策略:超时未响应则升级
- 告警协同:多人处理模式
自动化修复 链接到标题
- Self-Healing:检测到异常自动触发修复脚本
- Ansible Runner:执行运维操作
架构优势 链接到标题
| 特性 | 说明 |
|---|---|
| 高可用 | Alertmanager 去重和分组避免告警风暴 |
| 灵活路由 | 支持按告警类型路由到不同 Agent |
| AI 增强 | OpenClaw 提供智能分析和自然语言交互 |
| 易于扩展 | 只需添加 node-exporter 和修改 prometheus.yml |
| 开源可控 | 全部使用开源组件,无厂商锁定 |
注意事项 链接到标题
- Token 安全:OpenClaw Token 勿提交到 git,保管在
.env文件 - 网络连通性:确保监控服务器能访问各节点 9100 端口
- 存储容量:根据监控节点数量合理规划 Prometheus 存储
- 告警抑制:合理配置 inhibit_rules 避免级联告警
- 定期维护:定期更新各组件版本,修复安全漏洞