690811ccfb445248e9cd06479c4c5a88fe1ecae9
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

1) #!/bin/bash
2) #
3) # Nagios plugin to check Postgresql streamin replication state
4) # 
5) # Could be use on Master or on standby node
6) #
7) # Requirement : 
8) #
Benjamin Renard Improve help message and docs

Benjamin Renard authored 6 years ago

9) #     On master node : Slaves must be able to connect with user from recovery.conf
10) #                      to database with the same name (or another specify with -D)
11) #                      as trust (or via md5 using password specify in ~/.pgpass).
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

12) #
Benjamin Renard Improve help message and docs

Benjamin Renard authored 6 years ago

13) #     On standby node : PG_USER must be able to connect localy on the database
14) #                       with the same name (or another specify with -D) as trust
15) #                       (or via md5 using password specify in ~/.pgpass).
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

16) #
17) # Author : Benjamin Renard <brenard@easter-eggs.com>
Benjamin Renard Improve help message and docs

Benjamin Renard authored 6 years ago

18) # Date : Fri, 25 Aug 2017 15:57:57 +0200
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

19) # Source : http://git.zionetrix.net/check_pg_streaming_replication
20) #
21) 
22) PG_USER=postgres
Benjamin Renard Add -U parameter to provide...

Benjamin Renard authored 6 years ago

23) PG_MASTER_USER=""
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

24) PSQL_BIN=/usr/bin/psql
25) PG_MAIN=/var/lib/postgresql/9.1/main
Benjamin Renard Try to auto-detect PG_MAIN...

Benjamin Renard authored 6 years ago

26) if [ -f /etc/debian_version ]
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

27) then
Benjamin Renard Try to auto-detect PG_MAIN...

Benjamin Renard authored 6 years ago

28) 	AUTO_PG_MAIN=$( ls -1d /var/lib/postgresql/9*/main 2> /dev/null|sort -n|tail -n 1 )
29) 	[ -n "$AUTO_PG_MAIN" -a -d "$AUTO_PG_MAIN" ] && PG_MAIN=$AUTO_PG_MAIN
30) elif [ -f /etc/redhat-release ]
31) then
32) 	AUTO_PG_MAIN=$( ls -1d /var/lib/pgsql/9*/data 2> /dev/null|sort -n|tail -n 1 )
33) 	[ -n "$AUTO_PG_MAIN" -a -d "$AUTO_PG_MAIN" ] && PG_MAIN=$AUTO_PG_MAIN
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

34) fi
35) RECOVERY_CONF_FILENAME=recovery.conf
36) RECOVERY_CONF=""
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

37) PG_DEFAULT_PORT=5432
Benjamin Renard Use PG_DB also on slave and...

Benjamin Renard authored 6 years ago

38) PG_DB=""
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

39) 
40) DEBUG=0
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

41) 
42) function usage () {
43) 	cat << EOF
44) Usage : $0 [-d] [-h] [options]
45) 	-u pg_user		Specify Postgres user (Default : $PG_USER)
46) 	-b psql_bin		Specify psql binary path (Default : $PSQL_BIN)
47) 	-m pg_main		Specify Postgres main directory path
Benjamin Renard Try to auto-detect PG_MAIN...

Benjamin Renard authored 6 years ago

48) 				(By default, try to auto-detect it, on your system it :
49)                                 $PG_MAIN)
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

50) 	-r recovery_conf	Specify Postgres recovery configuration file path
Benjamin Renard Improve help message and docs

Benjamin Renard authored 6 years ago

51) 				(Default : [PG_MAIN]/$RECOVERY_CONF_FILENAME)
Benjamin Renard Add -U parameter to provide...

Benjamin Renard authored 6 years ago

52) 	-U pg_master_user	Specify Postgres user to use on master (Default : user from recovery.conf file)
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

53) 	-p pg_port		Specify default Postgres master TCP port (Default : $PG_DEFAULT_PORT)
Benjamin Renard Use PG_DB also on slave and...

Benjamin Renard authored 6 years ago

54) 	-D dbname		Specify DB name on Postgres master/slave to connect on (Default : PG_USER)
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

55) 	-d			Debug mode
56) 	-h 			Show this message
57) EOF
58) 	exit 0
59) }
60) 
Benjamin Renard Add -U parameter to provide...

Benjamin Renard authored 6 years ago

61) while getopts "hu:b:m:r:U:p:D:d" OPTION
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

62) do
63) 	case $OPTION in
64) 		u)
65) 			PG_USER=$OPTARG
66) 		;;
67) 		b)
68) 			PSQL_BIN=$OPTARG
69) 		;;
70) 		m)
71) 			PG_MAIN=$OPTARG
72) 		;;
73) 		r)
74) 			RECOVERY_CONF=$OPTARG
75) 		;;
Benjamin Renard Add -U parameter to provide...

Benjamin Renard authored 6 years ago

76) 		U)
77) 			PG_MASTER_USER=$OPTARG
78) 		;;
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

79) 		p)
80) 			PG_DEFAULT_PORT=$OPTARG
81) 		;;
Benjamin Renard Add -D parameter and retrei...

Benjamin Renard authored 6 years ago

82) 		D)
83) 			PG_DB=$OPTARG
84) 		;;
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

85) 		d)
86) 			DEBUG=1
87) 		;;
88) 		h)
89) 			usage
90) 		;;
91) 		\?)
92) 			echo -n "Unkown option"
93) 			usage
94) 	esac
95) done
96) 
97) # Check PG_USER
98) [ -z "$PG_USER" ] && echo "UNKNOWN : Postgres user not specify" && exit 3
99) id "$PG_USER" > /dev/null 2>&1
100) [ $? -ne 0 ] && echo "UNKNOWN : Invalid Postgres user ($PG_USER)" && exit 3
101) 
102) # Check PSQL_BIN
103) [ ! -x "$PSQL_BIN" ] && echo "UNKNOWN : Invalid psql bin path ($PSQL_BIN)" && exit 3
104) 
105) # Check PG_MAIN
106) [ ! -d "$PG_MAIN/" ] && echo "UNKNOWN : Invalid Postgres main directory path ($PG_MAIN)" && exit 3
107) 
108) # Check RECOVERY_CONF
109) [ -z "$RECOVERY_CONF" ] && RECOVERY_CONF="$PG_MAIN/$RECOVERY_CONF_FILENAME"
110) 
111) # Check PG_DEFAULT_PORT
112) [ $( echo "$PG_DEFAULT_PORT"|grep -c -E '^[0-9]*$' ) -ne 1 ] && "UNKNOWN : Postgres default master TCP port must be an integer." && exit 3
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

113) 
Benjamin Renard Use PG_DB also on slave and...

Benjamin Renard authored 6 years ago

114) # If PG_DB is not provided with -D parameter, use PG_USER as default value
115) [ -z "$PB_DB" ] && PG_DB="$PG_USER"
116) 
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

117) function psql_get () {
Benjamin Renard Use PG_DB also on slave and...

Benjamin Renard authored 6 years ago

118) 	echo "$1"|su - $PG_USER -c "$PSQL_BIN -d "$PG_DB" -t -P format=unaligned"
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

119) }
120) 
121) function debug() {
122) 	if [ $DEBUG -eq 1 ]
123) 	then
124) 		echo "[DEBUG] $1"
125) 	fi
126) }
127) 
Benjamin Renard Manage options

Benjamin Renard authored 9 years ago

128) debug "Running options :
129) PG_USER = $PG_USER
130) PSQL_BIN = $PSQL_BIN
131) PG_MAIN = $PG_MAIN
132) RECOVERY_CONF = $RECOVERY_CONF
133) PG_DEFAULT_PORT = $PG_DEFAULT_PORT"
134) 
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

