docker-entrypoint.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env bash
  2. DEBUG=0
  3. if [ "$1" == "debug" ]; then
  4. DEBUG=1
  5. set -x
  6. shift
  7. fi
  8. # InfluxDB variables
  9. influxdb_proto=${INFLUXDB_PROTO:-http}
  10. influxdb_host=${INFLUXDB_HOST:-influxdb}
  11. influxdb_port=${INFLUXDB_PORT:-8086}
  12. influxdb_db=${INFLUXDB_DB:-speedtest}
  13. influxdb_url="${influxdb_proto}://${influxdb_host}:${influxdb_port}"
  14. # run speedtest & store result
  15. json_result=$(speedtest -f json --accept-license --accept-gdpr)
  16. # Extract data from speedtest result
  17. result_id=$(echo "${json_result}" | jq -r '.result.id')
  18. ping_latency=$(echo "${json_result}" | jq -r '.ping.latency')
  19. download_bandwidth=$(echo "${json_result}" | jq -r '.download.bandwidth')
  20. upload_bandwidth=$(echo "${json_result}" | jq -r '.upload.bandwidth')
  21. packet_loss=$(echo "${json_result}" | jq -r '.packetLoss')
  22. # Write metric to InfluxDB
  23. INFLUXDB_APPEND="speedtest,result_id=${result_id} ping_latency=${ping_latency},download_bandwidth=${download_bandwidth},upload_bandwidth=${upload_bandwidth},packet_loss=${packet_loss}"
  24. [ ${DEBUG} -eq 1 ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}"
  25. # Ensure InfluxDB database exists
  26. curl \
  27. -d "q=CREATE DATABASE ${influxdb_db}" \
  28. "${influxdb_url}/query"
  29. curl \
  30. -d "${INFLUXDB_APPEND}" \
  31. "${influxdb_url}/write?db=${influxdb_db}"