From e0e6bd6a993f23a96985192095181bf411875d9c Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Tue, 22 Apr 2025 08:34:34 -0500 Subject: [PATCH] Refactor run_iperf function to parse JSON output and improve error logging --- runtest.sh | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/runtest.sh b/runtest.sh index d30fa21..7e3b0d3 100755 --- a/runtest.sh +++ b/runtest.sh @@ -71,29 +71,36 @@ run_iperf() { local tmp_err tmp_err=$(mktemp) - local result - result=$(iperf3 "${args[@]}" 2>"$tmp_err" | jq -r ' - if .error then - "iperf3-error" - elif has("end") | not then - "no-end" - elif .end | has("sum_received") then - .end.sum_received.bits_per_second - elif .end | has("sum") then - .end.sum.bits_per_second - else - "unexpected-format" - end' || echo "execution-failed") + local tmp_json + tmp_json=$(mktemp) - if [[ "$result" == "iperf3-error" || "$result" == "no-end" || "$result" == "unexpected-format" || "$result" == "execution-failed" ]]; then - echo "$(date -Iseconds),iperf $mode $direction to $target failed with '$result'" >>"$FAILURE_LOG" + local result + result=$(iperf3 "${args[@]}" -J >"$tmp_json" 2>"$tmp_err") + + parsed=$(jq -r ' + if .error then + "iperf3-error" + elif has("end") | not then + "no-end" + elif .end | has("sum_received") then + .end.sum_received.bits_per_second + elif .end | has("sum") then + .end.sum.bits_per_second + else + "unexpected-format" + end' "$tmp_json" || echo "execution-failed") + + if [[ "$parsed" == "iperf3-error" || "$parsed" == "no-end" || "$parsed" == "unexpected-format" || "$parsed" == "execution-failed" ]]; then + timestamp=$(date -Iseconds) + echo "$timestamp,iperf $mode $direction to $target failed with '$parsed'" >>"$FAILURE_LOG" echo "[stderr] $(cat "$tmp_err")" >>"$FAILURE_LOG" + echo "[json] $(cat "$tmp_json")" >>"$FAILURE_LOG" echo "0" else - echo "$result" + echo "$parsed" fi - rm -f "$tmp_err" + rm -f "$tmp_err" "$tmp_json" } # Start test email