使用 Node Exporter 监控 Linux 主机
Prometheus 的 Node Exporter 暴露了大量的各种硬件和内核相关指标。
在本指南中,你将:
- 在
localhost
上启动 node_exporter - 在
localhost
上启动一个配置为从运行中的 node_exporter 收集指标的 Prometheus 实例
注:尽管 Prometheus Node Exporter 只适用于 *nix 系统,但 Windows 上有一个实现类似功能的 Windows exporter。
安装并运行 Node Exporter
Prometheus Node Exporter 是一个独立静态二进制文件,你可以通过 tarball 方式安装。从 Prometheus 的下载页面下载并解压缩,最后运行:
# 注意:替换为上述下载页面中的 URL。# <VERSION>、<OS> 和 <ARCH> 是占位符。wget https://github.com/prometheus/node_exporter/releases/download/v<VERSION>/node_exporter-<VERSION>.<OS>-<ARCH>.tar.gztar xvfz node_exporter-*.*-amd64.tar.gzcd node_exporter-*.*-amd64./node_exporter
你应该看到类似于以下的输出,指示 node_exporter 已启动并正在使用端口9100暴露指标:
INFO[0000] Starting node_exporter (version=0.16.0, branch=HEAD, revision=d42bd70f4363dced6b77d8fc311ea57b63387e4f) source="node_exporter.go:82"INFO[0000] Build context (go=go1.9.6, user=root@a67a9bc13a69, date=20180515-15:53:28) source="node_exporter.go:83"INFO[0000] Enabled collectors: source="node_exporter.go:90"INFO[0000] - boottime source="node_exporter.go:97"...INFO[0000] Listening on :9100 source="node_exporter.go:111"
Node Exporter 指标
一旦安装并运行了 node_exporter,可以通过 cURL 访问/metrics
端点来验证 node_exporter 是否正在导出指标:
curl http://localhost:9100/metrics
你应该会看到类似于以下的输出:
# HELP go_gc_duration_seconds A summary of the GC invocation durations.# TYPE go_gc_duration_seconds summarygo_gc_duration_seconds{quantile="0"} 3.8996e-05go_gc_duration_seconds{quantile="0.25"} 4.5926e-05go_gc_duration_seconds{quantile="0.5"} 5.846e-05# etc.
成功!node_exporter 现在正在暴露 Prometheus 可以抓取的指标,包括输出中位于下方的大量系统指标(前缀为node_
)。要查看这些指标(以及帮助和类型信息),使用以下命令:
curl http://localhost:9100/metrics | grep "node_"
配置你的 Prometheus 实例
本地运行的 Prometheus 实例需要正确配置以访问 Node Exporter 指标。以下是一个示例配置文件prometheus.yml
,将告诉 Prometheus 实例如何通过localhost:9100
从 Node Exporter 抓取并设置抓取频率:
global: scrape_interval: 15s
scrape_configs:- job_name: node static_configs: - targets: ['localhost:9100']
要安装 Prometheus,请下载适用于你平台的最新发布下载并解压缩它:
wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.*-amd64.tar.gztar xvf prometheus-*.*-amd64.tar.gzcd prometheus-*.*
一旦 Prometheus 安装完成,使用--config.file
标志指向你之前创建的[配置](#配置您的 Prometheus 实例)来启动它:
./prometheus --config.file=./prometheus.yml
通过 Prometheus 表达式浏览器探索 Node Exporter 指标
现在 Prometheus 开始从运行的 node_exporter 实例抓取指标,你可以使用 Prometheus UI(即表达式浏览器)来探索这些指标。跳转到浏览器中的localhost:9090/graph
,并在页面顶部的主要表达式栏中输入表达式。表达式栏看起来如下:
Node Exporter 专用的指标前缀为node_
,包括诸如node_cpu_seconds_total
和node_exporter_build_info
等指标。
下方是一些示例指标:
指标 | 意义 |
---|---|
rate(node_cpu_seconds_total{mode="system"}[1m]) | 过去一分钟平均的 system 模式下的 CPU 时间,每秒(单位:秒) |
node_filesystem_avail_bytes | 非根用户可用的文件系统空间(单位:字节) |
rate(node_network_receive_bytes_total[1m]) | 过去一分钟平均的接收网络流量,每秒(单位:字节) |
该文档基于 Prometheus 官方文档翻译而成。