为什么选择 Prometheus + Grafana
《监控与备份》覆盖了轻量方案(uptime 探测 + 邮件告警)。当实例变多、需要历史趋势与可视化面板时,Prometheus(指标采集存储)+ Grafana(可视化)+ Node Exporter(主机指标)是开源事实标准。本文给出在 DMIT 上的最小可用部署。
组件分工
| 组件 | 职责 |
|---|---|
| Prometheus | 定时拉取(pull)各节点指标并存储时序数据 |
| Node Exporter | 暴露主机的 CPU、内存、磁盘、网络指标(9100 端口) |
| Grafana | 读取 Prometheus 数据源,渲染仪表盘 |
| Alertmanager | 接收 Prometheus 告警规则触发的事件,去重并路由通知 |
安装 Node Exporter(每台被监控机)
apt install prometheus-node-exporter -y
systemctl enable --now prometheus-node-exporter
curl -s localhost:9100/metrics | head -3 # 验证输出
安装 Prometheus(监控中心机)
apt install prometheus -y
编辑 /etc/prometheus/prometheus.yml 添加抓取目标:
scrape_configs:
- job_name: "dmit-nodes"
scrape_interval: 15s
static_configs:
- targets:
- "10.0.0.11:9100"
- "10.0.0.12:9100"
- "10.0.0.13:9100"
systemctl restart prometheus
# 访问 http://SERVER:9090/targets 确认全部 UP
安装 Grafana 并导入面板
apt install -y adduser libfontconfig1 musl
wget https://dl.grafana.com/oss/release/grafana_11.*_amd64.deb
apt install -y ./grafana_11.*_amd64.deb
systemctl enable --now grafana-server
- 访问
http://SERVER:3000,默认账号 admin/admin,立即改密码 - Data sources → 添加 Prometheus → URL 填
http://localhost:9090 - Dashboards → Import → 输入社区经典面板 ID 1860(Node Exporter Full)→ 选择数据源
核心告警规则
编辑 /etc/prometheus/alert.rules.yml:
groups:
- name: host-alerts
rules:
- alert: InstanceDown
expr: up == 0
for: 2m
labels: {severity: critical}
annotations: {summary: "{{ $labels.instance }} 已宕机"}
- alert: HighCPU
expr: 100 - (avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
for: 10m
annotations: {summary: "{{ $labels.instance }} CPU 持续高于 85%"}
- alert: DiskAlmostFull
expr: (1 - node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes) > 0.9
for: 5m
annotations: {summary: "{{ $labels.instance }} 磁盘使用超 90%"} # 磁盘满处理见故障排查指南
- alert: MemoryPressure
expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) > 0.9
for: 5m
annotations: {summary: "{{ $labels.instance }} 可用内存低于 10%"}
在 prometheus.yml 中挂载 rule_files: ["alert.rules.yml"] 后重启生效。
安全要点
- 9090/3000/9100 端口不要裸暴露公网:防火墙仅放行监控网段,或用 Nginx 反代 + HTTPS + Basic Auth(见《HTTPS 配置》)
- 跨公网抓取建议建立 WireGuard/内网隧道,指标接口本身无认证
- Grafana 建独立查看账号,管理员不开弱密码
常见问题
target 显示 DOWN?
在被监控机上 curl localhost:9100/metrics 验证服务;再从监控机 telnet 对端 9100 检查防火墙(《安全清单》基线)。
图表没有数据?
确认 Grafana 数据源 URL 正确、Prometheus 时间范围内有抓取样本;用 up 指标在 Prometheus 页面直接查询验证。
磁盘被时序数据占满?
设置保留期:--storage.tsdb.retention.time=30d,或外接长期存储方案。
相关文章
本文核对日期:2026-08-18。软件版本以官方文档为准。