Installing Prometheus on the Raspberry Pi?

Monitoring systems play a vital role in observing the health of infrastructure and applications. Prometheus is an open-source monitoring solution that is a great fit for embedded devices like the Raspberry Pi. In this guide, we will walk through installing Prometheus on the Raspberry Pi and configuring it to monitor a sample application.

Installing Prometheus on the Raspberry Pi?

Prerequisites

Before we get started, there are a few prerequisites:

  • Raspberry Pi – This guide uses a Raspberry Pi 3 B+ but should work on any model
  • Raspbian OS – The standard Raspberry Pi OS
  • Basic Linux skills – We’ll be working on the command line
  • Static IP Address – Set your Raspberry Pi to use a static IP address

Step 1 – Install Prometheus

Prometheus is available as a pre-built binary for the ARM architecture so installation on the Raspberry Pi is straight-forward.

First, SSH into your Raspberry Pi and then run the following commands:

Download latest Prometheus binary 

wget https://github.com/prometheus/prometheus/releases/download/v2.22.0/prometheus-2.22.0.linux-armv7.tar.gz

Extract the tarball

tar xvfz prometheus-2.22.0.linux-armv7.tar.gz

Move prometheus and promtool binaries to /usr/local/bin/

sudo cp prometheus-2.22.0.linux-armv7/prometheus /usr/local/bin/

sudo cp prometheus-2.22.0.linux-armv7/promtool /usr/local/bin/

Check that Prometheus is working

prometheus –version

This will install the Prometheus server and the promtool utility to /usr/local/bin/.

Step 2 – Create Necessary Directories

Prometheus requires some directories for storing configuration, data, and logs. We need to create these directories:

#Prometheus configuration directory

sudo mkdir /etc/prometheus

 Prometheus data directory 

sudo mkdir /var/lib/prometheus

 Prometheus log directory

sudo mkdir /var/log/prometheus

Step 3 – Configure Prometheus

Next, we need to create a basic Prometheus configuration file. This controls what metrics Prometheus will scrape and various other settings.

Create a new file /etc/prometheus/prometheus.yml:

 Global config

global:

  scrape_interval:     15s # Scrape targets every 15 seconds

  evaluation_interval: 15s # Evaluate rules every 15 seconds

 Load and alert rules 

rule_files:

  – “alert.rules”

 Scrape config to monitor Prometheus itself

scrape_configs:

  – job_name: ‘prometheus’

    static_configs:

      – targets: [‘localhost:9090’]

This config scrapes the Prometheus server itself on port 9090. We’ll add additional scrape jobs later.

Step 4 – Create a Systemd Service

To manage Prometheus as a service, we’ll create a Prometheus systemd unit file. This will handle starting Prometheus at boot and provide commands to start, stop, and status the service.

Create a file /etc/systemd/system/prometheus.service:

[Unit]

Description=Prometheus Monitoring

After=network-online.target

[Service]

User=prometheus

Restart=on-failure

These environment variables avoid permission issues

Environment=PID_FILE=/var/run/prometheus.pid

Environment=DATA_DIR=/var/lib/prometheus

ExecStart=/usr/local/bin/prometheus \

        –config.file /etc/prometheus/prometheus.yml \

        –storage.tsdb.path ${DATA_DIR} \

        –web.console.templates=/etc/prometheus/consoles \

        –web.console.libraries=/etc/prometheus/console_libraries

ExecReload=/bin/kill -HUP $MAINPID

TimeoutStopSec=20s

SendSIGKILL=no

[Install]

WantedBy=multi-user.target

This unit file configures Prometheus to start on boot, run as the prometheus user, and use the configuration and data directories we created.

Now we can start Prometheus:

Reload systemd daemon

sudo systemctl daemon-reload

Enable Prometheus to start on boot 

sudo systemctl enable prometheus

 Start Prometheus

sudo systemctl start prometheus

Prometheus should now be up and running! We can check the status:

sudo systemctl status prometheus

And access the Prometheus UI at http://raspberrypi-ip:9090. The UI allows us to query connected targets and view metrics.

Step 5 – Install Node Exporter

Prometheus uses exporters to collect and expose metrics from hosts and services. We’ll install the node_exporter to monitor our Raspberry Pi system resources.

