User Tools

Site Tools


creating_the_speedtest_image_every_day

This is an old revision of the document!


Creating the SpeedTest image every day

So, I like to see how much I'm getting for my $52 per month, so I wrote this script a long time ago to track upload, download and delay numbers across my network connection.

First, there are two scripts in /usr/local/bin;

SpeedTest
#!/usr/bin/bash
# /usr/local/bin/SpeedTest
# Quick and simple log file of connection speed tests
# # SRJ 2016-12-26
YM=$(date +%Y-%m) # 2019-07
MT=$(date -d "+6 hours" +%Y-%m) # Could be 2019-08 if this is the last of the month
 
# Where we store data
Log=~/.SpeedTest.log
PLog=~/.PlotLog
 
# Where we store images
ARG1="/var/www/html/SpeedTest"
ARG2="/var/www/html/${YM}-SpeedTest"
 
# Date format in the log file
Date=$(date +"%Y-%m-%d %H:%M")
 
# Install with; npm install --global fast-cli
Data=($(/usr/local/bin/fast -u --json \
    | grep -E 'latency|downloadSpeed|uploadSpeed' \
    | cut -d : -f 2 | tr -d ',' \
    | tr -d '\n' ; echo "")) 
 
# Formated data; delay, download, upload
FData="${Data[2]} ${Data[0]} ${Data[1]}"
 
# Save it to the log like; 2019-07-20 12:09 14.72 514.17 134.54
echo "${Date} ${FData}" >> ${Log}
 
# Dump the last 128 lines into the plotlog
tail -128 ${Log} > ${PLog}
 
# Run the gnuplot script on the plotlog
/usr/local/bin/PlotSpeedTest 
 
# If this is the end of the month, copy the current image to an archive image.
if [ ${YM} != ${MT} ] 
        then 
        cp ${ARG1}.png ${ARG2}.png
        fi

Part two, the gnuplot script;

PlotSpeedTest
#!/usr/bin/gnuplot
# /usr/local/bin/PlotSpeedTest
# # SRJ 2016-12-26
fmt = '%Y-%m-%d %H:%M';        # Format used for printing out time
time_diff=4*60*60;             # Seconds between local time zone and UTC
curr_time_utc = time(0);       # Get UTC time
curr_time_loc = curr_time_utc - time_diff;     # Adjust to local time zone
set title "Connection speeds for 32 days past, CBT Fioptics\nLast run: ".strftime(fmt, curr_time_loc);
set style data lines
set term pngcairo size 1820, 980
set timefmt "%Y-%m-%d %H:%M"
set xdata time
set format x "%b-%d"
set xtics rotate
set xlabel "Time, tics are daily, samples are every six hours (09 */6 * * *)...
    Tested using Sindre Sorhus' fast-cli."
set autoscale y
set autoscale xfix
set style line 1 lt 2 lc rgb "black" lw 4
set style line 2 lt 2 lc rgb "green" lw 2
set style line 3 lt 2 lc rgb "red" lw 2
set grid
set key bottom left
set output "/dev/null"
 
plot '/root/.PlotLog' using 1:4
max_Dn = GPVAL_DATA_Y_MAX
min_Dn = GPVAL_DATA_Y_MIN
 
plot '/root/.PlotLog' using 1:5
max_Up = GPVAL_DATA_Y_MAX
min_Up = GPVAL_DATA_Y_MIN
 
plot '/root/.PlotLog' using 1:3
max_Dl = GPVAL_DATA_Y_MAX
min_Dl = GPVAL_DATA_Y_MIN
 
set output "/var/www/html/SpeedTest.png"
plot \
'/root/.PlotLog' using 1:4 title sprintf("Download, Mbs, Max:%g Min:%g",  max_Dn, min_Dn ) with lines ls 1, \
'/root/.PlotLog' using 1:5 title sprintf("Upload, Mbs, Max:%g Min:%g",  max_Up, min_Up ) with lines ls 2, \
'/root/.PlotLog' using 1:3 title sprintf("Delay, ms, Max:%g Min:%g",  max_Dl, min_Dl ) with lines ls 3

Part three, this is called by cron every six hours with;

crontab
09 */6 * * * /usr/local/bin/SpeedTest

It works well, I have a text log going back for years, I have monthly images saved in the web server, and one that updates every six hours that I link on my phone, just to brag; https://www.stevejonescomputers.com/SpeedTest.png

creating_the_speedtest_image_every_day.1708707817.txt.gz · Last modified: by steve