Linux 服务监控与告警体系建设:Prometheus + Grafana + Alertmanager 实战
一、监控体系设计原则与 Prometheus 架构
监控黄金原则:
- RED(Rate、Errors、Duration):请求速率、错误率、响应时长
- USE(Utilization、Saturation、Errors):资源利用率、饱和度、错误
- 分层监控:主机层 → 服务层 → 应用层 → 业务层
Prometheus 架构组件:
- Prometheus Server:时序数据库 + 查询引擎
- Exporters:指标采集器(node_exporter、mysqld_exporter 等)
- Service Discovery:静态/动态服务发现(Kubernetes、Consul、文件等)
与 Systemd 集成优势: Prometheus 可以直接通过 systemd_exporter 监控服务状态(ActiveState、RestartCount 等),结合 journald 日志指标,形成完整可观测性。
二、生产环境部署实战
1. 服务器规划建议
- Prometheus + Alertmanager:独立监控节点(8核16G+)
- Grafana:可与 Prometheus 同机或独立
2. 安装 Prometheus(最新版示例)
# 下载并解压
wget https://github.com/prometheus/prometheus/releases/download/v3.0.0/prometheus-3.0.0.linux-amd64.tar.gz
tar xvf prometheus-3.0.0.linux-amd64.tar.gz
cd prometheus-3.0.0.linux-amd64
# 创建 systemd 服务单元
cat > /etc/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus Server
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/data/prometheus \
--web.listen-address=0.0.0.0:9090
Restart=always
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
3. 基础 prometheus.yml 配置
global:
scrape_interval:15s
evaluation_interval:15s
scrape_configs:
-job_name:'prometheus'
static_configs:
-targets:['localhost:9090']
-job_name:'node_exporter'
static_configs:
-targets:['192.168.1.10:9100','192.168.1.11:9100']
-job_name:'systemd'
static_configs:
-targets:['192.168.1.10:9100']
metrics_path:/metrics
4. node_exporter 部署
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
# 创建 systemd 服务类似 Prometheus
三、关键 Exporters 配置与服务指标采集
1. systemd_exporter(强烈推荐) 监控所有 Systemd 服务状态:
# 启动参数
--collector.unit-include=nginx.service|mysql.service|redis.service
重要指标:
systemd_unit_state{state="active"}systemd_unit_start_time_secondssystemd_unit_restart_total
2. 常用服务 Exporters
- Nginx:nginx_exporter 或 nginxlog_exporter
- PostgreSQL:postgres_exporter
- Blackbox:黑盒探测(HTTP、TCP、ICMP)
3. 自定义服务指标暴露 在应用代码中使用 Prometheus Client Library(Go、Java、Python 等)暴露自定义指标:
app_http_requests_total{path="/api/user", method="POST"}app_db_connection_pool_size
四、Grafana 仪表盘构建实战
1. 安装 Grafana
apt install grafana -y # 或 yum
systemctl enable --now grafana-server
2. 推荐仪表盘(导入 ID)
3. 生产级仪表盘设计
变量设置(instance、job)实现多服务器切换。
五、Alertmanager 智能告警体系
1. Alertmanager 配置
route:
group_by:['alertname','cluster','service']
group_wait:30s
group_interval:5m
repeat_interval:4h
receiver:'wechat'
receivers:
-name:'wechat'
wechat_configs:
-corp_id:'xxx'
agent_id:'xxx'
api_secret:'xxx'
message:'{{ .CommonAnnotations.summary }}'
-name:'email'
email_configs:
-to:'ops@example.com'
2. Prometheus 告警规则(rules.yml)
groups:
-name:service_alerts
rules:
-alert:ServiceDown
expr:up{job="node_exporter"}==0
for:1m
labels:
severity:critical
annotations:
summary:"{{ $labels.instance }} 服务宕机"
-alert:HighCPUUsage
expr:100-(avgby(instance)(irate(node_cpu_seconds_total{mode="idle"}[5m]))*100)>85
for:5m
labels:
severity:warning
-alert:HighMemoryUsage
expr:(node_memory_MemTotal_bytes-node_memory_MemAvailable_bytes)/node_memory_MemTotal_bytes*100>90
for:3m
3. 告警抑制与静默
六、生产环境最佳实践
- Prometheus 联邦 + Remote Write 到 Thanos/Cortex/VictoriaMetrics 长期存储
- Alertmanager 集群(--cluster.peer)
- 避免高基数标签(user_id、session_id 等)
- HTTPS + Basic Auth / OAuth2
七、常见监控故障排查案例
案例1:指标采集不到 排查:
curl http://target:9100/metrics
promtool check config prometheus.yml
journalctl -u prometheus
案例2:告警风暴 解决:增加 for: 时间、合理分组、使用 inhibition_rules。
案例3:Grafana 查询慢 优化:使用 recording rules 预聚合、增加 Grafana 查询缓存、升级 VictoriaMetrics。
案例4:systemd 服务状态不准确 解决:确保 systemd_exporter 使用最新版本,并正确配置 --collector.unit-exclude。
案例5:内存泄漏未及时发现 方案:增加 process_resident_memory_bytes + predict_linear() 趋势预测告警。