zorruno wikki: An internet Speed test to InfluxDB with Docker

An internet Speed test to InfluxDB with Docker


Versions

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

Summary

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

Notes

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

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.

ToDo

  • 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

Refs and similar projects

Ookla speedtest cli: https://www.speedtest.net/apps/cli

This was the most useful and code was borrowed from here:
internet-speedtest-docker: https://www.github.com/pedrocesar-ti/internet-speedtest-docker
A forked/slimmed version of the above:
internet-speedtest-docker https://hub.docker.com/r/kjake/internet-speedtest-docker
docker-speedtest-analyser: https://www.github.com/roest01/docker-speedtest-analyser
speedy: https://github.com/stefanwalther/speedy

dockerfile

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

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

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


--
CategoryDocker
CategoryHomeAutomation