Download the latest ARM build:

wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-armv7.tar.gz

tar xvfz node_exporter-1.0.1.linux-armv7.tar.gz

Move the exporter to /usr/local/bin:

sudo cp node_exporter-1.0.1.linux-armv7/node_exporter /usr/local/bin

Create a systemd unit file /etc/systemd/system/node_exporter.service:

[Unit]

Description=Node Exporter

After=network.target

[Service]

User=nodeexporter

Group=nodeexporter

Type=simple

ExecStart=/usr/local/bin/node_exporter

[Install]  

WantedBy=multi-user.target

This will run the exporter as the nodeexporter user.

Now enable the exporter:

sudo useradd -rs /bin/false nodeexporter

sudo systemctl daemon-reload

sudo systemctl enable node_exporter

sudo systemctl start node_exporter

The node exporter will now expose system metrics on port 9100.

Step 6 – Configure Prometheus Targets

We need to update our Prometheus config to scrape metrics from the node exporter.

Edit /etc/prometheus/prometheus.yml and add the following job:

 – job_name: ‘node’

    static_configs:

      – targets: [‘localhost:9100’]

Now restart Prometheus for the change to take effect:

sudo systemctl restart prometheus

Prometheus will now scrape system metrics from the exporter. We can view graphs and dashboards for this data in the Prometheus UI.

Step 7 – Monitor a Sample Application

Let’s have Prometheus monitor a sample application to see the process in action. We’ll use a simple Flask app that exposes Prometheus metrics.

First, install Flask:

sudo pip install flask prometheus_client

Now create an app.py with the following code:

python

from flask import Flask

from prometheus_client import make_wsgi_app, Counter, Gauge, Histogram

from werkzeug.middleware.dispatcher import DispatcherMiddleware

from random import random

app = Flask(__name__)

requests = Counter(‘app_requests’, ‘Total app requests’)

inprogress = Gauge(‘app_requests_inprogress’, ‘App requests in progress’)

last_request = Gauge(‘app_last_request’, ‘Time of last request’)

response_time = Histogram(‘app_response_time’, ‘App response time distribution’)

@app.route(‘/’)

def hello():

  requests.inc()

  inprogress.inc()

  last_request.set_to_current_time()

  start = time.time()

  time.sleep(random()) # simulate random processing time

  end = time.time()

  response_time.observe(end start)

  inprogress.dec()

  return ‘Hello World!’

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {

  ‘/metrics’: make_wsgi_app()  

if __name__ == ‘__main__’:

  app.run(host=‘0.0.0.0’, port=8000)

This simple app exposes a /metrics endpoint with Prometheus metrics on total requests, requests in progress, last request time, and response time distribution.

Now start the app:

python app.py

The app will run on port 8000. Let’s add it as a Prometheus target.

Update /etc/prometheus/prometheus.yml:

 – job_name: ‘app’

    static_configs:

      – targets: [‘localhost:8000’]

Restart Prometheus and we should begin collecting metrics for the application. You can browse to the Prometheus UI to graph and query the metrics.

This demonstrates how we can leverage exporters to monitor any application code by simply exposing metrics to Prometheus.

Conclusion

In this guide we walked through installing Prometheus and Node Exporter on a Raspberry Pi. We configured Prometheus to monitor system resources and created a sample application to demonstrate exposing custom application metrics.

Prometheus is a powerful open-source monitoring solution that works great on single board computers like the Raspberry Pi. With the ability to monitor infrastructure, applications, and custom metrics, Prometheus gives excellent visibility into your systems.

Key Takeaways

  • Prometheus provides powerful time-series monitoring and alerting suitable for devices like the Raspberry Pi
  • Exporters expose metrics from systems and applications for Prometheus to scrape
  • The Node Exporter allows monitoring of Raspberry Pi system resources
  • Applications can expose metrics by adding a /metrics endpoint
  • Careful Prometheus configuration is important to only scrape necessary targets

Frequently Asked Questions

  1. Does Prometheus work on other single board computers like the BeagleBone or Orange Pi?
    Yes, Prometheus can be installed on any Linux-based single board computer that can run the ARM binary. The same installation process would apply.
  1. What other exporters are useful to monitor on a Raspberry Pi?
    Some other useful exporters include statsd_exporter for application metrics, snmp_exporter for network device metrics, blackbox_exporter for blackbox monitoring of endpoints, and consul_exporter if using Consul service discovery.
  1. What are best practices for monitoring remote Raspberry Pis with Prometheus?
    Enable logging and metrics exposure on the remote devices. Scrape the remote devices metrics over the network using a single central Prometheus server. Ensure remote devices are secure and minimize attack surface.
  1. What Grafana plugins work well with Prometheus on a Raspberry Pi?
    The Prometheus data source, Pie Chart plugin, and Status History panel are very useful for building monitoring dashboards for Raspberry Pis. The Statusmap panel can provide nice topology maps of multiple Pis.
  1. How do Prometheus alerts and notifications work?
    Prometheus can send alerts based on configured alerting rules. It supports various notification integrations like email, PagerDuty, Slack, OpsGenie. Useful for alerts on metrics or server down.
  1. Can Prometheus monitor Kubernetes on a cluster of Raspberry Pis?
    Yes, using the Prometheus Kubernetes operator and node-exporter makes it easy to monitor a Kubernetes cluster running on Raspberry Pis. Metrics are natively exposed.
  1. What are the requirements for running Prometheus at scale?
    Prometheus servers can be scaled horizontally. Need fast network, SSD storage, and time synchronization. Can shard by having Prometheus servers scrape only subsets of targets.
  1. Does Prometheus support high availability configurations?
    Yes, you can run multiple Prometheus servers in a high availability setup with replicas scraping the same targets. Servers use a hash ring to determine which scrapes metrics.
  1. How do I collect metrics from JMX-based Java applications on a Raspberry Pi?
    Use the JMX exporter to expose JMX metrics to Prometheus. Set -javaagent JMX arguments. Scrape the JMX exporter in Prometheus like other exporters.
  1. What push gateway options exist for ephemeral Raspberry Pi devices?
    For ephemeral devices, use the Pushgateway. This allows short-lived jobs to expose metrics to Prometheus on exit via HTTP. Useful for batch jobs.
  1. Can Prometheus monitor itself and its components?
    Yes, Prometheus exposes extensive metrics about itself including scrape status, rule evaluation, and alerts. Useful for monitoring the monitoring system.
  1. Does Prometheus have data retention limits?
    Prometheus is designed for operational monitoring so has limited data retention. Retention is configurable per time series but defaults to 15 days. For long term storage use a TSDB.
  1. How can I use Grafana with Prometheus on a Raspberry Pi?
    Install Grafana, add Prometheus as a data source, then build dashboards using PromQL queries. Grafana+Prometheus provides a full monitoring stack on the Pi.
  1. What benefits does Prometheus have over other monitoring systems?
    Prometheus uses a pull model, has native Kubernetes support, PromQL querying, and does not require storage backends. It integrates well into cloud-native infrastructure.
  1. What types of metrics should I avoid exposing to Prometheus?
    Avoid business metrics unrelated to system health, sensitive customer data, and high cardinality labels which cause metric explosion. Follow best practices for labeling.
  1. How do I monitor SSL certificate expiry with Prometheus?
    Use the blackbox_exporter to periodically check SSL certificate information. Expose an expiration metric from the date. Alert if getting close to expiry.
  1. Can Prometheus be used with Python applications on the Raspberry Pi?
    Yes, the prometheus_client Python library makes it easy to expose metrics from Python apps. Decorate functions with metrics decorators to instrument code.
  1. Is SNMP monitoring possible with Prometheus?
    Yes, use the snmp_exporter to poll SNMP devices and expose metrics to Prometheus. Set SNMP credentials in exporter config to integrate network gear monitoring.
  1. Does scraping metrics with Prometheus impact Raspberry Pi performance?
    Scraping is lightweight, but enable HTTP keep-alives and increase scrape intervals to minimize load. Scrape targets in parallel, and use dedicated scrape servers if needed.
  1. Where can I find more documentation and guides on Prometheus?
    The Prometheus website has excellent documentation on all aspects of operation, configuration, integrations and usage. Their docs cover metrics best practices also.

 

Leave a Comment