This file is indexed.

/usr/sbin/watchdog_setup is in pgpool2 3.6.1-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env bash
#
# Copyright (c) 2013-2016 PgPool Global Development Group
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of the
# author not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission. The author makes no representations about the
# suitability of this software for any purpose.  It is provided "as
# is" without express or implied warranty.
#-------------------------------------------------------------------
# Set up watchdog enabled pgpool-II and PostgreSQL temporary
# installation in current directory for *testing* purpose.  Do not use
# this tool for production environment!
# Note that this script uses pgpool_setup as a work horse.
#
# usage: watchdog_setup [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n][-n num_clusters][-p base_port][--no-stop][-d]
# -wn num_pgpool: create num_pgpool pgpool nodes. The default is 3. Must be greater than 1.
# -wp watchdog_base_port: starting port number. The default is
#  50000.
#-------------------------------------------
# Configuration section
#-------------------------------------------
# Number of Pgpool-II installations.
W_NUM_PGPOOL=${W_NUM_PGPOOL:-"3"}

# Starting port number.
W_BASE_PORT=${W_BASE_PORT:-"50000"}

# PostgreSQL startig port number.
export PGBASEPORT=`expr $W_BASE_PORT + 1000`

# number of ports used in a single pgpool-II installation.
# (port, pcp_port, wd_port, wd_heartbeat_port)
num_ports_per_node=4

#-------------------------------------------
# End of configuration section
#-------------------------------------------
#
# user name
WHOAMI=`whoami`

# our root directory
BASEDIR=`pwd`

# location of pgpool_setup
PGPOOL_SETUP=${PGPOOL_SETUP:-"pgpool_setup"}

# PostgreSQL bin directory
INITDB=$PGBIN/initdb
PG_CTL=$PGBIN/pg_ctl
PSQL=$PGBIN/psql

#-------------------------------------------
# set postgresql.conf
# argument: PostgreSQL database cluster directory
#-------------------------------------------
function set_postgresql_conf
{
    PGCONF=$1/postgresql.conf

    echo "listen_addresses = '*'" >> $PGCONF
    echo "port = $PORT" >> $PGCONF
    echo "logging_collector = on" >> $PGCONF
    echo "log_filename = '%A.log'" >> $PGCONF
    echo "log_line_prefix = '%p %t '" >> $PGCONF
    echo "log_truncate_on_rotation = on" >> $PGCONF
    echo "log_statement = 'all'" >> $PGCONF
    echo "max_prepared_transactions = 10" >> $PGCONF
    echo "unix_socket_directories = '$PGSOCKET_DIR'" >> $PGCONF 

    if [ $MODE = "s" ];then
	echo "hot_standby = on" >> $PGCONF
	echo "wal_level = hot_standby" >> $PGCONF
	echo "max_wal_senders = $NUMCLUSTERS" >> $PGCONF
	echo "archive_mode = on" >> $PGCONF
	echo "archive_command = 'cp %p $BASEDIR/archivedir/%f </dev/null'" >> $PGCONF
    elif [ $MODE = 'r' ];then
	echo "wal_level = archive" >> $PGCONF
	echo "archive_mode = on" >> $PGCONF
	echo "archive_command = 'cp %p $BASEDIR/archivedir/%f </dev/null'" >> $PGCONF
    fi

    ed $1/pg_hba.conf <<EOF
/^#local *replication/s/^#//p
/^#host *replication/s/^#//p
/^#host *replication/s/^#//p
w
q
EOF

}

#-------------------------------------------
# set pgpool.conf
# argument: absolute path to pgpool.conf
#-------------------------------------------
function set_pgpool_conf {
    echo "sr_check_user = '$WHOAMI'" >> $CONF
    echo "recovery_user = '$WHOAMI'" >> $CONF
    echo "recovery_password = ''"  >> $CONF
    echo "recovery_1st_stage_command = 'basebackup.sh'" >> $CONF

    if [ $MODE = "r" ];then
	echo "recovery_2nd_stage_command = 'pgpool_recovery_pitr'" >> $CONF
    fi

    echo "health_check_period = 10" >> $CONF
    echo "health_check_user = '$WHOAMI'" >> $CONF
    OIDDIR=$BASEDIR/log/pgpool/oiddir
    mkdir -p $OIDDIR
    echo "memqcache_oiddir = '$OIDDIR'" >> $CONF
    echo "log_per_node_statement = on" >> $CONF

    if [ $MODE = "s" ];then
	echo "failover_command = '$FAILOVER_SCRIPT %d %h %p %D %m %M %H %P %r %R'" >> $CONF
    fi

    echo "socket_dir = '$PGSOCKET_DIR'" >> $CONF
    echo "pcp_socket_dir = '$PGSOCKET_DIR'" >> $CONF
}

#-------------------------------------------
# wait for pgpool comes up
#-------------------------------------------
function wait_for_pgpool_startup {
    timeout=20

    while [ $timeout -gt  0 ]
    do
	$PSQL -p $PGPOOL_PORT -c "show pool_nodes" postgres >/dev/null 2>&1
	if [ $? = 0 ];then
	    #		        echo "pgpool-II comes up after `expr 20 - $timeout` seconds"
	    break;
	fi
	timeout=`expr $timeout - 1`
	sleep 1
    done
}

