使用Node Exporter監控Linux主機指標

Prometheus Node Exporter 暴露了各種硬體和核心相關的指標。

在本指南中,你將:

  • localhost 上啟動 Node Exporter
  • localhost 上啟動一個 Prometheus 例項,該例項配置為從正在執行的 Node Exporter 中抓取指標
注意雖然 Prometheus Node Exporter 適用於 *nix 系統,但也有用於 Windows 的 Windows exporter ,其目的類似。

安裝和執行Node Exporter

Prometheus Node Exporter 是一個單一的靜態二進位制檔案,你可以 透過tarball 安裝。從 Prometheus 下載頁面 下載後,解壓並執行它。

# NOTE: Replace the URL with one from the above mentioned "downloads" page.
# Node Exporter is available for multiple OS targets and architectures.
# Downloads are available at:
# https://github.com/prometheus/node_exporter/releases/download/v<VERSION>/node_exporter-<VERSION>.<OS>-<ARCH>.tar.gz
#
# <VERSION>, <OS>, and <ARCH> are placeholders:
# - <VERSION>: Release version (e.g., 1.10.2)
# - <OS>: Operating system (e.g., linux, darwin, freebsd)
# - <ARCH>: Architecture (e.g., amd64, arm64, 386)
#
# For this example, we will use Node Exporter version 1.10.2 for a Linux system with amd64 architecture.

wget https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz
tar xvfz node_exporter-1.10.2.linux-amd64.tar.gz
cd node_exporter-1.10.2.linux-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 端點來驗證指標是否正在匯出。

curl https://:9100/metrics

你應該看到類似這樣的輸出

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 3.8996e-05
go_gc_duration_seconds{quantile="0.25"} 4.5926e-05
go_gc_duration_seconds{quantile="0.5"} 5.846e-05
# etc.

成功!Node Exporter現在正在暴露Prometheus可以抓取的指標,包括輸出中更下方的各種系統指標(字首為 node_)。要檢視這些指標(以及幫助和型別資訊),請執行以下操作:

curl https://: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.gz
tar xvf prometheus-*.*-amd64.tar.gz
cd prometheus-*.*

Prometheus安裝後,你可以使用 --config.file 標誌指向你上面建立的Prometheus配置來啟動它。

./prometheus --config.file=./prometheus.yml

透過Prometheus表示式瀏覽器探索Node Exporter指標

現在Prometheus正在從執行中的Node Exporter例項抓取指標,你可以使用Prometheus UI(也稱為表示式瀏覽器)來探索這些指標。在瀏覽器中導航到 localhost:9090/graph,並使用頁面頂部的主要表示式欄輸入表示式。表示式欄如下所示:

Prometheus expressions browser

Node Exporter特有的指標以 node_ 開頭,包括 node_cpu_seconds_totalnode_exporter_build_info 等指標。

點選下方連結檢視一些示例指標

指標含義
rate(node_cpu_seconds_total{mode="system"}[1m])過去一分鐘內,系統模式下每秒CPU時間的平均消耗量(以秒為單位)
node_filesystem_avail_bytes非root使用者可用的檔案系統空間(以位元組為單位)
rate(node_network_receive_bytes_total[1m])過去一分鐘內,每秒接收網路流量的平均值(以位元組為單位)

本頁內容