From eb875a08a194e28537c6c5ea3f42b6c2c0c5faff Mon Sep 17 00:00:00 2001 From: Drake Panzer Date: Wed, 7 Jun 2023 14:28:15 -0600 Subject: [PATCH] add check_iperf3.sh --- check_iperf3.sh | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 check_iperf3.sh diff --git a/check_iperf3.sh b/check_iperf3.sh new file mode 100755 index 0000000..e96df58 --- /dev/null +++ b/check_iperf3.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +function print_help { + echo "Usage: $0 --server SERVER --warning WARNING --critical CRITICAL --rsa-key RSA_KEY --username USERNAME --password PASSWORD" + echo "" + echo "Arguments:" + echo " --server The iperf3 server to test against" + echo " --warning Warning level for the speed test in Mbits/s" + echo " --critical Critical level for the speed test in Mbits/s" + echo " --rsa-key Path to the RSA public key for authentication" + echo " --username Username for authentication" + echo " --password Password for authentication" + echo "" + exit 3 +} + +if ! command -v iperf3 &>/dev/null; then + echo "UNKNOWN - iperf3 is not installed" + exit 3 +fi + +if [[ $# -eq 0 ]]; then + print_help +fi + +while [[ $# -gt 0 ]]; do + key="$1" + + case $key in + --server) + SERVER="$2" + shift + shift + ;; + --warning) + WARNING="$2" + shift + shift + ;; + --critical) + CRITICAL="$2" + shift + shift + ;; + --rsa-key) + RSA_KEY="$2" + shift + shift + ;; + --username) + USERNAME="$2" + shift + shift + ;; + --password) + PASSWORD="$2" + shift + shift + ;; + *) + print_help + ;; + esac +done + +if [[ -z "$SERVER" ]] || [[ -z "$WARNING" ]] || [[ -z "$CRITICAL" ]] || [[ -z "$RSA_KEY" ]] || [[ -z "$USERNAME" ]] || [[ -z "$PASSWORD" ]]; then + echo "UNKNOWN - All arguments are required" + print_help +fi + +export IPERF3_PASSWORD="$PASSWORD" + +RESULT=$(iperf3 -c $SERVER -u -b 0 -J -t 10 --rsa-public-key-path $RSA_KEY --username $USERNAME 2>&1) +if [[ $? -ne 0 ]]; then + echo "UNKNOWN - iperf3 command failed: $RESULT" + exit 3 +fi + +if ! echo "$RESULT" | jq -e '.end.sum.bits_per_second' >/dev/null; then + echo "UNKNOWN - Invalid JSON output from iperf3: $RESULT" + exit 3 +fi + +SPEED_RAW=$(echo "$RESULT" | jq '.end.sum.bits_per_second / 1000000') +SPEED=$(printf "%.0f" "$SPEED_RAW") + +if (($(echo "$SPEED < $CRITICAL" | bc -l))); then + echo "CRITICAL - Speed: ${SPEED} Mbits/s" + exit 2 +elif (($(echo "$SPEED < $WARNING" | bc -l))); then + echo "WARNING - Speed: ${SPEED} Mbits/s" + exit 1 +else + echo "OK - Speed: ${SPEED} Mbits/s" + exit 0 +fi