1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- #!/usr/bin/env bash
- DEBUG=0
- if [ "$1" == "debug" ]; then
- DEBUG=1
- set -x
- shift
- fi
- # InfluxDB variables
- influxdb_proto=${INFLUXDB_PROTO:-http}
- influxdb_host=${INFLUXDB_HOST:-influxdb}
- influxdb_port=${INFLUXDB_PORT:-8086}
- influxdb_db=${INFLUXDB_DB:-speedtest}
- influxdb_url="${influxdb_proto}://${influxdb_host}:${influxdb_port}"
- # run speedtest & store result
- json_result=$(speedtest -f json --accept-license --accept-gdpr)
- declare -A results
- # Extract data from speedtest result
- result_id=$(echo "${json_result}" | jq -r '.result.id')
- results['ping_latency']=$(echo "${json_result}" | jq -r '.ping.latency')
- results['download_bandwidth']=$(echo "${json_result}" | jq -r '.download.bandwidth')
- results['upload_bandwidth']=$(echo "${json_result}" | jq -r '.upload.bandwidth')
- results['packet_loss']=$(echo "${json_result}" | jq -r '.packetLoss')
- # Write metric to InfluxDB
- INFLUXDB_APPEND=''
- for key in "${!results[@]}"
- do
- value="${results[${key}]}"
- if [ "${value}" != 'null' ]; then
- if [ -z ${INFLUXDB_APPEND} ]; then
- INFLUXDB_APPEND="${key}=${value}"
- else
- INFLUXDB_APPEND="${INFLUXDB_APPEND},${key}=${value}"
- fi
- fi
- done
- INFLUXDB_APPEND="speedtest,result_id=${result_id} ${INFLUXDB_APPEND}"
- [ ${DEBUG} -eq 1 ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}"
- # Ensure InfluxDB database exists
- curl \
- -d "q=CREATE DATABASE ${influxdb_db}" \
- "${influxdb_url}/query"
- curl \
- -d "${INFLUXDB_APPEND}" \
- "${influxdb_url}/write?db=${influxdb_db}"
|