Skip to content

Instantly share code, notes, and snippets.

@foosel

foosel/Dockerfile

Created May 12, 2019
Embed
What would you like to do?
Simple Dockerfile & composition to run a speedtest2mqtt setup using speedtest-go from https://github.com/showwin/speedtest-go

speedtest-go can be found here. Place it in the same directory as the above files.

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-go
  • 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-go:
build: .
container_name: speedtest-go
environment:
- SPEEDTEST_INTERVAL=1200
FROM alpine:3.7
ADD entrypoint.sh /opt/speedtest2mqtt.sh
COPY speedtest-go /usr/bin/speedtest-go
RUN apk update && \
apk add ca-certificates mosquitto-clients && \
rm -rf /var/cache/apk/* && \
chmod 777 /opt/speedtest2mqtt.sh
ENTRYPOINT ["/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="/usr/bin/speedtest-go $SPEEDTEST_OPTIONS"
MQTT_CMD="/usr/bin/"
while true; do
echo "Running $SPEEDTEST_CMD..."
output=$($SPEEDTEST_CMD)
echo "--- Output: -------------------------------------------------"
echo $output
echo "-------------------------------------------------------------"
download=$(echo $output | sed -n -r 's/.*Download: ([0-9]+\.[0-9]+).*/\1/p')
upload=$(echo $output | sed -n -r 's/.*Upload: ([0-9]+\.[0-9]+).*/\1/p')
ping=$(echo $output | sed -n -r 's/.*Latency: ([0-9]+\.[0-9]+)ms.*/\1/p')
server_id=$(echo $output | sed -n -r 's/.*Target Server: \[([0-9]+)\].*/\1/p')
payload="{\"server_id\": \"$server_id\", \"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 "Server ID: $server_id"
echo "-------------------------------------------------------------"
echo "Payload: $payload"
echo "-------------------------------------------------------------"
echo
echo "Sending data to MQTT @ $MQTT_HOST/$MQTT_TOPIC..."
echo $payload | /usr/bin/mosquitto_pub -h ${MQTT_HOST} -i ${MQTT_ID} -l -t ${MQTT_TOPIC} ${MQTT_OPTIONS} -u ${MQTT_USER} -P ${MQTT_PASS}
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
You can’t perform that action at this time.