Monitoring a Raspberry Pi with Python, InfluxDB and Grafana

Inspired by this guide, I wrote this script to send system stats to InfluxDB 2.0 for a Pi that’s running as my off site backup server. More info about that project (with 3D printed case) in this article. While this is aimed at Raspberry Pi based systems, it should work on any other machine that runs a Debian based operating system.

Update 1: As part of this project to build a 1u Raspberry Pi enclosure, I have included a second script that includes libraries to read data from a DS18B20 temperature sensor that is connected to the 1-wire GPIO pin of the raspberry pi.

Update 2: I have added a Grafana dashboard template (.json file) to the GitHub repo because the InfluxDB dashboards can be a bit limited.

Github repo for this project: thatguy-za/rpi-stats-influxdbv2

Installation and setup guide

  1. Install Docker Engine (guide here)
  2. Install influxdb (I did this in docker – guide here) and setup an API token (guide here). Note: Influxdb only support 64bit operating systems.
  3. Run sudo apt install -y python3-pip to install python and pip.
  4. Run sudo pip install psutil influxdb-client to install the psutil library that we use to pull stats and the influxdb client to send the data to influxdb.
  5. Download the pi_stats_influx.py script from my GitHub repo linked above
  6. Update the script with your influxdb details (API key, server connection details, etc).
  7. Run crontab -e and add this script to run every minute by adding * * * * * python /home/pi/rpi-stats-influx.py to the end of the file
  8. Upload the dashboard_template.json file to influxdb so you have a basic dashboard to view metrics.
  9. Customise the graphs to suit your needs.
  10. Enjoy 😀

*The product links in this post may contain affiliate links. I donate 20% of these earnings to the Good Work Foundation to help innovate learning in South Africa’s rural communities.

Thanks for making it to the end of the post!

Leave a Reply

Your email address will not be published. Required fields are marked *