rewrite check_iperf3.sh

This commit is contained in:
Cyberes 2023-06-07 17:40:41 -06:00
parent 0c11d3a94e
commit 9b043801df
Signed by: cyberes
GPG Key ID: 6B4A33836A9500FE
1 changed files with 64 additions and 79 deletions

View File

@ -1,33 +1,14 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function print_help { # Default values
echo "Usage: $0 --server SERVER --warning WARNING --critical CRITICAL --rsa-key RSA_KEY --username USERNAME --password PASSWORD" SERVER=""
echo "" WARNING_LEVEL=""
echo "Arguments:" CRITICAL_LEVEL=""
echo " --server The iperf3 server to test against" RSA_PUBLIC_KEY=""
echo " --warning Warning level for the speed test in Mbits/s" USERNAME=""
echo " --critical Critical level for the speed test in Mbits/s" PASSWORD=""
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 ! command -v jq &>/dev/null; then
echo "UNKNOWN - jq is not installed"
exit 3
fi
if [[ $# -eq 0 ]]; then
print_help
fi
# Parse named arguments
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
key="$1" key="$1"
@ -38,17 +19,17 @@ while [[ $# -gt 0 ]]; do
shift shift
;; ;;
--warning) --warning)
WARNING="$2" WARNING_LEVEL="$2"
shift shift
shift shift
;; ;;
--critical) --critical)
CRITICAL="$2" CRITICAL_LEVEL="$2"
shift shift
shift shift
;; ;;
--rsa-key) --rsa-public-key)
RSA_KEY="$2" RSA_PUBLIC_KEY="$2"
shift shift
shift shift
;; ;;
@ -63,40 +44,44 @@ while [[ $# -gt 0 ]]; do
shift shift
;; ;;
*) *)
print_help shift
;; ;;
esac esac
done done
if [[ -z "$SERVER" ]] || [[ -z "$WARNING" ]] || [[ -z "$CRITICAL" ]] || [[ -z "$RSA_KEY" ]] || [[ -z "$USERNAME" ]] || [[ -z "$PASSWORD" ]]; then # Check if required arguments are provided
echo "UNKNOWN - All arguments are required" if [[ -z "$SERVER" ]] || [[ -z "$WARNING_LEVEL" ]] || [[ -z "$CRITICAL_LEVEL" ]]; then
print_help echo "Usage: $0 --server SERVER --warning WARNING_LEVEL --critical CRITICAL_LEVEL [--rsa-public-key RSA_PUBLIC_KEY] [--username USERNAME] [--password PASSWORD]"
exit -1
fi fi
# Set IPERF3_PASSWORD environment variable
export IPERF3_PASSWORD="$PASSWORD" export IPERF3_PASSWORD="$PASSWORD"
RESULT=$(iperf3 -c $SERVER -u -b 0 -J -t 10 --rsa-public-key-path $RSA_KEY --username $USERNAME 2>&1) # Run iperf3 command with optional arguments
if [[ -n "$RSA_PUBLIC_KEY" ]] && [[ -n "$USERNAME" ]]; then
OUTPUT=$(iperf3 -c "$SERVER" -i 1 -t 10 -f m --rsa-public-key-path "$RSA_PUBLIC_KEY" --username "$USERNAME" 2>&1)
else
OUTPUT=$(iperf3 -c "$SERVER" -i 1 -t 10 -f m 2>&1)
fi
# Check if iperf3 command failed
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
echo "UNKNOWN - iperf3 command failed: $RESULT" echo "UNKNOWN - iperf3 command failed: $OUTPUT"
exit 3 exit -1
fi fi
if ! echo "$RESULT" | jq -e '.end.sum.bits_per_second' >/dev/null; then # Extract the receiver bitrate
echo "UNKNOWN - Invalid JSON output from iperf3: $RESULT" RECEIVER_BITRATE=$(echo "$OUTPUT" | grep -Eo '[0-9]+(\.[0-9]+)? Mbits/sec' | tail -1 | awk '{print $1}')
exit 3
fi
STREAM_RESULT=$(echo "$RESULT" | jq '.end.streams[] | select(.udp.sender == (!0)) | .udp.bits_per_second') # Check the receiver bitrate against warning and critical levels
SPEED_RAW=$(echo "$STREAM_RESULT" | jq 'if length > 0 then . / 1000000 else 0 end') if (( $(echo "$RECEIVER_BITRATE < $CRITICAL_LEVEL" | bc -l) )); then
SPEED=$(printf "%.0f" "$SPEED_RAW") echo "CRITICAL - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
if (($(echo "$SPEED < $CRITICAL" | bc -l))); then
echo "CRITICAL - Speed: ${SPEED} Mbits/s"
exit 2 exit 2
elif (($(echo "$SPEED < $WARNING" | bc -l))); then elif (( $(echo "$RECEIVER_BITRATE < $WARNING_LEVEL" | bc -l) )); then
echo "WARNING - Speed: ${SPEED} Mbits/s" echo "WARNING - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
exit 1 exit 1
else else
echo "OK - Speed: ${SPEED} Mbits/s" echo "OK - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
exit 0 exit 0
fi fi