在linux节点上安装node exporter随记

来自三线的随记

MONITORING LINUX HOST METRICS WITH THE NODE EXPORTER

The Prometheus Node Exporter exposes a wide variety of hardware- and kernel-related metrics.

Systemd configure file example

[Unit]
Description=node_exporter
After=network.target 

[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/exporter/node_exporter/node_exporter\
          --web.listen-address=:20001\
          --collector.systemd\
          --collector.systemd.unit-whitelist=(sshd|nginx).service\
          --collector.processes\
          --collector.tcpstat\
          --collector.supervisord
[Install]
WantedBy=multi-user.target

binary file download

https://github.com/prometheus/node_exporter/releases


official documentation

https://prometheus.io/docs/guides/node-exporter/


下載node exporter 二進制文件壓縮包

在節點上執行命令下載

wget "https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz" 


解壓壓縮包

[root@master sanXian]# tar -xzf node_exporter-1.0.1.linux-amd64.tar.gz
[root@master sanXian]# ls
node_exporter-1.0.1.linux-amd64  node_exporter-1.0.1.linux-amd64.tar.gz


將 node_exporter-1.0.1.linux-amd64 目錄下的 node_exporter 二進制文件複製到 /usr/local/bin 路徑下

注意需要給予該二進制文件nobody用戶可執行權限

[root@master sanXian]# cd node_exporter-1.0.1.linux-amd64/

[root@master node_exporter-1.0.1.linux-amd64]# ls
LICENSE  node_exporter  NOTICE

[root@master node_exporter-1.0.1.linux-amd64]# cp node_exporter /usr/local/bin/

[root@master node_exporter-1.0.1.linux-amd64]# ls -l /usr/local/bin/node_exporter
-rwxr-xr-x. 1 root root 19657731 Oct 22 12:17 /usr/local/bin/node_exporter


直接複製下文全部文字命令並在節點上執行以創建systemd service文件

cat <<EOF > /etc/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target 

[Service]
User=nobody
Group=nobody
Type=simple
ExecStart=/usr/local/bin/node_exporter\\
          --collector.systemd\\
          --collector.processes

[Install]
WantedBy=multi-user.target
EOF


重載系統 systemd 配置

執行命令 systemctl daemon-reload

啟動服務並且設置服務自啟

執行命令 systemctl enable --now node_exporter

[root@master system]# systemctl enable --now node_exporter
Created symlink from /etc/systemd/system/multi-user.target.wants/node_exporter.service to /etc/systemd/system/node_exporter.service.


查看服務運行狀態

執行命令systemctl status node_exporter

能夠得到類似下文的回顯結果

主要看Active屬性值為active(running)就好了

[root@master system]# systemctl status node_exporter
● node_exporter.service - node_exporter
   Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-10-22 14:20:44 CST; 6min ago
 Main PID: 27847 (node_exporter)
   CGroup: /system.slice/node_exporter.service
           └─27847 /usr/local/bin/node_exporter --collector.systemd --collector.processes


測試接口

在節點上使用命令curl -I {{節點IP}}:9100/metrics訪問接口

能夠得到類似下文結果

[root@master system]# curl -I 192.168.150.166:9100/metrics
HTTP/1.1 200 OK
Content-Type: text/plain; version=0.0.4; charset=utf-8
Date: Thu, 22 Oct 2020 06:34:49 GMT 

或直接使用瀏覽器訪問{{節點IP}}:9100/metrics能夠看到相關指標信息則為正常