dd33e94
#!/bin/sh
dd33e94
# pgpool    This is the init script for starting up pgpool-II
dd33e94
#
bd76801
# chkconfig: - 64 36
dd33e94
# description: Starts and stops the pgpool daemon
dd33e94
# processname: pgpool
dd33e94
# pidfile:	/var/run/pgpool.pid
dd33e94
#
dd33e94
# v1.0.0 Devrim GUNDUZ <devrim@CommandPrompt.com>
dd33e94
# - Initial version of Red Hat / Fedora init script
572cfb2
#
572cfb2
# v2.2 Devrim GUNDUZ <devrim@CommandPrompt.com>
572cfb2
# - New and improved version which has some fixes.
ef8d61e
#
ef8d61e
# v2.2.5 Devrim GUNDUZ <devrim@CommandPrompt.com>
ef8d61e
# - Fix logging.
bd3d3b6
bd3d3b6
if [ -r /etc/sysconfig/pgpool ]; then
bd3d3b6
    . /etc/sysconfig/pgpool
bd3d3b6
fi
bd3d3b6
dd33e94
# Source function library.
dd33e94
INITD=/etc/rc.d/init.d
dd33e94
. $INITD/functions
dd33e94
dd33e94
# Get function listing for cross-distribution logic.
dd33e94
TYPESET=`typeset -f|grep "declare"`
dd33e94
dd33e94
# Get config.
dd33e94
. /etc/sysconfig/network
dd33e94
dd33e94
# Check that networking is up.
dd33e94
# We need it for pgpool
dd33e94
[ "${NETWORKING}" = "no" ] && exit 0
dd33e94
dd33e94
# Find the name of the script
bd3d3b6
NAME=`basename $0`
bd3d3b6
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
bd3d3b6
then
bd3d3b6
	NAME=${NAME:3}
bd3d3b6
fi
dd33e94
ef8d61e
# For SELinux we need to use 'runuser' not 'su'
ef8d61e
if [ -x /sbin/runuser ]
ef8d61e
then
ef8d61e
    SU=runuser
ef8d61e
else
ef8d61e
    SU=su
ef8d61e
fi
ef8d61e
dd33e94
# Set defaults for configuration variables
dd33e94
PGPOOLENGINE=/usr/bin
dd33e94
PGPOOLDAEMON=$PGPOOLENGINE/pgpool
bd3d3b6
PGPOOLCONF=/etc/pgpool-II/pgpool.conf
bd3d3b6
PGPOOLPIDDIR=/var/run/pgpool
dd33e94
PGPOOLLOG=/var/log/pgpool.log
dd33e94
dd33e94
test -x $PGPOOLDAEMON || exit 5
dd33e94
bd3d3b6
# Check whether the config file exists or not
bd3d3b6
if [ ! -r $PGPOOLCONF ]
dd33e94
then
dd33e94
	echo "$PGPOOLCONF not found"
dd33e94
	RETVAL=1
dd33e94
	failure	
dd33e94
	exit
dd33e94
fi
dd33e94
bd3d3b6
# Create the log file if it does not exist
bd3d3b6
if [ ! -x $PGPOOLLOG ]
bd3d3b6
then
bd3d3b6
	touch $PGPOOLLOG
bd3d3b6
	chown postgres: $PGPOOLLOG
bd3d3b6
fi
bd3d3b6
bd3d3b6
if [ ! -d $PGPOOLPIDDIR ]
bd3d3b6
then
bd3d3b6
	mkdir $PGPOOLPIDDIR
bd3d3b6
	chown postgres: $PGPOOLPIDDIR