#-------------------------------------------
# wait for pgpool reload finished
#-------------------------------------------
function wait_for_pgpool_reload {
    timeout=20
    num_node=$1

    while [ $timeout -gt  0 ]
    do
	N=`$PSQL -p $PGPOOL_PORT -c "show pool_status" test | grep backend_data | wc -l`
	if [ $N = $num_node ];then
	    break;
	fi
	timeout=`expr $timeout - 1`
	sleep 1
    done
}

#-------------------------------------------
# Set watchdog params to pgpool.conf
#-------------------------------------------
function set_watchdog_params {
    id=$1
    num_pgpool=$2
    base_port=$3
    priority=`expr $num_pgpool - $id`
    n=0
    conf=etc/pgpool.conf
    wd_port=`expr $base_port + \( $id \* $num_ports_per_node \) + 2`
    wd_heartbeat_port=`expr $wd_port + 1`

    cat >> $conf <<EOF
use_watchdog = on
wd_interval = 1
wd_priorty = $priority
wd_hostname = 'localhost'
wd_port = $wd_port
wd_heartbeat_port = $wd_heartbeat_port
EOF

    suf=0
    while [ $n -lt $num_pgpool ]
    do
	if [ $id != $n ];then
	    echo "heartbeat_destination$suf = 'localhost'" >> $conf
	    pgpool_port=`expr $base_port + \( $n \* $num_ports_per_node \)`
	    pcp_port=`expr $pgpool_port + 1`
	    wd_port=`expr $pcp_port + 1`
	    wd_heartbeat_port=`expr $wd_port + 1`
	    echo "heartbeat_destination_port$suf = $wd_heartbeat_port" >> $conf
	    echo "other_pgpool_hostname$suf = 'localhost'" >> $conf
	    echo "other_pgpool_port$suf = $pgpool_port" >> $conf
	    echo "other_wd_port$suf = $wd_port" >> $conf
	    suf=`expr $suf + 1`
	fi
	n=`expr $n + 1`
    done
}

#################################################################################
#
# main script
#
################################################################################
function usage()
{
    echo "usage: $0 [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n] [-n num_clusters] [-p base_port] [-pg pg_base_port][--no-stop] [-d]";exit 1
}

#-------------------------------------------
# Argument check
# usage: $0  [-wn num_pgpool][-wp watchdog_base_port][-m r|s|n][-n num_clusters][-p base_port][-pg pg_base_port][--no-stop][-d]
#-------------------------------------------
#
# default mode is streaming replication mode
MODE="s"
NO_STOP="false"

while [ $# -gt 0 ]
do
    if [ $1 = "-wn" ];then
	shift
	W_NUM_PGPOOL=$1
    elif [ $1 = "-wp" ];then
	shift
	W_BASE_PORT=$1
	PGBASEPORT=`expr $W_BASE_PORT + 1000`

	# rest are pgpool_setup args

    elif [ $1 = "-m" ];then
	shift
	case $1 in
	    r ) MODE="r";;
	    s ) MODE="s";;
	    n ) MODE="n";;
	    * ) usage;;
	esac
    elif [ $1 = "-n" ];then
	shift
	export NUMCLUSTERS=$1
    elif [ $1 = "-p" ];then
	shift
	export BASEPORT=$1
    elif [ $1 = "-pg" ];then
	shift
	export PGBASEPORT=$1
    elif [ $1 = "--no-stop" ];then
	shift
	NO_STOP="true"
    elif [ $1 = "-d" ];then
	export PGPOOLDEBUG="true"
	shift;
    elif [ $1 = "--help" -o $1 = "-o" ];then
	usage
	exit
    else
	usage
	exit
    fi
    shift
done

#-------------------------------------------
# Make sure that current directory is empty
#-------------------------------------------
if [ "`/bin/ls`" != "" ]
then
    echo "$0: Current directory is not empty. Please remove files and directories then try again."
    exit 1
fi

#-------------------------------------------
# everything looks good. starting setup...
#-------------------------------------------
echo "Satrting set up "

#-------------------------------------------
# Run pgpool_setup
#-------------------------------------------

STARTALL=$BASEDIR/startall
SHUTDOWNALL=$BASEDIR/shutdownall

cnt=0

while [ $cnt -lt $W_NUM_PGPOOL ]
do
    echo "============= setting up pgpool $cnt ============="
    mkdir pgpool$cnt
    cd pgpool$cnt
    port=`expr $W_BASE_PORT + \( $cnt \* $num_ports_per_node \)`
    $PGPOOL_SETUP -p $port -pg $PGBASEPORT
    set_watchdog_params $cnt $W_NUM_PGPOOL $W_BASE_PORT

    # remove "pg_ctl start" line from startall/shutdownall script in other than pgpool0
    if [ $cnt != 0 ]
    then
	sed -i '/.*data.*/d' startall
	sed -i '/.*data.*/d' shutdownall
    fi

    echo "cd pgpool$cnt" >> $STARTALL
    echo "./startall" >> $STARTALL
    echo "cd .." >> $STARTALL
    echo "cd pgpool$cnt" >> $SHUTDOWNALL
    echo "./shutdownall" >> $SHUTDOWNALL
    echo "cd .." >> $SHUTDOWNALL

    cd ..
    cnt=`expr $cnt + 1`
done