Skip to content

Instantly share code, notes, and snippets.

@dashbad
Created June 30, 2016 00:33
Show Gist options
  • Save dashbad/c13830d552223a16a0f3d2f9d746d471 to your computer and use it in GitHub Desktop.
Save dashbad/c13830d552223a16a0f3d2f9d746d471 to your computer and use it in GitHub Desktop.
Bash script to run speedtest-cli on freebsd (pfsense) and send results to influxdb
#!/usr/local/bin/bash
#This script requires speedtest-cli to function!!!!!!!!!!!!!!!!!!!!!!!
#Prepare to start the loop and warn the user
echo "Press [CTRL+C] to stop..."
#Store the speedtest results into a variable
results=$(/usr/local/bin/python2.7 /root/speedtest-cli --simple)
echo "$results"
#Lets try to find the lines we are looking for
while read -r line; do
#Check if we have the line we are looking for
if [[ $line == *"Ping"* ]]
then
ping=$line
fi
if [[ $line == *"Download"* ]]
then
download=$line
fi
if [[ $line == *"Upload"* ]]
then
upload=$line
fi
done <<< "$results"
echo "$ping"
echo "$download"
echo "$upload"
#Break apart the results based on a space
IFS=' ' read -ra arrping <<< "$ping"
ping=${arrping[1]}
IFS=' ' read -ra arrdownload <<< "$download"
download=${arrdownload[1]}
IFS=' ' read -ra arrupload <<< "$upload"
upload=${arrupload[1]}
#Convet to mbps
download=`echo - | /usr/bin/awk "{print $download * 1048576}"`
upload=`echo - | /usr/bin/awk "{print $upload * 1048576}"`
#download=$((download * 1048576))
#upload=$((upload * 1048576))
echo "$ping"
echo "$download"
echo "$upload"
#Write to the database
/usr/local/bin/curl -i -XPOST 'http://192.168.1.5:8086/write?db=main' --data-binary "speedtest,metric=ping value=$ping"
/usr/local/bin/curl -i -XPOST 'http://192.168.1.5:8086/write?db=main' --data-binary "speedtest,metric=download value=$download"
/usr/local/bin/curl -i -XPOST 'http://192.168.1.5:8086/write?db=main' --data-binary "speedtest,metric=upload value=$upload"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment