Prometheus监控Linux服务器系统

Node_exporter部署脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# 按需修改
# CentOS7增强版部署脚本 - 支持多架构/安全增强/智能检测

# 全局配置
set -euo pipefail
VERSION="1.9.0"
GREEN='\e[32m'
RED='\e[31m'
RESET='\e[0m'

# 0. 系统检测
echo -e "${GREEN}[INFO] 系统初始化检测...${RESET}"
ARCH=$(uname -m)
case $ARCH in
x86_64) ARCH="amd64";;
aarch64) ARCH="arm64";;
*) echo -e "${RED}[ERROR] 不支持的架构: $ARCH${RESET}"; exit 1 ;;
esac

# 1. 修复yum源(仅限CentOS)
if grep -q "CentOS" /etc/os-release; then
echo -e "${GREEN}[INFO] 修复yum源...${RESET}"
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo || {
echo -e "${RED}[ERROR] yum源更新失败${RESET}";
exit 1;
}
sudo yum clean all
sudo yum makecache
fi

# 2. 依赖检查与安装
declare -a DEPS=("wget" "tar" "openssl")
for pkg in "${DEPS[@]}"; do
if ! command -v $pkg &> /dev/null; then
echo -e "${GREEN}[INFO] 安装依赖: $pkg...${RESET}"
sudo yum install -y $pkg || {
echo -e "${RED}[ERROR] $pkg 安装失败${RESET}";
exit 1;
}
fi
done

# 3. 下载并解压(多架构支持)
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${VERSION}/node_exporter-${VERSION}.linux-${ARCH}.tar.gz"
echo -e "${GREEN}[INFO] 下载 Node Exporter...${RESET}"
if ! wget -q -O node_exporter.tar.gz "$DOWNLOAD_URL"; then
echo -e "${RED}[ERROR] 下载失败,请检查版本或网络${RESET}"
exit 1
fi
tar zxf node_exporter.tar.gz || { echo -e "${RED}[ERROR] 解压失败${RESET}"; exit 1; }

# 4. 安全部署
echo -e "${GREEN}[INFO] 安全部署配置...${RESET}"
[ -d /opt/prometheus/ ] || sudo mkdir -p /opt/prometheus/
sudo mv "node_exporter-${VERSION}.linux-${ARCH}/" "/opt/prometheus/node_exporter/"
sudo mkdir -p "/opt/prometheus/node_exporter/data"


# 5. 用户权限管理
if ! id "prometheus" &>/dev/null; then
echo -e "${GREEN}[INFO] 创建 prometheus 用户...${RESET}"
sudo useradd -r -s /sbin/nologin prometheus || {
echo -e "${RED}[ERROR] 用户创建失败${RESET}";
exit 1;
}
fi
sudo chown -R prometheus:prometheus "/opt/prometheus/node_exporter"

# 6. 服务配置(动态路径)
cat <<EOF | sudo tee /etc/systemd/system/node_exporter.service > /dev/null
[Unit]
Description=Node Exporter
Documentation=https://prometheus.io/docs/guides/node-exporter/
After=network.target

[Service]
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/node_exporter/node_exporter \
--web.listen-address=0.0.0.0:9100 \
--collector.systemd \
--collector.systemd.unit-whitelist="(ssh|docker|prometheus).service"
Restart=on-failure
RestartSec=30s
LimitNOFILE=65536
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
EOF

# 7. 防火墙配置
configure_firewall() {
if systemctl is-active --quiet firewalld; then
sudo firewall-cmd --permanent --add-port=9100/tcp && \
sudo firewall-cmd --reload
elif command -v iptables &> /dev/null; then
sudo iptables -A INPUT -p tcp --dport 9100 -j ACCEPT
sudo service iptables save
else
echo -e "${RED}[WARN] 未检测到防火墙服务,请手动开放端口 9100${RESET}"
fi
}
configure_firewall

# 8. 启动服务
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter

# 9. 部署验证
check_service() {
if ! systemctl is-active --quiet node_exporter; then
echo -e "${RED}[ERROR] 服务启动失败,查看日志:journalctl -u node_exporter -n 50${RESET}"
exit 1
fi
echo -e "${GREEN}[SUCCESS] 服务已启动,监听端口:${RESET}"
ss -tunlp | grep 9100
curl -s http://localhost:9100/metrics | grep 'node_' | head -5
}
check_service

