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
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:
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()
res = s.results.dict()
# format the data as a single measurement for influx
body = [
# connect to influx
ifclient = InfluxDBClient(ifhost,ifport,ifuser,ifpass,ifdb)
# write the measurement
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):
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: