scripts/monitoring/check_postgresql_replication.sh

48 lines
1.2 KiB
Bash
Raw Normal View History

2019-05-14 11:22:04 +02:00
#!/bin/sh
2023-10-19 17:53:44 +02:00
2019-05-14 11:22:04 +02:00
# 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
2023-10-19 17:53:44 +02:00
args=$(getopt hw:c: "$*")
set -- "$args"
2019-05-14 11:22:04 +02:00
warning=600
critical=3600
while :; do
case "$1" in
2023-10-19 17:53:44 +02:00
-h) echo "Usage $0: [-h] [-w warning (default: $warning)] [-c critical (default: $critical)]"
2019-05-14 11:22:04 +02:00
shift;
exit 2;
;;
-w) warning=$2
shift; shift;
;;
-c) critical=$2
shift; shift;
;;
--)
shift; break;
;;
esac
done
2023-10-19 17:53:44 +02:00
delay=$(/usr/local/bin/psql --port=6432 --username=postgres --pset="expanded=yes" --command="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 }')
2019-05-14 11:22:04 +02:00
2023-10-19 17:53:44 +02:00
if [ "$delay" -gt "$critical" ]
2019-05-14 11:22:04 +02:00
then
echo "CRITICAL: $delay seconds behind master"
exit 2
2023-10-19 17:53:44 +02:00
elif [ "$delay" -gt "$warning" ]
2019-05-14 11:22:04 +02:00
then
echo "WARNING: $delay seconds behind master"
exit 1
else
echo "OK: $delay seconds behind master"
exit 0
fi