How to configure Prometheus monitoring server with Grafana 6 on Ubuntu 22.04

Installing Prometheus monitoring server

  1. This article will instruct you on configuring the Prometheus Monitoring Server with Grafana 6.

  2. Grafana should be installed in the system before start with this steps.

  3. First we need prepare Environment, To run Prometheus safely on our server, we have to create a user for Prometheus and Node Exporter.
sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus
sudo useradd --no-create-home --shell /bin/false node_exporter
  1. Create the folders required to store the binaries of Prometheus and its configuration files.
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
  1. Set the ownership of these directories to our prometheus user, to make sure that Prometheus can access to these folders.
sudo chown prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus
  1. Download the latest version of Node Exporter.
wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz
  1. Unpack the downloaded archive. This will create a directory node_exporter-0.16.0.linux-amd64, containing the executable, a readme and license file.
tar xvf node_exporter-0.16.0.linux-amd64.tar.gz
  1. Copy the binary file into the directory /usr/local/bin and set the ownership to the user you have created in step previously.
sudo cp node_exporter-0.16.0.linux-amd64/node_exporter /usr/local/bin
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
  1. Remove the leftover files of Node Exporter, as they are not needed any longer.
rm -rf node_exporter-0.16.0.linux-amd64.tar.gz node_exporter-0.16.0.linux-amd64
  1. To run Node Exporter automatically on each boot, a Systemd service file is required. Create the following file by opening it in Nano:
sudo nano /etc/systemd/system/node_exporter.service
  1. Copy the following information in the service file, save it and exit Nano.
[Unit]
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
  1. Reload Systemd to use the newly defined service
sudo systemctl daemon-reload
  1. Run Node Exporter by typing the following command.
sudo systemctl start node_exporter
  1. Verify that the software has been started successfully:
sudo systemctl status node_exporter
  1. You will see an output like this

server monitor installer

  1. If everything is working, enable Node Exporter to be started on each boot of the server.
sudo systemctl enable node_exporter
  1. Download and Unpack Prometheus latest release of Prometheus. As exemplified, the version is 2.2.1.
sudo apt-get update && apt-get upgrade
wget https://github.com/prometheus/prometheus/releases/download/v2.2.1/prometheus-2.2.1.linux-amd64.tar.gz
tar xfz prometheus-*.tar.gz
cd prometheus-*
  1. Copy the binary files into the /usr/local/bin/directory
sudo cp ./prometheus /usr/local/bin/
sudo cp ./promtool /usr/local/bin/
  1. Set the ownership of these files to the prometheus user previously created.
sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtool
  1. Copy the consoles and console_libraries directories to /etc/prometheus.
sudo cp -r ./consoles /etc/prometheus
sudo cp -r ./console_libraries /etc/prometheus
  1. Set the ownership of the two folders, as well as of all files that they contain, to our prometheus user.
sudo chown -R prometheus:prometheus /etc/prometheus/consoles
sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries
  1. In our home folder, remove the source files that are not needed anymore
cd .. && rm -rf prometheus-*
  1. Open the file prometheus.yml in a text editor.
sudo nano /etc/prometheus/prometheus.yml
  1. File should look like this example:
global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  # - "first.rules"
  # - "second.rules"

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  1. Set the ownership of the file to our Prometheus user
sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml
  1. Start running Prometheus by using following command.
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
  1. The server starts displaying multiple status messages and the information that the server has started:

server monitor installer

  1. Open your browser and type http://IP.OF.YOUR.SERVER:9090 to access the Prometheus interface.If it is working end task by pressing Ctrl+C./

web interface will look this

server monitor installer

  1. The server is working now, but it can not be launched automatically at boot, create a new systemd configuration file that will tell your OS which services should it launch automatically during the boot process.
sudo nano /etc/systemd/system/prometheus.service
  1. Copy the following information in the file and save it, then exit the editor
[Unit]
  Description=Prometheus Monitoring
  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
  ExecReload=/bin/kill -HUP $MAINPID

[Install]
  WantedBy=multi-user.target
  1. To use the new service, reload the system and enable prometheus
sudo systemctl daemon-reload
sudo systemctl enable prometheus
  1. Start Prometheus.
sudo systemctl start prometheus
  1. Prometheus looks like this, when you open by using web server http://your.server.ip:9000

server monitor installer server monitor installer

Configuring Prometheus with Grafana

  1. Now you have to create a Prometheus data source
  • Click on the Grafana logo to open the sidebar.
  • Click on “Data Sources” in the sidebar.
  • Choose “Add New”.
  • Select “Prometheus” as the data source.
  • Set the Prometheus server URL (in our case: http://localhost:9090/)
  • Click “Add” to test the connection and to save the new data source.

Your settings should look like this:

server monitor installer

  1. Here is an example of a Dashboard that uses the CPU usage of our node and presents it in Grafana. server monitor installer

CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS