Simon Hearne

Web Performance Consultant

Email Twitter LinkedIn Github RSS

Reporting Raspberry Pi System Metrics to InfluxDB


How to set up regular system stat reports from a Raspberry Pi into a local InfluxDB

❤️ 4 likes 🔁 1 repost 💬 3 comments 🔗 1 share as of 08:00, June 21

Now that you’ve got your Pi set up with Influx & Grafana, it’s time to start collecting some data! We will use a simple Python script to collect system statistics and push it to Influx - you can use this as-is or modify it to your needs.

We will need to first install the PIP package manager if it is not already on your Pi:

sudo apt install -y python-pip

And then use pip to install the python packages psutil and influxdb:

sudo pip install psutil influxdb

The python script below will use psutil to collect some system stats and influx to insert them into your influx db:

#!/usr/bin/env python

import datetime
import psutil
from influxdb import InfluxDBClient

# influx configuration - edit these
ifuser = "grafana"
ifpass = "<yourpassword>"
ifdb   = "home"
ifhost = ""
ifport = 8086
measurement_name = "system"

# take a timestamp for this measurement
time = datetime.datetime.utcnow()

# collect some stats from psutil
disk = psutil.disk_usage('/')
mem = psutil.virtual_memory()
load = psutil.getloadavg()

# format the data as a single measurement for influx
body = [
        "measurement": measurement_name,
        "time": time,
        "fields": {
            "load_1": load[0],
            "load_5": load[1],
            "load_15": load[2],
            "disk_percent": disk.percent,
            "disk_used": disk.used,
            "mem_percent": mem.percent,
            "mem_used": mem.used

# connect to influx
ifclient = InfluxDBClient(ifhost,ifport,ifuser,ifpass,ifdb)

# write the measurement

View gist on GitHub

Create this script in your home directory, e.g. /home/pi/ and make the script executable with chmod +x

Test the script out by running it with ./ and check that no errors are thrown. If all is good, we can set the script to run reqularly using cron

Run crontab -e to edit your user’s crontab and add an entry * * * * * /home/pi/ to the bottom of the file, this will run the script every minute. The script is very lightweight so running every minute should be fine.

See the docs for psutil to get ideas for other stats you can collect.

You can now simply add a panel to your Grafana instance to show your system stats over time. Here is a sample dashboard you can import which includes the panel shown below:

Pi Stats Grafana dashboard.

Work with me!

I'm currrently available to consult - from web performance workshops to reviewing new site designs, third-party audits to global performance assessments. Head over to my Consultancy page to see the kind of work I help my clients with and for details on how to get started.

Join the conversation

The comments on this page are fed by tweets, using, and a modified version of jekyll-webmention_io. To comment, just send a webmention or use a link below to interact via twitter.

Recent reposts

Recent comments

  1. JF Baratte

    Hello, thanks it’s better but it seems that it can’t find the database home and looking ifdb instead. Did i make a mistake in my active db ? Regards

  2. Simon Hearne

    Yep you need to edit the db config lines in your script to match your setup - database, username and password

  3. JF Baratte

    I forgot that update . It works well now

    Thank you very much


Recent shares