diff --git a/check_pg_streaming_replication b/check_pg_streaming_replication index b6bbfde..7b74cbf 100755 --- a/check_pg_streaming_replication +++ b/check_pg_streaming_replication @@ -19,22 +19,19 @@ # Source : http://git.zionetrix.net/check_pg_streaming_replication # -PG_USER=postgres +DEFAULT_PG_USER=postgres +DEFAULT_PG_VERSION=9.1 +DEFAULT_PG_MAIN=/var/lib/postgresql/$PG_VERSION/main +DEFAULT_PG_PORT=5432 +PG_USER="" +PG_VERSION="" +PG_MAIN="" PG_MASTER_USER="" PSQL_BIN=/usr/bin/psql -PG_MAIN=/var/lib/postgresql/9.1/main -if [ -f /etc/debian_version ] -then - AUTO_PG_MAIN=$( ls -1d /var/lib/postgresql/9*/main 2> /dev/null|sort -n|tail -n 1 ) - [ -n "$AUTO_PG_MAIN" -a -d "$AUTO_PG_MAIN" ] && PG_MAIN=$AUTO_PG_MAIN -elif [ -f /etc/redhat-release ] -then - AUTO_PG_MAIN=$( ls -1d /var/lib/pgsql/9*/data 2> /dev/null|sort -n|tail -n 1 ) - [ -n "$AUTO_PG_MAIN" -a -d "$AUTO_PG_MAIN" ] && PG_MAIN=$AUTO_PG_MAIN -fi +PG_LSCLUSTER_BIN=/usr/bin/pg_lsclusters RECOVERY_CONF_FILENAME=recovery.conf RECOVERY_CONF="" -PG_DEFAULT_PORT=5432 +PG_DEFAULT_PORT="" PG_DEFAULT_APP_NAME=$( hostname ) PG_DB="" CHECK_CUR_MASTER_XLOG=1 @@ -46,27 +43,30 @@ DEBUG=0 function usage () { cat << EOF Usage : $0 [-d] [-h] [options] - -u pg_user Specify Postgres user (Default : $PG_USER) - -b psql_bin Specify psql binary path (Default : $PSQL_BIN) - -m pg_main Specify Postgres main directory path - (By default, try to auto-detect it, on your system it : - $PG_MAIN) + -u pg_user Specify local Postgres user (Default: try to auto-detect or use $DEFAULT_PG_USER) + -b psql_bin Specify psql binary path (Default: $PSQL_BIN) + -B pg_lsclusters_bin Specify pg_lsclusters binary path (Default: $PG_LSCLUSTER_BIN) + -V pg_version Specify Postgres version (Default: try to auto-detect or use $DEFAULT_PG_VERSION) + -m pg_main Specify Postgres main directory path (Default: try to auto-detect or use + $DEFAULT_PG_MAIN) -r recovery_conf Specify Postgres recovery configuration file path - (Default : [PG_MAIN]/$RECOVERY_CONF_FILENAME) - -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 : $PG_DEFAULT_PORT) - -D dbname Specify DB name on Postgres master/slave to connect on (Default : PG_USER) + (Default: [PG_MAIN]/$RECOVERY_CONF_FILENAME) + -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 $DEFAULT_PG_PORT) + -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 XLOG file of the master host is the same - of the last replay XLOG file (Default : $CHECK_CUR_MASTER_XLOG) - -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) + of the last replay XLOG file (Default: $CHECK_CUR_MASTER_XLOG) + -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) -d Debug mode -h Show this message EOF exit 0 } -while getopts "hu:b:m:r:U:p:D:C:w:c:d" OPTION +while getopts "hu:b:B:V:m:r:U:p:D:C:w:c:d" OPTION do case $OPTION in u) @@ -75,6 +75,12 @@ do b) PSQL_BIN=$OPTARG ;; + B) + PG_LSCLUSTER_BIN=$OPTARG + ;; + V) + PG_VERSION=$OPTARG + ;; m) PG_MAIN=$OPTARG ;; @@ -111,6 +117,52 @@ do esac done +function debug() { + if [ $DEBUG -eq 1 ] + then + >&2 echo -e "[DEBUG] $1" + fi +} + +debug "Starting options (before handling auto-detection/default values) : +PG_VERSION = $PG_VERSION +PG_DB = $PG_DB +PG_USER = $PG_USER +PSQL_BIN = $PSQL_BIN +PG_LSCLUSTER_BIN = $PG_LSCLUSTER_BIN +PG_MAIN = $PG_MAIN +RECOVERY_CONF = $RECOVERY_CONF +PG_DEFAULT_PORT = $PG_DEFAULT_PORT +PG_DEFAULT_APP_NAME = $PG_DEFAULT_APP_NAME +CHECK_CUR_MASTER_XLOG = $CHECK_CUR_MASTER_XLOG +REPLAY_WARNING_DELAY = $REPLAY_WARNING_DELAY +REPLAY_CRITICAL_DELAY = $REPLAY_CRITICAL_DELAY +" + +# Auto-detect PostgreSQL information using pg_lsclusters +if [ -x "$PG_LSCLUSTER_BIN" ] +then + PG_CLUSTER=$( $PG_LSCLUSTER_BIN -h 2>/dev/null|head -n1 ) + if [ -n "$PG_CLUSTER" ] + then + debug "pg_lsclusters output:\n\t$PG_CLUSTER" + # Output example: + # 9.6 main 5432 online,recovery postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log + [ -z "$PG_VERSION" ] && PG_VERSION=$( echo "$PG_CLUSTER"|awk -F ' +' '{print $1}' ) + [ -z "$PG_DEFAULT_PORT" ] && PG_DEFAULT_PORT=$( echo "$PG_CLUSTER"|awk -F ' +' '{print $3}' ) + [ -z "$PG_USER" ] && PG_USER=$( echo "$PG_CLUSTER"|awk -F ' +' '{print $5}' ) + [ -z "$PG_MAIN" ] && PG_MAIN=$( echo "$PG_CLUSTER"|awk -F ' +' '{print $6}' ) + fi +else + debug "pg_lsclusters not found ($PG_LSCLUSTER_BIN): parameters auto-detection disabled" +fi + +# If auto-detection failed, use default values +[ -z "$PG_USER" ] && PG_USER="$DEFAULT_PG_USER" +[ -z "$PG_VERSION" ] && PG_VERSION="$DEFAULT_PG_VERSION" +[ -z "$PG_MAIN" ] && PG_MAIN="$DEFAULT_PG_MAIN" +[ -z "$PG_DEFAULT_PORT" ] && PG_DEFAULT_PORT="$DEFAULT_PG_PORT" + # Check PG_USER [ -z "$PG_USER" ] && echo "UNKNOWN : Postgres user not specified" && exit 3 id "$PG_USER" > /dev/null 2>&1 @@ -143,17 +195,12 @@ function psql_master_get () { echo "$sql"|sudo -u $PG_USER $PSQL_BIN -U $M_USER -h $M_HOST -w -p $M_PORT -d $PG_DB -t -P format=unaligned } -function debug() { - if [ $DEBUG -eq 1 ] - then - >&2 echo "[DEBUG] $1" - fi -} - debug "Running options : +PG_VERSION = $PG_VERSION PG_DB = $PG_DB PG_USER = $PG_USER PSQL_BIN = $PSQL_BIN +PG_LSCLUSTER_BIN = $PG_LSCLUSTER_BIN PG_MAIN = $PG_MAIN RECOVERY_CONF = $RECOVERY_CONF PG_DEFAULT_PORT = $PG_DEFAULT_PORT