Skip to content

Instantly share code, notes, and snippets.

@foosel

foosel/Dockerfile Secret

Created Mar 30, 2021
Embed
What would you like to do?
Simple Dockerfile & composition to run a speedtest2mqtt setup using Ookla's CLI speedtest from https://www.speedtest.net/apps/cli

Build and fire up the container stack with docker-compose up --build -d.

Configuration can be done through environment variables in docker-compose.yml:

  • MQTT_HOST - mqtt server, defaults to localhost
  • MQTT_ID - id to use for connecting to mqtt server, defaults to speedtest
  • MQTT_TOPIC - topic to push data to, defaults to speedtest/status
  • MQTT_OPTIONS - additional mqtt options, defaults to -V mqttv311
  • MQTT_USER - user for mqtt server
  • MQTT_PASS - password for mqtt server
  • SPEEDTEST_OPTIONS - additional options for speedtest
  • SPEEDTEST_INTERVAL - interval in which to run speedtest, in seconds, defaults to 60

Configuration of sensors in Home Assistant:

sensor:
  - platform: mqtt
    state_topic: speedtest/status
    unit_of_measurement: ms
    icon: mdi:speedometer
    name: Speedtest Ping
    value_template: "{{ value_json.ping }}"

  - platform: mqtt
    state_topic: speedtest/status
    unit_of_measurement: Mbit/s
    icon: mdi:speedometer
    name: Speedtest Download
    value_template: "{{ value_json.download }}"    

  - platform: mqtt
    state_topic: speedtest/status
    unit_of_measurement: Mbit/s
    icon: mdi:speedometer
    name: Speedtest Upload
    value_template: "{{ value_json.upload }}"
version: '2.1'
services:
speedtest-rust:
build: .
container_name: speedtest-ookla
environment:
- SPEEDTEST_INTERVAL=1200
FROM ubuntu:latest
RUN apt-get update \
&& apt-get install -y gnupg1 apt-transport-https dirmngr mosquitto-clients jq
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 379CE192D401AB61 \
&& echo "deb https://ookla.bintray.com/debian generic main" | tee /etc/apt/sources.list.d/speedtest.list \
&& apt-get update \
&& apt-get install -y speedtest
ADD entrypoint.sh /opt/speedtest2mqtt.sh
RUN chmod 777 /opt/speedtest2mqtt.sh
CMD [ "/opt/speedtest2mqtt.sh" ]
#!/bin/sh
MQTT_HOST=${MQTT_HOST:-localhost}
MQTT_ID=${MQTT_ID:-speedtest}
MQTT_TOPIC=${MQTT_TOPIC:-speedtest/status}
MQTT_OPTIONS=${MQTT_OPTIONS:-"-V mqttv311"}
MQTT_USER=${MQTT_USER:-""}
MQTT_PASS=${MQTT_PASS:-""}
SPEEDTEST_OPTIONS=${SPEEDTEST_OPTIONS:-""}
SPEEDTEST_INTERVAL=${SPEEDTEST_INTERVAL:-60}
SPEEDTEST_CMD="speedtest --accept-license --accept-gdpr --format=json $SPEEDTEST_OPTIONS"
MQTT_CMD="/usr/bin/mosquitto_pub"
while true; do
echo "Running $SPEEDTEST_CMD..."
output=$($SPEEDTEST_CMD)
exitcode=$?
echo "--- Output: -------------------------------------------------"
echo $output
echo "-------------------------------------------------------------"
if [ $exitcode -eq 0 ]; then
download=$(echo $output | jq .download.bandwidth)
upload=$(echo $output | jq .upload.bandwidth)
ping=$(echo $output | jq .ping.latency)
download=$(printf %.2f "$((download*8))e-6")
upload=$(printf %.2f "$((upload*8))e-6")
payload="{\"upload\": $upload, \"download\": $download, \"ping\": $ping}"
echo
echo "--- Parsed result -------------------------------------------"
echo "Ping: $ping ms"
echo "Download: $download MBit/s"
echo "Upload: $upload MBit/s"
echo "-------------------------------------------------------------"
echo "Payload: $payload"
echo "-------------------------------------------------------------"
echo
echo "Sending data to MQTT @ $MQTT_HOST/$MQTT_TOPIC..."
echo $payload | $MQTT_CMD -h ${MQTT_HOST} -i ${MQTT_ID} -l -t ${MQTT_TOPIC} ${MQTT_OPTIONS} -u ${MQTT_USER} -P ${MQTT_PASS}
fi
echo
echo "Sleeping $SPEEDTEST_INTERVAL seconds..."
echo
sleep $SPEEDTEST_INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment