2023-06-07 14:31:09 -06:00
#!/usr/bin/env bash
2023-05-30 12:38:25 -06:00
SERVER_ADDRESS = ""
SECRET = ""
REALM = ""
PORT = 3478
PROTOCOL = "udp"
TIMEOUT = 10
2023-05-30 15:36:51 -06:00
LOST_PACKETS_WARN = 3
LOST_PACKETS_CRIT = 5
SEND_DROPPED_WARN = 3
SEND_DROPPED_CRIT = 5
AVG_RTT_WARN = 100
AVG_RTT_CRIT = 200
2023-06-06 23:32:00 -06:00
REQUESTED_RELAY_ADDR = "false"
2023-05-30 12:38:25 -06:00
2023-06-06 23:32:00 -06:00
while getopts "s:S:u:r:p:w:c:W:C:R:T:x:" opt; do
2023-05-30 12:38:25 -06:00
case $opt in
s)
SERVER_ADDRESS = " $OPTARG "
; ;
S)
SECRET = " $OPTARG "
; ;
r)
REALM = " $OPTARG "
; ;
p)
PORT = " $OPTARG "
; ;
2023-05-30 15:36:51 -06:00
w)
LOST_PACKETS_WARN = " $OPTARG "
2023-05-30 12:38:25 -06:00
; ;
2023-05-30 15:36:51 -06:00
c)
LOST_PACKETS_CRIT = " $OPTARG "
; ;
W)
SEND_DROPPED_WARN = " $OPTARG "
; ;
C)
SEND_DROPPED_CRIT = " $OPTARG "
; ;
R)
AVG_RTT_WARN = " $OPTARG "
; ;
T)
AVG_RTT_CRIT = " $OPTARG "
2023-05-30 12:38:25 -06:00
; ;
2023-06-06 23:32:00 -06:00
x)
REQUESTED_RELAY_ADDR = " -x $OPTARG "
; ;
2023-05-30 12:38:25 -06:00
*)
2023-06-06 23:32:00 -06:00
echo " Usage: $0 -s SERVER_ADDRESS -S SECRET -r REALM [-p PORT] [-P PROTOCOL] [-t TIMEOUT] [-w LOST_PACKETS_WARN] [-c LOST_PACKETS_CRIT] [-W SEND_DROPPED_WARN] [-C SEND_DROPPED_CRIT] [-R AVG_RTT_WARN] [-T AVG_RTT_CRIT] [-x REQUESTED_RELAY_ADDR] "
2023-05-30 15:36:51 -06:00
exit -1
2023-05-30 12:38:25 -06:00
; ;
esac
done
# Check if required options are set
if [ [ -z " $SERVER_ADDRESS " || -z " $SECRET " || -z " $REALM " ] ] ; then
2023-06-06 23:32:00 -06:00
echo " Usage: $0 -s SERVER_ADDRESS -S SECRET -r REALM [-p PORT] [-P PROTOCOL] [-t TIMEOUT] [-w LOST_PACKETS_WARN] [-c LOST_PACKETS_CRIT] [-W SEND_DROPPED_WARN] [-C SEND_DROPPED_CRIT] [-R AVG_RTT_WARN] [-T AVG_RTT_CRIT] [-x REQUESTED_RELAY_ADDR] "
2023-05-30 15:36:51 -06:00
exit -1
2023-05-30 12:38:25 -06:00
fi
if ! command -v turnutils_uclient & >/dev/null; then
2023-06-06 23:32:00 -06:00
echo "UNKNOWN - turnutils_uclient not found! Please install coturn. Make sure to run \`sudo apt install coturn; sudo service coturn stop; sudo systemctl disable coturn\`"
2023-05-30 12:38:25 -06:00
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
2023-05-30 15:36:51 -06:00
PEER_ADDRESS_OUT = $( stun " $SERVER_ADDRESS " -p " $PORT " -v 1 2>& 1)
PEER_ADDRESS = $( echo " $PEER_ADDRESS_OUT " | grep "MappedAddress" | awk -F'[ =:]+' '{print $2}' )
2023-05-30 12:38:25 -06:00
if [ [ -z " $PEER_ADDRESS " ] ] ; then
2023-05-30 15:36:51 -06:00
echo "UNKNOWN - failed to fetch this host's public IP using the coturn server as a STUN server."
echo " $PEER_ADDRESS_OUT "
exit -1
2023-05-30 12:38:25 -06:00
fi
2023-06-06 23:32:00 -06:00
TURNUTILS_OUTPUT = $( turnutils_uclient " $SERVER_ADDRESS " -s -W " $SECRET " -r " $REALM " -p " $PORT " -e " $PEER_ADDRESS " $REQUESTED_RELAY_ADDR -B -y 2>& 1)
2023-05-30 12:38:25 -06:00
if [ $? -eq 0 ] ; then
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)
2023-05-30 15:36:51 -06:00
EXIT_CODE = 0
STATUS = "OK"
if [ " $LOST_PACKETS " -ge " $LOST_PACKETS_CRIT " ] || [ " $SEND_DROPPED " -ge " $SEND_DROPPED_CRIT " ] || [ " $AVG_RTT " -ge " $AVG_RTT_CRIT " ] ; then
STATUS = "CRITICAL"
EXIT_CODE = 2
elif [ " $LOST_PACKETS " -ge " $LOST_PACKETS_WARN " ] || [ " $SEND_DROPPED " -ge " $SEND_DROPPED_WARN " ] || [ " $AVG_RTT " -ge " $AVG_RTT_WARN " ] ; then
STATUS = "WARNING"
EXIT_CODE = 1
fi
echo " $STATUS - connected to TURN server $SERVER_ADDRESS : $PORT . lost_packets= $LOST_PACKETS % send_dropped= $SEND_DROPPED % avg_rtt= ${ AVG_RTT } ms avg_jitter= ${ AVG_JITTER } ms | lost_packets_percent= $LOST_PACKETS ; $LOST_PACKETS_WARN ; $LOST_PACKETS_CRIT ;0;100 send_dropped_percent= $SEND_DROPPED ; $SEND_DROPPED_WARN ; $SEND_DROPPED_CRIT ;0;100 avg_rtt= ${ AVG_RTT } ms; $AVG_RTT_WARN ; $AVG_RTT_CRIT ;0; avg_jitter= ${ AVG_JITTER } ms;0; " exit $EXIT_CODE
2023-05-30 12:38:25 -06:00
else
echo "CRITICAL - failed to connect to TURN server:"
echo " $TURNUTILS_OUTPUT "
exit 2
fi