Add -E parameter to allow to specify expected mode and fix auto-detection on PG>=12

This commit is contained in:
Benjamin Renard 2024-06-03 16:05:45 +02:00
parent 8d172e944c
commit bd87f52a76
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 54 additions and 26 deletions

View file

@ -55,7 +55,7 @@ Usage: ./check_pg_streaming_replication [-d] [-h] [options]
-m pg_main Specify Postgres main directory path (Default: try to auto-detect or use
/var/lib/postgresql//main)
-r recovery_conf Specify Postgres recovery configuration file path
(Default: [PG_MAIN]/recovery.conf for PG <= 11, [PG_MAIN]/postgresql.auto.conf for PG >= 12)
( Default: [PG_MAIN]/recovery.conf on PG <= 11, [PG_MAIN]/postgresql.auto.conf on PG >= 12)
-U pg_master_user Specify Postgres user to use on master (Default: user from recovery.conf file)
-p pg_port Specify default Postgres master TCP port (Default: same as local PostgreSQL
port if detected or use 5432)
@ -66,7 +66,9 @@ Usage: ./check_pg_streaming_replication [-d] [-h] [options]
-w replay_warn_delay Specify the replay warning delay in second (Default: 3)
-c replay_crit_delay Specify the replay critical delay in second (Default: 5)
-e expected_sync_state The expected replication state ('sync' or 'async', default: sync)
-E expected_mode The expected mode ('master', 'hot-standby' or 'auto', default: 'auto')
-d Debug mode
-h Show this message
```
## Copyright

View file

@ -43,6 +43,7 @@ CHECK_CUR_MASTER_LSN=1
REPLAY_WARNING_DELAY=3
REPLAY_CRITICAL_DELAY=5
EXPECTED_SYNC_STATE=sync
EXPECTED_MODE=auto
DEBUG=0
@ -69,13 +70,14 @@ Usage: $0 [-d] [-h] [options]
-w replay_warn_delay Specify the replay warning delay in second (Default: $REPLAY_WARNING_DELAY)
-c replay_crit_delay Specify the replay critical delay in second (Default: $REPLAY_CRITICAL_DELAY)
-e expected_sync_state The expected replication state ('sync' or 'async', default: $EXPECTED_SYNC_STATE)
-E expected_mode The expected mode ('master', 'hot-standby' or 'auto', default: '$EXPECTED_MODE')
-d Debug mode
-h Show this message
EOF
[ -n "$ERROR" ] && exit 1 || exit 0
}
while getopts "hu:b:B:V:m:r:U:p:D:C:w:c:e:d" OPTION
while getopts "hu:b:B:V:m:r:U:p:D:C:w:c:e:E:d" OPTION
do
case $OPTION in
u)
@ -119,6 +121,11 @@ do
usage "Invalid expected replication state '$OPTARG'. Possible values: sync or async."
EXPECTED_SYNC_STATE=$OPTARG
;;
E)
[ "$OPTARG" != "master" -a "$OPTARG" != "hot-standby" -a "$OPTARG" != "auto" ] && \
usage "Invalid expected mode '$OPTARG'. Possible values: master, hot-standby or auto."
EXPECTED_MODE=$OPTARG
;;
d)
DEBUG=1
;;
@ -151,6 +158,8 @@ PG_DEFAULT_APP_NAME = $PG_DEFAULT_APP_NAME
CHECK_CUR_MASTER_LSN = $CHECK_CUR_MASTER_LSN
REPLAY_WARNING_DELAY = $REPLAY_WARNING_DELAY
REPLAY_CRITICAL_DELAY = $REPLAY_CRITICAL_DELAY
EXPECTED_SYNC_STATE = $EXPECTED_SYNC_STATE
EXPECTED_MODE = $EXPECTED_MODE
"
# Auto-detect PostgreSQL information using pg_lsclusters
@ -192,6 +201,8 @@ id "$PG_USER" > /dev/null 2>&1
if [ -z "$RECOVERY_CONF" ]; then
[ $PG_VERSION -le 11 ] && RECOVERY_CONF_FILENAME="recovery.conf" || RECOVERY_CONF_FILENAME="postgresql.auto.conf"
RECOVERY_CONF="$PG_MAIN/$RECOVERY_CONF_FILENAME"
else
RECOVERY_CONF_FILENAME=$( basename "$RECOVERY_CONF" )
fi
# Check PG_DEFAULT_PORT
@ -262,10 +273,23 @@ debug "Postgres is running"
RECOVERY_MODE=0
[ "$( psql_get 'SELECT pg_is_in_recovery();' )" == "t" ] && RECOVERY_MODE=1
if [ -f $RECOVERY_CONF ]
then
debug "File recovery.conf found. Hot-standby mode."
if [ "$EXPECTED_MODE" == "auto" ]; then
debug "Auto-detect mode"
if [[ $RECOVERY_MODE -eq 1 ]]; then
debug "Postgres is in recovery mode. Hot-standby mode."
EXPECTED_MODE="hot-standby"
elif [ -f $RECOVERY_CONF -a $( grep -cE '^\s*primary_conninfo' $RECOVERY_CONF ) -gt 0 ]; then
debug "File $RECOVERY_CONF_FILENAME found and contain primary_conninfo. Hot-standby mode."
EXPECTED_MODE="hot-standby"
else
debug "Postgres not in recovery mode and file $RECOVERY_CONF_FILENAME not found " \
"(or does not contain primary_conninfo). Master mode."
EXPECTED_MODE="master"
fi
fi
if [ "$EXPECTED_MODE" == "hot-standby" ]
then
# Check recovery mode
if [ $RECOVERY_MODE -ne 1 ]
then
@ -429,9 +453,8 @@ then
echo "Last sent/writed LSN: '$M_CUR_SENT_LSN' / '$M_CUR_WRITED_LSN'"
[ "$LAST_RECEIVED_LSN" != "$LAST_REPLAYED_LSN" ] && echo "Replay delay: ${REPLAY_DELAY}s"
exit 0
else
debug "File recovery.conf not found. Master mode."
else if [ "$EXPECTED_MODE" == "master" ]
then
# Check recovery mode
if [ $RECOVERY_MODE -eq 1 ]
then
@ -496,4 +519,7 @@ else
echo "Current master LSN: $CURRENT_LSN"
echo -e "$STANDBY_CLIENTS_TXT"
exit $EXIT_CODE
else
echo "UNKNOWN - Invalid mode '$EXPECTED_MODE'"
exit 3
fi