当前位置:网站首页 >  攻略

资深运维亲授:10分钟搭建企业级服务器监控告警系统实操指南

时间:2026年06月16日 11:58:44 来源:易频IT社区

前置准备

你需要提前准备以下资源,所有操作均使用root权限执行,避免权限不足报错

  • 1台配置1核2G以上、操作系统为CentOS7+/Ubuntu20.04+的云服务器
  • 服务器安全组/防火墙开放80、9090、9100、3000、9093端口
  • 若需配置告警,提前准备钉钉/企业微信群的自定义机器人webhook地址

环境预配置

CentOS系统执行以下命令:

``` yum update -y yum install -y wget tar vim ```

Ubuntu系统执行以下命令:

``` apt update -y apt install -y wget tar vim ```

第一步:安装监控核心组件Prometheus

Prometheus负责采集、存储监控指标数据,执行以下命令安装:

``` 下载官方稳定版安装包 wget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz 解压 tar zxvf prometheus-2.47.0.linux-amd64.tar.gz 移动到系统软件目录 mv prometheus-2.47.0.linux-amd64 /usr/local/prometheus 创建数据存储目录 mkdir /usr/local/prometheus/data ```

配置系统服务实现开机自启,创建/usr/lib/systemd/system/prometheus.service文件,写入以下完整内容:

``` [Unit] Description=Prometheus After=network.target [Service] User=root ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data Restart=on-failure [Install] WantedBy=multi-user.target ```

启动服务并验证:

``` systemctl daemon-reload systemctl start prometheus systemctl enable prometheus 验证是否启动成功,返回healthy即为正常 curl localhost:9090/-/healthy ```

第二步:安装服务器指标采集组件Node Exporter

Node Exporter负责采集服务器CPU、内存、磁盘、网络等基础指标,执行以下命令安装:

``` wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz tar zxvf node_exporter-1.6.1.linux-amd64.tar.gz mv node_exporter-1.6.1.linux-amd64 /usr/local/node_exporter ```

配置系统服务,创建/usr/lib/systemd/system/node_exporter.service文件,写入以下内容:

``` [Unit] Description=Node Exporter After=network.target [Service] User=root ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target ```

启动服务并验证:

``` systemctl daemon-reload systemctl start node_exporter systemctl enable node_exporter 验证,返回大量指标数据即为正常 curl localhost:9100/metrics ```

配置Prometheus拉取Node Exporter数据,编辑/usr/local/prometheus/prometheus.yml,找到scrape_configs字段,在末尾添加以下内容:

``` - job_name: "node_exporter" static_configs: - targets: ["localhost:9100"] ```

重启Prometheus生效:systemctl restart prometheus

第三步:安装可视化监控面板Grafana

资深运维亲授:10分钟搭建企业级服务器监控告警系统实操指南

Grafana负责将Prometheus的指标数据转化为可视化图表,按操作系统执行对应命令:

CentOS系统:

``` wget https://dl.grafana.com/oss/release/grafana-10.1.0-1.x86_64.rpm yum install -y grafana-10.1.0-1.x86_64.rpm ```

Ubuntu系统:

``` wget https://dl.grafana.com/oss/release/grafana_10.1.0_amd64.deb dpkg -i grafana_10.1.0_amd64.deb apt install -f -y ```

启动服务:

``` systemctl start grafana-server systemctl enable grafana-server ```

配置可视化面板:

  • 访问http://你的服务器IP:3000,初始账号密码均为admin,首次登录后修改密码
  • 左侧菜单栏点击「齿轮图标-Data sources-Add data source」,选择Prometheus,URL栏填写http://localhost:9090,点击「Save & test」,提示成功即为配置完成
  • 左侧菜单栏点击「四个方块图标-Import」,在Import via grafana.com栏输入面板ID 8919,点击Load,数据源选择刚配置的Prometheus,点击Import即可看到完整的服务器监控面板

第四步:配置阈值告警

首先安装告警组件Alertmanager:

``` wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz tar zxvf alertmanager-0.25.0.linux-amd64.tar.gz mv alertmanager-0.25.0.linux-amd64 /usr/local/alertmanager ```

创建系统服务文件/usr/lib/systemd/system/alertmanager.service

``` [Unit] Description=Alertmanager After=network.target [Service] User=root ExecStart=/usr/local/alertmanager/alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml Restart=on-failure [Install] WantedBy=multi-user.target ```

配置告警规则,创建/usr/local/prometheus/rules.yml文件,写入以下完整规则:

``` groups: - name: server-alert rules: - alert: CPUHighUsage expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) 100) > 80 for: 5m labels: severity: warning annotations: summary: "告警:服务器 {{ $labels.instance }} CPU使用率过高" description: "CPU使用率当前为 {{ $value | printf \"%.2f\" }}%,已持续5分钟" - alert: MemoryHighUsage expr: 100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes 100) > 85 for: 3m labels: severity: warning annotations: summary: "告警:服务器 {{ $labels.instance }} 内存使用率过高" description: "内存使用率当前为 {{ $value | printf \"%.2f\" }}%,已持续3分钟" - alert: DiskHighUsage expr: 100 - (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"} 100) > 90 for: 2m labels: severity: critical annotations: summary: "告警:服务器 {{ $labels.instance }} 根分区磁盘使用率过高" description: "磁盘使用率当前为 {{ $value | printf \"%.2f\" }}%,已持续2分钟" ```

修改Prometheus配置文件/usr/local/prometheus/prometheus.yml,添加以下内容:

``` 关联告警规则文件 rule_files: - "rules.yml" 关联Alertmanager alerting: alertmanagers: - static_configs: - targets: ["localhost:9093"] ```

修改Alertmanager配置文件/usr/local/alertmanager/alertmanager.yml,替换为以下内容(钉钉为例,企业微信仅需修改webhook地址即可):

``` global: resolve_timeout: 5m route: group_by: ['alertname'] group_wait: 10s group_interval: 10s repeat_interval: 1h receiver: 'webhook' receivers: - name: 'webhook' webhook_configs: - url: '替换为你的钉钉机器人webhook地址' send_resolved: true ```

重启所有服务生效

``` systemctl restart prometheus systemctl start alertmanager systemctl enable alertmanager ```

测试告警:执行yum install -y stress && stress -c 4压测CPU,5分钟后即可收到告警通知,按Ctrl+C停止压测。

常见问题排查

  • 端口访问失败:CentOS执行firewall-cmd --add-port=端口号/tcp --permanent && firewall-cmd --reload,Ubuntu执行ufw allow 端口号/tcp,同时确认云服务器安全组已开放对应端口
  • Prometheus无数据:访问http://服务器IP:9090,点击「Status-Targets」,查看node_exporter状态是否为UP,若为DOWN检查Node Exporter是否正常启动、配置文件中target地址是否正确
  • 收不到告警:检查机器人是否配置了关键词,规则的summary中必须包含你设置的关键词(比如上文配置的“告警”),同时检查Alertmanager配置文件中的webhook地址是否正确
标签 资深运维

相关推荐

最新

热门

推荐

精选

标签

易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。

Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图