Metrics Reporting

14 May 2023

Setting up Metric Reporting

Prometheus and Grafana make a great duo for setting up system monitoring and metrics reporting on a machine. The following are my notes on how to get setup:

Prometheus

Prometheus is an open-source system monitoring and alerting toolkit. Here’s how you can set it up on your Ubuntu machine:

  1. Update your system:

     sudo apt update && sudo apt upgrade -y
    
  2. Add the user and group for Prometheus:

     sudo useradd --no-create-home --shell /bin/false prometheus
    
  3. Create the directories and assign ownership to Prometheus user:

     sudo mkdir /etc/prometheus
     sudo mkdir /var/lib/prometheus
     sudo chown prometheus:prometheus /etc/prometheus
     sudo chown prometheus:prometheus /var/lib/prometheus
    
  4. Download and extract Prometheus:

    Go to the Prometheus download page (https://prometheus.io/download/) and find the latest version, then replace ${PROMETHEUS_VERSION} in the following commands with that version.

     wget https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz
     tar xvf prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz
     cd prometheus-${PROMETHEUS_VERSION}.linux-amd64
    
  5. Move the Prometheus and Promtool binaries:

     sudo cp prometheus /usr/local/bin/
     sudo cp promtool /usr/local/bin/
    
  6. Set the owner to the Prometheus user:

     sudo chown prometheus:prometheus /usr/local/bin/prometheus
     sudo chown prometheus:prometheus /usr/local/bin/promtool
    
  7. Move the consoles and console_libraries directories to /etc/prometheus:

     sudo cp -r consoles /etc/prometheus
     sudo cp -r console_libraries /etc/prometheus
    
  8. Set the owner on the /etc/prometheus directories:

     sudo chown -R prometheus:prometheus /etc/prometheus/consoles
     sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries
    
  9. Create the prometheus.yml file:

     sudo nano /etc/prometheus/prometheus.yml
    

    You can start with a minimal config such as:

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

    Then save and exit the file (Ctrl+X, Y, Enter).

  10. Set the owner on the Prometheus configuration file:

    sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml
    
  11. Create a systemd service file for Prometheus:

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

    Add the following to the new file:

    ```ini [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 \

Grafana

Here are the steps to install Grafana on Ubuntu:

  1. Update your system:

     sudo apt update
     sudo apt upgrade -y
    
  2. Add the Grafana GPG key:

    Grafana signs their packages with a GPG key to ensure that the package is authentic. You can add the key with this command:

     curl https://packages.grafana.com/gpg.key | sudo apt-key add -
    
  3. Add the Grafana repository to your APT sources:

     echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
    
  4. Update your package list and install Grafana:

     sudo apt update
     sudo apt install grafana
    
  5. Start and enable the Grafana service:

    Grafana comes with a systemd service file to start on boot. To enable and start it, use these commands:

     sudo systemctl start grafana-server
     sudo systemctl enable grafana-server
    
  6. Check Grafana service status:

    You can use the following command to verify that Grafana is running:

     sudo systemctl status grafana-server
    
  7. Access Grafana:

    Now you should be able to access Grafana by going to the following URL in your web browser:

     http://your_server_ip:3000
    

    The default login is “admin” for the username and “admin” for the password. On the first login, Grafana will ask you to change the default password.

That’s it! You’ve successfully installed Grafana on your Ubuntu machine. Now you can connect Grafana to your Prometheus instance and start creating dashboards for your data.

Top