Difference between revisions of "Prometheus"

From Nick's Personal Wiki
Jump to navigation Jump to search
(Created page with "Placeholder text. =Prometheus= ==Downloading Prometheus== You may need to check the Prometheus documentation/downloads for the latest version [https://prometheus.io/downloa...")
 
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Placeholder text.
 
Placeholder text.
  
=Prometheus=
+
=Installing Prometheus=
  
 
+
==Downloading The Package==
==Downloading Prometheus==
 
 
You may need to check the Prometheus documentation/downloads for the latest version [https://prometheus.io/download/ here.]
 
You may need to check the Prometheus documentation/downloads for the latest version [https://prometheus.io/download/ here.]
  
Line 10: Line 9:
 
  <nowiki>wget INSERT_LINK_HERE</nowiki>
 
  <nowiki>wget INSERT_LINK_HERE</nowiki>
  
 +
==Extracting The Package==
 
Extract the file:
 
Extract the file:
 
  <nowiki>tar xfvz prometheus-WHATEVER-VERSION-YOU-HAVE.tar.gz</nowiki>
 
  <nowiki>tar xfvz prometheus-WHATEVER-VERSION-YOU-HAVE.tar.gz</nowiki>
  
 
+
=Creating Prometheus User=
==Creating Prometheus User==
 
  
 
Create a user account for Prometheus, without a home folder and with a nologin shell:
 
Create a user account for Prometheus, without a home folder and with a nologin shell:
  <nowiki>sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus<nowiki>
+
  <nowiki>sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus</nowiki>
  
  
==Create Prometheus Directories==
+
=Create Prometheus Directories=
  
 +
==Create The Directories==
 
Create the below directories:
 
Create the below directories:
 
  <nowiki>sudo mkdir -p /etc/prometheus /var/lib/prometheus</nowiki>
 
  <nowiki>sudo mkdir -p /etc/prometheus /var/lib/prometheus</nowiki>
  
 +
==Relocate The Directories/Files==
 
Copy the two files below, from within the 'prometheus-WHATEVER-VERSION-YOU-HAVE' directory, to '/usr/local/bin/'
 
Copy the two files below, from within the 'prometheus-WHATEVER-VERSION-YOU-HAVE' directory, to '/usr/local/bin/'
 
  <nowiki>sudo cp prometheus /usr/local/bin/
 
  <nowiki>sudo cp prometheus /usr/local/bin/
 
sudo cp promtool /usr/local/bin/</nowiki>
 
sudo cp promtool /usr/local/bin/</nowiki>
  
 +
Copy the below directories, again from within the 'prometheus-WHATEVER-VERSION-YOU-HAVE' directory, to the '/etc/prometheus/' directory:
 +
<nowiki>sudo cp -r consoles/ /etc/prometheus/
 +
sudo cp -r console_libraries/ /etc/prometheus/</nowiki>
 +
 +
Then we need to move the provided .yml file for prometheus to the 'etc/prometheus/' directory:
 +
<nowiki>sudo cp prometheus.yml /etc/prometheus/prometheus.yml</nowiki>
 +
 +
==Changing Ownership Of The Files==
 
Change ownership of the files to the prometheus user we created:
 
Change ownership of the files to the prometheus user we created:
  <nowiki>sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool</nowiki>
+
  <nowiki>sudo chown prometheus:prometheus /usr/local/bin/prometheus
 +
sudo chown prometheus:prometheus /usr/local/bin/promtool</nowiki>
 +
 
 +
<nowiki>sudo chown -R prometheus:prometheus /etc/prometheus
 +
sudo chown -R prometheus:prometheus /var/lib/prometheus</nowiki>
 +
 
 +
=Create Systemd Service for Prometheus=
 +
 
 +
==Creating Systemd Service File==
 +
Create the service file itself:
 +
<nowiki>sudo vim /etc/systemd/system/prometheus.service</nowiki>
  
 +
Include the following:
 +
<nowiki>
 +
[Unit]
 +
Description=Prometheus
 +
Wants=network-online.target
 +
After=network-online.target
  
Copy the below directories, again from within the 'prometheus-WHATEVER-VERSION-YOU-HAVE' directory, to the '/etc/prometheus/' directory:
+
[Service]
  <nowiki>sudo cp -r consoles/ /etc/prometheus/
+
User=prometheus
sudo cp -r console_libraries/ /etc/prometheus/</nowiki>
+
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 \
 +
    --storage.tsdb.retention.time=15d \
 +
    --web.enable-lifecycle \ # Each line requires a trailing \ EXCEPT the last line of the ExecStart block
 +
    --web.listen-address=:PORT # Uses 9090 by default if web.listen-address is not provided, can use a different port if specified here
 +
 
 +
[Install]
 +
Wantedby=multi-user.target
 +
</nowiki>
 +
 
 +
==Starting The Service==
 +
Once the service file is created and all the files/directories are where they should be, the Prometheus service can be started:
 +
  <nowiki>
 +
#Reload the daemons
 +
sudo systemctl daemon-reload
 +
 
 +
#Start the prometheus service
 +
sudo systemctl start prometheus
 +
 
 +
#Check the status of Prometheus
 +
sudo systemctl status prometheus
 +
 
 +
#If the service starts successfully, enable the service to start on every boot
 +
sudo systemctl enable prometheus
 +
</nowiki>
 +
 
 +
Then we can verify that Prometheus is running and listening on the specified port:
 +
<nowiki>curl http://localhost:PORT/-/healthy</nowiki>
 +
If it is working properly, the output should look like:
 +
<nowiki>Prometheus Server is Healthy</nowiki>
 +
 
 +
=Configure Prometheus=
 +
 
 +
==File Location==
 +
Configuring Prometheus requires the editing of the '/etc/prometheus/prometheus.yml' file.
 +
 
 +
==Example File==
 +
Below is a basic config file for Prometheus:
 +
<nowiki>
 +
global:
 +
    scrape_interval: 15s
 +
 
 +
scrape_configs:
 +
    - job_name: 'prometheus'
 +
      static_configs:
 +
        - targets: ['localhost:PROMETHEUS_PORT'] </nowiki>
  
==Create Systemd Service for Prometheus==
+
Indentation and formatting is CRUCIAL in this file, triple check everything is correct.
  
 +
You can also run the promtool utility to verify via:
 +
<nowiki>promtool check config /etc/prometheus/prometheus.yml</nowiki>
  
 +
If you additionally use the node exporter, [[Node_Exporter_(Prometheus)|more info here]], include the below with it's job name and port number as well.
 +
<nowiki>
 +
    - job_name: 'node_exporter'
 +
      static_configs:
 +
        - targets: ['localhost:NODE_PORT']</nowiki>
  
==Configure Prometheus==
+
When the config file is updated, update Prometheus via:
 +
<nowiki>curl -X POST http://localhost:PORT/-/reload</nowiki>

Latest revision as of 03:34, 28 March 2026

Placeholder text.

Installing Prometheus

Downloading The Package

You may need to check the Prometheus documentation/downloads for the latest version here.

Copy the URL for the version you need from that page (should be a Github link) and paste it into the below:

wget INSERT_LINK_HERE

Extracting The Package

Extract the file:

tar xfvz prometheus-WHATEVER-VERSION-YOU-HAVE.tar.gz

Creating Prometheus User

Create a user account for Prometheus, without a home folder and with a nologin shell:

sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus


Create Prometheus Directories

Create The Directories

Create the below directories:

sudo mkdir -p /etc/prometheus /var/lib/prometheus

Relocate The Directories/Files

Copy the two files below, from within the 'prometheus-WHATEVER-VERSION-YOU-HAVE' directory, to '/usr/local/bin/'

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

Copy the below directories, again from within the 'prometheus-WHATEVER-VERSION-YOU-HAVE' directory, to the '/etc/prometheus/' directory:

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

Then we need to move the provided .yml file for prometheus to the 'etc/prometheus/' directory:

sudo cp prometheus.yml /etc/prometheus/prometheus.yml

Changing Ownership Of The Files

Change ownership of the files to the prometheus user we created:

sudo chown prometheus:prometheus /usr/local/bin/prometheus 
sudo chown prometheus:prometheus /usr/local/bin/promtool
sudo chown -R prometheus:prometheus /etc/prometheus 
sudo chown -R prometheus:prometheus /var/lib/prometheus

Create Systemd Service for Prometheus

Creating Systemd Service File

Create the service file itself:

sudo vim /etc/systemd/system/prometheus.service

Include the following:

[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 \
    --storage.tsdb.retention.time=15d \
    --web.enable-lifecycle \ # Each line requires a trailing \ EXCEPT the last line of the ExecStart block
    --web.listen-address=:PORT # Uses 9090 by default if web.listen-address is not provided, can use a different port if specified here

[Install]
Wantedby=multi-user.target

Starting The Service

Once the service file is created and all the files/directories are where they should be, the Prometheus service can be started:

#Reload the daemons
sudo systemctl daemon-reload

#Start the prometheus service
sudo systemctl start prometheus

#Check the status of Prometheus
sudo systemctl status prometheus

#If the service starts successfully, enable the service to start on every boot
sudo systemctl enable prometheus

Then we can verify that Prometheus is running and listening on the specified port:

curl http://localhost:PORT/-/healthy

If it is working properly, the output should look like:

Prometheus Server is Healthy

Configure Prometheus

File Location

Configuring Prometheus requires the editing of the '/etc/prometheus/prometheus.yml' file.

Example File

Below is a basic config file for Prometheus:

global:
    scrape_interval: 15s

scrape_configs:
    - job_name: 'prometheus'
      static_configs:
        - targets: ['localhost:PROMETHEUS_PORT'] 

Indentation and formatting is CRUCIAL in this file, triple check everything is correct.

You can also run the promtool utility to verify via:

promtool check config /etc/prometheus/prometheus.yml

If you additionally use the node exporter, more info here, include the below with it's job name and port number as well.

    - job_name: 'node_exporter'
      static_configs:
        - targets: ['localhost:NODE_PORT']

When the config file is updated, update Prometheus via:

curl -X POST http://localhost:PORT/-/reload