docker-entrypoint.sh 1.1 KB

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