[Linux-HA] PostgreSQL RA

Lars Marowsky-Bree lmb at suse.de
Thu Jun 22 16:00:33 MDT 2006


On 2006-06-22T14:42:43, Serge Dubrouski <sergeyfd at gmail.com> wrote:

> Hello -
> 
> Attached is RA for PostgreSQL. I'm glad to share it to the community
> if anybody is interested.

Cool, please do!

Some points:

> #!/bin/sh
> #
> # Description:	Manages a PostrgreSQL Server as an OCF High-Availability
> #		resource under Heartbeat/LinuxHA control
> #
> #
> # Author:	Serge Dubrouski

What's the license?

> #
> # OCF parameters:
> #  OCF_RESKEY_pgctl  - Path to pg_ctl. Default /usr/bin/pg_ctl
> #  OCF_RESKET_start_opt - Startup options. Default ""
> #  OCF_RESKEY_psql   - Path to psql. Defauilt is /usr/bin/psql
> #  OCF_RESKEY_pgdata - PGDATA directory. Default is /var/lib/pgsql/data
> #  OCF_RESKEY_pgdba  - userID that manages DB. Default is postgres
> #  OCF_RESKEY_pgdb   - database to monitor. Default is template1

Doesn't match the parameters you list in the metadata section.

> unset LC_ALL; export LC_ALL
> unset LANGUAGE; export LANGUAGE
> 
> prefix=/usr/local
> exec_prefix=/usr/local
> SH=/bin/sh

Should probably be filled in by configure macros.

The handling of "usage" and "methods" is also slightly confusing, I
mean, it's a static list, why not put it into usage as such too?

> runasowner() {
>     if [ $US = $PGDBA ]
>     then
> 	$SH -c ". /home/${PGDBA}/.bash_profile; $*"
>     else
> 	su $PGDBA -c ". /home/${PGDBA}/.bash_profile; $*"
>     fi

Hard-coding the path to /home/* is probably not a good idea.

> #pgsql_stop: Stop PostgreSQL	
> pgsql_stop() {
>     if ! pgsql_status
>     then
>         #Already stopped
>         return $OCF_SUCCESS
>     fi

Is there anything here - shared memory segments or some such thing -
which might require cleaning up before it can be started again?

>     if [ -x $PGCTL ]
>     then
>         # Stop PostgreSQL do not wait for clients to disconnect
> 	runasowner "$PGCTL -D $PGDATA stop -m fast > /dev/null 2>&1"
>     else
> 	ocf_log err "$PGCTL not found!"
>        # Can't return error status because Linux-HA doesn't support number
>        # of retries for an action and will keep trying to stop a service
>        # indefinitealy
>         return $OCF_SUCCESS
>     fi

Actuallt not true, when we attempt to stop you and you've got stonith
configured AND stopping fails, guess which big hammer is going to come
out next ;-)

Anyway, you're verifying that PGCTL exists already before stop can ever
be called, this check isn't required here.

>     if pgsql_status
>     then
>        #PostgreSQL is still up. Use another shutdown mode.
>        runasowner "$PGCTL -D $PGDATA stop -m immediate > /dev/null 2>&1"
>     fi
> 
>     return $OCF_SUCCESS
> }
> 
> #
> # pgsql_status: is PostgreSQL up?
> #
> 
> pgsql_status() {
>     if [ -f $PIDFILE  ]
>     then
>         process_running `head -n 1 $PIDFILE`
>     else
>         : No pid file
>         false
>     fi
> }
> 
> 
> #
> # return TRUE if a process with given PID is running
> #
> process_running() {
>     PID=$1
>     # Use /proc if it looks like it's here...
>     if [ -d /proc -a -d /proc/1 ]
>     then
>         [ -d /proc/$PID ]
>     else
>         kill -0 "$PID" >/dev/null 2>&1
>     fi

This whole effort is probably quite pointless, kill -0 right away is
much better - if anything, it'd be worth validating that this process
is, in fact, postgres ;-)

> #
> # pgsql_monitor
> #
> 
> pgsql_monitor() {
>     if ! pgsql_status
>     then
> 	ocf_log info "PostgreSQL is down"
> 	return $OCF_NOT_RUNNING
>     fi
>     
>     $PSQL -U $PGDBA $PGDB -c 'select now();' >/dev/null 2>&1
>     
>     if [ $? -ne  0 ]
>     then
> 	ocf_log err "PostgreSQL $PGDB isn't running"
> 	return $OCF_ERR_GENERIC
>     fi
>     
>     return $OCF_SUCCESS
> }
> 
> #
> #   'main' starts here...
> #
> 
> 
> if [ $# -ne 1 ]
> then
>     usage
>     exit 1
> fi
> 
> US=`id -u -n`
> US=`echo $US`

What's that second "echo" statement for?

> if
>     [ $US != root ]
> then
>     ocf_log err "$0 must be run as root"
>     exit 1
> fi
> 
> PGCTL=${OCF_RESKEY_pgctl:-/usr/bin/pg_ctl}
> STARTOPT=${OCF_RESKEY_start_opt:-""}
> PSQL=${OCF_RESKEY_psql:-/usr/bin/psql}
> PGDATA=${OCF_RESKEY_pgdata:-/var/lib/pgctl/data}
> PGDBA=${OCF_RESKEY_pgdba:-postgres}
> PGDB=${OCF_RESKEY_pgdb:-template1}
> PIDFILE=${PGDATA}/postmaster.pid
> 
> if [ ! -x $PGCTL ]
> then
>     ocf_log err "Can't run $PGCTL"
>     exit $OCF_ERR_GENERIC
> fi
>     
> if [ ! -x $PSQL ]
> then
>     ocf_log err "Can't run $PSQL"
>     exit $OCF_ERR_GENERIC
> fi
> 
> # What kind of method was invoked?
> case "$1" in
>     start)      pgsql_start               
>                 exit $?;;
> 
>     stop)       pgsql_stop
>                 exit $?;;
> 
>     status)     if pgsql_status
>                 then
>                     ocf_log info "PostgreSQL is up"
>                     exit $OCF_SUCCESS
>                 else
>                     ocf_log info "PostgreSQL is down"
>                     exit $OCF_NOT_RUNNING
>                 fi
>                 exit $?;;
> 
>     monitor)    pgsql_monitor
>                 exit $?;;
> 
>     methods)    pgsql_methods
>                 exit $?;;
> 		
>     meta_data)  meta_data
>                 exit $OCF_SUCCESS;;
> esac

I'd add a catch-all "*)" to catch wrong operations.


Sincerely,
    Lars Marowsky-Brée

-- 
High Availability & Clustering
SUSE Labs, Research and Development
SUSE LINUX Products GmbH - A Novell Business	 -- Charles Darwin
"Ignorance more frequently begets confidence than does knowledge"




More information about the Linux-HA mailing list