135) # Postgres is running ?
136) if [ $DEBUG -eq 0 ]
137) then
138) 	psql_get '\q' 2> /dev/null
139) else
140) 	psql_get '\q'
141) fi
142) if [ $? -ne 0 ]
143) then
144) 	echo "CRITICAL : Postgres is not running !"
145) 	exit 2
146) fi
147) debug "Postgres is running"
148) 
149) RECOVERY_MODE=0
150) [ $( psql_get 'SELECT pg_is_in_recovery();' ) == "t" ] && RECOVERY_MODE=1
151) 
152) if [ -f $RECOVERY_CONF ]
153) then
154) 	debug "File recovery.conf found. Hot-standby mode."
155) 	
156) 	# Check recovery mode
157) 	if [ $RECOVERY_MODE -ne 1 ]
158) 	then
159) 		echo "CRITICAL : Not in recovery mode while recovery.conf file found !"
160) 		exit 2
161) 	fi
162) 	debug "Postgres is in recovery mode"
163) 
164) 	LAST_XLOG_RECEIVE=$( psql_get "SELECT pg_last_xlog_receive_location()" )
165) 	debug "Last xlog file receive : $LAST_XLOG_RECEIVE"
166) 	LAST_XLOG_REPLAY=$( psql_get "SELECT pg_last_xlog_replay_location()" )
167) 	debug "Last xlog file replay : $LAST_XLOG_REPLAY"
168) 
169) 
170) 	# Get master connection informations from recovery.conf file
171) 	MASTER_CONN_INFOS=$( egrep '^ *primary_conninfo' $RECOVERY_CONF|sed "s/^ *primary_conninfo *= *[\"\']\([^\"\']*\)[\"\'].*$/\1/" )
172) 	if [ ! -n "$MASTER_CONN_INFOS" ]
173) 	then
174) 		echo "UNKNOWN : Can't retreive master connection informations form recovery.conf file"
175) 		exit 3
176) 	fi
177) 	debug "Master connection informations : $MASTER_CONN_INFOS"
178) 
179) 	M_HOST=$( echo "$MASTER_CONN_INFOS"|sed 's/^.*host= *\([^ ]*\) *.*$/\1/' )
180) 	if [ ! -n "$M_HOST" ]
181) 	then
182) 		echo "UNKNOWN : Can't retreive master host from recovery.conf file"
183) 		exit 3
184) 	fi
185) 	debug "Master host : $M_HOST"
186) 
187) 	M_PORT=$( echo "$MASTER_CONN_INFOS"|sed 's/^.*port= *\([^ ]*\) *.*$/\1/' )
188) 	if [ ! -n "$M_PORT" ]
189) 	then
190) 		debug "Master port not specify, use default : $PG_DEFAULT_PORT"
191) 		M_PORT=$PG_DEFAULT_PORT
192) 	else
193) 		debug "Master port : $M_PORT"
194) 	fi
Benjamin Renard Add -D parameter and retrei...

Benjamin Renard authored 6 years ago

195) 
Benjamin Renard Add -U parameter to provide...

Benjamin Renard authored 6 years ago

196) 	if [ -n "$PG_MASTER_USER" ]
Benjamin Renard Add -D parameter and retrei...

Benjamin Renard authored 6 years ago

197) 	then
Benjamin Renard Add -U parameter to provide...

Benjamin Renard authored 6 years ago

198) 		debug "Master user provided by command-line, use it : $PG_MASTER_USER"
199) 		M_USER="$PG_MASTER_USER"
Benjamin Renard Add -D parameter and retrei...

Benjamin Renard authored 6 years ago

200) 	else
Benjamin Renard Add -U parameter to provide...

Benjamin Renard authored 6 years ago

201) 		M_USER=$( echo "$MASTER_CONN_INFOS"|sed 's/^.*user= *\([^ ]*\) *.*$/\1/' )
202) 		if [ ! -n "$M_USER" ]
203) 		then
204) 			debug "Master user not specify, use default : $PG_USER"
205) 			M_USER=$PG_USER
206) 		else
207) 			debug "Master user : $M_USER"
208) 		fi
Benjamin Renard Add -D parameter and retrei...

Benjamin Renard authored 6 years ago

209) 	fi
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

210) 	
211) 	# Get current xlog file from master
Benjamin Renard Add -D parameter and retrei...

Benjamin Renard authored 6 years ago

212) 	M_CUR_XLOG="$( echo 'SELECT pg_current_xlog_location()'|su - $PG_USER -c "$PSQL_BIN -U $M_USER -h $M_HOST -p $M_PORT -d $PG_DB -t -P format=unaligned" )"