RSS
Search

Collecting SpeedTest results in Influx on Raspberry Pi

How to set up regular SpeedTest reports into a local InfluxDB on a Raspberry Pi

Now that you've got your Pi set up with Influx & Grafana and you're collecting some system stats, it's time to measure network performance!

SpeedTest.net by Ookla is probably the most popular connection testing services, and they handily provide a CLI to run tests programmatically. We'll create a simple python script which runs a test and sends the data to influxdb, then set it to run once every 15 minutes using Cron.

First, we need to install the speedtest-cli client:

sudo apt install -y python-pip
sudo pip install speedtest-cli influxdb

Then we can create a python script rpi-speedtest-influx.py in our home directory to run tests:

#!/usr/bin/env python

import datetime
import speedtest
from influxdb import InfluxDBClient

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

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

# run a single-threaded speedtest using default server
s = speedtest.Speedtest()
s.get_best_server()
s.download(threads=1)
s.upload(threads=1)
res = s.results.dict()

# format the data as a single measurement for influx
body = [
{
"measurement": measurement_name,
"time": time,
"fields": {
"download": res["download"],
"upload": res["upload"],
"ping": res["ping"]
}
}
]

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

# write the measurement
ifclient.write_points(body)

View gist on GitHub

We'll need to make it executable: chmod +x rpi-speedtest-influx.py and then we can run it to test it out: ./rpi-speedtest-influx.py. You should be able to see the measurement in influx as soon as the script has exited, check by running influx CLI and execute the following to check your measurement (swapping database and measurement names for those you defined in your python script):

use home
select * from speedtest

If everything looks ok, we can add a cron job for our user (using crontab -e) to run this script at a regular interval, e.g. every 15 minutes: */15 * * * * /home/pi/rpi-speedtest-influx.py. Any more than 15 minutes is probably unnecessary and may have an adverse effect on your perceived network quality.

You can now simply add a panel to your Grafana instance to show the download, upload and ping results over time. Here is a sample dashboard you can import which includes the panel shown below:

SpeedTest Grafana dashboard.
SpeedTest Grafana dashboard.