docker-entrypoint.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. declare -A results
  17. # Extract data from speedtest result
  18. result_id=$(echo "${json_result}" | jq -r '.result.id')
  19. results['ping_latency']=$(echo "${json_result}" | jq -r '.ping.latency')
  20. results['download_bandwidth']=$(echo "${json_result}" | jq -r '.download.bandwidth')
  21. results['upload_bandwidth']=$(echo "${json_result}" | jq -r '.upload.bandwidth')
  22. results['packet_loss']=$(echo "${json_result}" | jq -r '.packetLoss')
  23. # Write metric to InfluxDB
  24. INFLUXDB_APPEND=''
  25. for key in "${!results[@]}"
  26. do
  27. value="${results[${key}]}"
  28. if [ "${value}" != 'null' ]; then
  29. if [ -z ${INFLUXDB_APPEND} ]; then
  30. INFLUXDB_APPEND="${key}=${value}"
  31. else
  32. INFLUXDB_APPEND="${INFLUXDB_APPEND},${key}=${value}"
  33. fi
  34. fi
  35. done
  36. INFLUXDB_APPEND="speedtest,result_id=${result_id} ${INFLUXDB_APPEND}"
  37. [ ${DEBUG} -eq 1 ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}"
  38. # Ensure InfluxDB database exists
  39. curl \
  40. -d "q=CREATE DATABASE ${influxdb_db}" \
  41. "${influxdb_url}/query"
  42. curl \
  43. -d "${INFLUXDB_APPEND}" \
  44. "${influxdb_url}/write?db=${influxdb_db}"