Skip to content

Instantly share code, notes, and snippets.

@foosel

foosel/Dockerfile Secret

Created Mar 31, 2021
Embed
What would you like to do?
Simple Dockerfile & composition to run a speedtest2mqtt setup using librespeed-cli from https://github.com/librespeed/speedtest-cli

Download the latest release of librespeed-cli for your target architecture and unpack the librespeed-cli binary into the build directory. 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 libretest-cli, see here for possible options
  • 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:
librespeed:
build: librespeed
container_name: librespeed
hostname: librespeed.nas.lan
environment:
- SPEEDTEST_INTERVAL=1200
FROM alpine:3.7
ADD entrypoint.sh /opt/speedtest2mqtt.sh
COPY librespeed-cli /usr/bin/librespeed-cli
RUN apk update && \
apk add ca-certificates mosquitto-clients jq && \
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="librespeed-cli --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)
upload=$(echo $output | jq .upload)
ping=$(echo $output | jq .ping)
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