rewrite check_iperf3.sh
This commit is contained in:
parent
0c11d3a94e
commit
9b043801df
|
@ -1,33 +1,14 @@
|
|||
#!/usr/bin/env 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 ! command -v jq &>/dev/null; then
|
||||
echo "UNKNOWN - jq is not installed"
|
||||
exit 3
|
||||
fi
|
||||
|
||||
if [[ $# -eq 0 ]]; then
|
||||
print_help
|
||||
fi
|
||||
# Default values
|
||||
SERVER=""
|
||||
WARNING_LEVEL=""
|
||||
CRITICAL_LEVEL=""
|
||||
RSA_PUBLIC_KEY=""
|
||||
USERNAME=""
|
||||
PASSWORD=""
|
||||
|
||||
# Parse named arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
key="$1"
|
||||
|
||||
|
@ -38,17 +19,17 @@ while [[ $# -gt 0 ]]; do
|
|||
shift
|
||||
;;
|
||||
--warning)
|
||||
WARNING="$2"
|
||||
WARNING_LEVEL="$2"
|
||||
shift
|
||||
shift
|
||||
;;
|
||||
--critical)
|
||||
CRITICAL="$2"
|
||||
CRITICAL_LEVEL="$2"
|
||||
shift
|
||||
shift
|
||||
;;
|
||||
--rsa-key)
|
||||
RSA_KEY="$2"
|
||||
--rsa-public-key)
|
||||
RSA_PUBLIC_KEY="$2"
|
||||
shift
|
||||
shift
|
||||
;;
|
||||
|
@ -63,40 +44,44 @@ while [[ $# -gt 0 ]]; do
|
|||
shift
|
||||
;;
|
||||
*)
|
||||
print_help
|
||||
shift
|
||||
;;
|
||||
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
|
||||
# Check if required arguments are provided
|
||||
if [[ -z "$SERVER" ]] || [[ -z "$WARNING_LEVEL" ]] || [[ -z "$CRITICAL_LEVEL" ]]; then
|
||||
echo "Usage: $0 --server SERVER --warning WARNING_LEVEL --critical CRITICAL_LEVEL [--rsa-public-key RSA_PUBLIC_KEY] [--username USERNAME] [--password PASSWORD]"
|
||||
exit -1
|
||||
fi
|
||||
|
||||
# Set IPERF3_PASSWORD environment variable
|
||||
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
|
||||
echo "UNKNOWN - iperf3 command failed: $RESULT"
|
||||
exit 3
|
||||
echo "UNKNOWN - iperf3 command failed: $OUTPUT"
|
||||
exit -1
|
||||
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
|
||||
# Extract the receiver bitrate
|
||||
RECEIVER_BITRATE=$(echo "$OUTPUT" | grep -Eo '[0-9]+(\.[0-9]+)? Mbits/sec' | tail -1 | awk '{print $1}')
|
||||
|
||||
STREAM_RESULT=$(echo "$RESULT" | jq '.end.streams[] | select(.udp.sender == (!0)) | .udp.bits_per_second')
|
||||
SPEED_RAW=$(echo "$STREAM_RESULT" | jq 'if length > 0 then . / 1000000 else 0 end')
|
||||
SPEED=$(printf "%.0f" "$SPEED_RAW")
|
||||
|
||||
if (($(echo "$SPEED < $CRITICAL" | bc -l))); then
|
||||
echo "CRITICAL - Speed: ${SPEED} Mbits/s"
|
||||
# Check the receiver bitrate against warning and critical levels
|
||||
if (( $(echo "$RECEIVER_BITRATE < $CRITICAL_LEVEL" | bc -l) )); then
|
||||
echo "CRITICAL - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
|
||||
exit 2
|
||||
elif (($(echo "$SPEED < $WARNING" | bc -l))); then
|
||||
echo "WARNING - Speed: ${SPEED} Mbits/s"
|
||||
elif (( $(echo "$RECEIVER_BITRATE < $WARNING_LEVEL" | bc -l) )); then
|
||||
echo "WARNING - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
|
||||
exit 1
|
||||
else
|
||||
echo "OK - Speed: ${SPEED} Mbits/s"
|
||||
echo "OK - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
|
||||
exit 0
|
||||
fi
|
||||
|
|
Loading…
Reference in New Issue