在运维工作中,及时发现服务器异常至关重要。本文将详细介绍如何在 CentOS 系统中配置告警规则,帮助你实现对 CPU、内存、磁盘等关键指标的实时监控。无论你是刚接触 Linux 的小白,还是有一定经验的运维人员,都能轻松上手。

服务器在运行过程中可能会出现 CPU 使用率飙升、内存不足、磁盘空间耗尽等问题。如果没有及时告警,可能导致服务中断、数据丢失等严重后果。通过配置 CentOS告警规则配置,你可以提前收到通知,快速响应问题。
本文以 Prometheus + Alertmanager 为例进行演示。Prometheus 是一个开源的监控系统,Alertmanager 负责处理告警通知(如邮件、钉钉、企业微信等)。
首先,在你的 CentOS 服务器上安装 Node Exporter:
wget 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 <启动服务:
sudo useradd -rs /bin/false node_exportersudo systemctl daemon-reloadsudo systemctl start node_exportersudo systemctl enable node_exporter接下来,在 Prometheus 中定义告警规则。假设你已安装好 Prometheus,编辑其配置文件 prometheus.yml,添加以下内容:
rule_files: - "alert_rules.yml"scrape_configs: - job_name: 'node' static_configs: - targets: ['localhost:9100']然后创建告警规则文件 alert_rules.yml:
groups:- name: instance-alerts rules: - alert: HighCpuUsage expr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80 for: 2m labels: severity: warning annotations: summary: "High CPU usage on {{ $labels.instance }}" description: "CPU usage is above 80% for more than 2 minutes." - alert: LowMemory expr: (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 < 10 for: 2m labels: severity: critical annotations: summary: "Low memory on {{ $labels.instance }}" description: "Available memory is less than 10%." - alert: DiskSpaceLow expr: (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100 < 10 for: 2m labels: severity: critical annotations: summary: "Low disk space on {{ $labels.instance }}" description: "Root partition available space is less than 10%."这些规则分别监控:CPU使用率超过80%、可用内存低于10%、根分区磁盘空间低于10%。这正是 Linux服务器告警设置 的核心部分。
编辑 Alertmanager 配置文件 alertmanager.yml,以邮件为例:
global: smtp_smarthost: 'smtp.example.com:587' smtp_from: 'alert@example.com' smtp_auth_username: 'alert@example.com' smtp_auth_password: 'your_password'route: receiver: 'email-notifications'receivers:- name: 'email-notifications' email_configs: - to: 'admin@example.com'重启 Prometheus 和 Alertmanager 使配置生效:
sudo systemctl restart prometheussudo systemctl restart alertmanager你可以通过访问 Prometheus Web UI(默认 http://your-server:9090)查看告警状态。当触发条件满足时,Alertmanager 会自动发送邮件通知。
至此,你已经完成了完整的 CentOS系统监控 与 Prometheus告警规则 配置流程!
通过本文,你学会了如何在 CentOS 上部署监控组件、编写告警规则并配置通知渠道。合理的 CentOS告警规则配置 能极大提升系统稳定性与运维效率。建议根据实际业务需求调整阈值和通知方式。
小贴士:定期检查告警规则的有效性,避免“告警疲劳”——即太多无效告警导致真正的问题被忽略。
本文由主机测评网于2025-12-09发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025125292.html