tripleo-upgrade/templates/l3_agent_stop_ping.sh.j2

55 lines
2.0 KiB
Django/Jinja

#!/bin/bash
#
# Script which stops the ICMP connectivity check on validates that
# there is no packet loss.
set -eu -o pipefail
# In seconds.
LOSS_THRESHOLD=${1:-{{ loss_threshold|default(60)|int }}}
LOSS_THRESHOLD_PERCENT=${2:-{{ loss_threshold_percent|default(1)|int }}}
# useful for testing
BASE_DIR="${2:-${HOME}}"
# Return total cut time in seconds
function get_cut_time()
{
local start=$(head -2 $PING_RESULT_LOG | tail -1 | grep -o '\[[^.]*' | tr -d '[[]]')
local end=$(tail -n 5 $PING_RESULT_LOG | head -1 | grep -o '\[[^.]*' | tr -d '[[]]')
echo "($end - $start)*${PING_RESULT}/100" | bc -ql
}
# kill the ping process
kill -s INT $(/usr/sbin/pidof ping)
# print the ping results
PING_RESULT_LOG=$(find "${BASE_DIR}" -maxdepth 1 -iname 'ping_results*' | sort | tail -1)
tail -2 $PING_RESULT_LOG
# if only one packet is lost, test succeeded
TRANSMITTED=$(grep 'packet loss' $PING_RESULT_LOG | sed 's/^\([0-9]*\) packets transmitted,.*/\1/')
RECEIVED=$(grep 'packet loss' $PING_RESULT_LOG | sed 's/.*packets transmitted, \([0-9]*\) received,.*/\1/')
if [ $(( ${TRANSMITTED} - ${RECEIVED} )) -eq 1 ]; then
echo "Only one ping was not replied during the update/upgrade - test succeeded"
exit 0
fi
# check results
PING_RESULT=$( grep 'packet loss' $PING_RESULT_LOG | sed 's/.*,\s\(.*\)%.*$/\1/' )
CUT_TIME=$(get_cut_time)
# bc >: 0 when false, 1: when true.
if [ $(echo "${CUT_TIME} > ${LOSS_THRESHOLD}" | bc ) -ne 0 ]; then
LC_NUMERIC=C \
printf "Ping loss higher than %.0f seconds detected (%.0f seconds)\n" \
${LOSS_THRESHOLD} ${CUT_TIME}
exit 1
fi
# Failsafe for very short task where % of total is small because total
# time is low: 100% packet loss of a tasks that last 10min is 6seconds
# but we should still fail.
if [ $(echo "${PING_RESULT} > ${LOSS_THRESHOLD_PERCENT}" | bc ) -ne 0 ]; then
LC_NUMERIC=C \
printf "Ping loss higher than %.0f %% detected (%.0f %%)\n" \
${LOSS_THRESHOLD_PERCENT} ${PING_RESULT}
exit 1
fi