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
|
#!/bin/sh
#
# Description: Manages a PostrgreSQL Server as an OCF High-Availability
# resource under Heartbeat/LinuxHA control
#
#
# Author: Serge Dubrouski (sergeyfd@gmail.com)
# Copyright 2006 Serge Dubrouski <sergeyfd@gmail.com>
# License: GNU General Public License (GPL)
#
# 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
###############################################################################
# Initialization:
. @hb_libdir@/ocf-shellfuncs
unset LC_ALL; export LC_ALL
unset LANGUAGE; export LANGUAGE
SH=@SHELL@
usage() {
cat <<-! >&1
usage: $0 start|stop|status|monitor|methods
$0 manages a PostgreSQL Server as an HA resource.
The 'start' operation starts the PostgreSQL server.
The 'stop' operation stops the PostgreSQL server.
The 'status' operation reports whether the PostgreSQL is up.
The 'monitor' operation reports whether the PostgreSQL is running.
The 'methods' operation reports on the methods $0 supports.
!
return $OCF_ERR_ARGS
}
meta_data() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="pgsql">
<version>1.0</version>
<longdesc lang="en">
Resource script for PostgreSQL. It manages a PostgreSQL as an HA resource.
</longdesc>
<shortdesc lang="en">pgsql resource agent</shortdesc>
<parameters>
<parameter name="pgctl" unique="0" required="0">
<longdesc lang="en">
Path to pg_ctl command.
</longdesc>
<shortdesc lang="en">pgctl</shortdesc>
<content type="string" default="/usr/bin/pgctl" />
</parameter>
<parameter name="start_opt" unique="0" required="0">
<longdesc lang="en">
Start options. "-i -p 5432" for example.
</longdesc>
<shortdesc lang="en">start_opt</shortdesc>
<content type="string" default="" />
</parameter>
<parameter name="psql" unique="0" required="0">
<longdesc lang="en">
Path to psql command.
</longdesc>
<shortdesc lang="en">psql</shortdesc>
<content type="string" default="/usr/bin/psql" />
</parameter>
<parameter name="pgdata" unique="0" required="0">
<longdesc lang="en">
Path PostgreSQL data directory.
</longdesc>
<shortdesc lang="en">pgdata</shortdesc>
<content type="string" default="/var/lib/pgsql/data" />
</parameter>
<parameter name="pgdba" unique="0" required="0">
<longdesc lang="en">
User that owns PostgreSQL.
</longdesc>
<shortdesc lang="en">pgdba</shortdesc>
<content type="string" default="postgres" />
</parameter>
<parameter name="pgdb" unique="0" required="0">
<longdesc lang="en">
Database that will be used for monitoring.
</longdesc>
<shortdesc lang="en">pgdb</shortdesc>
<content type="string" default="template1" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="120" />
<action name="stop" timeout="120" />
<action name="status" timeout="60" />
<action name="monitor" depth="0" timeout="30" interval="30" start-delay="10" />
<action name="meta-data" timeout="5" />
<action name="methods" timeout="5" />
</actions>
</resource-agent>
END
}
#
# Run the given command in the Resource owner environment...
#
runasowner() {
su $PGDBA -c ". ~${PGDBA}/.bash_profile; $*"
}
#
# methods: What methods/operations do we support?
#
pgsql_methods() {
cat <<-!
start
stop
status
monitor
methods
!
}
#pgsql_start: Starts PostgreSQL
pgsql_start() {
if pgsql_status
then
ocf_log info "PostgreSQL is already running. PID=`cat $PIDFILE`"
return $OCF_SUCCESS
fi
if [ -x $PGCTL ]
then
# Check if we need to create a log file
[ ! -f /var/log/pgsql.log ] && touch /var/log/pgsql.log
# Set the right ownership
if runasowner "$PGCTL -D $PGDATA -l /var/log/pgsql.log -o "\'$STARTOPT\'" start > /dev/null 2>&1"
then
# Probably started.....
ocf_log info "PostgreSQL start command sent."
else
ocf_log err "Can't start PostgreSQL."; return $OCF_ERR_GENERIC
fi
else
ocf_log err "$PGCTL not found!"
return $OCF_ERR_GENERIC
fi
if ! pgsql_status
then
sleep 5
if ! pgsql_status
then
echo "ERROR: PostgreSQL is not running!"
return $OCF_ERR_GENERIC
fi
fi
return $OCF_SUCCESS
}
#pgsql_stop: Stop PostgreSQL
pgsql_stop() {
if ! pgsql_status
then
#Already stopped
return $OCF_SUCCESS
fi
# Stop PostgreSQL do not wait for clients to disconnect
runasowner "$PGCTL -D $PGDATA stop -m fast > /dev/null 2>&1"
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
PID=`head -n 1 $PIDFILE`
process_running $PID && [ `ps -p $PID | grep postmaster | wc -l` -eq 1 ]
else
: No pid file
false
fi
}
#
# return TRUE if a process with given PID is running
#
process_running() {
kill -0 "$1" >/dev/null 2>&1
}
#
# 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`
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
case "$1" in
methods) pgsql_methods
exit $?;;
meta_data) meta_data
exit $OCF_SUCCESS;;
esac
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 $?;;
esac
usage
|