Prometheus is an open source monitoring and alerting platform.

Originally founded by Soundcloud in 2012, the Prometheus project, which has since been adopted by some prominent company Abe, has become a much larger project with a very active developer and community. And in 2016, Prometheus graduated under the Cloud Native Computing Foundation (CNCF).

Basic concepts you need to know

Basically, Prometheus collects data and metrics from the target server via HTTP endpoints and stores all data as a time series, which in Prometheus is identified by metric names and key/value pairs.

Prometheus provides flexibility through the Prometheus Query Language (PromQL), which allows you to query the Prometheus time series database.

On the target server, you must install an “exporter” application that exposes all data and metrics to Prometheus. The node Exporter is a common export for monitoring Linux machines.

Node exporters expose hardware – and kernel-related matrices from Linux machines in the form of a single binary file that exposes data-side metrics to the Prometheus server.

A prerequisite for

In this article, you’ll learn how to install the Prometheus monitoring tool and Node Exporter on a Rocky Linux 8 system.

Before you begin, make sure you have met the following requirements:

  • Operating system: Rocky Linux 8.5
  • Root permissions

In this example, we use a Rocky Linux system with IP address “192.168.1.10”.

Now let’s get started.

Add a new user and directory

First, you create a new system user “Prometheus,” and then a new configuration directory and data directory for the Prometheus installation.

1. Run the following command to create user Prometheus:

sudo adduser -M -r -s /sbin/nologin prometheus
Copy the code
  1. After that, use the following commands to create a new configuration directory ‘/etc/prometheus’ and a data directory ‘/var/lib/prometheus’.
sudo mkdir /etc/prometheus 
sudo mkdir /var/lib/prometheus
Copy the code

All Prometheus configurations are available in the /etc/prometheus directory, and all Prometheus data is automatically saved to the /var/lib/prometheus directory.

Install Prometheus on Rocky Linux

In this step, you will manually install the Prometheus monitoring system from a tarball or tar.gz file.

Select the Prometheus version from this link. For this example, we will install the latest version of Prometheus.

  1. Change the working directory to “/usr/src” and download the Prometheus binary using the following command.
CD/usr/SRC wget HTTP: / / https://github.com/prometheus/prometheus/releases/download/v2.31.1/prometheus-2.31.1.linux-amd64.tar.gzCopy the code

After the download process is complete, unzip the Prometheus file.

The tar - xf Prometheus - 2.31.1. Linux - amd64. Tar. GzCopy the code

You will now see a new directory “Prometheus-version.os”.

2. Next, copy all Prometheus configurations to the ‘/etc/prometheus’ directory and the binary ‘Prometheus’ to the’ /usr/local/bin ‘directory.

Set the environment variable PROM_SRC to the directory /usr/src/prometheus-*.

export PROM_SRC=/usr/src/prometheus-*
Copy the code

Copy Prometheus. Yml from the Prometheus configuration to /etc/prometheus.

sudo cp $PROM_SRC/prometheus.yml/etc/prometheus/
Copy the code

Copy the binaries Prometheus and promtool to the /usr/local/bin/ directory.

sudo cp $PROM_SRC/prometheus /usr/local/bin/ 
sudo cp $PROM_SRC/promtool /usr/local/bin/
Copy the code

Copy the Prometheus console template and libraries to the /etc/prometheus directory.

sudo cp -r $PROM_SRC/consoles /etc/prometheus 
sudo cp -r $PROM_SRC/console_libraries /etc/prometheus
Copy the code
  1. Next, using nano editor to edit Prometheus configuration “/ etc/Prometheus/Prometheus. Yml”.
nano /etc/prometheus/prometheus.yml
Copy the code

In the “scrape_configs” option, you might want to add monitoring jobs and how to grab all the data from the target.

The default configuration contains the default monitoring job name Prometheus and target server localhost. Use the Static_configs option.

Change the target from localhost:9090 to server IP address 192.168.1.10:9090, as shown in the following figure.

# A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: "prometheus" # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: [192.168.1.10:9090 ""]Copy the code

Save the configuration and exit.

  1. Now run the following command to change the configuration and data directory to user ‘Promethues’.
sudo chown prometheus:prometheus /etc/prometheus 
sudo chown prometheus:prometheus /var/lib/prometheus
Copy the code

You have now completed the basic Prometheus installation.

Example Set the Prometheus service file

In this step, you will set Prometheus as a Systemd service.

  1. Use the nano editor to create a new systemd service file ‘/ etc/systemd/system/Prometheus. Service’.
