check_coturn: add perfdata and levels

This commit is contained in:
Cyberes 2023-05-30 15:36:51 -06:00
parent 29c09666e8
commit 4878b668c4
2 changed files with 46 additions and 20 deletions

View File

@ -6,8 +6,14 @@ REALM=""
PORT=3478 PORT=3478
PROTOCOL="udp" PROTOCOL="udp"
TIMEOUT=10 TIMEOUT=10
LOST_PACKETS_WARN=3
LOST_PACKETS_CRIT=5
SEND_DROPPED_WARN=3
SEND_DROPPED_CRIT=5
AVG_RTT_WARN=100
AVG_RTT_CRIT=200
while getopts "s:S:u:r:p:P:t:" opt; do while getopts "s:S:u:r:p:w:c:W:C:R:T:" opt; do
case $opt in case $opt in
s) s)
SERVER_ADDRESS="$OPTARG" SERVER_ADDRESS="$OPTARG"
@ -21,27 +27,39 @@ while getopts "s:S:u:r:p:P:t:" opt; do
p) p)
PORT="$OPTARG" PORT="$OPTARG"
;; ;;
P) w)
PROTOCOL="$OPTARG" LOST_PACKETS_WARN="$OPTARG"
;; ;;
t) c)
TIMEOUT="$OPTARG" LOST_PACKETS_CRIT="$OPTARG"
;;
W)
SEND_DROPPED_WARN="$OPTARG"
;;
C)
SEND_DROPPED_CRIT="$OPTARG"
;;
R)
AVG_RTT_WARN="$OPTARG"
;;
T)
AVG_RTT_CRIT="$OPTARG"
;; ;;
*) *)
echo "Usage: $0 -s SERVER_ADDRESS -S SECRET -r REALM [-p PORT] [-P PROTOCOL] [-t TIMEOUT]" 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]"
exit 1 exit -1
;; ;;
esac esac
done done
# Check if required options are set # Check if required options are set
if [[ -z "$SERVER_ADDRESS" || -z "$SECRET" || -z "$REALM" ]]; then 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]" 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]"
exit 1 exit -1
fi fi
if ! command -v turnutils_uclient &>/dev/null; then if ! command -v turnutils_uclient &>/dev/null; then
echo "UNKNOWN - turnutils_uclient not found! Please install coturn" echo "UNKNOWN - turnutils_uclient not found! Please install coturn. Make sure to run \`service coturn stop; systemctl disable coturn\`"
exit -1 exit -1
fi fi
@ -51,27 +69,35 @@ if ! command -v stun &>/dev/null; then
fi fi
# Fetch the user's public IP using the coturn server as a STUN server # 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}') PEER_ADDRESS_OUT=$(stun "$SERVER_ADDRESS" -p "$PORT" -v 1 2>&1)
PEER_ADDRESS=$(echo "$PEER_ADDRESS_OUT" | grep "MappedAddress" | awk -F'[ =:]+' '{print $2}')
if [[ -z "$PEER_ADDRESS" ]]; then if [[ -z "$PEER_ADDRESS" ]]; then
echo "UNKNOWN Failed to fetch the user's public IP using the coturn server as a STUN server." echo "UNKNOWN - failed to fetch this host's public IP using the coturn server as a STUN server."
exit 1 echo "$PEER_ADDRESS_OUT"
exit -1
fi fi
TURNUTILS_OUTPUT=$(turnutils_uclient -s -W "$SECRET" -r "$REALM" -p "$PORT" -e "$PEER_ADDRESS" -B -y "$SERVER_ADDRESS" 2>&1) TURNUTILS_OUTPUT=$(turnutils_uclient -s -W "$SECRET" -r "$REALM" -p "$PORT" -e "$PEER_ADDRESS" -B -y "$SERVER_ADDRESS" 2>&1)
if [ $? -eq 0 ]; then 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) 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) 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_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) 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_CODE=0
exit 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
else else
echo "CRITICAL - failed to connect to TURN server:" echo "CRITICAL - failed to connect to TURN server:"
echo "$TURNUTILS_OUTPUT" echo "$TURNUTILS_OUTPUT"

View File

@ -212,7 +212,7 @@ def main():
table_data.append((device['device'], device['size'], device['alloc'], device['free'], device['frag'], table_data.append((device['device'], device['size'], device['alloc'], device['free'], device['frag'],
device['cap'], device['health'], states[device['device']])) device['cap'], device['health'], states[device['device']]))
print(list_to_markdown_table(table_data, align='left', seperator='!', borders=False)) print(list_to_markdown_table(table_data, align='center', seperator='!', borders=False))
sys.exit(exit_code) sys.exit(exit_code)
elif args.check_type in ['cache', 'log']: elif args.check_type in ['cache', 'log']: