Julkaistu 13.8.2015 kirjoittanut Jyrki Pesonen
Howto: speedtest-cli with rrd
There are several howto’s(link is external) around on how to install and use speedtest-cli(link is external), the commandline flavor of Ookla -based test site(link is external).
I wanted something more, not much but being curious on what happens around in my small systems, I stapled together this pile of spaghetti…
It uses speedtest-cli(link is external) as initial data input, RRDtool(link is external) as visualizer, bash and misc programs related to it, and whatever (link is external)webserver for publishing. I’m not guru on bash, mentioning this ’cause I do enjoy good omg-w4tDidUDo -spam from the ones that are…
1. Install speedtest-cli and make sure it works:
wget https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
sudo mv speedtest_cli.py /usr/local/bin/speedtest-cli
sudo chmod a+rx /usr/local/bin/speedtest-cli
speedtest-cli
(in case you didn’t figure above out, just head to the website(link is external) and don’t bother reading more)
After final command you should get results on your ping, upload and download. Let’s move forward.
2. Write code that figures out your hometown server and uses it to test speed and gives you a nice pic to share the results. (using vi is considered ultrageeky, reason why I nano)
mkdir -p /usr/src/scripts/speedtest
nano /usr/src/scripts/speedtest/get_my_speedtest_status.sh
#!/bin/bash
# This is ’what speedtest-cli –server XXXXX –share’ typically gives:
#
# Retrieving speedtest.net configuration…
# Retrieving speedtest.net server list…
# Testing from Masternet Inc (185.9.12.1)…
# Hosted by Masternet Inc (Eastwesttown) [1.41 km]: 15.286 ms
# Testing download speed………………………………….
# Download: 98.56 Mbit/s
# Testing upload speed…………………………………………..
# Upload: 1.91 Mbit/s
# Share results: https://www.speedtest.net/result/076719.png
cd /usr/src/scripts/speedtest
DATE=`/bin/date ”+%Y.%m.%d-%H%M%S”`
#this a regexp to figure out numbers most of the kind
re=’^-?[0-9]+([.][0-9]+)?$’
MYPING=0
MYUPLOAD=0
MYDOWNLOAD=0
# Replace this with our known city
MYTOWN=”Eastwesttown”
MYSERVER=`/usr/local/bin/speedtest-cli –list | grep -i $MYTOWN|awk ’{print $1}’|sed -e ”s/)//g”`
#echo ”server=$MYSERVER”
if [[ $MYSERVER =~ $re ]] ;
then
MYRETURN=`/usr/local/bin/speedtest-cli –server $MYSERVER –share`
# echo ”return=$MYRETURN”
echo ”$MYRETURN”>tmp
# cat tmp
MYPING=`cat tmp|grep -i ”^Hosted by”|awk ’{print $(NF-1)}’`
# echo ”ping=$MYPING”
MYUPLOAD=`cat tmp|grep -i ”^Download”|awk ’{print $(NF-1)}’`
# echo ”uplink=$MYUPLOAD”
MYDOWNLOAD=`cat tmp|grep -i ”^Upload”|awk ’{print $(NF-1)}’`
# echo ”downlink=$MYDOWNLOAD”
MYLINK=`cat tmp|grep -i ”^Share results”|awk ’{print $(NF)}’`
echo $MYLINK>/var/www/speedtest/lastlink2pic
# echo ”piclink=$MYLINK”
fi
if [[ $MYPING =~ $re ]] && [[ $MYUPLOAD =~ $re ]] && [[ $MYDOWNLOAD =~ $re ]] ;
then
# echo ”Got values:”
echo ”$MYPING”
echo ”$MYUPLOAD”
echo ”$MYDOWNLOAD”
else
# echo ”NO values:”
echo ”0”
echo ”0”
echo ”0”
fi
And then save.
3. Make another code that uses above to dump the data into RRD:
nano /usr/src/scripts/speedtest/update_my_speedtest_rrd
#!/bin/bash
DATE=`/bin/date ”+%Y.%m.%d %H:%M:%S”`
MYMONTH=`/bin/date ”+%Y-%m-%d”`
MYPATH=”/var/www/speedtest/speedtest.rrd”
/home/root/scripts/speedtest/get_speedtest_status.sh>/var/www/speedtest/raw.txt
MYDATA=`cat /var/www/speedtest/raw.txt|tail -n20|gawk ’BEGIN{ORS=”:”;i=0;}{print;i=i+1;}END{if(i<3) for(;i<3;i++)print ”-400”;}’|sed -e ’s/:$//g’`
echo -e ”$MYDATA\r\n”
echo -e ”$DATE:$MYDATA”>>/var/www/speedtest/log-$MYMONTH.txt
rrdtool update $MYPATH N:$MYDATA
and save,
4. Now create initial RRD database, you might want to read few faq’s around the net on rrdtool create.
nano /usr/src/scripts/speedtest/create_my_speedtest_rrd
#!/bin/bash
DATE=`/bin/date ”+%Y.%m.%d-%H%M%S”`
MYPATH=”/var/www/speedtest/speedtest.rrd”
rrdtool create ”$MYPATH” \
–start N –step 3600 \
DS:MY-PING:GAUGE:7200:0:6000 \
DS:MY-DOWNLINK:GAUGE:7200:0:3000 \
DS:MY-UPLINK:GAUGE:7200:0:3000 \
RRA:MIN:0.5:60:8785 \
RRA:MAX:0.5:60:8785 \
RRA:AVERAGE:0.5:1:8785
save and run:
mkdir -p /var/www/speedtest
bash /usr/src/scripts/speedtest/create_my_speedtest_rrd
5. now create code for updating the graphs:
nano /usr/src/scripts/speedtest/graph_speedtest_rrd
#!/bin/bash
DATE=`/bin/date ”+%Y.%m.%d-%H%M%S”`
MYRRD=”/var/www/speedtest/speedtest.rrd”
MYPATH=”/var/www/speedtest/”
cd $MYPATH
# last 12hours: –end now –start end-43200s
# last 24hours: –end now –start end-86400s
# last week: –end now –start end-604800s
# last month: –end now –start end-2419200s
# last year: –end now –start end-31536000s
PARAMS_ARRAY=(”–” ”-s e-12h” ”-s e-24h” ”-s e-1w” ”-s e-1m” ”-s e-1y”)
TITLE_ARRAY=(”” ”Last 12h” ”Last 24h” ”Last week” ”Last month” ”Last year”)
FILENAME_ARRAY=(”a” ”12h” ”24h” ”week” ”month” ”year”)
#DS:MY-PING:GAUGE:120:0:6000 \
#DS:MY-DOWNLINK:GAUGE:120:0:3000 \
#DS:MY-UPLINK:GAUGE:120:0:3000 \
for N in {0..5}
do
rrdtool graphv -J ”$MYPATH”my-ping-”${FILENAME_ARRAY[$N]}”.png -a PNG –title=”Ping ${TITLE_ARRAY[$N])}” \
–vertical-label ”ms” \
”${PARAMS_ARRAY[$N]}” \
”DEF:T=speedtest.rrd:MY-PING:AVERAGE” \
”DEF:TMIN=speedtest.rrd:MY-PING:MIN” \
”DEF:TMAX=speedtest.rrd:MY-PING:MAX” \
’AREA:TMIN#00ff00:Ping min’ \
’AREA:TMIN#ffb6c1:Ping max’ \
’LINE1:T#ff0000:Ping’ \
’GPRINT:T:LAST:Last Ping\: %2.1lf ms’ \
’GPRINT:TMAX:MAX:Max Ping\: %2.1lf ms’ \
’GPRINT:TMIN:MIN:Min Ping\: %2.1lf ms’
rrdtool graphv -J ”$MYPATH”my-downlink-”${FILENAME_ARRAY[$N]}”.png -a PNG –title=”Downlink speed ${TITLE_ARRAY[$N])}” \
–vertical-label ”Mbit/s” \
”${PARAMS_ARRAY[$N]}” \
”DEF:T=speedtest.rrd:MY-DOWNLINK:AVERAGE” \
”DEF:TMIN=speedtest.rrd:MY-DOWNLINK:MIN” \
”DEF:TMAX=speedtest.rrd:MY-DOWNLINK:MAX” \
’AREA:TMIN#00ff00:Down min’ \
’AREA:TMIN#ffb6c1:Down max’ \
’LINE1:T#ff0000:Downlink’ \
’GPRINT:T:LAST:Last downlink\: %2.1lf Mbit/s’ \
’GPRINT:TMAX:MAX:Max downlink\: %2.1lf Mbit/s’ \
’GPRINT:TMIN:MIN:Min donwlink\: %2.1lf Mbit/s’
rrdtool graphv -J ”$MYPATH”my-uplink-”${FILENAME_ARRAY[$N]}”.png -a PNG –title=”Uplink speed ${TITLE_ARRAY[$N])}” \
–vertical-label ”Mbit/s” \
”${PARAMS_ARRAY[$N]}” \
”DEF:T=speedtest.rrd:MY-UPLINK:AVERAGE” \
”DEF:TMIN=speedtest.rrd:MY-UPLINK:MIN” \
”DEF:TMAX=speedtest.rrd:MY-UPLINK:MAX” \
’AREA:TMIN#00ff00:Up min’ \
’AREA:TMIN#ffb6c1:Up max’ \
’LINE1:T#ff0000:Uplink’ \
’GPRINT:T:LAST:Last uplink\: %2.1lf Mbit/s’ \
’GPRINT:TMAX:MAX:Max uplink\: %2.1lf Mbit/s’ \
’GPRINT:TMIN:MIN:Min uplink\: %2.1lf Mbit/s’
done
6. Easy, eh? Now prepare minimalistic index.php:
nano /var/www/speedtest/index.php
|
![]() |
![]() |
![]() |