sudo nano /etc/systemd/system/prometheus.service
Copy the code

Copy and paste the following configuration.

[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
Copy the code

Save the configuration and exit.

  1. Next, reload the Systemd manager to apply the new configuration.
sudo systemctl daemon-reload
Copy the code
  1. Now execute the following command to start and enable the Prometheus service, and then check its current status.
sudo systemctl enable --now prometheus 
sudo systemctl status prometheus
Copy the code

If the installation is successful, you should see that the Prometheus service is active and running, as shown below.

The Prometheus monitoring tool is now accessible on TCP port ‘9090.

  1. Open your web browser and enter the server IP address for port “9090” in the address bar.
http://192.168.1.10:9090/
Copy the code

You will see the Prometheus dashboard query below.

You have now installed Prometheus. Go to next to install Node Exporter.

Install the node exporter on Rocky Linux

Node exporters are part of the Prometheus project. You can use the node exporter to export metrics from a Linux server to a Prometheus server.

In this step, you will install the node exporter on your Rocky Linux system through the tar.gz file.

Check this link for the latest version of the node exporter. In this example, we will install the node exporter version 1.3.0.

  1. Change the working directory to “/usr/src” and use the following command to download the node exporter binaries.
cd /usr/src/ 
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.0/node_exporter-1.3.0.linux-amd64.tar.gz
Copy the code

If the download process is complete, extract the node exporter file.

The tar - xf node_exporter - 1.3.0. Linux - amd64. Tar. GzCopy the code

You will now get a new directory “node_exclame-version.os”.

  1. Next, use the following command to move the node exporter binaries to the directory ‘/usr/local/bin ‘.
mv node_exporter-*/node_exporter /usr/local/bin
Copy the code

Go to the next step to set the node exporter as the Systemd service.

Set Node_exporter to a service

Before creating the node exporter service file, create a new system user “node_EXPORTER”.

  1. Run the following command to create a new system user.
sudo adduser -M -r -s /sbin/nologin node_exporter
Copy the code
  1. Next, use the nano editor to create a new service file for the node export “/etc/systemd/system/node_expor.service”.
sudo nano /etc/systemd/system/node_exporter.service
Copy the code

Copy and paste the following configuration.

[Unit] 
Description=Node Exporter 
After=network.target 

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

[Install] 
WantedBy=multi-user.target
Copy the code

Save the configuration and exit.

  1. Now reload the Systemd manager to apply the new configuration.
sudo systemctl daemon-reload
Copy the code
  1. Run the following command to start and enable the service “node_exporter”.
sudo systemctl enable --now node_exporter
Copy the code

After that, check the current status of the service “node_exporter.”

sudo systemctl status node_exporter
Copy the code

Ensure that the node_exporter service is active and running.

  1. The node exporter runs on the default port 9100. Use the ss command below to verify.
ss -aplnt | grep node
Copy the code

You should see output similar to the following.

State Recv -q send-q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=683,fd=4)) LISTEN 0 128 *:9090 *:* users:(("prometheus",pid=709,fd=8)) LISTEN 0 128 *:9100 *:* users:(("node_exporter",pid=5786,fd=3)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=683,fd=6))Copy the code

And you’ve finished installing Node Exporter on a Rocky Linux system.

Add node_exporter to Prometheus

After Prometheus and node exporter are installed, add the node exporter to the Prometheus server.

  1. Edit the Prometheus configuration using the Nano editor.
sudo nano /etc/prometheus/prometheus.yml
Copy the code

Under the “scrape_config” section, add a new Prometheus job using the following configuration. And be sure to change the server IP address using the target server.

  - job_name: 'node_exporter_metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ['SERVER-IP:9100']
Copy the code

Save the configuration and exit.

  1. Now restart the Prometheus service to apply the new configuration.
sudo systemctl restart prometheus
Copy the code

You have added a node exporter to the Prometheus server.

Verify Prometheus and node exporter installation

1. Back to the Prometheus dashboard, click on the ‘Status’ menu and select’ Targets’.

You’ll see two different endpoints, “Prometheus” and “node_exporter_metrics”.

  1. Next, move to the menu “Chart” and type a PromQL query on the search bar. For this example, use the “node_OS_info” query to check the detailed operating system.

You will see detailed information about the current operating system.

  1. For another example, check the network speed using the query ‘node_network_speed_bytes’ and then move to the TAB’ Graph ‘. You should see output similar to the following.

You have now successfully installed Prometheus and the node exporter on a Rocky Linux system.