添加配置

vim prometheus/prometheus.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
# 定义一个名为 node-exporter 的采集任务
- job_name: 'node-exporter'
# 采集间隔为15秒
scrape_interval: 15s
static_configs:
# 采集本地 Node Exporter 数据
- targets: ['node_exporter:9100']
labels:
instance: Prometheus服务器
# 采集 IP 为 192.168.56.102 的 Node Exporter 数据
- targets: ['192.168.56.102:9100']
labels:
instance: 102服务器

常用监控指标

http://192.168.56.101:9090/graph

CPU 监控指标

指标名称含义备注
node_cpu_seconds_total{mode="idle"}CPU在空闲模式下花费的总秒数计算使用率:(1 - avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance)
node_cpu_seconds_total{mode="user"}CPU在用户模式下花费的总秒数
node_cpu_seconds_total{mode="system"}CPU在系统模式下花费的总秒数
node_load11分钟内的系统平均负载建议结合CPU核心数评估,当值 > CPU核心数说明负载过高
node_load55分钟内的系统平均负载长期负载指标,反映系统持续压力
node_load1515分钟内的系统平均负载趋势分析参考值

内存监控指标

指标名称含义备注
node_memory_MemTotal_bytes系统总内存大小(字节)单位转换:/ (1024^3) 转换为GB
node_memory_MemFree_bytes空闲内存量(字节)
node_memory_MemAvailable_bytes可用内存量(字节)更准确的内存可用指标(>=3.14内核版本)
node_memory_Buffers_bytes缓冲区内存量(字节)
node_memory_Cached_bytes缓存内存量(字节)
node_memory_SwapTotal_bytes总交换空间大小(字节)
node_memory_SwapFree_bytes空闲交换空间大小(字节)

磁盘监控指标

指标名称含义备注
node_disk_read_bytes_total磁盘读取总字节数配合rate()函数计算IO吞吐量:rate(node_disk_read_bytes_total[5m])
node_disk_written_bytes_total磁盘写入总字节数配合rate()函数计算IO吞吐量:rate(node_disk_written_bytes_total[5m])
node_disk_reads_completed_total完成的磁盘读操作总数
node_disk_writes_completed_total完成的磁盘写操作总数
node_disk_io_time_seconds_total磁盘I/O操作所花费的总时间(秒)

文件系统监控指标

指标名称含义备注
node_filesystem_free_bytes{mount="/"}根分区剩余空间(字节)预测空间耗尽:predict_linear(node_filesystem_free_bytes[6h], 6*3600)
node_filesystem_size_bytes文件系统总大小(字节)
node_filesystem_avail_bytes文件系统可用空间(字节)
node_filesystem_files文件系统中文件总数
node_filesystem_files_free文件系统中可用文件数
node_filesystem_size_bytes文件系统总大小(字节)查询总磁盘大小: node_filesystem_size_bytes{fstype!="", mountpoint="/"}
node_filesystem_avail_bytes文件系统可用空间(字节)查询空闲磁盘大小: node_filesystem_avail_bytes{fstype!="", mountpoint="/"}

网络监控指标

指标名称含义备注
node_network_receive_bytes_total网络接收总字节数按设备筛选:{device="eth0"}
node_network_transmit_bytes_total网络发送总字节数按设备筛选:{device="eth0"}
node_network_receive_errs_total网络接收错误总数
node_network_transmit_errs_total网络发送错误总数
node_network_receive_drop_total网络接收丢弃总数
node_network_transmit_drop_total网络发送丢弃总数

常用监控指标报警

放置配置文件

linux_resource_alerts.yml 上传至 Prometheus 配置目录的 rules 子目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
groups:
- name: linux_cpu_alerts
rules:
# CPU 整体使用率严重过高(严重级别)
- alert: CPU使用率严重过高
expr: 100 - (avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) * 100) > 90
for: 5m
labels:
severity: critical
resource: cpu
annotations:
summary: "Linux实例CPU使用率严重过高"
description: "实例 {{ $labels.instance }} 的CPU使用率已持续5分钟超过90%,当前使用率:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `top`/`htop` 查看占用CPU的进程
2. 检查是否有异常进程(如死循环、资源泄漏)
3. 必要时重启高占用进程或扩容CPU

