Speedtest_logger/Linux/speedtest.sh

307 lines
8.5 KiB
Bash
Executable File

#!/bin/bash
# Settings
# # --------------WAN Number--------------
# Set WAN Number. This is usefull if you are testing multiple connections and having
# them log to the same location
WAN="SET WAN NUMBER"
# # --------------Log File Settings--------------
# Do you want the script to log to a CSV file? Set log variable to 1 for yes, 0 for no
# If you set 0 for no, you dont have to fill in LOG_FILE setting
log=1
# Set the path to the log file
LOG_FILE="./speedtest.csv"
# # --------------Speedtest-cli Settings--------------
# Do you want to use Speedtest-cli? Set log variable to 1 for yes, 0 for no
SPEEDTESTCLI="SET TO 0 or 1"
# # --------------Iperf3 Settings--------------
# Do you want to use Iperf3? Set log variable to 1 for yes, 0 for no
# If you set 0 for no, you dont have to fill in the IPERF3_SERVER_IP setting
IPERF3="SET TO 0 or 1"
# Set the iPerf3 server IP or Hostname that you want to use
IPERF3_SERVER_IP="IPERF3.SERVER.IPor.HOSTNAME"
# # --------------InflixDB Settings--------------
# Do you want the script to output to a inflix Database? Set INFLUX variable to 1 for yes, 0 for no
# If you set 0 for no, you dont have to fill in the INFLUXDB_NAME or INFLUXDB_HOST settings
INFLUX="SET TO 0 or 1"
# Set Inflix DB Name
INFLUXDB_NAME="SET_DB_NAME"
#Set Influx DB Hostname or IP
INFLUXDB_HOST="SET_HOSTNAME_OR_IP"
# # --------------DISCORD Settings--------------
# Do you want the script to output to a DISCORD webhook? Set DISCORD variable to 1 for yes, 0 for no
# If you set 0 for no, you dont have to fill in the WEBHOOK_URL setting
DISCORD="SET TO 0 or 1"
# Set your DISCORD webhook URL
WEBHOOK_URL="DISCORD_WEBHOOK_URL"
# # --------------SpeedTest Script-----------------
# # --------------Get Current IP Info--------------
# Get IP info
INFO=$(curl -s https://ipv4.am.i.mullvad.net/json | python -m json.tool)
# Get Reported IP
IP=$(echo "$INFO" | grep -oE '"ip":\s*"[^"]+"' | grep -oE '[0-9.]+')
# Get Reported ISP
ORG=$(echo "$INFO" | grep -oP '(?<="organization":")[^"]+')
# # --------------IPERF3 Test Script--------------
# Do you want to use Iperf3?
if [ "$IPERF3" -eq 1 ]; then
echo "IPERF3 Variable is set to 1. Running IPERF3 test"
# Run iperf3 upload and parse the output
IPERF_UP=$(iperf3 -R -c "$IPERF3_SERVER_IP" -J)
# Check if the iperf3 output is empty or contains errors
if [ -z "$IPERF_UP" ] || [[ "$IPERF_UP" == *"error"* ]]; then
echo "Iperf3 test failed: $IPERF_UP"
exit 1
fi
# Run iperf3 and parse the output
IPERF_DOWN=$(iperf3 -c "$IPERF3_SERVER_IP" -J)
# Check if the iperf3 download output is empty or contains errors
if [ -z "$IPERF_DOWN" ] || [[ "$IPERF_DOWN" == *"error"* ]]; then
echo "Iperf3 test failed: $IPERF_DOWN"
exit 1
fi
# Extract download and upload speeds from iperf3 output
DOWNLOAD=$(echo "$IPERF_UP" | jq -r '.end.sum_received.bits_per_second')
UPLOAD=$(echo "$IPERF_DOWN" | jq -r '.end.sum_sent.bits_per_second')
# Convert the download and upload speeds to Mbps
DOWNLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $DOWNLOAD / (1024*1024) }")
UPLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $UPLOAD / (1024*1024) }")
# Check if the required fields are missing or invalid
if [ -z "$DOWNLOAD_Mbps" ] || [ -z "$UPLOAD_Mbps" ]; then
echo "Missing or invalid iperf3 fields: DOWNLOAD=$DOWNLOAD_Mbps, UPLOAD=$UPLOAD_Mbps"
exit 1
fi
if [ "$log" -eq 1 ]; then
echo "Log Variable is set to 1. Writing to log."
# Append the result to the log file
echo "$(date +"%Y-%m-%d %H:%M:%S"),$WAN,iperf3,$ORG,$IP,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE"
# Check if the variable is set to 0
elif [ "$log" -eq 0 ]; then
echo "Log Variable is set to 0. Skipping log to file"
# Variable is not set to 1 or 0
else
echo "DISCORD Variable is not set to a valid value."
fi
# Do you want the script to output to a DISCORD webhook?
if [ "$DISCORD" -eq 1 ]; then
echo "DISCORD Variable is set to 1. Sending to DISCORD"
# Prepare the payload for the DISCORD webhook
curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
{
"content": null,
"embeds": [
{
"title": "Iperf3 Result - WAN $WAN",
"color": 5814783,
"fields": [
{
"name": "Date and Time of Test:",
"value": "$(date +'%Y-%m-%d %H:%M:%S')"
},
{
"name": "Iperf3 Server Used:",
"value": "$IPERF3_SERVER_IP"
},
{
"name": "ISP Tested:",
"value": "$ORG"
},
{
"name": "IP Tested:",
"value": "$IP"
},
{
"name": "Download Test Result:",
"value": "$DOWNLOAD_Mbps mbps"
},
{
"name": "Upload Test Result:",
"value": "$UPLOAD_Mbps mbps"
}
]
}
],
"attachments": []
}
EOF
# Check if the variable is set to 0
elif [ "$DISCORD" -eq 0 ]; then
echo "DISCORD Variable is set to 0. Skipping send to DISCORD"
# Variable is not set to 1 or 0
else
echo "DISCORD Variable is not set to a valid value."
fi
# Do you want the script to output to a INFLUX DB?
if [ "$INFLUX" -eq 1 ]; then
echo "INFLUX Variable is set to 1."
# Send Data to DB
influx --database='INFLUXDB_NAME' --host='INFLUXDB_HOST' --execute="INSERT 'INFLUXDB_NAME',wan=$WAN,test_type=,iperf3,isp=$ORG,ip=$IP download=$DOWNLOAD_Mbps,upload=$UPLOAD_Mbps"
# Check if INFLUX is set to 0
elif [ "$INFLUX" -eq 0 ]; then
echo "Skipping send to INFLUX"
# Variable for INFLUX is not set to 1 or 0
else
echo "Variable for INFLUX is not set to a valid value."
fi
fi
# # --------------SPEEDTEST-CLI Test Script--------------
# Do you want to use Speedtest-cli?
if [ "$SPEEDTESTCLI" -eq 1 ]; then
echo "SPEEDTESTCLI Variable is set to 1. Running SPEEDTEST-CLI test"
# Run the speedtest and parse the output
SPEEDTEST=$(speedtest-cli --csv)
# Check if the output is empty or contains errors
if [ -z "$SPEEDTEST" ] || [[ "$SPEEDTEST" == *"ERROR"* ]]; then
echo "Speedtest failed: $SPEEDTEST"
exit 1
fi
PING=$(echo "$SPEEDTEST" | awk -F',' '{print $6}')
DOWNLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $7}')
UPLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $8}')
# Check if the required fields are missing or invalid
if [ -z "$PING" ] || [ -z "$DOWNLOAD" ] || [ -z "$UPLOAD" ]; then
echo "Missing or invalid speedtest fields: PING=$PING, DOWNLOAD=$DOWNLOAD, UPLOAD=$UPLOAD"
exit 1
fi
# Convert the download and upload speeds to Mbps
DOWNLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $DOWNLOAD / (1024*1024) }")
UPLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $UPLOAD / (1024*1024) }")
if [ "$log" -eq 1 ]; then
echo "Variable is set to 1. Running command A."
# Append the result to the log file
echo "$(date +"%Y-%m-%d %H:%M:%S"),$WAN,Speedtest.net,$ORG,$IP,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE"
# Check if the variable is set to 0
elif [ "$log" -eq 0 ]; then
echo "Skipping log to file"
# Variable is not set to 1 or 0
else
echo "Log to File Variable is not set to a valid value."
fi
if [ "$DISCORD" -eq 1 ]; then
echo "DISCORD Variable is set to 1."
# Prepare the payload for the Discord webhook
curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
{
"content": null,
"embeds": [
{
"title": "Speedtest.net Result - WAN $WAN",
"color": 5814783,
"fields": [
{
"name": "Date and Time of Test:",
"value": "$(date +'%Y-%m-%d %H:%M:%S')"
},
{
"name": "ISP Tested:",
"value": "$ORG"
},
{
"name": "IP Tested:",
"value": "$IP"
},
{
"name": "Ping:",
"value": "$PING ms"
},
{
"name": "Download Test Result:",
"value": "$DOWNLOAD_Mbps mbps"
},
{
"name": "Upload Test Result:",
"value": "$UPLOAD_Mbps mbps"
}
]
}
],
"attachments": []
}
EOF
# Check if the variable is set to 0
elif [ "$DISCORD" -eq 0 ]; then
echo "Skipping send to Discord"
# Variable is not set to 1 or 0
else
echo "DIRCORD Variable is not set to a valid value."
fi
# Do you want the script to output to a INFLUX DB?
if [ "$INFLUX" -eq 1 ]; then
echo "INFLUX Variable is set to 1."
# Send Data to DB
influx --database='INFLUXDB_NAME' --host='INFLUXDB_HOST' --execute="INSERT 'INFLUXDB_NAME',wan=$WAN,test_type=,speedtest.net,isp=$ORG,ip=$IP download=$DOWNLOAD_Mbps,upload=$UPLOAD_Mbps"
# Check if INFLUX is set to 0
elif [ "$INFLUX" -eq 0 ]; then
echo "Skipping send to INFLUX"
# Variable for INFLUX is not set to 1 or 0
else
echo "Variable for INFLUX is not set to a valid value."
fi
fi