Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active Oct 18, 2021
Embed
What would you like to do?
Log speedtest results on Firewalla
#!/bin/bash
# v 0.18
# IFTTTKEY=adsfadsfkdsf
BASEDIR=$(dirname $0)
IFTTTKEY="$(cat $BASEDIR/IFTTT.data | grep IFTTTKEY| cut -f2 -d "=" )"
EVENT="FWspeedtest"
# set to false if you don't want to log speed to a google Spreadsheet
GoogleSpreadsheet=true
# Create a webhook with IFTT where:
# event = speedtest
# set a Speedtest server ID. Set to blank to let speedtest choose for you. example below
# SERVER=""
# SERVER="-s 34208"
# set to false if you don't want to log speed on your firewalla
LOCALLOG=true
# Give your WAN ports names. Leave blank if not used. No spaces in names.
WAN1="Sail"
WAN2="LTE"
eth1=eth0
eth2=eth1
log=/data/logspeed.log
installed=$(which speedtest)
installer="/home/pi/.firewalla/config/post_main.d/install_speedtest.sh"
if [ -z "$installed" ]; then
echo -e "\n\nspeedcheck not instaleld!\n"
if [ -f "$installer" ]; then
echo Running installer
read -p "Do you want to continue? (y|n) ? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Can't continue without speedtest."
exit
else
echo "Running installer..."
/home/pi/.firewalla/config/post_main.d/install_speedtest.sh
fi
else
echo -e "Download https://gist.github.com/mbierman/9ac6a35622ee5a0c631ed6f6ad74b722,\nrun it to install speedtest and try again."
exit
fi
else
echo speedtest installed!
fi
now=$(date +"%D %H:%M" | sed -e "s|/|-|g")
if [ -n "$WAN1" ]; then
echo "Testing $WAN1..."
WAN1P=$(echo \"$now\",\"$WAN1\",$(speedtest $SERVER --interface=$eth1 --format=csv | sed -e 's|,|.|'))
WAN1L=$(echo $WAN1P | cut -f5 -d"," | sed -e 's|"||g')
WAN1J=$(echo $WAN1P | cut -f6 -d"," | sed -e 's|"||g')
WAN1D=$(printf "%.1f" $(echo "scale=2;$(echo $WAN1P | cut -f8 -d","| sed -e 's|"||g')/125000" | bc))
WAN1U=$(printf "%.1f" $(echo "scale=2;$(echo $WAN1P | cut -f9 -d","| sed -e 's|"||g')/125000" | bc))
WAN1URL=$(echo $WAN1P | cut -f12 -d","| sed -e 's|"||g')
echo -e "$WAN1 Down: $WAN1D $WAN1 Up: $WAN1U\n\n"
else
echo "No WAN1"
fi
if [ -n "$WAN2" ]; then
echo "Testing $WAN2..."
WAN2P=$(echo \"$now\",\"$WAN2\",$(speedtest $SERVER --interface=$eth2 --format=csv | sed -e 's|,|.|'))
WAN2L=$(echo $WAN2P | cut -f5 -d"," | sed -e 's|"||g')
WAN2J=$(echo $WAN2P | cut -f6 -d"," | sed -e 's|"||g')
WAN2D=$(printf "%.1f" $(echo "scale=2;$(echo $WAN2P | cut -f8 -d","| sed -e 's|"||g')/125000" | bc))
WAN2U=$(printf "%.1f" $(echo "scale=2;$(echo $WAN2P | cut -f9 -d","| sed -e 's|"||g')/125000" | bc))
WAN2URL=$(echo $WAN2P | cut -f12 -d","| sed -e 's|"||g')
echo -e "$WAN2 Down: $WAN2D $WAN2 Up: $WAN2U\n\n"
else
echo "No WAN2"
fi
if [ -n "$IFTTTKEY" ] && [ "$GoogleSpreadsheet" = "true" ]; then
if [ -n "$WAN1" ]; then
curl -X POST -H "Content-Type: application/json" -d "{\"value1\": \"$now|||$WAN1|||$WAN1U|||$WAN1D|||$WAN1URL|||$WAN1L|||$WAN1J\"}" https://maker.ifttt.com/trigger/$EVENT/with/key/$IFTTTKEY
else
echo "Skipping WAN1 report..."
fi
if [ -n "$WAN2" ]; then
sleep 10s
curl -X POST -H "Content-Type: application/json" -d "{\"value1\": \"$now|||$WAN2|||$WAN2U|||$WAN2D|||$WAN2URL|||$WAN2L|||$WAN2J\"}" https://maker.ifttt.com/trigger/$EVENT/with/key/$IFTTTKEY
else
echo "Skipping WAN2 report"
fi
else
echo "IFTTT disabled"
fi
if [ "$LOCALLOG" = "true" ]; then
# Create a log file if it doesn't exist and add a header
HEADER='\"date\",\"ISP\",\"server name\",\"server id\",\"latency\",jitter\",\"packet loss\",\"download\",\"upload\",\"download bytes\",\"upload bytes\",\"share url\"'
if [ ! -f "$log" ]; then
echo -e "\n\nno logfile!\n"
echo $HEADER > $log
fi
echo $WAN1P | tee -a $log
echo $WAN2P | tee -a $log
echo $HEADER > $log.$$
sed -n '2,2000p' $log | tail -2000 >> $log.$$
mv $log.$$ $log
else
echo "Local log disabled"
fi
@mbierman

This comment has been minimized.

Copy link
Owner Author

@mbierman mbierman commented Aug 27, 2021

Update:
I simplified the way things are sent to IFTTT. Everything goes into value1 now. If you are a new user, don't worry about this. If you are upgrading, just adjust your IFTTT applet so that value1 is in the first field.

see also this script to install speedtest.

Save this script to /data/logspeed.sh.

You can run this with a cron job if you like. To do that, go to

cd /home/pi/.firewalla/config/
vi user_crontab

and add a line like this:
0 * * * * timeout -s SIGKILL 5m /data/logspeed.sh

that will run the test every hour.

You can choose to send the output to a Google Sheet via IFTTT, to a log file on Firewalla, or both. This supports single or dual WAN firewalla configurations. You can just remove the name of either WAN to disable logging on either WAN if you like. WANs with no name will be skipped.

Updates

  • The following will be exported to Google Sheets: Date | ISP | Upload | Download | URL | Latency | Jitter
  • Now the speedtest server is configurable
  • All Speedtest data is saved to the logfile on Firewalla: Date|ISP|Server name|Server id|Latency|Jitter|Packet loss|Downl
    oad|Upload|Download bytes|Upload bytes|Share url

IFTTT setup

Create an applet on IFTTT as follows:
IF web request (choose an event trigger name I use, “FWspeedtest”), THEN add row to google spreadsheet. Put the variable “Value1” in the first field.

You can test the web trigger by going to https://ifttt.com/maker_webhooks and click on Documentation and you can test the webhook. Use a few values for value1 like, “date|||ISP||latency|||jitter|||packet loss|||download|||upload|||share url" the , “|||" separates columns. They will appear in the order you list them left to right.

Take the last field after “key” on that page and create a file called, IFTTT.data In the /data directory or where you saved this script.

IFTTTKEY=[the value from the key above]

Sample Spreadsheet
Here is a snapshot of the spreadheet you can copy if you don't want to take the time to figure out how to make the graphs.

Sample Graphs
Dual Wan Speedtest
Sail speedtest
LTE speedtest
Upload Comparison
Download Comparison

@cstrat

This comment has been minimized.

Copy link

@cstrat cstrat commented Aug 29, 2021

Champion thank you for sharing!
I ended up just using a node script for mine as I am much more familiar with that than bash.

One issue I had was inbound NAT only seems to work on the external interface. Had to do some juggling with HTTP headers otherwise my nginx proxy wasn't working with the firewalla post.

I was wondering if the Firewalla resets cron on reboot?
I'd also never seen timeout in the cron, but that makes sense with speediest, stop it getting stuck for too long.

@mbierman

This comment has been minimized.

Copy link
Owner Author

@mbierman mbierman commented Aug 30, 2021

@cstrat if you edit the cron normally, changes may be lost at updates and maybe reboots? But if you follow the instructions above Firewalla will append the file above with crontab and it does persist.

@cstrat

This comment has been minimized.

Copy link

@cstrat cstrat commented Aug 30, 2021

Is there a step missing with the cron or do I need to reboot the Firewalla for it to pickup that user_crontab file?

@mbierman

This comment has been minimized.

Copy link
Owner Author

@mbierman mbierman commented Aug 30, 2021

@cstrat yeah, edit the file and reboot. Firewalla will then add your cron entries to the system and they will stay in place until you edit or remove the file and reboot again.

@cstrat

This comment has been minimized.

Copy link

@cstrat cstrat commented Aug 30, 2021

Awesome, thanks! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment