zorruno wikki: An internet Speed test to InfluxDB with Docker

An internet Speed test to InfluxDB with Docker


Notes

Uses the Ookla speedtest cli to test the internet connection regularly, and push the results to influx db. 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?

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

FROM python:2-alpine

RUN mkdir -p /app
RUN apk add --no-cache curl
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

WARNING: My wiki editor stuff up spacing currently and may transform spaces into tabs... so this YML file likely won't work if you don't tidy it up sorry (I plan to fix the editor...)
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
FILE="/data/test_internet.log"
TEST_INTERVAL=30

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

	/data/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://192.168.X.X:8086/write?db=speedtest' --data-binary "download,host=local value=$DOWNLOAD"
	curl -i -XPOST 'http://192.168.X.X:8086/write?db=speedtest' --data-binary "upload,host=local value=$UPLOAD"
	sleep $TEST_INTERVAL

done


--
CategoryDocker
CategoryHomeAutomation