diff --git a/README.md b/README.md index 99b1885..77fd1ec 100644 --- a/README.md +++ b/README.md @@ -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] - -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 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) - -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) - -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) - -D dbname Specify DB name on Postgres master/slave to connect on (Default: PG_USER, must - match with .pgpass one is used) - -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) - -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) - -d Debug mode + -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 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) + -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 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) + -D dbname Specify DB name on Postgres master/slave to connect on (Default: PG_USER, must + match with .pgpass one is used) + -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) + -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 diff --git a/check_pg_streaming_replication b/check_pg_streaming_replication index 157e9d6..976260b 100755 --- a/check_pg_streaming_replication +++ b/check_pg_streaming_replication @@ -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