icinga2-checks/check_iperf3.sh

88 lines
2.0 KiB
Bash
Raw Normal View History

2023-06-07 14:31:09 -06:00
#!/usr/bin/env bash
2023-06-07 14:28:15 -06:00
2023-06-07 17:40:41 -06:00
# Default values
SERVER=""
WARNING_LEVEL=""
CRITICAL_LEVEL=""
RSA_PUBLIC_KEY=""
USERNAME=""
PASSWORD=""
2023-06-07 14:28:15 -06:00
2023-06-07 17:40:41 -06:00
# Parse named arguments
2023-06-07 14:28:15 -06:00
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
2023-06-07 17:40:41 -06:00
--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
;;
2023-06-07 14:28:15 -06:00
esac
done
2023-06-07 17:40:41 -06:00
# 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
2023-06-07 14:28:15 -06:00
fi
2023-06-07 17:40:41 -06:00
# Set IPERF3_PASSWORD environment variable
2023-06-07 14:28:15 -06:00
export IPERF3_PASSWORD="$PASSWORD"
2023-06-07 17:40:41 -06:00
# 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)
2023-06-07 14:28:15 -06:00
fi
2023-06-07 17:40:41 -06:00
# Check if iperf3 command failed
if [[ $? -ne 0 ]]; then
echo "UNKNOWN - iperf3 command failed: $OUTPUT"
exit -1
2023-06-07 14:28:15 -06:00
fi
2023-06-07 17:40:41 -06:00
# Extract the receiver bitrate
RECEIVER_BITRATE=$(echo "$OUTPUT" | grep -Eo '[0-9]+(\.[0-9]+)? Mbits/sec' | tail -1 | awk '{print $1}')
2023-06-07 14:28:15 -06:00
2023-06-07 17:40:41 -06:00
# 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"
2023-06-07 14:28:15 -06:00
exit 2
2023-06-07 17:40:41 -06:00
elif (( $(echo "$RECEIVER_BITRATE < $WARNING_LEVEL" | bc -l) )); then
echo "WARNING - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
2023-06-07 14:28:15 -06:00
exit 1
else
2023-06-07 17:40:41 -06:00
echo "OK - Receiver Bitrate: $RECEIVER_BITRATE Mbits/sec"
2023-06-07 14:28:15 -06:00
exit 0
fi