Sorting by

×

Internet Speed test to InfluxDB & MQTT with Docker

Changes

V1.1 2021-04-12 Added mosquitto_pub and publish data to MQTT as well as influbdb

Summary

Use of the Ookla speedtest cli to test the internet connection regularly, and push the results to influx db & MQTT.

An influx db instance must already be running somewhere, and the database created.

This method was simplified from a bunch of similar projects – the ones I found had multiple container builds with influx/grafana included, or logged to a csv… and I’d then need to use telegraf to push to the db. I just wanted something that ran from a script and pushed to influxdb via http directly.

Be aware that this can use a LOT of bandwidth if you run it continually. Each use of the speedtest pulls a random set of data of a size which depends on your connection speed. Eg if you have say a basic 100Mbps up and 50Mbps down, it will likely pull about 170MB of data for each full test. If you do that every 5 minutes, for a whole day you will use 50GB of data, every day.

References and Similar Projects

dockerfile

FROM python:2-alpine

RUN mkdir -p /app
RUN apk add --no-cache curl
RUN apk add --no-cache mosquitto-clients
RUN wget -O /app/speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
RUN chmod +x /app/speedtest-cli
COPY test_internet.sh /app/test_internet.sh
CMD ["sh","/app/test_internet.sh"]

docker-compose.yml

version: '3.3'
services:
    speedtest:
        build: .
        volumes:
            - "/dockervolumes/speedtest/data:/app/data"
            - "/etc/localtime:/etc/localtime:ro"
            - "/etc/timezone:/etc/timezone:ro"
        container_name: speedtest
        hostname: speedtest
        restart: unless-stopped

test_internet.sh

#!/bin/sh

# V1.1 2021-04-12 Added mosquitto_pub and publish data to MQTT as well as influbdb

FILE="/data/test_internet.log"
MQTT_TOPIC="maintopic/SubTopic"
SERVER_ADDRESS="192.168.X.X"
TEST_INTERVAL=30

while true
do
        TIMESTAMP=$(date '+%s')

        /app/speedtest-cli > $FILE

        DOWNLOAD=$(cat $FILE | grep "Download:" | awk -F " " '{print $2}')
        UPLOAD=$(cat $FILE | grep "Upload:" | awk -F " " '{print $2}')
        echo "Download: $DOWNLOAD Upload: $UPLOAD    $TIMESTAMP"
        curl -i -XPOST http://$SERVER_ADDRESS:8086/write?db=speedtest --data-binary "download,host=local value=$DOWNLOAD"
        curl -i -XPOST http://$SERVER_ADDRESS:8086/write?db=speedtest --data-binary "upload,host=local value=$UPLOAD"
        mosquitto_pub -h $SERVER_ADDRESS -t $MQTT_TOPIC -m '{ "DownloadSpeed" : '$DOWNLOAD' , "UploadSpeed" : '$UPLOAD' }'
        sleep $TEST_INTERVAL

done

ToDo List

  • add secrets for hostname etc in ENV
  • improve grep mess
  • run as non root
  • replace curl with wget
  • stick in a git repo
  • Needs some error control to be sure it is still running
  • Needs timeout checks and maybe round robin on servers?
  • Should log latency also
  • Have it test ‘on the minute’ rather than just in a loop? (maybe use cron)
  • Have some randomness built in to test times
  • Also log current bandwidth being used for my network overall?
  • add MQTT as well as influxdb support

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment