fuel-ccp-rabbitmq/service/files/rabbitmq-check-helpers.sh.j2

93 lines
1.9 KiB
Django/Jinja
Executable File

#!/usr/bin/env bash
MARKER_PATH=/tmp/rabbit-startup-marker
# How many seconds we give a node before successfull liveness checks
# become mandatory.
FRESH_NODE_TIMEOUT={{ rabbitmq.node_startup_timeout }}
LP=""
set-log-prefix() {
LP="[${1:?}]"
}
log-it() {
echo "$LP" "$@"
}
prepend-log-prefix() {
awk -v lp="$LP" '{print lp " " $0}'
}
marker-state() {
if [[ ! -f $MARKER_PATH ]]; then
echo "missing"
return 0
fi
local marker_time
marker_time="$(cat $MARKER_PATH)"
local end_of_fresh_time=$((FRESH_NODE_TIMEOUT + $marker_time))
local now
now=$(date +%s)
if [[ $now -le $end_of_fresh_time ]]; then
echo "fresh"
return 0
fi
echo "stale"
return 0
}
ping-node() {
local result
result="$(rabbitmqctl eval 'ok.' 2>&1)"
if [[ "$result" == "ok" ]]; then
return 0
fi
log-it "ping-node error:"
echo "$result" | prepend-log-prefix
return 1
}
is-node-booting() {
local result
result="$(rabbitmqctl eval 'is_pid(erlang:whereis(rabbit_boot)).' 2>&1)"
case "$result" in
true)
return 0
;;
false)
return 1
;;
*)
log-it "is-node-booting error:"
echo "$result" | prepend-log-prefix
return 1
;;
esac
}
is-node-healthy() {
local result
result=$(rabbitmqctl node_health_check -t 30 2>&1)
if [[ "$result" =~ "Health check passed" ]]; then
return 0
fi
echo "$result" | prepend-log-prefix
return 1
}
is-node-properly-clustered() {
result="$(rabbitmqctl eval 'autocluster:cluster_health_check().' 2>&1)"
if [[ $result =~ ^SUCCESS: ]]; then
return 0
elif [[ $result =~ ^FAILURE: ]]; then
echo "$result" | prepend-log-prefix
return 1
fi
log-it "Unexpected health-check output, giving the node the benefit of the doubt"
echo "$result" | prepend-log-prefix
return 0
}