diff --git a/librenms-speedtest.sh b/librenms-speedtest.sh index 671ef6c..70a70ae 100644 --- a/librenms-speedtest.sh +++ b/librenms-speedtest.sh @@ -1,20 +1,21 @@ #!/bin/bash # LibreNMS Speedtest plugin +RRDGraphsDir=/opt/librenms-speedtest/rrd +PNGImagesDir=/opt/librenms-speedtest/png +SpeedtestResultDir=/opt/librenms-speedtest/tmp -RRDGraphsLocation=/opt/librenms-speedtest/rrd -SpeedtestResulstTMP=/opt/librenms-speedtest/tmp case $1 in (create) - # Create the Ping measurement RRD - rrdtool create $RRDGraphsLocation/speedtest_ping.rrd -s 1800 \ - DS:PING:GAUGE:3600:0:1000 \ + # Create the Latency measurement RRD + rrdtool create $RRDGraphsDir/speedtest-latency.rrd -s 1800 \ + DS:LATENCY:GAUGE:3600:0:1000 \ RRA:AVERAGE:0.5:1:576 \ RRA:AVERAGE:0.5:6:672 \ RRA:AVERAGE:0.5:24:732 \ RRA:AVERAGE:0.5:144:1460 # Create the Bandwidth measurement RRD - rrdtool create $RRDGraphsLocation/speedtest_bandwidth.rrd -s 1800 \ + rrdtool create $RRDGraphsDir/speedtest-bandwidth.rrd -s 1800 \ DS:DOWN:GAUGE:3600:0:1000 \ DS:UP:GAUGE:3600:0:1000 \ RRA:AVERAGE:0.5:1:576 \ @@ -22,83 +23,42 @@ SpeedtestResulstTMP=/opt/librenms-speedtest/tmp RRA:AVERAGE:0.5:24:732 \ RRA:AVERAGE:0.5:144:1460 ;; - (speedtest) - # Get the EPOCH date of the moment we start the test + (run) + # Get the date of the moment we start the test, in epoch format DATE=$(/bin/date +%s) # Generate speedtest results, store them in a temp file - speedtest-cli --simple --share > $SpeedtestResulstTMP/speedtest.results 2>/dev/null + speedtest-cli --simple --share > $SpeedtestResultDir/speedtest-results 2>/dev/null - # Get the Ping time - PingTime=$(cat $SpeedtestResulstTMP/speedtest.results | grep Ping | sed -r 's/\s+//g'| cut -d":" -f2 | cut -d"m" -f1) + # Get the Latency + Latency=$(cat $SpeedtestResultDir/speedtest-results | grep Ping | sed -r 's/\s+//g'| cut -d":" -f2 | cut -d"m" -f1) # Get the Download speed - DownloadSpeed=$(cat $SpeedtestResulstTMP/speedtest.results | grep Download | sed -r 's/\s+//g'| cut -d":" -f2 | cut -d"M" -f1) + DownloadSpeed=$(cat $SpeedtestResultDir/speedtest-results | grep Download | sed -r 's/\s+//g'| cut -d":" -f2 | cut -d"M" -f1) # Get the Upload speed - UploadSpeed=$(cat $SpeedtestResulstTMP/speedtest.results | grep Upload | sed -r 's/\s+//g'| cut -d":" -f2 | cut -d"M" -f1) + UploadSpeed=$(cat $SpeedtestResultDir/speedtest-results | grep Upload | sed -r 's/\s+//g'| cut -d":" -f2 | cut -d"M" -f1) # Update the RRD graphs - rrdtool update $RRDGraphsLocation/speedtest_ping.rrd $DATE:$PingTime - rrdtool update $RRDGraphsLocation/speedtest_bandwidth.rrd $DATE:$DownloadSpeed:$UploadSpeed + rrdtool update $RRDGraphsDir/speedtest-latency.rrd $DATE:$Latency + rrdtool update $RRDGraphsDir/speedtest-bandwidth.rrd $DATE:$DownloadSpeed:$UploadSpeed ;; (graph) - /usr/bin/rrdtool graph $RRDGraphsLocation/upload.png \ - --start "-3day" \ - -c "BACK#000000" \ - -c "SHADEA#000000" \ - -c "SHADEB#000000" \ - -c "FONT#DDDDDD" \ - -c "CANVAS#202020" \ - -c "GRID#666666" \ - -c "MGRID#AAAAAA" \ - -c "FRAME#202020" \ - -c "ARROW#FFFFFF" \ - -u 1.1 -l 0 -v "Upload" -w 1100 -h 250 -t "Upload Speed - `/bin/date +%A", "%d" "%B" "%Y`" \ - DEF:upload=$RRDGraphsLocation/upload.rrd:upload:AVERAGE \ - AREA:upload\#FFFF00:"Upload speed (Mbit/s)" \ - GPRINT:upload:MIN:"Min\: %3.2lf " \ - GPRINT:upload:MAX:"Max\: %3.2lf" \ - GPRINT:upload:LAST:"Current\: %3.2lf\j" \ - COMMENT:"\\n" - - /usr/bin/rrdtool graph $RRDGraphsLocation/download.png \ - --start "-3day" \ - -c "BACK#000000" \ - -c "SHADEA#000000" \ - -c "SHADEB#000000" \ - -c "FONT#DDDDDD" \ - -c "CANVAS#202020" \ - -c "GRID#666666" \ - -c "MGRID#AAAAAA" \ - -c "FRAME#202020" \ - -c "ARROW#FFFFFF" \ - -u 1.1 -l 0 -v "Download" -w 1100 -h 250 -t "Download Speed - `/bin/date +%A", "%d" "%B" "%Y`" \ - DEF:download=$RRDGraphsLocation/download.rrd:download:AVERAGE \ - AREA:download\#00FF00:"Download speed (Mbit/s)" \ - GPRINT:download:MIN:"Min\: %3.2lf " \ - GPRINT:download:MAX:"Max\: %3.2lf" \ - GPRINT:download:LAST:"Current\: %3.2lf\j" \ - COMMENT:"\\n" - - /usr/bin/rrdtool graph $RRDGraphsLocation/echoreply.png \ - --start "-3day" \ - -c "BACK#000000" \ - -c "SHADEA#000000" \ - -c "SHADEB#000000" \ - -c "FONT#DDDDDD" \ - -c "CANVAS#202020" \ - -c "GRID#666666" \ - -c "MGRID#AAAAAA" \ - -c "FRAME#202020" \ - -c "ARROW#FFFFFF" \ - -u 1.1 -l 0 -v "Ping" -w 1100 -h 250 -t "Ping Response - `/bin/date +%A", "%d" "%B" "%Y`" \ - DEF:echoreply=$RRDGraphsLocation/echoreply.rrd:echoreply:AVERAGE \ - AREA:echoreply\#FF0000:"Ping Response (ms)" \ - GPRINT:echoreply:MIN:"Min\: %3.2lf " \ - GPRINT:echoreply:MAX:"Max\: %3.2lf" \ - GPRINT:echoreply:LAST:"Current\: %3.2lf\j" \ - COMMENT:"\\n" + # Create the Latency PNG + rrdtool graph $PNGImagesDir/speedtest-latency.png -J -a PNG -s -1day \ + --title="LATENCY Test" \ + --color CANVAS#000000 \ + --vertical-label "ms" \ + DEF:P=$RRDGraphsDir/speedtest-latency.rrd:LATENCY:AVERAGE \ + DEF:PMIN=$RRDGraphsDir/speedtest-latency.rrd:LATENCY:MIN \ + DEF:PMAX=$RRDGraphsDir/speedtest-latency.rrd:LATENCY:MAX \ + VDEF:Pavg=P,AVERAGE \ + LINE1:Pavg#cc3300:"Average" \ + LINE2:P#3d61ab:"LATENCY (ms)\n" \ + GPRINT:Pavg:"Average LATENCY %2.1lf ms\n" \ + -h 250 -w 525 -y1:2 \ + --color GRID#dddddd \ + --color MGRID#aaaaaa > /dev/null 2>&1 ;; (*)