I can be described as a network engineer, sometimes, and sometimes a system admin. As such, keeping track of how customers internet is working is reasonably important.
Most customers use Speedtest.net as a way of testing their internet connection, so I figured I would clobber together a script that keeps a bit of history, and make some pretty graphs 😅.
Write a bash script to create, update, and graph, some results
Profit!
Prerequisites
I’m not going to talk you through setting up a Linux server.
I will assume that you have a running server.
Your linux server can be any flavour you like, I prefer Debian and Ubuntu distros, and I prefer aptitude as a package manager; you can use whatever you like 🤗.
I’m also not going to talk you through setting up a web server .. sort that out yourself 😜.
You NEED unlimited data on your internet!!!!
Let me say that again .. YOU NEED UNLIMITED DATA ON YOUR INTERNET !!!
This script will thrash your internet usage!
Installing the packages, and setting up directories
Right, let’s start with installing rrdtool, Gawk, and grep sudo aptitude install rrdtool gawk grep
That’s pretty easy !
Install Ookla Speedtest cli
So you can head over to the Ookla Speedtest cli website and download the appropriate package. In my case it’s the Linux x86_64 package, which is at version 1.0.0. wget https://bintray.com/ookla/download/download_file?file_path=ookla-speedtest-1.0.0-x86_64-linux.tgz -O /tmp/speedtest.tgz
Now let’s unpack it: tar xvfz /tmp/speedtest.tgz -C /tmp
And put it in a usable spot on the OS: sudo mv /tmp/speedtest /usr/local/bin/speedtest
Running Speedtest cli
The first time you run speedtest you will be asked to agree to a license:
Now you’re ready to go!
You can list out the nearest servers like so:
This particular customer I am testing from is connected through Vocus.
So I’m going to use Vocus to test against.
Sweet !
Create a directory
Ok, so now let’s create a directory to store history, and images. sudo mkdir /var/www/html/rrd sudo chown paul:paul /var/www/html/rrd
Note: I have changed the owner of the directory to the user ‘paul’. Change to whatever user you’re going to be running this script as.
The RRD Script
Ok, so here’s where the magic happens. So let’s skip straight to the script:
Let’s do some ‘splaining
So firstly, copy everything, save it in a file (I called mine speedtest_rrd.sh), and chmod appropriately.
Let me make an assumption that all your binary or executable files are in ~/bin.
chmod 755 ~/bin/speedtest_rrd.sh
The script can take one argument ( $1 ).
Argument 1: (create)
This creates the rrd files to store history in, and creates some rrd info that we are going to display.
Argument 2: (update)
This updates the rrd database with new values.
Argument 3: (graph)
This draws the pretty graphs !
You may have noticed right at the very beginning of the script this line: /usr/local/bin/speedtest -s 5749 -p no > /tmp/speedtest.txt 2>/dev/null
This runs the speedtest cli, and saves the output into your /tmp directory.
Running the script
Now let’s run it, and schedule it, then look at the results.
Running the script manually
Firstly we need to create the rrd database: ~/bin/speedtest_rrd.sh create
result:
simple !
Now let’s do a test: ~/bin/speedtest_rrd.sh update
and now, let’s create a graph: ~/bin/speedtest_rrd.sh graph
Alright ! we have some data !
Scheduling the script
Ok, so now we have some data, let’s start auto running the data collection.
Remember me saying at the beginning you need unlimited data on your internet?
Let me say that again .. YOU NEED UNLIMITED DATA ON YOUR INTERNET !!!
This script will thrash your internet usage!
So with that warning out of the way, let’s chuck it in cron:
note: I am running this script as the user ‘paul’. Change to whatever user you are running this script as.
The result
That’s it! we’re running. Let that script chug away for a few days, and you’ll get a result that looks like this:
Happy speedtesting !
7 Comments
Steve
This is really helpful, thank you! I made some minor edits to the script; I moved the actual speed test down to the update function so it wouldn’t run a second time when creating the graphs.
paul
Oh duh!! Good Spotting Steve!
Xavier
Hello, thank you for this tutorial it works great. On the other hand if I change the value of the crontab to 15 minutes …. the graphs are not updated. Do you have an idea?
Hi Xavier,
That’s odd, it shouldn’t have any effect.
Xavier
For the graphs to work with a crontab of 15 min you have to modify the value 600 of “DS:upload:GAUGE:600:0:U " I put it at 1000 and everything works.
Scott
This is great. I’ve got it running every 15 min to save some data. I’m running it on a Mac and for no reason I could figure out I couldn’t get cron to work, so I put it into a LaunchAgent. I’ve gone through the RRDtool help pages and can’t find the answer. Is there any way to widen the bars since I only need 4 per hour instead of 12 with a 5 min interval? I can’t figure out the setting. Any help would be great.
JerWah
Thanks! I was about 10% into writing my own version of this, and a google on how to render rrd graphs brought me here!. Saved me a ton of time. As someone else mentioned, I also moved the actual call to speedtest down into the update block but other than that it worked perfectly.
7 Comments