AW: Postgres resource script

"Martin Bene" <[email protected]> Wed, 14 Aug 2002 09:35:10 +0200
Newsgroups gmane.linux.failsafe
Message-ID <[email protected]>
Hi Tabitha,

> Does anyone have a postgres resource script? 
> I am trying to fiqure out how to write one but not having 
> much luck. 

Yes, I've written a postgres resource script; current status: "Tested on my
system, works for me". I've submitted a patch to Lars for inclusion into CVS
but it hasn't yet been included. 

If you just want to add the resource to an existing installation, I'll
include a seperate file that you can untar in
/usr/lib/failsafe/resource_types. after untar, you'll need to install the new
resource type using the cmgr comandline interface (install resource_type
postgres in cluster <your_cluster_name>)

Bye, Martin

Here's the original mail again:

> -----Ursprüngliche Nachricht-----
> Von: Martin Bene 
> Gesendet: Donnerstag, 09. Mai 2002 22:49
> An: Lars Marowsky-Bree
> Cc: [email protected]
> Betreff: new postgres resource script
> 
> 
> Hi Lars,
> 
> The patch below adds a new resource type for postgres 
> databases. it's not excessively well tested, but works for 
> me. monitoring is rudimentary at the moment (only checks that 
> the server is running, doesn't try to execute any commands). 
> 
> the patch does not touch the rpm spec file, so the new files 
> won't get included in any RPMs you build; same goes for the 
> previous patches for drbd btw.
> 
> The 2nd patch just adds the resource files to the rpm spec so 
> you get drbd and postgres resource types included.
> 
> 
> Bye, Martin
>
postgres.diff (application/octet-stream, 14 KB)
diff -urN cvs/cmd/srm/scripts/Makefile cluster_services/cmd/srm/scripts/Makefile
--- cvs/cmd/srm/scripts/Makefile	Thu May  9 22:11:33 2002
+++ cluster_services/cmd/srm/scripts/Makefile	Thu May  9 21:56:01 2002
@@ -65,6 +65,7 @@
 	lprng \
 	ICP_volume \
 	LVM_volume \
+	postgres \
         $(NULL)
 
 # All targets descend into the subdirectories listed in $(SUBDIRS).
diff -urN cvs/cmd/srm/scripts/postgres/Makefile cluster_services/cmd/srm/scripts/postgres/Makefile
--- cvs/cmd/srm/scripts/postgres/Makefile	Thu Jan  1 01:00:00 1970
+++ cluster_services/cmd/srm/scripts/postgres/Makefile	Thu May  9 21:59:24 2002
@@ -0,0 +1,59 @@
+#!gmake
+#
+# Makefile for CI/cmd/srm/scripts/postgres
+#
+# Copyright (c) 2001 SuSE Linux AG.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of version 2 of the GNU General Public License as
+# published by the Free Software Foundation.
+# 
+# This program is distributed in the hope that it would be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# 
+# Further, this software is distributed without any warranty that it is
+# free of the rightful claim of any third person regarding infringement
+# or the like.  Any license provided herein, whether implied or
+# otherwise, applies only to this software file.  Patent licenses, if
+# any, provided herein do not apply to combinations of this program with
+# other software, or any other product whatsoever.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
+# 
+# Contact information: Lars Marowsky-Bree <[email protected]>
+
+#
+# Every Makefile must define TOPDIR to be the path to the top of
+# the ISM. In this case the top is one directory up. The include/builddefs file
+# must then be included.
+#
+
+TOPDIR = ../../../../..
+include $(TOPDIR)/include/builddefs
+
+AGENT = postgres
+SCRIPTS = control create_resource_type
+
+LSOURCES = $(SCRIPTS) $(AGENT)
+
+all headers exports: 
+
+install:
+	$(INSTALL) -d $(CI_RESOURSE_DIR)/$(AGENT)
+	$(INSTALL) -m 755 $(SCRIPTS) $(CI_RESOURSE_DIR)/$(AGENT)
+	$(INSTALL) $(AGENT) $(CI_RESOURSE_DIR)/$(AGENT)
+	$(INSTALL) -S control $(CI_RESOURSE_DIR)/$(AGENT)/start
+	$(INSTALL) -S control $(CI_RESOURSE_DIR)/$(AGENT)/stop
+	$(INSTALL) -S control $(CI_RESOURSE_DIR)/$(AGENT)/restart
+	$(INSTALL) -S control $(CI_RESOURSE_DIR)/$(AGENT)/monitor
+	$(INSTALL) -S control $(CI_RESOURSE_DIR)/$(AGENT)/exclusive
+
+#
+# Always include ismrules using the $(BUILDRULES) macro anywhere after
+# the first target is defined.
+#
+include $(BUILDRULES)
+
diff -urN cvs/cmd/srm/scripts/postgres/control cluster_services/cmd/srm/scripts/postgres/control
--- cvs/cmd/srm/scripts/postgres/control	Thu Jan  1 01:00:00 1970
+++ cluster_services/cmd/srm/scripts/postgres/control	Thu May  9 21:56:46 2002
@@ -0,0 +1,212 @@
+#!/bin/sh
+# 
+# Control script for postgres resource
+#
+# Copyright (C) 2001 SuSE Linux AG
+#
+# Author: Joachim Gleissner <[email protected]>
+#         originaly for oracle resource type
+#         modified for postgres by Martin Bene <[email protected]>
+#
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+# set some global variables
+test -z "$FAILSAFE_DIR" && FAILSAFE_DIR=/usr/lib/failsafe
+test -z "$SCRIPTLIB" && SCRIPTLIB=$FAILSAFE_DIR/common_scripts/scriptlib2.sh
+
+# load the common lib
+. $SCRIPTLIB || exit -1
+
+
+is_db_running()
+{
+    # first test, check for sockfile with active process
+    if [ ! -S $sockfile ]; then
+	s2_log_debug "Socket file $sockfile doesn't exist. Assuming $resource_name is down."
+	return 1
+    fi
+
+    # now check if there's an active process on the socket
+    if ! $FUSER $sockfile > /dev/null ; then
+	s2_log "No process listening on $sockfile. Assuming stale sockfile, resource $resource_name is down."
+	return 1
+    fi
+
+    return 0
+}
+
+start_db()
+{
+    if [ -f "$sockfile" ] ; then
+        s2_log "removing stale lockfile $sockfile for resource $resource_name"
+        rm -f $sockfile
+    fi
+
+    if [ -f "$data/postmaster.pid" ]; then
+        s2_log "removing old pid file $data/postmaster.pid for resource $resource_name"
+        rm -f $data/postmaster.pid
+    fi
+
+    if [ ! -x $base/bin/postmaster -o ! -x $base/bin/pg_ctl ] ; then
+        s2_log "postmaster or pg_ctl executable not found in $base/bin, aborting"
+        return 1
+    fi
+
+    if [ ! -d $data/base ] ; then
+        s2_log "Database directory $data/base not found, aborting"
+        return 1
+    fi
+
+    if $FUSER -s -n tcp $port ; then
+	s2_log "No postmaster socket, but tcp port $port in use, aborting"
+	return 1
+    fi
+
+    if [ ! -d /var/log/postgres ] ; then
+	mkdir -p /var/log/postgres
+	chown $AdmUser /var/log/postgres
+	if [ ! -d /var/log/postgres ] ; then
+	    s2_log "Can't create postgres log directory /var/log/postgres, aborting"
+	    return 1
+	fi
+    fi
+
+    CMD="$base/bin/pg_ctl start -D $data -l /var/log/postgres/$resource_name.log"
+    s2_log_debug "starting postmaster using $CMD"
+    s2_run_command -u $AdmUser -t 60 "$CMD" "starting postgres database"
+    # wait for postmaster to come up
+    local retry=12
+    while [ $retry -ge 0 ] && ! is_db_running ; do
+	sleep 5
+	retry=$[retry-1]
+    done
+
+    local exit_status
+    if is_db_running ; then
+	s2_log_debug "postgres database started for resource $resource_name"
+	exit_status=0
+    else
+	s2_log "postgres database startup failed for resource $resource_name"
+	exit_status=1
+    fi
+
+    return $exit_status
+}
+
+process_resource()
+{
+    action=$1
+    resource_name=$2
+    resource_attributes="$3"
+
+    # get needed fuser util.
+    FUSER=`which fuser 2>/dev/null`
+    if [ -z "$FUSER" ]; then
+        if [ -x /bin/fuser ]; then
+            FUSER=/bin/fuser
+        elif [ -x /sbin/fuser ]; then
+           FUSER=/sbin/fuser
+        else
+	   s2_log "fuser command not found, aborting"
+           s2_write_resource_status $HA_CMD_FAILED $resource_name
+	   return
+        fi
+    fi
+
+
+    # get attributes
+    port=$(s2_get_attribute_value port "$resource_attributes")
+    sockfile=$(s2_get_attribute_value sockfile "$resource_attributes")
+    data=$(s2_get_attribute_value data "$resource_attributes")
+    base=$(s2_get_attribute_value base "$resource_attributes")
+    AdmUser=$(s2_get_attribute_value AdmUser "$resource_attributes")
+
+    exit_status=0
+
+    case $action in
+
+	start)
+	    if ! is_db_running ; then
+		start_db
+		exit_status=$?
+	    else
+		s2_log "postgres already running for resource $resource_name"
+	    fi
+	    ;;
+
+	stop)
+	    # try nicely
+	    if is_db_running ; then
+		CMD="$base/bin/pg_ctl stop -D $data" 
+		s2_log_debug "stopping postmaster (smart) for resource $resource_name"
+	        s2_run_command -t 30 "$CMD -m smart" "stopping postmaster (smart)"
+	        if is_db_running ; then
+	            # try again
+		    s2_log "stopping postmaster (fast) for resource $resource_name"
+	            s2_run_command -t 20 "$CMD -m fast" "stopping postmaster (fast)"
+		    if is_db_running ; then
+			# final try - brute force
+		        s2_log "stopping postmaster (immediate) for resource $resource_name"
+	            	s2_run_command -t 10 "$CMD -m immediate" "stopping postmaster (immediate)"
+		    fi
+		fi
+	        if is_db_running ; then
+		   s2_log "Shutdown of postgres database failed for resource $resource_name"
+		   exit_status=1
+		else
+		  s2_log_debug "postgres database shut down for resource $resource_name"
+		fi
+	    else
+		s2_log "postgres database is not running for resource $resource_name"
+	    fi
+	    ;;
+
+	restart)
+	    if ! is_db_running ; then
+		start_db
+		exit_status=$?
+	    else
+		s2_log "postgres already running for resource $resource_name"
+	    fi
+	    ;;
+
+	monitor)
+	    is_db_running
+	    exit_status=$?
+	    ;;
+		
+	exclusive)
+	    if is_db_running ; then
+		s2_log "postgres database $resource_name running on node"
+		exit_status=1
+	    fi
+	    ;;
+
+	*)
+	    s2_log "unknown action $action, don't know what to do"
+	    exit_status=1
+	    ;;
+
+    esac
+
+    if [ $exit_status -eq 0 ]; then 
+        s2_write_resource_status $HA_SUCCESS $resource_name
+    else
+        s2_write_resource_status $HA_CMD_FAILED $resource_name
+    fi
+}
+
+s2_execute
diff -urN cvs/cmd/srm/scripts/postgres/create_resource_type cluster_services/cmd/srm/scripts/postgres/create_resource_type
--- cvs/cmd/srm/scripts/postgres/create_resource_type	Thu Jan  1 01:00:00 1970
+++ cluster_services/cmd/srm/scripts/postgres/create_resource_type	Thu May  9 21:56:37 2002
@@ -0,0 +1,106 @@
+#!/bin/sh
+# 
+# create_resource_type
+#
+# Copyright (C) 2001 SuSE Linux AG
+#
+# Author: Joachim Gleissner <[email protected]>
+#         originaly for oracle resource type
+#         modified for postgres by Martin Bene <[email protected]>
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+
+#
+# This script takes two parameters
+# $1 - Configuration database name
+# $2 - Cluster name
+#
+# Use resourceModify to create template resource type in the
+# configuration database. A cluster-wide resource type definition
+# is created.
+#
+
+#
+# Check arguments 
+#
+if [ $# -ne 2 ]; then
+    echo "create-resource-type called with invalid number of arguments";
+    echo "create-resource-type <database file> <cluster name>"
+    exit 1
+fi
+
+DBFILE=$1
+CLUSTERNAME=$2
+RESTYPENAME=postgres
+
+#
+# Call restypeModify to create volumes resource type
+#
+/usr/lib/sysadm/privbin/restypeModify _CDB_DB=$DBFILE \
+_RESOURCE_TYPE=$RESTYPENAME _CLUSTER=$CLUSTERNAME \
+_CREATE=true _RESOURCE_TYPE_ORDER=801	\
+_RESOURCE_TYPE_PREDEFINED=true \
+_RESOURCE_TYPE_RESTART_MODE=1 _RESOURCE_TYPE_RESTART_COUNT=1 \
+_NUM_ACTIONS=5 \
+_ACTION_0=start \
+_ACTION_MAXEXECTIME_0=400000	\
+_ACTION_MONINTERVAL_0=0 \
+_ACTION_STARTMONTIME_0=0 \
+_ACTION_1=stop \
+_ACTION_MAXEXECTIME_1=80000	\
+_ACTION_MONINTERVAL_1=0 \
+_ACTION_STARTMONTIME_1=0 \
+_ACTION_2=monitor \
+_ACTION_MAXEXECTIME_2=40000	\
+_ACTION_MONINTERVAL_2=20000 \
+_ACTION_STARTMONTIME_2=50000 \
+_ACTION_3=exclusive \
+_ACTION_MAXEXECTIME_3=50000	\
+_ACTION_MONINTERVAL_3=0 \
+_ACTION_STARTMONTIME_3=0 \
+_ACTION_4=restart \
+_ACTION_MAXEXECTIME_4=80000	\
+_ACTION_MONINTERVAL_4=0 \
+_ACTION_STARTMONTIME_4=0 \
+_NUM_RESOURCE_KEYS=5	\
+_RESOURCE_KEY_0=port				\
+_RESOURCE_KEY_DATATYPE_0=integer 		\
+_RESOURCE_KEY_DEFAULT_0=5432 			\
+_RESOURCE_KEY_1=sockfile			\
+_RESOURCE_KEY_DATATYPE_1=string 		\
+_RESOURCE_KEY_DEFAULT_1=/tmp/.s.PGSQL.5432 	\
+_RESOURCE_KEY_2=base				\
+_RESOURCE_KEY_DATATYPE_2=string			\
+_RESOURCE_KEY_DEFAULT_2=/usr/local/pgsql	\
+_RESOURCE_KEY_3=data				\
+_RESOURCE_KEY_DATATYPE_3=string 		\
+_RESOURCE_KEY_DEFAULT_3=/var/dbase		\
+_RESOURCE_KEY_4=AdmUser				\
+_RESOURCE_KEY_DATATYPE_4=string			\
+_RESOURCE_KEY_DEFAULT_4=postgres
+
+/usr/lib/sysadm/privbin/restypeDependencyAdd \
+	_RESOURCE_TYPE=$RESTYPENAME _CLUSTER=$CLUSTERNAME \
+	_NUM_DEPENDENCIES=2 \
+	_DEPENDENCY_TYPE_0=Filesystem \
+	_DEPENDENCY_TYPE_1=IP_address
+
+if [ $? -ne 0 ]; then
+    echo "restypeModify command failed"
+    exit 1
+else
+    exit 0
+fi
diff -urN cvs/cmd/srm/scripts/postgres/postgres cluster_services/cmd/srm/scripts/postgres/postgres
--- cvs/cmd/srm/scripts/postgres/postgres	Thu Jan  1 01:00:00 1970
+++ cluster_services/cmd/srm/scripts/postgres/postgres	Thu May  9 21:56:42 2002
@@ -0,0 +1,54 @@
+postgres.properFormat= \
+With this resource you can make a postgres instance highly \
+available. The resource name is a unique string that can \
+be used to identify the postgres service. 
+
+postgres.port.label= \
+	Server Port
+
+postgres.port.glossary= \
+	glossary.postgres.port
+
+glossary.postgres.port= \
+	TCP Port the postgres server listens on
+
+postgres.sockfile.label= \
+	Socket file
+
+postgres.sockfile.glossary= \
+	glossary.postgres.sockfile
+
+glossary.postgres.sockfile= \
+	Socket file used by postmaster for local connections. \
+	This is used for monitoring and to check if the service \
+	is already running.
+
+postgres.base.label= \
+	Base Directory
+
+postgres.base.glossary= \
+	glossary.postgres.base
+
+glossary.postgres.base= \
+	Base Directory where postgres has been installed. \
+	The resource scripts expect to find postgres lib/ and bin/ \
+	directories here.
+
+postgres.data.label= \
+	Database directory 
+
+postgres.data.glossary= \
+	glossary.postgres.data
+
+glossary.postgres.data= \
+	Postgres database directory. This directory gets passed to the \
+	postmaster in the -D datadir parameter.
+
+postgres.AdmUser.label= \
+	Admin User
+
+postgres.AdmUser.glossary= \
+	glossary.postgres.AdmUser
+
+glossary.postgres.AdmUser= \
+	Operating system user the postgres database server should be run as
rpm.patch (application/octet-stream, 1.8 KB)
Index: build/spec
===================================================================
RCS file: /cvs/failsafe/FailSafe/build/spec,v
retrieving revision 1.19
diff -u -r1.19 spec
--- build/spec	2002/05/03 11:16:08	1.19
+++ build/spec	2002/05/09 20:26:13
@@ -601,6 +601,27 @@
 %attr(755, root, root) $(DIST_PREFIX)/resource_types/Samba/stop
 %attr(755, root, root) $(DIST_PREFIX)/diags/Samba.pl
 
+#CI/cmd/srm/scripts/drbd
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/control
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/create_resource_type
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/drbd
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/exclusive
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/monitor
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/README.drbd
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/restart
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/start
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/drbd/stop
+
+#CI/cmd/srm/scripts/postgres
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/control
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/create_resource_type
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/exclusive
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/monitor
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/postgres
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/restart
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/start
+%attr(755, root, root) $(DIST_PREFIX)/resource_types/postgres/stop
+
 #CI/cmd/srm/scripts/sapdb
 %attr(755, root, root) %dir /usr/lib/failsafe/resource_types/sapdb
 %attr(755, root, root) /usr/lib/failsafe/resource_types/sapdb/exclusive
postgres.tgz (application/x-compressed, 3.4 KB) - not displayed