Linux 监控告警实战:Prometheus + Grafana,服务器状态监控
引言:凌晨 3 点的故障惊魂
上个月,我负责的服务器在凌晨 3 点挂了。
没有监控,我是第二天早上才发现的。
客户投诉电话打爆了:
"网站怎么打不开了?"
"订单无法提交了!"
"数据同步失败!"
我慌了,赶紧登录服务器查看:
• 磁盘 100% 满(日志写爆了)
• CPU 负载 100%(进程死循环)
• 内存耗尽(内存泄漏)
修复花了 2 小时,客户流失了 20%。
如果我有监控系统,这些问题提前 1 小时就能收到告警,半夜就修复了,根本不会等到早上。
从那以后,我下定决心:一定要搭建监控系统。
今天这篇文章,就是教你如何用 Prometheus + Grafana 搭建企业级监控告警系统,让服务器状态一目了然,问题第一时间发现。
一、什么是监控系统?为什么你需要它?
1.1 监控系统是什么?
监控系统:实时采集服务器指标,可视化展示,异常时告警
比喻:
• 服务器 = 你的身体
• 监控系统 = 体检仪表盘
• 告警系统 = 发烧时的温度计
1.2 为什么需要监控?
没有监控的风险:
• 故障发现滞后:用户先发现问题,你才知道
• 问题定位困难:不知道哪里出问题
• 被动响应:客户投诉后才处理
• 数据丢失风险:磁盘满了导致数据损坏
有监控的好处:
• ✅ 提前发现:CPU/内存/磁盘异常立即告警
• 快速定位:一眼看出哪个服务有问题
• 主动响应:用户还没发现就处理了
• 数据支持:历史数据分析,容量规划
二、Prometheus + Grafana 介绍
2.1 Prometheus
Prometheus:开源的监控数据采集和存储系统
特点:
• ✅ 拉取式采集:主动拉取目标数据
• 时序数据库:高性能存储时序数据
• PromQL:强大的查询语言
• 多种 Exporter:支持多种服务(Node、Nginx、MySQL 等)
2.2 Grafana
Grafana:开源的可视化平台
特点:
• ✅ 美观仪表板:拖拽式配置
• ✅ 多数据源:支持 Prometheus、InfluxDB、Elasticsearch
• ✅ 告警功能:多种告警渠道
• ✅ 插件丰富:面板、插件社区活跃
2.3 架构图
┌─────────────┐ │ 服务器 │ │ │ │ /metrics │ ←─── Prometheus 拉取 │ │ └─────────────┘ │ ↓ ┌─────────────┐ │ Prometheus │ │ │ └─────────────┘ │ ↓ ┌─────────────┐ │ Grafana │ ←─── 你在浏览器访问 │ │ └─────────────┘
三、安装 Prometheus
3.1 添加 Prometheus APT 仓库
sudo apt install software-properties-common sudo add-apt-repository ppa:prometheus-node/exporter sudo apt update3.2 安装 Prometheus
sudo apt install prometheus prometheus node-exporter -y3.3 配置 Prometheus
编辑配置文件:
sudo vim /etc/prometheus/prometheus.yml基础配置:
global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node' static_configs: - targets: ['localhost:9100']3.4 启动 Prometheus
sudo systemctl start prometheus sudo systemctl enable prometheus3.5 验证安装
curl http://localhost:9090/metrics输出:大量监控指标数据
四、安装 Grafana
4.1 添加 Grafana APT 仓库
sudo wget https://packages.grafana.com/gpg key sudo apt-key add - sudo echo "deb https://packages.grafana.com/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list4.2 安装 Grafana
sudo apt update sudo apt install grafana -y4.3 启动 Grafana
sudo systemctl start grafana-server sudo systemctl enable grafana-server4.4 访问 Grafana
打开浏览器:http://localhost:3000
默认登录:
• 用户名:admin
• 密码:admin
首次登录后会提示修改密码。
五、配置数据源
5.1 添加 Prometheus 数据源
步骤:
1. 登录 Grafana
2. 点击 Configuration → Data Sources → Add data source
3. 选择 Prometheus
4. 配置:
1. 点击 Save & Test
成功提示:"Data source is working"
六、实战案例 1:监控 Linux 系统指标
6.1 安装 Node Exporter
Node Exporter:采集 Linux 系统指标
已安装(前面安装 prometheus 时已包含)
验证:
curl http://localhost:9100/metrics6.2 配置 Prometheus 抓取
编辑配置:
sudo vim /etc/prometheus/prometheus.yml添加:
- job_name: 'node' static_configs: - targets: ['localhost:9100']重启 Prometheus:
sudo systemctl restart prometheus6.3 创建 Grafana 仪表板
步骤:
1. 登录 Grafana
2. 点击 + → Import
3. 输入 Dashboard ID(导入预定义仪表板)
4. Node Exporter Full(ID:1860)
效果:实时显示 CPU、内存、磁盘、网络指标
七、实战案例 2:监控 Nginx
7.1 启用 Nginx stub_status
编辑 Nginx 配置:
server { listen 127.0.0.1:8080; location /stub_status { stub_status on; access_log off; } }重启 Nginx:
sudo systemctl restart nginx7.2 配置 Prometheus 抓取
安装 Prometheus Exporter for Nginx:
cd /tmp git clone https://github.com/nginxinc/nginx-prometheus-exporter cd nginx-prometheus-exporter启动 Exporter:
./nginx-prometheus-exporter \ -nginx.scrape-uri=http://127.0.0.1:8080/stub_status \ -prometheus.uri=http://localhost:9090/metrics7.3 添加到 Prometheus
编辑配置:
- job_name: 'nginx' static_configs: - targets: ['localhost:9111']
八、实战案例 3:监控 MySQL
8.1 启用 MySQL 查询监控
安装 MySQL Exporter:
sudo apt install mysql-exporter -y配置数据库用户:
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'password' WITH GRANT PROCESS, REPLICATION CLIENT, REPLICATION SLAVE, SELECT ON *.* TO 'exporter'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;配置 Exporter:
sudo vim /etc/mysql/exporter/.my.cnf添加:
[client] user=exporter password=password host=127.0.0.1启动 Exporter:
mysqld_exporter \ --mysqld.address=127.0.0.1:3306 \ --datasource=performance_schema \ --collect.auto_increment.increment \ --collect.engine_innodb_rows \ --collect.table_schema
九、实战案例 4:监控 Docker 容器
9.1 安装 cAdvisor
cAdvisor:Google 出品的容器监控工具
docker run -d \ --name=cadvisor \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:ro \ --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:ro \ --publish=8080:8080 \ google/cadvisor:latest9.2 配置 Prometheus
添加配置:
- job_name: 'cadvisor' static_configs: - targets: ['localhost:8080']
十、配置告警
10.1 安装 Alertmanager
Prometheus 包含 Alertmanager
编辑配置:
sudo vim /etc/prometheus/alerts.yml示例告警规则:
groups: - name: alert rules: - alert: HighCPUUsage expr: 100 - (avg by (instance) (rate(node_cpu_seconds_total{mode!="idle"}[5m])) > 80 for: 5m labels: severity: warning annotations: summary: "High CPU usage detected"10.2 配置告警通知
邮件告警:
receivers: - name: 'email' email_configs: - to: admin@example.com from: alertmanager@example.com smarthost: localhost auth_username: admin auth_password: password钉钉告警:
receivers: - name: 'dingtalk' webhook_configs: - url: https://oapi.dingtalk.com/robot/send?access_token=xxx
十一、创建 Grafana 仪表板
11.1 系统监控仪表板
导入预定义面板:
Node Exporter Full(ID: 1860)
导入步骤:
1. 打开 Grafana
2. + → Import
3. 输入 1860
4. 选择 Prometheus 数据源
5. 点击 Import
11.2 创建自定义仪表板
创建新仪表板:
1. 点击 + → New Dashboard
2. 添加面板(Panel)
3. 选择查询(Query)
4. 配置可视化(Graph、Stat、Table)
示例查询:
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode!="idle"}[5m])) * 100效果图:实时 CPU 使用率曲线
十二、告警测试
12.1 触发告警
手动触发高 CPU:
等待 5 分钟,应该会触发告警。
12.2 验证告警
检查 Alertmanager 日志:
sudo journalctl -u prometheus-alertmanager -f检查 Grafana 通知:
查看 Dashboard → Notification 标签
十三、性能优化:避免监控拖垮服务器
13.1 降低采集频率
默认:15 秒采集一次
优化:30 秒或 1 分钟
修改配置:
global: scrape_interval: 1m13.2 保留时间序列
Prometheus 默认保留 15 天
修改配置:
global: retention: time: 30d13.3 压缩旧数据
定期清理:
curl -X POST http://localhost:9090/api/v1/delete_series \ --data '{"match[]": "{__name__=~\"job=\".*\"}"}
十四、监控最佳实践
14.1 监控关键指标
必监控指标:
1. CPU:
1. 内存:
1. 磁盘:
1. 网络:
1. 应用:
14.2 告警分级
P0 级(立即处理):
• 服务 down
• 数据库连接失败
• 磁�盘满了
P1 级(1 小时内处理):
• CPU > 90%
• 内存 > 95%
• 响应时间 > 5 秒
P2 级(1 天内处理):
• 磁盘 > 80%
• 预警规则触发
十五、总结:监控是最后一道防线
监控的价值:
• ✅ 提前发现:用户发现前就知道
• ✅ 快速定位:一眼看出哪里有问题
• ✅ 主动响应:客户投诉前就修复
• ✅ 数据支持:历史数据分析
记住:
监控不是"可有可无",而是"必须要有"。
没有监控的服务器就像盲人骑夜车,迟早出事。
最重要的是:今天就搭建,不要等。
如果觉得这篇文章有帮助,记得点赞、收藏、转发~
【互动话题】
你的服务器有监控吗?在评论区分享你的监控策略或问题,我会逐一回复~