icinga2-checks/check_coturn.sh

80 lines
2.4 KiB
Bash
Raw Normal View History

#!/bin/bash
SERVER_ADDRESS=""
SECRET=""
REALM=""
PORT=3478
PROTOCOL="udp"
TIMEOUT=10
while getopts "s:S:u:r:p:P:t:" opt; do
case $opt in
s)
SERVER_ADDRESS="$OPTARG"
;;
S)
SECRET="$OPTARG"
;;
r)
REALM="$OPTARG"
;;
p)
PORT="$OPTARG"
;;
P)
PROTOCOL="$OPTARG"
;;
t)
TIMEOUT="$OPTARG"
;;
*)
echo "Usage: $0 -s SERVER_ADDRESS -S SECRET -r REALM [-p PORT] [-P PROTOCOL] [-t TIMEOUT]"
exit 1
;;
esac
done
# Check if required options are set
if [[ -z "$SERVER_ADDRESS" || -z "$SECRET" || -z "$REALM" ]]; then
echo "Usage: $0 -s SERVER_ADDRESS -S SECRET -r REALM [-p PORT] [-P PROTOCOL] [-t TIMEOUT]"
exit 1
fi
if ! command -v turnutils_uclient &>/dev/null; then
echo "UNKNOWN - turnutils_uclient not found! Please install coturn"
exit -1
fi
if ! command -v stun &>/dev/null; then
echo "UNKNWON - stun not found! Please install stun-client"
exit -1
fi
# Fetch the user's public IP using the coturn server as a STUN server
PEER_ADDRESS=$(stun "$SERVER_ADDRESS" -p "$PORT" -v 1 2>&1 | grep "MappedAddress" | awk -F'[ =:]+' '{print $2}')
if [[ -z "$PEER_ADDRESS" ]]; then
echo "UNKNOWN Failed to fetch the user's public IP using the coturn server as a STUN server."
exit 1
fi
TURNUTILS_OUTPUT=$(turnutils_uclient -s -W "$SECRET" -r "$REALM" -p "$PORT" -e "$PEER_ADDRESS" -B -y "$SERVER_ADDRESS" 2>&1)
if [ $? -eq 0 ]; then
# TOT_SEND_BYTES=$(echo "$TURNUTILS_OUTPUT" | grep -m1 "start_mclient: tot_send_bytes" | awk -F'[~ ,]+' '{print $5}')
# TOT_RECV_BYTES=$(echo "$TURNUTILS_OUTPUT" | grep -m1 "start_mclient: tot_send_bytes" | awk -F'[~ ,]+' '{print $7}')
LOST_PACKETS=$(echo "$TURNUTILS_OUTPUT" | grep -m1 "Total lost packets" | awk -F'[(%)]' '{print $2"%"}' | tr -d '%' | cut -d. -f1)
SEND_DROPPED=$(echo "$TURNUTILS_OUTPUT" | grep -m1 "Total lost packets" | awk -F'[(%)]' '{print $5"%"}' | tr -d '%' | cut -d. -f1)
AVG_RTT=$(echo "$TURNUTILS_OUTPUT" | grep -m1 "Average round trip delay" | awk '{print $7}' | cut -d. -f1)
AVG_JITTER=$(echo "$TURNUTILS_OUTPUT" | grep -m1 "Average jitter" | awk '{print $5}' | cut -d. -f1)
echo "OK - connected to TURN server $SERVER_ADDRESS | lost_packets_percent=$LOST_PACKETS send_dropped_percent=$SEND_DROPPED avg_rtt=${AVG_RTT}ms avg_jitter=${AVG_JITTER}ms"
exit 0
else
echo "CRITICAL - failed to connect to TURN server:"
echo "$TURNUTILS_OUTPUT"
exit 2
fi