Bash - Speedtest.net Download/Upload/Ping monitoring
Jump to navigation
Jump to search
Purpose
This script uses the Python module speedtest-cli for testing internet bandwidth (Download/Upload/Ping) via speedtest.net.
It is not my script and I do not claim ownership of it (Source: Sander Filius: https://www.safiweb.nl/?p=1133).
This script will create 4 Virtual Sensors to monitor internet bandwidth and uptime:
- Download (Graph)
- Upload (Graph)
- Ping (Graph)
- Broadband (Switch to manually trigger Bash script)
Dependencies - hardware / software / operating system
- Python 2.4-3.5 at time of writing
- speedtest-cli Python module (https://github.com/sivel/speedtest-cli)
Steps
- Open a SSH session and install speedtest-cli using
sudo apt-get install speedtest-cli
- Within Domoticz open the Setup > Hardware screen
- Create 3 Virtual Sensors with the "Custom" Type named:
- Download
- Upload
- Ping
- Create 1 Virtual Sensors with the "Switch" Type named:
- Broadband
- Within Domoticz open the Setup > Devices screen and note the IDX of each new Virtual Sensor added, you'll need this in the Bash script
- Create a new file within /domoticz/scripts/ called speedtest.sh
- Change the permissions on speedtest.sh to allow it to run from crontab
- Edit the speedtest.sh file
- Copy and paste the contents of the Bash script section below
- Change the following fields:
- host (to match your IP address or localhost)
- idxdl (the IDX of the Download device)
- idxul (the IDX of the Upload device)
- idxpng (the IDX of the Ping device)
- idxbb (the IDX of the Broadband device)
- Save changes and run the Bash script, if successful after a moment it will display your Download, Upload and Ping
- Within Domoticz open the Setup > Log to confirm that Domoticz was informed of this data
- Within Domoticz open the Utility tab and check that data is being recorded
- Within Domoticz open the Switches tab
- Click Edit within the Broadband device
- Set the On action to "script:///home/pi/domoticz/scripts/speedtest.sh" (replace pi with your user and/or correct absolute path)
- For automatic reporting you could add a crontab task to run the Bash script speedtest.sh at given intervals (e.g. every 24 hours, etc.)
Bash script
#!/bin/bash
#setup
host='localhost'
#idx for download, upload and ping
idxdl=1
idxul=2
idxpng=3
idxbb=4
# speedtest server number
# serverst=yyyy
# no need to edit
# speedtest-cli --simple --server $serverst > outst.txt
speedtest-cli --simple > speedtest.txt
download=$(cat speedtest.txt | sed -ne 's/^Download: \([0-9]*\.[0-9]*\).*/\1/p')
upload=$(cat speedtest.txt | sed -ne 's/^Upload: \([0-9]*\.[0-9]*\).*/\1/p')
png=$(cat speedtest.txt | sed -ne 's/^Ping: \([0-9]*\.[0-9]*\).*/\1/p')
# output if you run it manually
echo "Download = $download Mbps"
echo "Upload = $upload Mbps"
echo "Ping = $png ms"
# Updating download, upload and ping ..
wget -q --delete-after "http://$host/json.htm?type=command¶m=udevice&idx=$idxdl&svalue=$download" >/dev/null 2>&1
wget -q --delete-after "http://$host/json.htm?type=command¶m=udevice&idx=$idxul&svalue=$upload" >/dev/null 2>&1
wget -q --delete-after "http://$host/json.htm?type=command¶m=udevice&idx=$idxpng&svalue=$png" >/dev/null 2>&1
# Reset Broadband switch
wget -q --delete-after "http://$host/json.htm?type=command¶m=udevice&idx=$idxbb&svalue=0" >/dev/null 2>&1
# Domoticz logging
wget -q --delete-after "http://$host/json.htm?type=command¶m=addlogmessage&message=speedtest.net-logging" >/dev/null 2>&1