How to setup Raspberry pi ISP speed testing exported to grafana

How to setup Raspberry pi ISP speed testing exported to grafana

Requirements

  • Raspberry Pi  4 Model B  | Other models may work but I've only tested it on this model
  • Micro SD Card
  • Ethernet connection | While you can run the tests on the wireless network you should run it on a cabled connection for best results
  • Lots of coffee

Installing influx

Step 1 | Update

As with every guide you see out there first step is to update your system to the newest version, do so by running the following command

sudo apt update && sudo apt upgrade

Step 2 | Add the InfluxDB Key

Since InfluxDB doesn't come in the default repository package we have to add the key for it manually so that the apt manager can search for the package and verify what's it's installing.

You can add the key by running the following command

wget -qO- <https://repos.influxdata.com/influxdb.key> | sudo apt-key add -

Step 3 | Add the InfluxDB Repository

Now that we have the key added we need to add the repository to our sources list so the apt manager can download it

echo "deb <https://repos.influxdata.com/debian> $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

Proceed to update the package list by running the following command

sudo apt update

Step 4 | Install InfluxDB

After all that work from before we can now simply install InfluxDB by running the following command

sudo apt install influxdb

Just like any regular application

Step 5 | Get it to start at boot (optional)

With InfluxDB now installed let's get it to run at boot since who wants to start this manually every time their Raspberry restarts

Making InfluxDB start at boot is pretty simple just run the following commands and you're done

sudo systemctl unmask influxdb
sudo systemctl enable influxdb

Step 6 | Liftoff!

Start by counting down like one of those flight control managers at SpaceX and write the following command into your terminal

sudo systemctl start influxdb

Using InfluxDB

Step 1 | Start using InfluxDB

Start off by starting to use InfluxDB using the command

influx

Step 2 | Create your first database

Since InfluxDB doesn't come with any databases by default we have to start off by creating one

You can easily do this by running the following command

CREATE DATABASE speedsterr

This will create a database called "speedsterr" You can change this to anything you'd like really I just prefer having this name.

Step 3 | Create your first user

Since we want our internet speed monitor to interface with InfluxDB to store the speed test results we need to create a user for it. You do this by running the following command

CREATE USER "speedsterr" WITH PASSWORD 'YouShouldProbablyChangeYourPassword'

This will create a user named "speedsterr" with the password "YouShouldProbablyChangeYourPassword". Now if you've got a keen eye you might have noticed that the password is actually something you should change. No really, CHANGE IT

Step 4 | Grant your new user permissions

Now you need to grant your user permission to do stuff on your new database, for the sake of simplicity we'll be granting our user all the permissions by running the following command

GRANT ALL ON "speedster" to "speedsterr"

Now this grans all permissions on the database "speedsterr" which we created in step 2 to the user "speedsterr" we created in the previous step.

And that's it you're done with InfluxDB for now. Go ahead and exit the Influx interface by writing

quit

Step 5 | Install the python library

So for us to be able to interface with InfluxDB from our python file which will be running the speed tester we need to install a library called influxdb do this by entering the following

sudo apt install python3-influxdb

Creating the speed monitor

Step 1 create your python file

Create a python file called main.py or anything else you'd like. You can do so by doing

nano main.py

Paste in the following code

import re
import subprocess
from influxdb import InfluxDBClient

response = subprocess.Popen('/usr/bin/speedtest --accept-license --accept-gdpr', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')

ping = re.search('Latency:\\s+(.*?)\\s', response, re.MULTILINE)
download = re.search('Download:\\s+(.*?)\\s', response, re.MULTILINE)
upload = re.search('Upload:\\s+(.*?)\\s', response, re.MULTILINE)
jitter = re.search('\\((.*?)\\s.+jitter\\)\\s', response, re.MULTILINE)

ping = ping.group(1)
download = download.group(1)
upload = upload.group(1)
jitter = jitter.group(1)

speed_data = [
    {
        "measurement" : "internet_speed",
        "tags" : {
            "host": "SpeedPi"
        },
        "fields" : {
            "download": float(download),
            "upload": float(upload),
            "ping": float(ping),
            "jitter": float(jitter)
        }
    }
]
client = InfluxDBClient('localhost', 8086, 'username', 'password', 'database')
client.write_points(speed_data)

Now in this, you only need to change the following line

client = InfluxDBClient('localhost', 8086, 'username', 'password', 'database')

With everything filled in your terminal should look something like this

after you've changed that line export it by hitting CTRL + X pressing Y then enter

You can check if the file is working by running

python3 main.py

Running it automatically

Now we've set everything up, but you don't want to run this manually every hour or so do you? That's why the Raspberry pi is so amazing it comes with crontab. A crontab is a tool that will run every time you tell it to. Since I want to run a test on my network every 30 minutes my cron task will look like this

*/30 * * * *

If you want to have it run on a different schedule you can easily set it up to do so using this website https://crontab.guru/

Here's an example of what it looks like, it's pretty easy to use and really helpful for beginners and pro's alike

Now it's time to actually implement the crontab. Go to your terminal and write

crontab -e

This will bring up the configuration file for crontab. By default, it looks like this

At the bottom of the file, you want to add the following line

*/30 * * * * python3 /home/pi/location/to/speedsterr/main.py

The first bit is the actual cron timing while the second part is the command you want to run. Since our script was made using python3 we want to run it using python3. When it comes to the location unless you have a different user than default or something like that it will be under "/home/pi/main.py" if it's in a different path than that you're most likely proficient enough to figure out where it is yourself

Show Comments