Преглед изворни кода

Remove any 'null' value

STATE:
Sometimes, the 'packet-loss' value is not provided by speedtest,
this breaks the append into the influxdb database.

FIX:
Everytime a 'null' value is present, we just remove it from the
append value.

Signed-off-by: Jeremy MAURO <jeremy.mauro@gmail.com>
Jeremy MAURO пре 4 година
родитељ
комит
c52d89e726
1 измењених фајлова са 20 додато и 6 уклоњено
  1. 20 6
      scripts/docker-entrypoint.sh

+ 20 - 6
scripts/docker-entrypoint.sh

@@ -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}" \