robinmanuelthiel / speedtest

Check internet bandwidth from a Docker container and save the results to an InfluxDB
MIT License
169 stars 48 forks source link

Define server to speedtest #14

Closed OmarGutierrezMolina closed 3 months ago

OmarGutierrezMolina commented 9 months ago

Is there any way to define the server where I want the speedtest to occur, for example, tell that use the Rogers (Canadian ISP) server to test the speed.

madnuttah commented 6 months ago

Hey, @OmarGutierrezMolina. @robinmanuelthiel. I hope you are fine and that it's ok for you if I leave this here.

It might not be perfect but did what I wanted:

#!/bin/bash
# These values can be overwritten with env variables
LOOP="${LOOP:-false}"
LOOP_DELAY="${LOOP_DELAY:-60}"
DB_SAVE="${DB_SAVE:-false}"
DB_HOST="${DB_HOST:-http://localhost:8086}"
DB_NAME="${DB_NAME:-speedtest}"
DB_USERNAME="${DB_USERNAME:-admin}"
DB_PASSWORD="${DB_PASSWORD:-password}"
TESTSERVER_ID="${TESTSERVER_ID:-testserverid}"

run_speedtest()
{
    DATE=$(date +%s)
    HOSTNAME=$(hostname)

    # Start speed test
    echo "Running a Speed Test..."
    date +%c
    JSON=$(speedtest --accept-license --accept-gdpr -s $TESTSERVER_ID -f json)
    ISP="$(echo $JSON | jq -r '.isp')"
    SERVERID="$(echo $JSON | jq -r '.server.id')"
    SERVER="$(echo $JSON | jq -r '.server.name')"
    SERVERLOCATION="$(echo $JSON | jq -r '.server.location')"
    DOWNLOAD="$(echo $JSON | jq -r '.download.bandwidth')"
    UPLOAD="$(echo $JSON | jq -r '.upload.bandwidth')"
    PING="$(echo $JSON | jq -r '.ping.latency')"
    echo "Your ISP is $ISP."
    echo "Your test server is $SERVER (ID $SERVERID) in $SERVERLOCATION."
    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."

    # Save results in the database
    if $DB_SAVE; 
    then
        echo "Saving values to database..."      
        curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
            --data-binary "download,host=$HOSTNAME value=$DOWNLOAD $DATE"
        curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
            --data-binary "upload,host=$HOSTNAME value=$UPLOAD $DATE"
        curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
            --data-binary "ping,host=$HOSTNAME value=$PING $DATE"  
        echo "Values saved."
    fi
}

if $LOOP;
then
    while :
    do
        run_speedtest
        echo "Running next test in ${LOOP_DELAY}s..."
        echo ""
        sleep $LOOP_DELAY
    done
else
    run_speedtest   
fi

You need to get the server ID from the website, you can view the sourcecode there you'll find it somewhere.

If there's demand, I could provide a forked repo of your work.

Cheers and all the best.

robinmanuelthiel commented 6 months ago

Feel free to add a PR, so we can make the server configurable! :)

robinmanuelthiel commented 3 months ago

Closed thanks to @bazzani and @madnuttah, you can now (in version 1.0.0) run:

docker run -e SPEEDTEST_SERVER_ID=32298 robinmanuelthiel/speedtest:latest

#or

docker run -e SPEEDTEST_HOSTNAME=speedtest1.synlinq.de robinmanuelthiel/speedtest:latest
bazzani commented 3 months ago

Nice to be able to contribute @robinmanuelthiel... there are a few more enhancements I would like to propose, I have opened a new PR just now.

georgelza commented 3 months ago

awesome, thanks Barry and thanks Robin for merging this in. G