Ubuntu告警规则配置全攻略(手把手教你设置Prometheus与Alertmanager实现系统监控告警)

来源:这里教程网 时间:2026-03-28 13:46:28 作者:

在现代运维工作中,Ubuntu告警规则配置是保障系统稳定运行的关键环节。通过合理设置监控与告警,可以提前发现潜在问题,避免服务中断。本文将带你从零开始,使用开源工具 Prometheus 和 Alertmanager,在 Ubuntu 系统上搭建一套完整的Ubuntu系统监控与告警体系。

一、准备工作

在开始之前,请确保你的 Ubuntu 系统已安装以下组件:

Prometheus(用于采集指标) Node Exporter(用于暴露系统指标) Alertmanager(用于处理告警通知)

如果尚未安装,可参考官方文档或使用以下命令快速安装 Node Exporter:

# 下载并安装 Node Exporterwget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gztar xvfz node_exporter-1.7.0.linux-amd64.tar.gzcd node_exporter-1.7.0.linux-amd64sudo cp node_exporter /usr/local/bin/# 创建 systemd 服务sudo tee /etc/systemd/system/node_exporter.service <

二、配置 Prometheus 告警规则

Prometheus 通过 YAML 文件定义Prometheus告警设置。我们将在

/etc/prometheus/rules/
目录下创建一个告警规则文件。

首先创建规则目录:

sudo mkdir -p /etc/prometheus/rules

然后编辑告警规则文件

system_alerts.yml

sudo nano /etc/prometheus/rules/system_alerts.yml

在文件中添加以下内容(这是一个典型的 CPU、内存和磁盘使用率过高告警规则):

groups:- name: system_alerts rules: - alert: HighCpuUsage expr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80 for: 5m labels: severity: warning annotations: summary: "High CPU usage on {{ $labels.instance }}" description: "CPU usage is above 80% for the last 5 minutes." - alert: HighMemoryUsage expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 85 for: 5m labels: severity: warning annotations: summary: "High memory usage on {{ $labels.instance }}" description: "Memory usage is above 85% for the last 5 minutes." - alert: DiskSpaceLow expr: (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100 < 10 for: 10m labels: severity: critical annotations: summary: "Low disk space on {{ $labels.instance }}" description: "Root filesystem has less than 10% space left."

三、修改 Prometheus 主配置文件

编辑 Prometheus 配置文件

/etc/prometheus/prometheus.yml
,加入告警规则路径和 Alertmanager 地址:

rule_files: - "rules/system_alerts.yml"alerting: alertmanagers: - static_configs: - targets: - localhost:9093

保存后重启 Prometheus 服务:

sudo systemctl restart prometheus

四、配置 Alertmanager 发送告警通知

Alertmanager 负责接收 Prometheus 的告警,并通过邮件、Slack、Webhook 等方式通知你。下面是一个简单的邮件通知配置示例。

编辑 Alertmanager 配置文件

/etc/alertmanager/alertmanager.yml

global: smtp_smarthost: 'smtp.gmail.com:587' smtp_from: 'your_email@gmail.com' smtp_auth_username: 'your_email@gmail.com' smtp_auth_password: 'your_app_password'route: receiver: 'email-notifications'receivers:- name: 'email-notifications' email_configs: - to: 'admin@example.com'

⚠️ 注意:若使用 Gmail,请开启“两步验证”并生成“应用专用密码”代替普通密码。

重启 Alertmanager 使配置生效:

sudo systemctl restart alertmanager

五、验证告警是否生效

你可以通过以下方式测试:

访问 Prometheus Web 界面(默认 http://localhost:9090),点击 “Alerts” 查看告警状态。 手动触发高负载(如运行
stress --cpu 4
)观察是否触发 CPU 告警。 检查邮箱是否收到 Alertmanager 发送的告警邮件。

六、总结

通过本教程,你已经掌握了完整的 Ubuntu告警规则配置流程,包括 Prometheus 告警规则编写、Alertmanager 通知设置等关键步骤。这套方案不仅适用于个人服务器,也适合中小企业生产环境。

记住,良好的Alertmanager配置教程实践应遵循“少而精”的原则——只对真正需要人工干预的问题发出告警,避免“告警疲劳”。

现在,你的 Ubuntu 系统已经具备了智能监控与自动告警能力!快去试试吧!

相关推荐

热文推荐