Difference between revisions of "Prometheus"

From Nick's Personal Wiki
Jump to navigation Jump to search
(Initial commit of the Prometheus page)
Line 93: Line 93:
  
 
==Configure Prometheus==
 
==Configure Prometheus==
 +
 +
Configuring Prometheus requires the editing of the '/etc/prometheus/prometheus.yml' file
 +
 +
Below is a basic config file for Prometheus:
 +
<nowiki>
 +
global:
 +
    scrape_interval: 15s
 +
 +
scrape_configs:
 +
    - job_name: 'prometheus'
 +
      static_configs:
 +
        - targets: ['localhost:9100']
 +
 +
    # If you additionally use the node exporter, include the below with it's job name and port number
 +
    - job_name: 'node'
 +
      static_configs:
 +
        - targets: ['localhost:PORT']
 +
</nowiki>
 +
 +
When the config file is updated, update Prometheus via:
 +
<nowiki>curl -X POST http://localhost:PORT/-/reload</nowiki>

Revision as of 02:28, 28 March 2026

Placeholder text.

Prometheus

Downloading Prometheus

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

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<nowiki>


==Create Prometheus Directories==

Create the below directories:
 <nowiki>sudo mkdir -p /etc/prometheus /var/lib/prometheus

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/

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

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


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
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus

Create Systemd Service for Prometheus

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

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

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

Below is a basic config file for Prometheus:

global:
    scrape_interval: 15s

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

    # If you additionally use the node exporter, include the below with it's job name and port number
    - job_name: 'node'
      static_configs:
        - targets: ['localhost:PORT']

When the config file is updated, update Prometheus via:

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