Installing Node Exporter with authorization and connecting to Prometheus in Centos 8

Node Exporter is a Prometheus exporter for collecting server health data with pluggable metric collectors. It allows you to measure various machine resources such as memory, disk and processor usage. Written in Go

Installing Node Exporter

Add a system user from which the Node Exporter will work

$ sudo useradd -r -M -s /bin/false node_exporter

Download node_exporter-1.0.1

$ wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz -P /tmp
$ cd /tmp

Unpack, transfer to the / usr / local / bin directory, assign the owner

$ tar -zxpvf node_exporter-1.0.1.linux-amd64.tar.gz
$ cd node_exporter-1.0.1.linux-amd64
$ sudo cp node_exporter /usr/local/bin
$ sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

Create Systemd Unit

$ sudo nano /etc/systemd/system/node_exporter.service
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

Add the service to startup, start it, check the status

$ sudo systemctl daemon-reload
$ sudo systemctl enable --now node_exporter
$ sudo systemctl status node_exporter

Checking port 9100

$ sudo ss -pnltu | grep 9100

We open it out

$ sudo firewall-cmd --add-port=9100/tcp  --permanent
$ sudo firewall-cmd --reload

Setting up authorization

Installing the httpd-tools utility

$ sudo dnf -y install httpd-tools

Generating a password

$ htpasswd -nBC 10 "" | tr -d ':n'
New password: password
Re-type new password: password

Create a directory where the node_exporter config will be located

$ sudo mkdir /opt/node_exporter

Create a config file for Node Exporter

$ sudo nano /opt/node_exporter/web.yml
#Если нужно https
#tls_server_config:
#  cert_file: node_exporter.crt
#  key_file: node_exporter.key
basic_auth_users:
  myuser: $2y$10$ZNpUythMY9kLqsldfkjsljfdlskjdflksdjf527W8kBkHPT4Rkl9C

Change owner

$ sudo chown -R node_exporter:node_exporter /opt/node_exporter

Editing the Systemd Unit

$ sudo nano /etc/systemd/system/node_exporter.service
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter --web.config=/opt/node_exporter/web.yml

[Install]
WantedBy=multi-user.target

We restart the service

$ sudo systemctl daemon-reload
$ sudo systemctl restart node_exporter
$ sudo systemctl status node_exporter

Go to the site http: //% your-ip%: 9100 /. Basic Auth should appear

Configuring Prometheus

On the Prometheus server, add authorization parameters to the Prometheus config

$ sudo nano /etc/prometheus/prometheus.yml
[…]
  - job_name: 'node'
    basic_auth:
      username: myuser
      password: password
#    scheme: https
#    tls_config:
#      ca_file: node_exporter.crt
    static_configs:
    - targets: ['localhost:9100'] 

where instead of localhost you need to specify the ip-address of the server where we installed the node exporter

Restart Prometheus

$ sudo systemctl restart prometheus
$ sudo systemctl status prometheus

Checking through the browser

http://localhost:9090/targets
Sidebar