跳转到内容
搜索 RunEntLinux 文档

仅搜索 RunEntLinux 通用文档,各发行版文档使用各自的搜索入口。

    浏览当前文档目录

    良好的系统监控是运维工作的基石。本文介绍从内置工具到专业监控栈的完整方案,帮助你在问题发生前发现隐患,在故障发生时快速定位根因。

    Cockpit 是 EL 系发行版自带的轻量级 Web 管理控制台,开箱即用,适合中小规模服务器的日常监控与管理。

    Terminal window
    # AlmaLinux/Rocky Linux 9 默认已预装 cockpit,如未安装:
    sudo dnf install cockpit -y
    # 启动并设为开机自启
    sudo systemctl enable --now cockpit.socket
    # 放行防火墙(Cockpit 使用 9090 端口)
    sudo firewall-cmd --permanent --add-service=cockpit
    sudo firewall-cmd --reload

    安装完成后,在浏览器中访问 https://你的服务器IP:9090,使用系统账号登录即可。

    Terminal window
    # 安装额外的管理模块
    sudo dnf install cockpit-storaged # 存储管理
    sudo dnf install cockpit-networkmanager # 网络管理
    sudo dnf install cockpit-podman # 容器管理
    sudo dnf install cockpit-machines # 虚拟机管理(需要 libvirt)

    Cockpit 支持从一台主机管理多台服务器。在主控机的 Cockpit 界面中添加远程主机即可,远程主机也需安装 Cockpit 并开放 9090 端口。

    Terminal window
    # 在被管理的远程服务器上执行同样的安装步骤
    sudo dnf install cockpit -y
    sudo systemctl enable --now cockpit.socket
    sudo firewall-cmd --permanent --add-service=cockpit
    sudo firewall-cmd --reload

    对于需要长期数据存储、灵活查询和告警的场景,Prometheus 是事实标准。

    node_exporter 运行在每台被监控的服务器上,收集系统指标。

    Terminal window
    # 创建专用用户
    sudo useradd --no-create-home --shell /sbin/nologin node_exporter
    # 示例版本:v1.12.1(2026-10 核对;装之前去 releases 页确认当前版本)
    cd /tmp
    curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.12.1/node_exporter-1.12.1.linux-amd64.tar.gz
    tar xzf node_exporter-1.12.1.linux-amd64.tar.gz
    sudo cp node_exporter-1.12.1.linux-amd64/node_exporter /usr/local/bin/
    sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

    创建 systemd 服务文件:

    Terminal window
    sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<'EOF'
    [Unit]
    Description=Prometheus Node Exporter
    After=network-online.target
    Wants=network-online.target
    [Service]
    User=node_exporter
    Group=node_exporter
    Type=simple
    ExecStart=/usr/local/bin/node_exporter \
    --collector.systemd \
    --collector.processes
    [Install]
    WantedBy=multi-user.target
    EOF
    sudo systemctl daemon-reload
    sudo systemctl enable --now node_exporter
    # 放行端口
    sudo firewall-cmd --permanent --add-port=9100/tcp
    sudo firewall-cmd --reload

    验证运行状态:

    Terminal window
    curl http://localhost:9100/metrics | head -20

    在监控主机上安装 Prometheus:

    Terminal window
    sudo useradd --no-create-home --shell /sbin/nologin prometheus
    # 创建目录
    sudo mkdir -p /etc/prometheus /var/lib/prometheus
    sudo chown prometheus:prometheus /var/lib/prometheus
    # 下载并安装
    cd /tmp
    # 示例版本:v3.15.0(2026-10 核对;v3 是当前主线,v2 不再跟进新特性,升级按官方 migration 说明)
    curl -LO https://github.com/prometheus/prometheus/releases/download/v3.15.0/prometheus-3.15.0.linux-amd64.tar.gz
    tar xzf prometheus-3.15.0.linux-amd64.tar.gz
    cd prometheus-3.15.0.linux-amd64
    sudo cp prometheus promtool /usr/local/bin/
    sudo cp -r consoles console_libraries /etc/prometheus/
    sudo chown -R prometheus:prometheus /etc/prometheus

    编写配置文件:

    Terminal window
    sudo tee /etc/prometheus/prometheus.yml > /dev/null <<'EOF'
    global:
    scrape_interval: 15s
    evaluation_interval: 15s
    scrape_configs:
    - job_name: "prometheus"
    static_configs:
    - targets: ["localhost:9090"]
    - job_name: "nodes"
    static_configs:
    - targets:
    - "192.168.1.10:9100"
    - "192.168.1.11:9100"
    - "192.168.1.12:9100"
    labels:
    env: "production"
    EOF
    sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml

    创建 systemd 服务:

    Terminal window
    sudo tee /etc/systemd/system/prometheus.service > /dev/null <<'EOF'
    [Unit]
    Description=Prometheus Monitoring
    After=network-online.target
    Wants=network-online.target
    [Service]
    User=prometheus
    Group=prometheus
    Type=simple
    ExecStart=/usr/local/bin/prometheus \
    --config.file=/etc/prometheus/prometheus.yml \
    --storage.tsdb.path=/var/lib/prometheus/ \
    --storage.tsdb.retention.time=30d \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries
    ExecReload=/bin/kill -HUP $MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
    sudo systemctl daemon-reload
    sudo systemctl enable --now prometheus
    sudo firewall-cmd --permanent --add-port=9090/tcp
    sudo firewall-cmd --reload

    访问 http://监控主机IP:9090 即可打开 Prometheus Web UI。

    Grafana 为 Prometheus 数据提供美观的仪表盘。

    Terminal window
    # 添加官方仓库
    sudo tee /etc/yum.repos.d/grafana.repo > /dev/null <<'EOF'
    [grafana]
    name=Grafana OSS
    baseurl=https://rpm.grafana.com
    repo_gpgcheck=1
    enabled=1
    gpgcheck=1
    gpgkey=https://rpm.grafana.com/gpg.key
    sslverify=1
    sslcacert=/etc/pki/tls/certs/ca-bundle.crt
    EOF
    sudo dnf install grafana -y
    sudo systemctl enable --now grafana-server
    # 放行端口
    sudo firewall-cmd --permanent --add-port=3000/tcp
    sudo firewall-cmd --reload

    访问 http://服务器IP:3000(默认账号密码均为 admin),然后:

    1. 进入 Configuration > Data Sources > Add data source
    2. 选择 Prometheus
    3. URL 填写 http://localhost:9090
    4. 点击 Save & Test

    推荐使用社区维护的 Node Exporter 仪表盘:

    1. 进入 Dashboards > Import
    2. 输入仪表盘 ID 1860(Node Exporter Full)
    3. 选择刚配置的 Prometheus 数据源
    4. 点击 Import

    在没有部署完整监控栈的场景下,简单脚本也能发挥作用。

    /usr/local/bin/check_system.sh
    #!/bin/bash
    # 检查系统负载、内存、磁盘使用率
    HOSTNAME=$(hostname)
    DATE=$(date '+%Y-%m-%d %H:%M:%S')
    LOAD=$(uptime | awk -F'load average:' '{print $2}' | xargs)
    UPTIME=$(uptime -p)
    # CPU 核心数(用于判断负载是否过高)
    CPU_CORES=$(nproc)
    # 内存使用率
    MEM_USAGE=$(free | awk '/Mem:/ {printf "%.1f%%", $3/$2 * 100}')
    # 磁盘使用率(检查所有挂载点)
    DISK_ALERT=""
    while read -r line; do
    usage=$(echo "$line" | awk '{print $5}' | tr -d '%')
    mount=$(echo "$line" | awk '{print $6}')
    if [ "$usage" -gt 85 ]; then
    DISK_ALERT="${DISK_ALERT} [警告] ${mount} 使用率 ${usage}%\n"
    fi
    done < <(df -h --type=ext4 --type=xfs | tail -n +2)
    echo "=============================="
    echo "系统状态报告 - ${DATE}"
    echo "主机名: ${HOSTNAME}"
    echo "运行时间: ${UPTIME}"
    echo "CPU 核心: ${CPU_CORES}"
    echo "负载均值: ${LOAD}"
    echo "内存使用: ${MEM_USAGE}"
    echo "=============================="
    if [ -n "$DISK_ALERT" ]; then
    echo -e "磁盘告警:\n${DISK_ALERT}"
    fi
    Terminal window
    chmod +x /usr/local/bin/check_system.sh
    /usr/local/bin/disk_alert.sh
    #!/bin/bash
    # 磁盘使用率超过阈值时发送告警邮件
    THRESHOLD=85
    MAILTO="admin@example.com"
    HOSTNAME=$(hostname)
    df -h --type=ext4 --type=xfs | tail -n +2 | while read -r line; do
    usage=$(echo "$line" | awk '{print $5}' | tr -d '%')
    partition=$(echo "$line" | awk '{print $1}')
    mount=$(echo "$line" | awk '{print $6}')
    if [ "$usage" -ge "$THRESHOLD" ]; then
    SUBJECT="[磁盘告警] ${HOSTNAME}: ${mount} 使用率 ${usage}%"
    BODY="主机: ${HOSTNAME}\n分区: ${partition}\n挂载点: ${mount}\n使用率: ${usage}%\n时间: $(date)"
    echo -e "$BODY" | mail -s "$SUBJECT" "$MAILTO"
    fi
    done
    /usr/local/bin/check_process.sh
    #!/bin/bash
    # 检查关键进程是否运行,如未运行则尝试重启并告警
    SERVICES=("nginx" "mysqld" "sshd")
    MAILTO="admin@example.com"
    HOSTNAME=$(hostname)
    for service in "${SERVICES[@]}"; do
    if ! systemctl is-active --quiet "$service"; then
    echo "[$(date)] ${service} 已停止,尝试重启..." >> /var/log/process_monitor.log
    systemctl start "$service"
    sleep 2
    if systemctl is-active --quiet "$service"; then
    STATUS="已自动恢复"
    else
    STATUS="重启失败,请立即处理!"
    fi
    SUBJECT="[进程告警] ${HOSTNAME}: ${service} ${STATUS}"
    echo "${SUBJECT}" | mail -s "$SUBJECT" "$MAILTO"
    fi
    done

    安装 Alertmanager:

    Terminal window
    cd /tmp
    # 示例版本:v0.34.1(2026-10 核对)
    curl -LO https://github.com/prometheus/alertmanager/releases/download/v0.34.1/alertmanager-0.34.1.linux-amd64.tar.gz
    tar xzf alertmanager-0.34.1.linux-amd64.tar.gz
    sudo cp alertmanager-0.34.1.linux-amd64/alertmanager /usr/local/bin/
    sudo cp alertmanager-0.34.1.linux-amd64/amtool /usr/local/bin/
    sudo mkdir -p /etc/alertmanager

    配置邮件告警:

    Terminal window
    sudo tee /etc/alertmanager/alertmanager.yml > /dev/null <<'EOF'
    global:
    smtp_smarthost: 'smtp.example.com:587'
    smtp_from: 'alertmanager@example.com'
    smtp_auth_username: 'alertmanager@example.com'
    smtp_auth_password: 'your_password'
    smtp_require_tls: true
    route:
    group_by: ['alertname', 'instance']
    group_wait: 30s
    group_interval: 5m
    repeat_interval: 4h
    receiver: 'email-admin'
    receivers:
    - name: 'email-admin'
    email_configs:
    - to: 'admin@example.com'
    send_resolved: true
    EOF

    创建告警规则文件:

    Terminal window
    sudo tee /etc/prometheus/alert_rules.yml > /dev/null <<'EOF'
    groups:
    - name: 系统告警
    rules:
    # 实例宕机
    - alert: InstanceDown
    expr: up == 0
    for: 2m
    labels:
    severity: critical
    annotations:
    summary: "实例 {{ $labels.instance }} 已宕机"
    description: "{{ $labels.instance }} 已超过 2 分钟无响应。"
    # CPU 使用率过高
    - alert: HighCpuUsage
    expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
    for: 5m
    labels:
    severity: warning
    annotations:
    summary: "{{ $labels.instance }} CPU 使用率过高"
    description: "CPU 使用率已超过 85%,当前值: {{ $value | printf \"%.1f\" }}%"
    # 内存使用率过高
    - alert: HighMemoryUsage
    expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 90
    for: 5m
    labels:
    severity: warning
    annotations:
    summary: "{{ $labels.instance }} 内存使用率过高"
    description: "内存使用率已超过 90%,当前值: {{ $value | printf \"%.1f\" }}%"
    # 磁盘空间不足
    - alert: DiskSpaceLow
    expr: (1 - node_filesystem_avail_bytes{fstype=~"ext4|xfs"} / node_filesystem_size_bytes) * 100 > 85
    for: 5m
    labels:
    severity: warning
    annotations:
    summary: "{{ $labels.instance }} 磁盘空间不足"
    description: "{{ $labels.mountpoint }} 使用率超过 85%,当前: {{ $value | printf \"%.1f\" }}%"
    EOF

    在 Prometheus 配置中引用告警规则和 Alertmanager:

    Terminal window
    # 在 /etc/prometheus/prometheus.yml 中添加:
    sudo tee /etc/prometheus/prometheus.yml > /dev/null <<'EOF'
    global:
    scrape_interval: 15s
    evaluation_interval: 15s
    alerting:
    alertmanagers:
    - static_configs:
    - targets:
    - localhost:9093
    rule_files:
    - "alert_rules.yml"
    scrape_configs:
    - job_name: "prometheus"
    static_configs:
    - targets: ["localhost:9090"]
    - job_name: "nodes"
    static_configs:
    - targets:
    - "192.168.1.10:9100"
    - "192.168.1.11:9100"
    EOF
    # 重载 Prometheus 配置
    sudo systemctl restart prometheus
    Terminal window
    # 编辑 crontab
    sudo crontab -e
    # 每 5 分钟检查磁盘
    */5 * * * * /usr/local/bin/disk_alert.sh
    # 每 2 分钟检查关键进程
    */2 * * * * /usr/local/bin/check_process.sh
    # 每天早上 8 点发送系统状态报告
    0 8 * * * /usr/local/bin/check_system.sh | mail -s "每日系统报告" admin@example.com

    日常运维中,以下命令可快速了解系统状态:

    Terminal window
    # 实时查看系统负载和进程
    top
    htop # 需要安装:dnf install htop
    # 查看负载均值
    uptime
    cat /proc/loadavg
    # 内存使用概况
    free -h
    # 磁盘使用概况
    df -hT
    du -sh /var/log/* # 查看某目录各子项大小
    # I/O 使用情况
    iostat -xz 1 5 # 需要安装:dnf install sysstat
    iotop # 需要安装:dnf install iotop
    # 网络连接统计
    ss -tunlp # 查看监听端口
    ss -s # 连接统计摘要
    # 查看实时网络流量
    iftop # 需要安装:dnf install iftop
    场景推荐方案
    单台服务器日常管理Cockpit
    快速检查Shell 脚本 + cron
    多台服务器长期监控Prometheus + node_exporter + Grafana
    告警通知Alertmanager / 脚本 + 邮件

    根据你的服务器规模和团队情况选择合适的方案。对于生产环境,建议至少部署 Prometheus + Grafana 以获得完整的可观测性。