scripts/monitoring/check_postgresql_replicatio...

47 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
# check postgresql (streaming) replication
# tested with postgresql 11.3 on FreeBSD 11.2
#
# pg_hba.conf access should be set to trust
# warning (-w) and critical (-c) thresholds can be set in seconds
# on a low trafic server don't hesitate to set them to a high value
args=`getopt hw:c: $*`
set -- $args
warning=600
critical=3600
while :; do
case "$1" in
-h) echo "Usage $0: [-h] [-w warning (default: $warning)] [-c critical (default: $critical)]"
shift;
exit 2;
;;
-w) warning=$2
shift; shift;
;;
-c) critical=$2
shift; shift;
;;
--)
shift; break;
;;
esac
done
delay=`/usr/local/bin/psql -U postgres --pset expanded=yes -c "select now() - pg_last_xact_replay_timestamp() AS replication_delay;" | tail -2 | head -1 | cut -d '|' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 1 | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'`
if [ $delay -gt $critical ]
then
echo "CRITICAL: $delay seconds behind master"
exit 2
elif [ $delay -gt $warning ]
then
echo "WARNING: $delay seconds behind master"
exit 1
else
echo "OK: $delay seconds behind master"
exit 0
fi