Prometheus and node_exporter install

Installing Prometheus

useradd --no-create-home --shell /bin/false prometheus
useradd --no-create-home --shell /bin/false node_exporter

mkdir /etc/prometheus
mkdir /var/lib/prometheus

chown prometheus:prometheus /etc/prometheus
chown prometheus:prometheus /var/lib/prometheus

Download Prometheus from https://prometheus.io/download/

Extract the tar ball:

tar xf prometheus-xxx.tar.gz
cd prometheus-xxx

cp prometheus /usr/local/bin
cp promtool /usr/local/bin/

chown prometheus:prometheus /usr/local/bin/prometheus
chown prometheus:prometheus /usr/local/bin/promtool

cp -r consoles /etc/prometheus
cp -r console_libraries /etc/prometheus

chown -R prometheus:prometheus /etc/prometheus/consoles
chown -R prometheus:prometheus /etc/prometheus/console_libraries

You can now rm the tarball and folder created when extracting.

Configuring prometheus:

vi /etc/prometheus/prometheus.yml

# ---------- begin /etc/prometheus/prometheus.yml

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

# ---------- end /etc/prometheus/prometheus.yml

chown prometheus:prometheus /etc/prometheus/prometheus.yml

# Try to run prometheus

sudo -u prometheus /usr/local/bin/prometheus \

        --config.file /etc/prometheus/prometheus.yml \
        --storage.tsdb.path /var/lib/prometheus/ \
        --web.console.templates=/etc/prometheus/consoles \
        --web.console.libraries=/etc/prometheus/console_libraries

Everything went well, right?

Let's create a new systemd file for prometheus:

vi /etc/systemd/system/prometheus.service

# ---------- begin /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target

# ---------- end /etc/systemd/system/prometheus.service

systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus

Installing node_exporter

Download node_exporter from https://prometheus.io/download/

Extract it and cd into the newly created folder

tar xf node_exporter-xxx.tar.gz
cd node_exporter-xxx

Copy the node_exporter binary

cp node_exporter /usr/local/bin
chown node_exporter:node_exporter /usr/local/bin/node_exporter

You can now delete the remaining node_exporter folder and tarball

Create a new systemd script for node_exporter

vi /etc/systemd/system/node_exporter.service

# ---------- begin /etc/systemd/system/node_exporter.service

Description=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

# ---------- end /etc/systemd/system/node_exporter.service

systemctl daemon-reload
systemctl start node_exporter
systemctl status node_exporter
systemctl enable node_exporter

So far so good!

Now we need to tell prometheus about how to reach node_exporter:

Add node_exporter to prometheus-yaml

vi /etc/prometheus/prometheus.yml

# ---------- begin /etc/systemd/system/node_exporter.service

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9100']   

# ---------- end /etc/systemd/system/node_exporter.service

Prometheus and node_exporter are installed, configured and working together!

Check prometheus console with your web browser

http://localhost:9090

Click Status, then Target. You should find two targets:

  • prometheus itself, which serves status about the prometheus server, and
  • node_exporter will serve stats about the host.

Click the endpoint for prometheus. Should be http://localhost:9090/metrics

At the bottom on the page, check the endpoint named

promhttp_metric_handler_requests_total{code="200"}

Refresh the page a few time, you should see the metric value increasing.


Copy the endpoint for this metric (promhttp_metric_handler_requests_total).

Go back to prometheus dashboard http://localhost:9090/graph

Paste the endpoint to the Expression form, then press execute. You will see only information about the endpoint you requested.


That's it for now!