prometheus/node_exporter
GitHub: prometheus/node_exporter
Prometheus 的官方主机指标导出器,用于采集硬件与操作系统层面的运行时数据。
Stars: 13625 | Forks: 2680
# Node exporter
[](https://github.com/prometheus/node_exporter/actions/workflows/ci.yml)


[][quay]
[][hub]
[][goreportcard]
用于 \*NIX 内核暴露的硬件和操作系统指标的 Prometheus exporter,使用 Go 编写并支持可插拔的指标收集器。
建议 Windows 用户使用 [Windows exporter](https://github.com/prometheus-community/windows_exporter)。
要暴露 NVIDIA GPU 指标,可以使用 [prometheus-dcgm
](https://github.com/NVIDIA/dcgm-exporter)。
## 安装与使用
如果您是 Prometheus 和 `node_exporter` 的新手,可以参考[简单的分步指南](https://prometheus.io/docs/guides/node-exporter/)。
`node_exporter` 默认监听 HTTP 端口 9100。有关更多选项,请参阅 `--help` 输出。
### Ansible
如果要使用 [Ansible](https://www.ansible.com/) 进行自动化安装,可以使用 [Prometheus 社区 role](https://github.com/prometheus-community/ansible)。
### Docker
`node_exporter` 旨在监控主机系统。在容器中部署需要格外小心,以免监控到容器本身。
在需要容器化部署的情况下,必须使用一些额外的 flag,以允许 `node_exporter` 访问主机命名空间。
请注意,您想要监控的任何非 root 挂载点都需要绑定挂载到容器中。
如果您启动容器用于主机监控,请指定 `path.rootfs` 参数。
此参数必须与主机 root 绑定挂载中的路径匹配。node_exporter 将使用 `path.rootfs` 作为前缀来访问主机文件系统。
```
docker run -d \
--net="host" \
--pid="host" \
-v "/:/host:ro,rslave" \
quay.io/prometheus/node-exporter:latest \
--path.rootfs=/host
```
对于 Docker compose,也需要进行类似的 flag 修改。
```
---
version: '3.8'
services:
node_exporter:
image: quay.io/prometheus/node-exporter:latest
container_name: node_exporter
command:
- '--path.rootfs=/host'
network_mode: host
pid: host
restart: unless-stopped
volumes:
- '/:/host:ro,rslave'
```
在某些系统上,`timex` 收集器需要额外的 Docker flag `--cap-add=SYS_TIME`,以便访问所需的系统调用。
## 收集器
每个操作系统对收集器的支持程度各不相同。下表列出了所有现有的收集器及其支持的系统。
通过提供 `--collector.` flag 可以启用收集器。
默认启用的收集器可以通过提供 `--no-collector.` flag 来禁用。
要仅启用某些特定的收集器,请使用 `--collector.disable-defaults --collector. ...`。
### 包含与排除 flags
一些收集器可以通过专用的 flags 配置为包含或排除特定模式。排除 flags 用于表示“除了...之外的所有”,而包含 flags 用于表示“仅限...”。请注意,这些 flags 在同时支持它们的收集器上是互斥的。
示例:
```
--collector.filesystem.mount-points-exclude=^/(dev|proc|sys|var/lib/docker/.+|var/lib/kubelet/.+)($|/)
```
列表:
收集器 | 范围 | 包含 Flag | 排除 Flag
--- | --- | --- | ---
arp | device | --collector.arp.device-include | --collector.arp.device-exclude
cpu | bugs | --collector.cpu.info.bugs-include | N/A
cpu | flags | --collector.cpu.info.flags-include | N/A
diskstats | device | --collector.diskstats.device-include | --collector.diskstats.device-exclude
ethtool | device | --collector.ethtool.device-include | --collector.ethtool.device-exclude
ethtool | metrics | --collector.ethtool.metrics-include | N/A
filesystem | fs-types | --collector.filesystem.fs-types-include | --collector.filesystem.fs-types-exclude
filesystem | mount-points | --collector.filesystem.mount-points-include | --collector.filesystem.mount-points-exclude
hwmon | chip | --collector.hwmon.chip-include | --collector.hwmon.chip-exclude
hwmon | sensor | --collector.hwmon.sensor-include | --collector.hwmon.sensor-exclude
interrupts | name | --collector.interrupts.name-include | --collector.interrupts.name-exclude
netdev | device | --collector.netdev.device-include | --collector.netdev.device-exclude
qdisk | device | --collector.qdisk.device-include | --collector.qdisk.device-exclude
slabinfo | slab-names | --collector.slabinfo.slabs-include | --collector.slabinfo.slabs-exclude
sysctl | all | --collector.sysctl.include | N/A
systemd | unit | --collector.systemd.unit-include | --collector.systemd.unit-exclude
### 默认启用
名称 | 描述 | 操作系统
---------|-------------|----
arp | 从 `/proc/net/arp` 暴露 ARP 统计信息。 | Linux
bcache | 从 `/sys/fs/bcache/` 暴露 bcache 统计信息。 | Linux
bonding | 暴露 Linux bonding 接口的已配置和活动从属设备数量。 | Linux
btrfs | 暴露 btrfs 统计信息 | Linux
boottime | 暴露从 `kern.boottime` sysctl 获取的系统启动时间。 | Darwin, Dragonfly, FreeBSD, NetBSD, OpenBSD, Solaris
conntrack | 显示 conntrack 统计信息(如果不存在 `/proc/sys/net/netfilter/` 则无操作)。 | Linux
cpu | 暴露 CPU 统计信息 | Darwin, Dragonfly, FreeBSD, Linux, Solaris, OpenBSD
cpufreq | 暴露 CPU 频率统计信息 | Linux, Solaris
diskstats | 暴露磁盘 I/O 统计信息。 | Darwin, Linux, OpenBSD
dmi | 从 `/sys/class/dmi/id/` 暴露桌面管理接口 (DMI) 信息 | Linux
edac | 暴露错误检测与纠正统计信息。 | Linux
entropy | 暴露可用熵。 | Linux
exec | 暴露执行统计信息。 | Dragonfly, FreeBSD
fibrechannel | 从 `/sys/class/fc_host/` 暴露光纤通道信息和统计信息。 | Linux
filefd | 从 `/proc/sys/fs/file-nr` 暴露文件描述符统计信息。 | Linux
filesystem | 暴露文件系统统计信息,例如已用磁盘空间。 | Darwin, Dragonfly, FreeBSD, Linux, OpenBSD
hwmon | 从 `/sys/class/hwmon/` 暴露硬件监控和传感器数据。 | Linux
infiniband | 暴露特定于 InfiniBand 和 Intel OmniPath 配置的网络统计信息。 | Linux
ipvs | 从 `/proc/net/ip_vs` 暴露 IPVS 状态,并从 `/proc/net/ip_vs_stats` 暴露统计信息。 | Linux
kernel_hung | 从 `/proc/sys/kernel/hung_task_detect_count` 暴露被检测为挂起的任务数量。 | Linux
loadavg | 暴露系统平均负载。 | Darwin, Dragonfly, FreeBSD, Linux, NetBSD, OpenBSD, Solaris
mdadm | 暴露有关 `/proc/mdstat` 中设备的统计信息(如果不存在 `/proc/mdstat` 则无操作)。 | Linux
meminfo | 暴露内存统计信息。 | Darwin, Dragonfly, FreeBSD, Linux, OpenBSD
netclass | 从 `/sys/class/net/` 暴露网络接口信息 | Linux
netdev | 暴露网络接口统计信息,如传输的字节数。 | Darwin, Dragonfly, FreeBSD, Linux, OpenBSD
netisr | 暴露 netisr 统计信息 | FreeBSD
netstat | 从 `/proc/net/netstat` 暴露网络统计信息。这与 `netstat -s` 的信息相同。 | Linux
nfs | 从 `/proc/net/rpc/nfs` 暴露 NFS 客户端统计信息。这与 `nfsstat -c` 的信息相同。 | Linux
nfsd | 从 `/proc/net/rpc/nfsd` 暴露 NFS 内核服务器统计信息。这与 `nfsstat -s` 的信息相同。 | Linux
nvme | 从 `/sys/class/nvme/` 暴露 NVMe 信息 | Linux
os | 从 `/etc/os-release` 或 `/usr/lib/os-release` 暴露操作系统发行版信息 | _任意_
powersupplyclass | 从 `/sys/class/power_supply` 暴露电源统计信息 | Linux
pressure | 从 `/proc/pressure/` 暴露压力停顿统计信息。 | Linux (kernel 4.20+ 和/或 [CONFIG\_PSI](https://www.kernel.org/doc/html/latest/accounting/psi.html))
rapl | 从 `/sys/class/powercap` 暴露各种统计信息。 | Linux
schedstat | 从 `/proc/schedstat` 暴露任务调度器统计信息。 | Linux
selinux | 暴露 SELinux 统计信息。 | Linux
sockstat | 从 `/proc/net/sockstat` 暴露各种统计信息。 | Linux
softnet | 从 `/proc/net/softnet_stat` 暴露统计信息。 | Linux
stat | 从 `/proc/stat` 暴露各种统计信息。这包括启动时间、forks 和中断。 | Linux
tapestats | 从 `/sys/class/scsi_tape` 暴露统计信息。 | Linux
textfile | 暴露从本地磁盘读取的统计信息。必须设置 `--collector.textfile.directory` flag。 | _任意_
thermal | 暴露热区统计信息,如 `pmset -g therm`。 | Darwin
thermal\_zone | 从 `/sys/class/thermal` 暴露热区和散热设备统计信息。 | Linux
time | 暴露当前系统时间。 | _任意_
timex | 暴露选定的 adjtimex(2) 系统调用统计信息。 | Linux
udp_queues | 从 `/proc/net/udp` 和 `/proc/net/udp6` 暴露 UDP rx_queue 和 tx_queue 的总长度。 | Linux
uname | 暴露由 uname 系统调用提供的系统信息。 | Darwin, FreeBSD, Linux, OpenBSD
vmstat | 从 `/proc/vmstat` 暴露统计信息。 | Linux
watchdog | 从 `/sys/class/watchdog` 暴露统计信息 | Linux
xfs | 暴露 XFS 运行时统计信息。 | Linux (kernel 4.4+)
zfs | 暴露 [ZFS](http://open-zfs.org/) 性能统计信息。 | FreeBSD, [Linux](http://zfsonlinux.org/), Solaris
### 默认禁用
`node_exporter` 还实现了许多默认禁用的收集器。其原因因收集器而异,可能包括:
* 高基数
* 超出 Prometheus `scrape_interval` 或 `scrape_timeout` 的过长运行时间
* 对主机的巨大资源需求
您可以根据需要,将额外的收集器添加到 `node_exporter` 的 init 系统或服务监督器的启动配置中来启用它们,但建议谨慎行事。一次最多启用一个,先在非生产系统上进行测试,然后手动在单个生产节点上启用。在启用额外的收集器时,您应该通过观察 `scrape_duration_seconds` 指标来仔细监控这一更改,以确保收集完成且不会超时。此外,还要监控 `scrape_samples_post_metric_relabeling` 指标以查看基数的变化。
名称 | 描述 | 操作系统
---------|-------------|----
buddyinfo | 暴露由 /proc/buddyinfo 报告的内存碎片统计信息。 | Linux
cgroups | 活跃和已启用 cgroups 数量的摘要 | Linux
cpu\_vulnerabilities | 从 sysfs 暴露 CPU 漏洞信息。 | Linux
devstat | 暴露设备统计信息 | Dragonfly, FreeBSD
drm | 使用 sysfs / DRM 暴露 GPU 指标,`amdgpu` 是唯一通过 DRM 暴露此信息的驱动程序 | Linux
drbd | 暴露分布式复制块设备统计信息(至版本 8.4) | Linux
ethtool | 暴露网络接口信息和网络驱动程序统计信息,等效于 `ethtool`、`ethtool -S` 和 `ethtool -i`。 | Linux
interrupts | 暴露详细的中断统计信息。 | Linux, OpenBSD
ksmd | 从 `/sys/kernel/mm/ksm` 暴露内核和系统统计信息。 | Linux
lnstat | 从 `/proc/net/stat/` 暴露统计信息。 | Linux
logind | 从 [logind](http://www.freedesktop.org/wiki/Software/systemd/logind/) 暴露会话计数。 | Linux
meminfo\_numa | 从 `/sys/devices/system/node/node[0-9]*/meminfo`、`/sys/devices/system/node/node[0-9]*/numastat` 暴露内存统计信息。 | Linux
mountstats | 从 `/proc/self/mountstats` 暴露文件系统统计信息。暴露详细的 NFS 客户端统计信息。 | Linux
network_route | 以指标形式暴露路由表 | Linux
pcidevice | 暴露 PCI 设备信息,包括其链路状态和父设备。 | Linux
perf | 暴露基于 perf 的指标(警告:指标取决于内核配置和设置)。 | Linux
processes | 从 `/proc` 暴露进程聚合统计信息。 | Linux
disc | 暴露 [排队规则](https://en.wikipedia.org/wiki/Network_scheduler#Linux_kernel) 统计信息 | Linux
slabinfo | 从 `/proc/slabinfo` 暴露 slab 统计信息。请注意,`/proc/slabinfo` 的权限通常为 0400,因此请进行适当的设置。 | Linux
softirqs | 从 `/proc/softirqs` 暴露详细的软中断统计信息。 | Linux
sysctl | 从 `/proc/sys` 暴露 sysctl 值。使用 `--collector.sysctl.include(-info)` 进行配置。 | Linux
swap | 从 `/proc/swaps` 暴露 swap 信息。 | Linux
systemd | 从 [systemd](http://www.freedesktop.org/wiki/Software/systemd/) 暴露服务和系统状态。 | Linux
tcpstat | 从 `/proc/net/tcp` 和 `/proc/net/tcp6` 暴露 TCP 连接状态信息。(警告:当前版本在高负载情况下存在潜在的性能问题。) | Linux
wifi | 暴露 WiFi 设备和站点统计信息。 | Linux
xfrm | 从 `/proc/net/xfrm_stat` 暴露统计信息 | Linux
zoneinfo | 暴露 NUMA 内存区指标。 | Linux
### 已弃用
这些收集器已被弃用,并将在下一个主要版本中删除。
名称 | 描述 | 操作系统
---------|-------------|----
ntp | 暴露本地 NTP 守护进程的健康状态以检查[时间](./docs/TIME.md) | _任意_
runit | 从 [runit](http://smarden.org/runit/) 暴露服务状态。 | _任意_
supervisord | 从 [supervisord](http://supervisord.org/) 暴露服务状态。 | _任意_
### Perf 收集器
由于内核配置和安全设置,`perf` 收集器在某些 Linux 系统上可能无法开箱即用。要允许访问,请设置以下 `sysctl` 参数:
```
sysctl -w kernel.perf_event_paranoid=X
```
- 2 仅允许用户空间测量(自 Linux 4.6 起默认)。
- 1 允许内核和用户测量(Linux 4.6 之前默认)。
- 0 允许访问 CPU 特定数据,但不允许原始 tracepoint 样本。
- -1 无限制。
根据配置的值,将提供不同的指标,在大多数情况下,`0` 将提供最完整的集合。有关更多信息,请参见 [`man 2
perf_event_open`](http://man7.org/linux/man-pages/man2/perf_event_open.2.html)。
默认情况下,`perf` 收集器将仅收集运行 `node_exporter` 的 CPU(即
[`runtime.NumCPU`](https://golang.org/pkg/runtime/#NumCPU)的指标。如果这不够(例如,如果您在运行 `node_exporter` 时将其 CPU 亲和性设置为特定 CPU),您可以使用 `--collector.perf.cpus` flag 指定备用 CPU 列表。例如,要在 CPU 2-6 上收集指标,您可以指定:`--collector.perf --collector.perf.cpus=2-6`。CPU 配置是零索引的,还可以设置步长值;例如
`--collector.perf --collector.perf.cpus=1-10:5` 将在 CPU 1、5 和 10 上进行收集。
当使用 `--collector.perf.tracepoint` flag 时,`perf` 收集器还可以收集 [tracepoint](https://www.kernel.org/doc/html/latest/core-api/tracepoint.html) 计数。可以使用 [`perf list`](http://man7.org/linux/man-pages/man1/perf.1.html) 或从 debugfs 找到 Tracepoint。此功能的使用示例为
`--collector.perf.tracepoint="sched:sched_process_exec"`。
### Sysctl 收集器
可以使用 `--collector.sysctl` 启用 `sysctl` 收集器。它支持使用 `--collector.sysctl.include` flag 将数字 sysctl 值作为指标暴露,并通过使用 `--collector.sysctl.include-info` flag 将字符串值作为信息指标暴露。这些 flags 可以重复使用。对于具有多个数值的 sysctl,可以提供一个可选的映射,将每个值作为单独的指标暴露。否则,将使用 `index` 标签来标识不同的字段。
#### 示例
##### 数值
###### 单个值
使用 `--collector.sysctl.include=vm.user_reserve_kbytes`:
`vm.user_reserve_kbytes = 131072` -> `node_sysctl_vm_user_reserve_kbytes 131072`
###### 多个值
一个 sysctl 可以包含多个值,例如:
```
net.ipv4.tcp_rmem = 4096 131072 6291456
```
使用 `--collector.sysctl.include=net.ipv4.tcp_rmem` 收集器将暴露:
```
node_sysctl_net_ipv4_tcp_rmem{index="0"} 4096
node_sysctl_net_ipv4_tcp_rmem{index="1"} 131072
node_sysctl_net_ipv4_tcp_rmem{index="2"} 6291456
```
如果索引具有已定义的含义(如本例所示),可以通过将映射附加到 --collector.sysctl.include flag 来将值映射到多个指标:
使用 `--collector.sysctl.include=net.ipv4.tcp_rmem:min,default,max` 收集器将暴露:
```
node_sysctl_net_ipv4_tcp_rmem_min 4096
node_sysctl_net_ipv4_tcp_rmem_default 131072
node_sysctl_net_ipv4_tcp_rmem_max 6291456
```
##### 字符串值
字符串值需要作为信息指标暴露。用户可以通过使用 `--collector.sysctl.include-info` flag 来选择它们。
###### 单个值
`kernel.core_pattern = core` -> `node_sysctl_info{key="kernel.core_pattern_info", value="core"} 1`
###### 多个值
给定以下 sysctl:
```
kernel.seccomp.actions_avail = kill_process kill_thread trap errno trace log allow
```
设置 `--collector.sysctl.include-info=kernel.seccomp.actions_avail` 将得到:
```
node_sysctl_info{key="kernel.seccomp.actions_avail", index="0", value="kill_process"} 1
node_sysctl_info{key="kernel.seccomp.actions_avail", index="1", value="kill_thread"} 1
...
```
### Textfile 收集器
`textfile` 收集器类似于 [Pushgateway](https://github.com/prometheus/pushgateway),它允许从批处理作业导出统计信息。它也可用于导出静态指标,例如机器具有的角色。Pushgateway 应该用于服务级别的指标。`textfile` 模块用于与机器绑定的指标。
要使用它,请在 `node_exporter` 命令行上设置 `--collector.textfile.directory` flag。收集器将使用 [文本格式](http://prometheus.io/docs/instrumenting/exposition_formats/) 解析该目录中与 glob `*.prom` 匹配的所有文件。**注意:** 不支持时间戳。
以原子方式推送 cron 作业的完成时间:
```
echo my_batch_job_completion_time $(date +%s) > /path/to/directory/my_batch_job.prom.$$
mv /path/to/directory/my_batch_job.prom.$$ /path/to/directory/my_batch_job.prom
```
使用标签静态设置机器的角色:
```
echo 'role{role="application_server"} 1' > /path/to/directory/role.prom.$$
mv /path/to/directory/role.prom.$$ /path/to/directory/role.prom
```
### 过滤已启用的收集器
默认情况下,`node_exporter` 将暴露所有已启用收集器的指标。这是收集指标的推荐方法,可避免在比较不同系列的指标时出现错误。
对于高级用例,可以向 `node_exporter` 传递一个可选的收集器列表来过滤指标。参数 `collect[]` 和 `exclude[]` 可以多次使用(但不能组合使用)。在 Prometheus 配置中,您可以在 [scrape config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#) 下使用此语法。
仅收集 `cpu` 和 `meminfo` 收集器指标:
```
params:
collect[]:
- cpu
- meminfo
```
收集所有已启用的收集器指标,但排除 `netdev`:
```
params:
exclude[]:
- netdev
```
这对于让不同的 Prometheus 服务器从节点收集特定指标非常有用。
## 开发构建与运行
前提条件:
* [Go 编译器](https://golang.org/dl/)
* RHEL/CentOS:`glibc-static` 包。
构建:
```
git clone https://github.com/prometheus/node_exporter.git
cd node_exporter
make build
./node_exporter
```
要查看所有可用的配置 flags:
```
./node_exporter -h
```
## 运行测试
```
make test
```
## TLS endpoint
**实验性功能**
exporter 通过新的 Web 配置文件支持 TLS。
```
./node_exporter --web.config.file=web-config.yml
```
有关更多详细信息,请参见 [exporter-toolkit web-configuration](https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)。
标签:API集成, Go, Prometheus, Ruby工具, 可观测性, 系统指标, 运维监控