为什么需要监控系统?

服务器数量超过 3 台之后,靠人工巡检就不现实了。你需要一个监控系统帮你 7×24 小时盯着:服务挂了自动告警、磁盘快满了提前通知、流量突增第一时间发现。

本文对比 Zabbix 和 Prometheus 两个主流监控方案,并给出实际搭建步骤。

一、监控方案选型

1.1 Zabbix vs Prometheus

对比维度ZabbixPrometheus
架构中心化 Server-Agent拉模式 Server-Exporter
安装难度中等,需要 LAMP/LEMP 环境简单,二进制直接运行
数据存储MySQL/PostgreSQL 关系库自研 TSDB 时序数据库
告警机制内置触发器+动作独立 Alertmanager 组件
可视化自带图表,可接 Grafana默认无,必须接 Grafana
适合场景传统 IDC、网络设备、标准监控云原生、容器、微服务、K8s
学习曲线中等较高
社区生态模板丰富、国内用户多社区活跃、CNCF 毕业项目

选型建议:

  • Zabbix:传统 IDC 机房、Windows/Linux 混用、网络设备多、团队监控经验少 → Zabbix
  • Prometheus:Kubernetes 容器环境、微服务架构、大数据量高维度、更现代的架构 → Prometheus
  • 两个都用:Zabbix 管基础设施,Prometheus 管应用层,Grafana 统一展示

二、Zabbix 实战搭建(15 分钟跑起来)

2.1 服务端安装(CentOS 环境)

# 安装 Zabbix 源
rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rhel/8/x86_64/zabbix-release-7.0-1.el8.noarch.rpm

# 安装 Zabbix Server + Web + Agent
dnf install -y zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-sql-scripts zabbix-agent2

# 安装数据库
dnf install -y mysql-server
systemctl start mysqld
systemctl enable mysqld
# 创建 Zabbix 数据库
mysql -uroot -p
CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost';
SET GLOBAL log_bin_trust_function_creators = 1;
QUIT;
# 导入初始数据
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql -uzabbix -p zabbix

# 关闭刚刚开启的 log_bin 设置
mysql -uroot -p -e "SET GLOBAL log_bin_trust_function_creators = 0;"
# 配置 Zabbix Server
vim /etc/zabbix/zabbix_server.conf

关键配置项:

DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=your_strong_password
# 启动 Zabbix Server
systemctl restart zabbix-server
systemctl enable zabbix-server

# 检查启动状态
systemctl status zabbix-server
journalctl -u zabbix-server -f  # 实时查看日志

2.2 Web 界面配置

访问 http://你的服务器IP/zabbix,按照向导完成安装:

  1. 检查前置条件(PHP 参数等)
  2. 配置数据库连接
  3. 设置 Zabbix Server 名称
  4. 默认管理员账号:Admin / zabbix

2.3 添加被监控主机(Agent)

每台需要监控的服务器上安装 Zabbix Agent:

# 被监控服务器上操作
rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rhel/8/x86_64/zabbix-release-7.0-1.el8.noarch.rpm
dnf install -y zabbix-agent2

# 配置 Agent
vim /etc/zabbix/zabbix_agent2.conf
# 关键配置
Server=ZABBIX_SERVER_IP    # Zabbix Server 的 IP
ServerActive=ZABBIX_SERVER_IP
Hostname=web01             # 这台机器的主机名
# 启动 Agent
systemctl restart zabbix-agent2
systemctl enable zabbix-agent2

回到 Zabbix Web 界面:「配置」→「主机」→「创建主机」,填写主机名和 Agent IP,选择「Linux by Zabbix Agent」模板,稍等几分钟就能看到数据了。

2.4 配置关键告警

示例:磁盘使用率超过 85% 告警

