#!/bin/bash
#
# Script which stops the ICMP connectivity check on validates that
# there is no packet loss.

# kill the ping process
kill -s INT $(/usr/sbin/pidof ping)

# print the ping results
PING_RESULT_LOG=$(find ~ -iname 'ping_results*' | sort | tail -1)
tail -2 $PING_RESULT_LOG

# check results
PING_RESULT=$( grep 'packet loss' $PING_RESULT_LOG | sed 's/.*,\s\(.*\)%.*$/\1/' )
if [ $(echo "$PING_RESULT > {{ loss_threshold|default(1)|int }}" | bc ) -ne 0 ]; then
    echo "Ping loss higher than {{ loss_threshold|default(1)|int }}% detected"
    exit 1
fi
