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 747331a9ec
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 54 additions and 26 deletions

View file

@ -48,25 +48,27 @@ ln -s /usr/local/src/check_pg_streaming_replication/check_pg_streaming_replicati
``` ```
Usage: ./check_pg_streaming_replication [-d] [-h] [options] Usage: ./check_pg_streaming_replication [-d] [-h] [options]
-u pg_user Specify local Postgres user (Default: try to auto-detect or use postgres) -u pg_user Specify local Postgres user (Default: try to auto-detect or use postgres)
-b psql_bin Specify psql binary path (Default: /usr/bin/psql) -b psql_bin Specify psql binary path (Default: /usr/bin/psql)
-B pg_lsclusters_bin Specify pg_lsclusters binary path (Default: /usr/bin/pg_lsclusters) -B pg_lsclusters_bin Specify pg_lsclusters binary path (Default: /usr/bin/pg_lsclusters)
-V pg_version Specify Postgres version (Default: try to auto-detect or use 9.1) -V pg_version Specify Postgres version (Default: try to auto-detect or use 9.1)
-m pg_main Specify Postgres main directory path (Default: try to auto-detect or use -m pg_main Specify Postgres main directory path (Default: try to auto-detect or use
/var/lib/postgresql//main) /var/lib/postgresql//main)
-r recovery_conf Specify Postgres recovery configuration file path -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) -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 -p pg_port Specify default Postgres master TCP port (Default: same as local PostgreSQL
port if detected or use 5432) port if detected or use 5432)
-D dbname Specify DB name on Postgres master/slave to connect on (Default: PG_USER, must -D dbname Specify DB name on Postgres master/slave to connect on (Default: PG_USER, must
match with .pgpass one is used) match with .pgpass one is used)
-C 1/0 Enable or disable check if the current LSN of the master host is the same -C 1/0 Enable or disable check if the current LSN of the master host is the same
of the last received LSN (Default: 1) of the last received LSN (Default: 1)
-w replay_warn_delay Specify the replay warning delay in second (Default: 3) -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) -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_sync_state The expected replication state ('sync' or 'async', default: sync)
-d Debug mode -E expected_mode The expected mode ('master', 'hot-standby' or 'auto', default: 'auto')
-d Debug mode
-h Show this message
``` ```
## Copyright ## Copyright

View file

@ -43,6 +43,7 @@ CHECK_CUR_MASTER_LSN=1
REPLAY_WARNING_DELAY=3 REPLAY_WARNING_DELAY=3
REPLAY_CRITICAL_DELAY=5 REPLAY_CRITICAL_DELAY=5
EXPECTED_SYNC_STATE=sync EXPECTED_SYNC_STATE=sync
EXPECTED_MODE=auto
DEBUG=0 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) -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) -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_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 -d Debug mode
-h Show this message -h Show this message
EOF EOF
[ -n "$ERROR" ] && exit 1 || exit 0 [ -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 do
case $OPTION in case $OPTION in
u) u)
@ -119,6 +121,11 @@ do
usage "Invalid expected replication state '$OPTARG'. Possible values: sync or async." usage "Invalid expected replication state '$OPTARG'. Possible values: sync or async."
EXPECTED_SYNC_STATE=$OPTARG 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) d)
DEBUG=1 DEBUG=1
;; ;;
@ -151,6 +158,8 @@ PG_DEFAULT_APP_NAME = $PG_DEFAULT_APP_NAME
CHECK_CUR_MASTER_LSN = $CHECK_CUR_MASTER_LSN CHECK_CUR_MASTER_LSN = $CHECK_CUR_MASTER_LSN
REPLAY_WARNING_DELAY = $REPLAY_WARNING_DELAY REPLAY_WARNING_DELAY = $REPLAY_WARNING_DELAY
REPLAY_CRITICAL_DELAY = $REPLAY_CRITICAL_DELAY REPLAY_CRITICAL_DELAY = $REPLAY_CRITICAL_DELAY
EXPECTED_SYNC_STATE = $EXPECTED_SYNC_STATE
EXPECTED_MODE = $EXPECTED_MODE
" "
# Auto-detect PostgreSQL information using pg_lsclusters # Auto-detect PostgreSQL information using pg_lsclusters
@ -192,6 +201,8 @@ id "$PG_USER" > /dev/null 2>&1
if [ -z "$RECOVERY_CONF" ]; then if [ -z "$RECOVERY_CONF" ]; then
[ $PG_VERSION -le 11 ] && RECOVERY_CONF_FILENAME="recovery.conf" || RECOVERY_CONF_FILENAME="postgresql.auto.conf" [ $PG_VERSION -le 11 ] && RECOVERY_CONF_FILENAME="recovery.conf" || RECOVERY_CONF_FILENAME="postgresql.auto.conf"
RECOVERY_CONF="$PG_MAIN/$RECOVERY_CONF_FILENAME" RECOVERY_CONF="$PG_MAIN/$RECOVERY_CONF_FILENAME"
else
RECOVERY_CONF_FILENAME=$( basename "$RECOVERY_CONF" )
fi fi
# Check PG_DEFAULT_PORT # Check PG_DEFAULT_PORT
@ -262,10 +273,23 @@ debug "Postgres is running"
RECOVERY_MODE=0 RECOVERY_MODE=0
[ "$( psql_get 'SELECT pg_is_in_recovery();' )" == "t" ] && RECOVERY_MODE=1 [ "$( psql_get 'SELECT pg_is_in_recovery();' )" == "t" ] && RECOVERY_MODE=1
if [ -f $RECOVERY_CONF ] if [ "$EXPECTED_MODE" == "auto" ]; then
then debug "Auto-detect mode"
debug "File recovery.conf found. Hot-standby 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 # Check recovery mode
if [ $RECOVERY_MODE -ne 1 ] if [ $RECOVERY_MODE -ne 1 ]
then then
@ -429,9 +453,8 @@ then
echo "Last sent/writed LSN: '$M_CUR_SENT_LSN' / '$M_CUR_WRITED_LSN'" 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" [ "$LAST_RECEIVED_LSN" != "$LAST_REPLAYED_LSN" ] && echo "Replay delay: ${REPLAY_DELAY}s"
exit 0 exit 0
else elif [ "$EXPECTED_MODE" == "master" ]
debug "File recovery.conf not found. Master mode." then
# Check recovery mode # Check recovery mode
if [ $RECOVERY_MODE -eq 1 ] if [ $RECOVERY_MODE -eq 1 ]
then then
@ -496,4 +519,7 @@ else
echo "Current master LSN: $CURRENT_LSN" echo "Current master LSN: $CURRENT_LSN"
echo -e "$STANDBY_CLIENTS_TXT" echo -e "$STANDBY_CLIENTS_TXT"
exit $EXIT_CODE exit $EXIT_CODE
else
echo "UNKNOWN - Invalid mode '$EXPECTED_MODE'"
exit 3
fi fi