# CPU 整体使用率偏高(警告级别)
- alert: CPU使用率偏高
expr: 100 - (avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) * 100) > 70
for: 10m
labels:
severity: warning
resource: cpu
annotations:
summary: "Linux实例CPU使用率偏高"
description: "实例 {{ $labels.instance }} 的CPU使用率已持续10分钟超过70%,当前使用率:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `top -b -n 1` 记录当前进程CPU占用
2. 观察后续趋势,若持续升高需排查异常进程

# CPU 内核等待IO时间过长
- alert: CPU内核等待IO时间过长
expr: avg(irate(node_cpu_seconds_total{mode="iowait"}[5m])) by (instance) * 100 > 30
for: 8m
labels:
severity: warning
resource: cpu_iowait
annotations:
summary: "Linux实例CPU内核等待IO时间过长"
description: "实例 {{ $labels.instance }} 的CPU iowait占比已持续8分钟超过30%,当前占比:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `iostat -x 1` 查看磁盘IO瓶颈
2. 检查是否有大量随机读写操作,优化磁盘IO密集型任务

- name: linux_memory_alerts
rules:
# 物理内存使用率严重过高
- alert: 物理内存使用率严重过高
expr: 100 - (
(node_memory_Active_anon_bytes + node_memory_Inactive_anon_bytes + node_memory_Active_file_bytes + node_memory_Inactive_file_bytes)
/ node_memory_Total_bytes * 100
) < 10
for: 5m
labels:
severity: critical
resource: memory
annotations:
summary: "Linux实例物理内存使用率严重过高"
description: "实例 {{ $labels.instance }} 的剩余物理内存已持续5分钟低于10%,当前剩余内存:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `free -h` 查看内存使用概况
2. 执行 `ps aux --sort=-%mem` 定位高内存进程
3. 检查是否存在内存泄漏,必要时重启进程

# 物理内存使用率偏高
- alert: 物理内存使用率偏高
expr: 100 - (
(node_memory_Active_anon_bytes + node_memory_Inactive_anon_bytes + node_memory_Active_file_bytes + node_memory_Inactive_file_bytes)
/ node_memory_Total_bytes * 100
) < 20
for: 10m
labels:
severity: warning
resource: memory
annotations:
summary: "Linux实例物理内存使用率偏高"
description: "实例 {{ $labels.instance }} 的剩余物理内存已持续10分钟低于20%,当前剩余内存:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `vmstat 1` 观察内存交换情况
2. 若为临时高内存占用,评估是否需要扩容内存

# Swap分区使用率过高
- alert: Swap分区使用率过高
expr: node_memory_SwapUsed_bytes / node_memory_SwapTotal_bytes * 100 > 80
for: 5m
labels:
severity: critical
resource: swap
annotations:
summary: "Linux实例Swap分区使用率过高"
description: "实例 {{ $labels.instance }} 的Swap分区使用率已持续5分钟超过80%,当前使用率:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `swapon --show` 查看Swap配置
2. 优先解决物理内存不足问题,避免频繁Swap导致系统卡顿

- name: linux_disk_alerts
rules:
# 磁盘分区使用率严重过高
- alert: 磁盘分区使用率严重过高
expr: 100 - (node_filesystem_avail_bytes{mountpoint!~"/tmp|/run|/sys|/proc|/dev.*"} / node_filesystem_size_bytes{mountpoint!~"/tmp|/run|/sys|/proc|/dev.*"} * 100) > 90
for: 5m
labels:
severity: critical
resource: disk_usage
annotations:
summary: "Linux实例磁盘分区使用率严重过高"
description: "实例 {{ $labels.instance }} 的磁盘分区 {{ $labels.mountpoint }} 使用率已持续5分钟超过90%,当前使用率:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `df -h {{ $labels.mountpoint }}` 确认分区使用
2. 执行 `du -sh {{ $labels.mountpoint }}/* | sort -rh | head -10` 定位大文件/目录
3. 清理无用日志或备份数据

# 磁盘分区使用率偏高
- alert: 磁盘分区使用率偏高
expr: 100 - (node_filesystem_avail_bytes{mountpoint!~"/tmp|/run|/sys|/proc|/dev.*"} / node_filesystem_size_bytes{mountpoint!~"/tmp|/run|/sys|/proc|/dev.*"} * 100) > 80
for: 10m
labels:
severity: warning
resource: disk_usage
annotations:
summary: "Linux实例磁盘分区使用率偏高"
description: "实例 {{ $labels.instance }} 的磁盘分区 {{ $labels.mountpoint }} 使用率已持续10分钟超过80%,当前使用率:{{ $value | humanizePercentage }}"
suggestion: |
1. 检查分区是否有持续增长的日志/数据文件
2. 提前规划磁盘扩容或数据清理策略

# 磁盘IO负载过高
- alert: 磁盘IO负载过高
expr: avg(irate(node_disk_io_time_seconds_total[5m])) by (instance, device) * 100 > 95
for: 5m
labels:
severity: critical
resource: disk_io
annotations:
summary: "Linux实例磁盘IO负载过高"
description: "实例 {{ $labels.instance }} 的磁盘 {{ $labels.device }} IO繁忙度已持续5分钟超过95%,当前繁忙度:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `iostat -x 1 {{ $labels.device }}` 查看IO详情
2. 优化IO密集型任务(如批量写入改为顺序写入)

- name: linux_network_alerts
rules:
# 网络出口流量过高
- alert: 网络出口流量过高
expr: irate(node_network_transmit_bytes_total{device!~"lo|docker.*|veth.*"}[5m]) * 8 / 1024 / 1024 > 500
for: 5m
labels:
severity: critical
resource: network_out
annotations:
summary: "Linux实例网络出口流量过高"
description: "实例 {{ $labels.instance }} 的网卡 {{ $labels.device }} 出口流量已持续5分钟超过500Mbps,当前流量:{{ $value | humanizeFloat }} Mbps"
suggestion: |
1. 执行 `iftop -i {{ $labels.device }}` 查看实时流量
2. 排查异常流量来源,必要时限制非必要流量

# 网络丢包率过高
- alert: 网络丢包率过高
expr: irate(node_network_transmit_drop_total{device!~"lo|docker.*|veth.*"}[5m]) / irate(node_network_transmit_packets_total{device!~"lo|docker.*|veth.*"}[5m]) * 100 > 1
for: 3m
labels:
severity: critical
resource: network_loss
annotations:
summary: "Linux实例网络丢包率过高"
description: "实例 {{ $labels.instance }} 的网卡 {{ $labels.device }} 发送丢包率已持续3分钟超过1%,当前丢包率:{{ $value | humanizePercentage }}"
suggestion: |
1. 执行 `ethtool -S {{ $labels.device }}` 查看网卡错误统计
2. 检查网络链路是否故障,或网卡是否过载

引用规则文件

编辑 Prometheus 主配置文件(通常为 prometheus.yml),在 rule_files 字段中添加规则文件路径:

1
2
3
4
5
global:
scrape_interval: 15s # 采集间隔,默认15秒
# 引用报警规则文件
rule_files:
- "rules/linux_resource_alerts.yml" # 或者 rules/*.yml

验证配置合法性

使用 Prometheus 自带工具 promtool 检查配置文件语法,避免因格式错误导致 Prometheus 启动失败:

1
2
3
4
# 检查规则文件
promtool check rules rules/linux_resource_alerts.yml
# 检查主配置文件(可选)
promtool check config prometheus.yml

若输出 SUCCESS: 15 rules found(共 15 条规则),表示配置合法。

重启 Prometheus 生效

根据 Prometheus 启动方式执行重启命令,示例:

  • 系统服务方式(systemd):
1
2
systemctl restart prometheus
systemctl status prometheus # 确认启动成功
  • 二进制启动方式:
1
2
3
4
# 先停止旧进程(假设进程ID为 12345)
kill 12345
# 重新启动
./prometheus --config.file=/etc/prometheus/prometheus.yml &

生效验证

  1. 访问 Prometheus Web 界面(默认端口 9090),路径:http://PrometheusIP:9090/rules
  2. 在规则列表中找到 linux_resource_alerts.yml 对应的 4 个规则组,确认「State」为「OK」(无报警时)或「Pending/Firing」(触发报警时);
  3. 任选一条规则,点击「Execute」可验证 expr 表达式是否能正常查询到数据(确保 node_exporter 指标采集正常)。

请查看:Prometheus 监控常用应用配置指南| 墨颜丶

Targets

Alerts