📈 第03章 · node-exporter 采集
系统指标采集
node-exporter 采集¶
Prometheus 负责"拉",但数据得先有人"产生"。node-exporter 就是那台"把系统指标翻译成 Prometheus 格式"的采集器:它把 CPU、内存、磁盘、网络、文件系统等状态变成一群带名字和标签的指标,暴露在 /metrics 上供 Prometheus 定时来抓。这一章讲清楚它采集什么、怎么装、怎么验证。
node-exporter 是什么¶
Prometheus 官方有一整套 exporter 生态,node-exporter 专门采集宿主机/节点层面的系统指标(不采集数据库、nginx 这类具体应用,那些另有专属 exporter)。
- 本质是一个进程,监听
9100端口; - 暴露
http://localhost:9100/metrics的纯文本指标; - Prometheus 把它当作一个 target,定时来抓。
在这台服务器上,它由 systemd 托管,名为 prometheus-node-exporter:
systemctl status prometheus-node-exporter
# Active: active (running) since Mon 2026-09-07 20:39:40 UTC; 16h ago
ss -ltnp | grep 9100
# LISTEN ... *:9100 ... users:(("prometheus-node",pid=13977))
安装(Debian/Ubuntu):
sudo apt-get install -y prometheus-node-exporter
sudo systemctl enable --now prometheus-node-exporter
提示 Debian 包的服务名是
prometheus-node-exporter(带prometheus-前缀),不是裸的node_exporter。如果用 Go 官方二进制,运行文件名才是node_exporter。用systemctl status之前先确认包名。
采集哪些指标¶
node-exporter 按子系统分组,最重要的几类:
| 指标前缀 | 采集内容 | 典型例子 |
|---|---|---|
node_cpu_seconds_total |
CPU 各 mode 累计秒数 | {cpu="0",mode="idle"} |
node_memory_* |
内存 | MemTotal_bytes、MemAvailable_bytes |
node_filesystem_* |
文件系统 | size_bytes、free_bytes、avail_bytes |
node_disk_read_bytes_total |
磁盘读字节(累计) | 配 rate 转速度 |
node_disk_written_bytes_total |
磁盘写字节(累计) | 同左 |
node_network_receive_bytes_total |
网络收字节(累计) | 同左 |
node_network_transmit_bytes_total |
网络发字节(累计) | 同左 |
node_load1 / 5 / 15 |
平均负载 | 当前 1/5/15 分钟 |
node_procs_running |
运行中进程数 | 即时快照 |
node_boot_time_seconds |
开机时间戳 | 用于算 uptime |
node_uname_info |
系统信息(label) | 主机名 nodename |
直接抓一页看看真实长啥样:
curl -s http://localhost:9100/metrics | head -15
会看到形如:
# HELP node_cpu_seconds_total Seconds the CPUs spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="idle"} 54291.1
node_cpu_seconds_total{cpu="0",mode="iowait"} 10.4
node_cpu_seconds_total{cpu="0",mode="system"} 1358.34
node_cpu_seconds_total{cpu="0",mode="user"} 3693.79
关键指标的读法¶
CPU 是 Counter,要转成"使用率"必须先后做 rate/irate 和「1 - idle」,典型查询:
100 - (avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
本机实时值约 42.9(42.9%)——这个公式在监控报告和告警里都用得到。
内存是 Gauge,可以直接相除得使用率(注意都用字节,单位一致才可约):
(1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100
本机 MemTotal 约 33.65GB,可用约 54.5%,使用率约 45.5(对应的报告里 MEM 显示也印证了这一点)。
磁盘要挑对挂载点,并排除 tmpfs 等伪文件系统:
1 - node_filesystem_free_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}
本机根分区空闲约 7.4%,即已用约 92.6%——记在报告和告警里,根分区是高风险点。
网络/磁盘速度要用 rate + sum,把多块卡/多盘合并:
sum(rate(node_network_receive_bytes_total[5m]))
sum(rate(node_disk_read_bytes_total[5m]))
本机网络接收速率约 87672 字节/秒(约 85.6 KB/s)。
验证采集:让 Prometheus 真正抓到¶
配置好了 node-exporter,Prometheus 还要在 scrape_configs 里声明它(第 2 章已配置 job_name: node)。验证是否真的抓到手:
# target 是否 up
curl -s localhost:9090/api/v1/targets | grep node
# 通过 PromQL 查 node 的相关指标
curl -s "localhost:9090/api/v1/query" --data-urlencode 'query=up{job="node"}'
# 结果: up{instance="localhost:9100",job="node"} 1 ← 值为 1 表示抓取成功
指标值 1 代表 up,0 代表 down——这本身就是最重要的存活告警来源。
让采集更贴近生产¶
- 多 target:
static_configs.targets里可放多台主机,['node1:9100','node2:9100']; - 自动发现:生产环境目标会变,用
file_sd_configs(文件发现)或consul_sd_configs/kubernetes_sd_configs代替静态列表; - 认证:给 node-exporter 加
--web.config.file启用 HTTPS/Basic Auth,防止 /metrics 被任意读取(A 团队凭指标能算出你的机器负载)。
易错点¶
- 单位是字节不是 KB:
node_memory_*_bytes、node_network_*_bytes都是字节,做加法/除法时保持一致,换算成 KB/MB 才好看。 - Counter 不能直接当"当前值":所有
*_total都是累计量,要看速度必须套rate()或irate()。 - 磁盘要排除 tmpfs/overlay:不写
mountpoint="/"或fstype!="tmpfs"会混进一堆虚拟文件系统,数值失真。 - 服务名带前缀:Debian 包是
prometheus-node-exporter,不是node_exporter,查服务用对名字。 - 防火墙记得放行 9100:9000/9090 都通但 9100 不通,target 就一直
down。
相关命令¶
systemctl status prometheus-node-exporter、curl -s localhost:9100/metrics、apt-get install prometheus-node-exporter、curl -s localhost:9090/api/v1/targets、node_exporter --version(官方二进制)。