bd3d3b6
fi
bd3d3b6
dd33e94
script_result=0
dd33e94
dd33e94
start(){
bd3d3b6
	pid=`pidof -s "$PGPOOLDAEMON"`
bd3d3b6
	if [ $pid ]
bd3d3b6
	then
bd3d3b6
		echo "pgpool is already running with pid $pid"
bd3d3b6
		failure "$PGPOQL_START"
bd3d3b6
		echo
bd3d3b6
		script_result=1
bd3d3b6
		exit 1
bd3d3b6
	fi
bd3d3b6
dd33e94
	PGPOOL_START=$"Starting ${NAME} service: "
dd33e94
dd33e94
	echo -n "$PGPOOL_START"
bd3d3b6
ef8d61e
	$SU -l postgres -c "$PGPOOLDAEMON -f $PGPOOLCONF $OPTS & " >> "$PGPOOLLOG" 2>&1 < /dev/null
dd33e94
	sleep 2
dd33e94
dd33e94
	pid=`pidof -s "$PGPOOLDAEMON"`
dd33e94
	if [ $pid ] 
dd33e94
	then
dd33e94
		success "$PGPOOL_START"
dd33e94
		touch /var/lock/subsys/${NAME}
dd33e94
		echo
dd33e94
	else
bd3d3b6
		failure "$PGPOQL_START"
dd33e94
		echo
dd33e94
		script_result=1
dd33e94
	fi
dd33e94
}
dd33e94
dd33e94
stop(){
dd33e94
	echo -n $"Stopping ${NAME} service: "
dd33e94
        if [ $UID -ne 0 ]; then
dd33e94
                RETVAL=1
dd33e94
                failure
dd33e94
        else
bd3d3b6
		killproc /usr/bin/pgpool
bd3d3b6
#		$PGPOOLDAEMON stop & >> "$PGPOOLLOG" 2>&1 < /dev/null
dd33e94
                RETVAL=$?
dd33e94
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/${NAME}
dd33e94
        fi;
dd33e94
        echo
dd33e94
        return $RETVAL
dd33e94
}
dd33e94
dd33e94
switch() {
dd33e94
	echo -n $"Sending switchover request to $NAME "
bd3d3b6
	$PGPOOLDAEMON -f $PGPOOLCONF switch >> "$PGPOOLLOG" 2>&1 < /dev/null
dd33e94
	RETVAL=$?
dd33e94
	echo
dd33e94
	if [ $RETVAL -eq 0 ] 
dd33e94
	then
dd33e94
		echo_success
dd33e94
	else
dd33e94
		echo_failure
dd33e94
	fi
dd33e94
	echo
dd33e94
}
dd33e94
dd33e94
restart(){
dd33e94
    stop
dd33e94
    start
dd33e94
}
dd33e94
dd33e94
reload(){
bd3d3b6
	echo -n $"Reloading ${NAME}: "
bd3d3b6
bd3d3b6
	if [ -n "`pidfileofproc $PGPOOLDAEMON`" ] ; then
bd3d3b6
		killproc $PGPOOLDAEMON -HUP
bd3d3b6
	else
bd3d3b6
		failure $"Reloading ${NAME}"
bd3d3b6
	fi
bd3d3b6
	RETVAL=$?
bd3d3b6
	echo
dd33e94
}
dd33e94
dd33e94
condrestart(){
bd3d3b6
	[ -e /var/lock/subsys/${NAME} ] && restart
dd33e94
}
dd33e94
dd33e94
condstop(){
bd3d3b6
	[ -e /var/lock/subsys/${NAME} ] && stop
dd33e94
}
dd33e94
dd33e94
# See how we were called.
dd33e94
case "$1" in
dd33e94
  start)
dd33e94
        start
dd33e94
        ;;
dd33e94
  stop)
dd33e94
        stop
dd33e94
        ;;
dd33e94
  switch)
dd33e94
        switch
dd33e94
        ;;
dd33e94
  status)
dd33e94
        status pgpool
dd33e94
        script_result=$?
dd33e94
        ;;
dd33e94
  restart)
dd33e94
        restart
dd33e94
        ;;
dd33e94
  reload|force-reload)
dd33e94
        reload
dd33e94
        ;;
dd33e94
  condrestart)
dd33e94
        condrestart
dd33e94
        ;;
dd33e94
  condstop)
dd33e94
        condstop
dd33e94
        ;;
dd33e94
  *)
dd33e94
        echo $"Usage: $0 {start|stop|switch|status|restart|condrestart|condstop|reload|force-reload}"
dd33e94
        exit 1
dd33e94
esac
dd33e94
dd33e94
exit $script_result