feat: Handle connection failures as 0 values

This commit is contained in:
Robin-Manuel Thiel
2025-01-10 13:58:12 +01:00
parent b1153d34e4
commit 9b93de5350
3 changed files with 31 additions and 14 deletions
+2 -1
View File
@@ -15,6 +15,7 @@ Running a Speed Test...
Your download speed is 334 Mbps (29284399 Bytes/s).
Your upload speed is 42 Mbps (4012944 Bytes/s).
Your ping is 6.223 ms.
Speedtest took 23 seconds."
```
## Configuration
@@ -89,6 +90,6 @@ volumes:
influxdb:
```
To configure Grafana, we need to add InfluxDB as a data source and create a dashboard with the upload and download values. You can find a demo dashboard configuration in the [/demo](/demo) folder.
To configure Grafana, we need to **add InfluxDB as a data source first** and then create a dashboard with the upload and download values. You can find a demo dashboard configuration in the [/demo](/demo) folder.
> **Hint:** The speedtest outputs values as bytes per second. Make sure to divide all values by 125000 in your dashboard to get the Mbps values.
+4 -1
View File
@@ -25,10 +25,13 @@ services:
speedtest:
image: robinmanuelthiel/speedtest:latest
build:
context: ../
dockerfile: Dockerfile
restart: always
environment:
- LOOP=true
- LOOP_DELAY=1800
- LOOP_DELAY=300
- DB_SAVE=true
- DB_HOST=http://influxdb:8086
- DB_NAME=speedtest
+25 -12
View File
@@ -14,25 +14,40 @@ run_speedtest()
{
DATE=$(date +%s)
HOSTNAME=$(hostname)
START_TIME=$(date +%s)
# Start speed test
if [ -n "$SPEEDTEST_SERVER_ID" ]; then
echo "Running a Speed Test with Server ID $SPEEDTEST_SERVER_ID... "
JSON=$(speedtest --accept-license --accept-gdpr -f json -s $SPEEDTEST_SERVER_ID)
echo "Running a Speed Test with Server ID $SPEEDTEST_SERVER_ID... "
JSON=$(speedtest --accept-license --accept-gdpr -f json -s $SPEEDTEST_SERVER_ID) || JSON=""
elif [ -n "$SPEEDTEST_HOSTNAME" ]; then
echo "Running a Speed Test with Hostname $SPEEDTEST_HOSTNAME... "
JSON=$(speedtest --accept-license --accept-gdpr -f json -o $SPEEDTEST_HOSTNAME)
echo "Running a Speed Test with Hostname $SPEEDTEST_HOSTNAME... "
JSON=$(speedtest --accept-license --accept-gdpr -f json -o $SPEEDTEST_HOSTNAME) || JSON=""
else
echo "Running a Speed Test with default host... "
JSON=$(speedtest --accept-license --accept-gdpr -f json)
echo "Running a Speed Test with default host... "
JSON=$(speedtest --accept-license --accept-gdpr -f json) || JSON=""
fi
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
# If JSON is empty, then the speedtest command failed.
# In this case set the values to 0.
if [ -z "$JSON" ]; then
DOWNLOAD=0
UPLOAD=0
PING=0
else
# Fetch the values from the JSON output and convert them to the correct units (converts null to 0)
DOWNLOAD="$(echo $JSON | jq -r '.download.bandwidth // 0')"
UPLOAD="$(echo $JSON | jq -r '.upload.bandwidth // 0')"
PING="$(echo $JSON | jq -r '.ping.latency // 0')"
fi
DOWNLOAD="$(echo $JSON | jq -r '.download.bandwidth')"
UPLOAD="$(echo $JSON | jq -r '.upload.bandwidth')"
PING="$(echo $JSON | jq -r '.ping.latency')"
echo "Your download speed is $(($DOWNLOAD / 125000 )) Mbps ($DOWNLOAD Bytes/s)."
echo "Your upload speed is $(($UPLOAD / 125000 )) Mbps ($UPLOAD Bytes/s)."
echo "Your ping is $PING ms."
echo "Speedtest took $DURATION seconds."
# Save results in the database
if $DB_SAVE;
@@ -56,13 +71,11 @@ fi
if $LOOP;
then
echo "Running speedtest forever... ♾️"
echo
echo "Running speedtest in a loop until stopped..."
while :
do
run_speedtest
echo "Running next test in ${LOOP_DELAY}s..."
echo ""
sleep $LOOP_DELAY
done
else