|
@@ -18,17 +18,31 @@ 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')
|
|
|
-ping_latency=$(echo "${json_result}" | jq -r '.ping.latency')
|
|
|
-download_bandwidth=$(echo "${json_result}" | jq -r '.download.bandwidth')
|
|
|
-upload_bandwidth=$(echo "${json_result}" | jq -r '.upload.bandwidth')
|
|
|
-packet_loss=$(echo "${json_result}" | jq -r '.packetLoss')
|
|
|
+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="speedtest,result_id=${result_id} ping_latency=${ping_latency},download_bandwidth=${download_bandwidth},upload_bandwidth=${upload_bandwidth},packet_loss=${packet_loss}"
|
|
|
+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}"
|
|
|
|
|
|
-[ ${DEBUG} -eq 1 ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}"
|
|
|
# Ensure InfluxDB database exists
|
|
|
curl \
|
|
|
-d "q=CREATE DATABASE ${influxdb_db}" \
|