「配置」→「动作」→「Trigger actions」→「创建动作」:

  • 名称:磁盘空间不足告警
  • 条件:触发器 = {Template OS Linux by Zabbix agent:Disk space utilization.avg(#1)}>85
  • 操作:发送告警到企业微信群机器人

企业微信机器人通知脚本:

#!/bin/bash
# /usr/lib/zabbix/alertscripts/wechat.sh

WEBHOOK_URL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY"
MESSAGE="$1"

curl -s -X POST "$WEBHOOK_URL"   -H "Content-Type: application/json"   -d "{
    "msgtype": "markdown",
    "markdown": {
      "content": "## ⚠️ Zabbix 告警\n$MESSAGE"
    }
  }"

三、Prometheus + Grafana 轻量方案

如果你觉得 Zabbix 太重,或者主要监控云原生应用,Prometheus 是更好的选择。

3.1 安装 Prometheus

# 下载 Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
tar xzf prometheus-2.53.0.linux-amd64.tar.gz
mv prometheus-2.53.0.linux-amd64 /opt/prometheus

# 创建 systemd 服务
cat > /etc/systemd/system/prometheus.service << 'EOF'
[Unit]
Description=Prometheus Server
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/prometheus
ExecStart=/opt/prometheus/prometheus   --config.file=/opt/prometheus/prometheus.yml   --storage.tsdb.path=/opt/prometheus/data   --web.listen-address=0.0.0.0:9090
Restart=always

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus

3.2 配置 prometheus.yml

global:
  scrape_interval: 15s      # 每15秒采集一次数据
  evaluation_interval: 15s  # 每15秒评估一次告警规则

scrape_configs:
  # Prometheus 自身监控
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  # 服务器基础监控 (Linux)
  - job_name: 'linux_servers'
    static_configs:
      - targets:
        - '192.168.1.10:9100'  # web01
        - '192.168.1.11:9100'  # web02
        - '192.168.1.20:9100'  # db01

3.3 被监控服务器安装 Node Exporter

每台被监控的服务器都需要安装 node_exporter 来暴露指标:

# 下载并安装 node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
tar xzf node_exporter-1.8.1.linux-amd64.tar.gz
mv node_exporter-1.8.1.linux-amd64/node_exporter /usr/local/bin/

# 创建 systemd 服务
cat > /etc/systemd/system/node_exporter.service << 'EOF'
[Unit]
Description=Prometheus Node Exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter   --web.listen-address=:9100   --path.rootfs=/
Restart=always

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter

# 查看是否正常采集数据(返回大量指标文本)
curl http://localhost:9100/metrics | head -20

3.4 安装 Grafana 展示面板

# CentOS 安装 Grafana
cat > /etc/yum.repos.d/grafana.repo << 'EOF'
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF

dnf install -y grafana
systemctl start grafana-server
systemctl enable grafana-server

访问 http://你的服务器IP:3000,默认账号密码都是 admin。

配置 Prometheus 数据源:

  1. 登录 Grafana → 设置 → Data Sources → Add data source
  2. 选择 Prometheus
  3. URL 填 http://localhost:9090
  4. 点击 Save & Test,看到绿色提示说明连接成功

导入现成仪表盘:

  1. 左侧菜单 → Dashboards → New → Import
  2. 输入 Dashboard ID 1860(Node Exporter Full 仪表盘)
  3. 选择 Prometheus 数据源
  4. 点击 Import,几秒钟后就能看到漂亮的监控面板

四、关键监控指标与告警规则

4.1 必须监控的指标

类别指标阈值说明
CPU使用率> 90%可能服务异常或资源不足
CPU平均负载> CPU核数×2系统过载
内存使用率> 90%可能内存泄漏
内存Swap 使用率> 50%内存紧张,需要扩内存
磁盘使用率> 85%需要清理或扩容
磁盘IO 等待> 30%磁盘性能瓶颈
网络带宽使用率> 80%需要升级带宽或优化流量
进程关键服务进程不存在服务挂了
系统SWAP 使用> 0物理内存不足

4.2 Prometheus 告警规则示例

# /opt/prometheus/rules/alerts.yml
groups:
  - name: server_alerts
    rules:
      - alert: 服务器宕机
        expr: up == 0
        for: 1m
        annotations:
          summary: "{{ $labels.instance }} 已宕机"
          description: "服务器 {{ $labels.instance }} 超过1分钟无数据"

      - alert: 磁盘使用率过高
        expr: (1 - (node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes{fstype!~"tmpfs|overlay"})) * 100 > 85
        for: 5m
        annotations:
          summary: "磁盘使用率过高 - {{ $labels.mountpoint }}"
          description: "{{ $labels.instance }} 的 {{ $labels.mountpoint }} 使用率已超过 85%"

      - alert: 内存使用率过高
        expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > 90
        for: 5m
        annotations:
          summary: "内存使用率过高 - {{ $labels.instance }}"
          description: "{{ $labels.instance }} 内存使用率已超过 90%(当前: {{ $value | humanizePercentage }})"

      - alert: CPU 负载过高
        expr: node_load15 > count(node_cpu_seconds_total{mode="idle"}) by(instance) * 2
        for: 10m
        annotations:
          summary: "CPU 负载过高 - {{ $labels.instance }}"
          description: "{{ $labels.instance }} 15分钟负载为 {{ $value }}"

在 prometheus.yml 中引入告警规则:

rule_files:
  - "rules/alerts.yml"

alerting:
  alertmanagers:
    - static_configs:
        - targets: ['localhost:9093']

4.3 安装 Alertmanager

wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
tar xzf alertmanager-0.27.0.linux-amd64.tar.gz
mv alertmanager-0.27.0.linux-amd64 /opt/alertmanager

cat > /etc/systemd/system/alertmanager.service << 'EOF'
[Unit]
Description=Alertmanager
After=network.target

[Service]
Type=simple
ExecStart=/opt/alertmanager/alertmanager   --config.file=/opt/alertmanager/alertmanager.yml
Restart=always

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl start alertmanager && systemctl enable alertmanager

配置企业微信告警接收:

# /opt/alertmanager/alertmanager.yml
global:
  resolve_timeout: 5m

route:
  receiver: 'wechat'
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h

receivers:
  - name: 'wechat'
    webhook_configs:
      - url: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY'
        send_resolved: true

五、告警通知方式一览

通知方式配置难度实时性适用场景
企业微信机器人简单即时团队内部首选
钉钉机器人简单即时阿里云环境常用
飞书机器人简单即时字节系团队
邮件中等1-5分钟延迟传统方式
短信高(需网关)即时严重告警
电话高(需网关)即时夜间紧急告警

六、监控系统部署后的日常维护

6.1 数据存储维护

Prometheus 数据默认只保留 15 天:

# 启动时指定保留时长(60天)
--storage.tsdb.retention.time=60d

Zabbix 数据库定期清理历史数据:

# Zabbix 自带 housekeeper 会自动清理老数据
# 也可以手动清理
mysql -uzabbix -p zabbix -e "DELETE FROM history WHERE clock < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));"

6.2 定期检查清单

  • [ ] 所有目标主机的 Agent/Exporter 是否在线
  • [ ] 告警规则是否正常触发
  • [ ] 磁盘空间(监控系统自身的磁盘也要监控)
  • [ ] 数据库大小是否合理
  • [ ] 告警接收人变更及时更新

七、总结

建立监控体系是运维工作从"被动救火"到"主动预防"的关键一步。

新手建议路径:

  1. 先上 Zabbix,不用太复杂监控服务器基础指标(CPU、内存、磁盘、网络)
  2. 配置几个核心告警:磁盘快满、服务挂了
  3. 熟悉了再上 Grafana 做可视化大屏
  4. 容器化之后可以上 Prometheus

两个值得注意的点:

  • 监控系统本身也需要监控——别出现"大家都没收到告警,因为监控服务器自己先挂了"
  • 告警不是越多越好——告警疲劳比没有告警更可怕,关键告警设置好,垃圾告警宁缺毋滥

最后送一句话:没有监控的系统,就像没有仪表盘的飞机——飞是能飞,但你不知道什么时候会掉下来。