icinga2-checks/check_iperf3.sh

88 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Default values
SERVER=""
WARNING_LEVEL=""
CRITICAL_LEVEL=""
RSA_PUBLIC_KEY=""
USERNAME=""
PASSWORD=""
# Parse named arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--server)
SERVER="$2"
shift
shift
;;
--warning)
WARNING_LEVEL="$2"
shift
shift
;;
--critical)
CRITICAL_LEVEL="$2"
shift
shift
;;
--rsa-public-key)
RSA_PUBLIC_KEY="$2"
shift
shift
;;
--username)
USERNAME="$2"
shift
shift
;;
--password)
PASSWORD="$2"
shift
shift
;;
*)
shift
;;
esac
done
# 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"
# 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: $OUTPUT"
exit -1
fi
# Extract the receiver bitrate
RECEIVER_BITRATE=$(echo "$OUTPUT" | grep -Eo '[0-9]+(\.[0-9]+)? Mbits/sec' | tail -1 | awk '{print $1}')
# 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 "$RECEIVER_BITRATE < $WARNING_LEVEL" | bc -l) )); then
echo "WARNING - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
exit 1
else
echo "OK - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
exit 0
fi