#!/bin/sh
#
# Description:  Manages a PostgreSQL Server as an OCF High-Availability
#               resource
#
# Authors:      Serge Dubrouski (sergeyfd@gmail.com) -- original RA
#               Florian Haas (florian@linbit.com) -- makeover
#               Takatoshi MATSUO (matsuo.tak@gmail.com) -- support replication
#               David Corlette (dcorlette@netiq.com) -- add support for non-standard library locations and non-standard port
#
# Copyright:    2006-2012 Serge Dubrouski <sergeyfd@gmail.com>
#                         and other Linux-HA contributors
# License:      GNU General Public License (GPL)
#
###############################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Use runuser if available for SELinux.
if [ -x /sbin/runuser ]; then
    SU=runuser
else
    SU=su
fi

#
# Get PostgreSQL Configuration parameter
#
get_pgsql_param() {
    local param_name

    param_name=$1
    perl_code="if (/^\s*$param_name[\s=]+\s*(.*)$/) {
       \$dir=\$1;
       \$dir =~ s/\s*\#.*//;
       \$dir =~ s/^'(\S*)'/\$1/;
       print \$dir;}"

    perl -ne "$perl_code" < $OCF_RESKEY_config
}

# Defaults
OCF_RESKEY_pgctl_default=/usr/bin/pg_ctl
OCF_RESKEY_psql_default=/usr/bin/psql
OCF_RESKEY_pgdata_default=/var/lib/pgsql/data
OCF_RESKEY_pgdba_default=postgres
OCF_RESKEY_pghost_default=""
OCF_RESKEY_pgport_default=5432
OCF_RESKEY_pglibs_default=/usr/lib
OCF_RESKEY_start_opt_default=""
OCF_RESKEY_ctl_opt_default=""
OCF_RESKEY_pgdb_default=template1
OCF_RESKEY_logfile_default=/dev/null
OCF_RESKEY_stop_escalate_default=90
OCF_RESKEY_monitor_user_default=""
OCF_RESKEY_monitor_password_default=""
OCF_RESKEY_monitor_sql_default="select now();"
OCF_RESKEY_check_wal_receiver_default="false"
# Defaults for replication
OCF_RESKEY_rep_mode_default=none
OCF_RESKEY_node_list_default=""
OCF_RESKEY_restore_command_default=""
OCF_RESKEY_archive_cleanup_command_default=""
OCF_RESKEY_recovery_end_command_default=""
OCF_RESKEY_master_ip_default=""
OCF_RESKEY_repuser_default="postgres"
OCF_RESKEY_primary_conninfo_opt_default=""
OCF_RESKEY_restart_on_promote_default="false"
OCF_RESKEY_tmpdir_default="/var/lib/pgsql/tmp"
OCF_RESKEY_xlog_check_count_default="3"
OCF_RESKEY_crm_attr_timeout_default="5"
OCF_RESKEY_stop_escalate_in_slave_default=90
OCF_RESKEY_replication_slot_name_default=""

: ${OCF_RESKEY_pgctl=${OCF_RESKEY_pgctl_default}}
: ${OCF_RESKEY_psql=${OCF_RESKEY_psql_default}}
: ${OCF_RESKEY_pgdata=${OCF_RESKEY_pgdata_default}}
: ${OCF_RESKEY_pgdba=${OCF_RESKEY_pgdba_default}}
: ${OCF_RESKEY_pghost=${OCF_RESKEY_pghost_default}}
: ${OCF_RESKEY_pgport=${OCF_RESKEY_pgport_default}}
: ${OCF_RESKEY_pglibs=${OCF_RESKEY_pglibs_default}}
: ${OCF_RESKEY_config=${OCF_RESKEY_pgdata}/postgresql.conf}
: ${OCF_RESKEY_start_opt=${OCF_RESKEY_start_opt_default}}
: ${OCF_RESKEY_ctl_opt=${OCF_RESKEY_ctl_opt_default}}
: ${OCF_RESKEY_pgdb=${OCF_RESKEY_pgdb_default}}
: ${OCF_RESKEY_logfile=${OCF_RESKEY_logfile_default}}
: ${OCF_RESKEY_stop_escalate=${OCF_RESKEY_stop_escalate_default}}
: ${OCF_RESKEY_monitor_user=${OCF_RESKEY_monitor_user_default}}
: ${OCF_RESKEY_monitor_password=${OCF_RESKEY_monitor_password_default}}
: ${OCF_RESKEY_monitor_sql=${OCF_RESKEY_monitor_sql_default}}
: ${OCF_RESKEY_check_wal_receiver=${OCF_RESKEY_check_wal_receiver_default}}

# for replication
: ${OCF_RESKEY_rep_mode=${OCF_RESKEY_rep_mode_default}}
: ${OCF_RESKEY_node_list=${OCF_RESKEY_node_list_default}}
: ${OCF_RESKEY_restore_command=${OCF_RESKEY_restore_command_default}}
: ${OCF_RESKEY_archive_cleanup_command=${OCF_RESKEY_archive_cleanup_command_default}}
: ${OCF_RESKEY_recovery_end_command=${OCF_RESKEY_recovery_end_command_default}}
: ${OCF_RESKEY_master_ip=${OCF_RESKEY_master_ip_default}}
: ${OCF_RESKEY_repuser=${OCF_RESKEY_repuser_default}}
: ${OCF_RESKEY_primary_conninfo_opt=${OCF_RESKEY_primary_conninfo_opt_default}}
: ${OCF_RESKEY_restart_on_promote=${OCF_RESKEY_restart_on_promote_default}}
: ${OCF_RESKEY_tmpdir=${OCF_RESKEY_tmpdir_default}}
: ${OCF_RESKEY_xlog_check_count=${OCF_RESKEY_xlog_check_count_default}}
: ${OCF_RESKEY_crm_attr_timeout=${OCF_RESKEY_crm_attr_timeout_default}}
: ${OCF_RESKEY_stop_escalate_in_slave=${OCF_RESKEY_stop_escalate_in_slave_default}}
: ${OCF_RESKEY_replication_slot_name=${OCF_RESKEY_replication_slot_name_default}}

usage() {
    cat <<EOF
        usage: $0 start|stop|status|monitor|promote|demote|notify|meta-data|validate-all|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 'promote' operation promotes the PostgreSQL server.
        The 'demote' operation demotes the PostgreSQL server.
        The 'validate-all' operation reports whether the parameters are valid.
        The 'methods' operation reports on the methods $0 supports.
EOF
  return $OCF_ERR_ARGS
}

meta_data() {
    cat <<EOF
<?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">Manages a PostgreSQL database instance</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="${OCF_RESKEY_pgctl_default}" />
</parameter>

<parameter name="start_opt" unique="0" required="0">
<longdesc lang="en">
Start options (-o start_opt in pg_ctl). "-i -p 5432" for example.
</longdesc>
<shortdesc lang="en">start_opt</shortdesc>
<content type="string" default="${OCF_RESKEY_start_opt_default}" />

</parameter>
<parameter name="ctl_opt" unique="0" required="0">
<longdesc lang="en">
Additional pg_ctl options (-w, -W etc..).
</longdesc>
<shortdesc lang="en">ctl_opt</shortdesc>
<content type="string" default="${OCF_RESKEY_ctl_opt_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="${OCF_RESKEY_psql_default}" />
</parameter>

<parameter name="pgdata" unique="0" required="0">
<longdesc lang="en">
Path to PostgreSQL data directory.
</longdesc>
<shortdesc lang="en">pgdata</shortdesc>
<content type="string" default="${OCF_RESKEY_pgdata_default}" />
</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="${OCF_RESKEY_pgdba_default}" />
</parameter>

<parameter name="pghost" unique="0" required="0">
<longdesc lang="en">
Hostname/IP address where PostgreSQL is listening
</longdesc>
<shortdesc lang="en">pghost</shortdesc>
<content type="string" default="${OCF_RESKEY_pghost_default}" />
</parameter>

<parameter name="pgport" unique="0" required="0">
<longdesc lang="en">
Port where PostgreSQL is listening
</longdesc>
<shortdesc lang="en">pgport</shortdesc>
<content type="integer" default="${OCF_RESKEY_pgport_default}" />
</parameter>

<parameter name="pglibs" unique="0" required="0">
<longdesc lang="en">
Custom location of the Postgres libraries. If not set, the standard location
will be used.
</longdesc>
<shortdesc lang="en">pglibs</shortdesc>
<content type="string" default="${OCF_RESKEY_pglibs_default}" />
</parameter>

<parameter name="monitor_user" unique="0" required="0">
<longdesc lang="en">
PostgreSQL user that pgsql RA will user for monitor operations. If it's not set
pgdba user will be used.
</longdesc>
<shortdesc lang="en">monitor_user</shortdesc>
<content type="string" default="${OCF_RESKEY_monitor_user_default}" />
</parameter>

<parameter name="monitor_password" unique="0" required="0">
<longdesc lang="en">
Password for monitor user.
</longdesc>
<shortdesc lang="en">monitor_password</shortdesc>
<content type="string" default="${OCF_RESKEY_monitor_password_default}" />
</parameter>

<parameter name="monitor_sql" unique="0" required="0">
<longdesc lang="en">
SQL script that will be used for monitor operations.
</longdesc>
<shortdesc lang="en">monitor_sql</shortdesc>
<content type="string" default="${OCF_RESKEY_monitor_sql_default}" />
</parameter>

<parameter name="config" unique="0" required="0">
<longdesc lang="en">
Path to the PostgreSQL configuration file for the instance.
</longdesc>
<shortdesc lang="en">Configuration file</shortdesc>
<content type="string" default="${OCF_RESKEY_pgdata}/postgresql.conf" />
</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="${OCF_RESKEY_pgdb_default}" />
</parameter>

<parameter name="logfile" unique="0" required="0">
<longdesc lang="en">
Path to PostgreSQL server log output file.
</longdesc>
<shortdesc lang="en">logfile</shortdesc>
<content type="string" default="${OCF_RESKEY_logfile_default}" />
</parameter>

<parameter name="socketdir" unique="0" required="0">
<longdesc lang="en">
Unix socket directory for PostgreSQL.

If you use PostgreSQL 9.3 or higher and define unix_socket_directories in the postgresql.conf, then you must set socketdir to determine which directory is used for psql command.
</longdesc>
<shortdesc lang="en">socketdir</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="stop_escalate" unique="0" required="0">
<longdesc lang="en">
Number of seconds to wait for stop (using -m fast) before resorting to -m immediate
</longdesc>
<shortdesc lang="en">stop escalation</shortdesc>
<content type="integer" default="${OCF_RESKEY_stop_escalate_default}" />
</parameter>

<parameter name="rep_mode" unique="0" required="0">
<longdesc lang="en">
Replication mode may be set to "async" or "sync" or "slave".
They require PostgreSQL 9.1 or later.
Once set, "async" and "sync" require node_list, master_ip, and
restore_command parameters,as well as configuring PostgreSQL
for replication (in postgresql.conf and pg_hba.conf).

"slave" means that RA only makes recovery.conf before starting
to connect to primary which is running somewhere.
It dosen't need master/slave setting.
It requires master_ip restore_command parameters.
</longdesc>
<shortdesc lang="en">rep_mode</shortdesc>
<content type="string" default="${OCF_RESKEY_rep_mode_default}" />
</parameter>

<parameter name="node_list" unique="0" required="0">
<longdesc lang="en">
All node names. Please separate each node name with a space.
This is required for replication.
</longdesc>
<shortdesc lang="en">node list</shortdesc>
<content type="string" default="${OCF_RESKEY_node_list_default}" />
</parameter>

<parameter name="restore_command" unique="0" required="0">
<longdesc lang="en">
restore_command for recovery.conf.
This is required for replication.
</longdesc>
<shortdesc lang="en">restore_command</shortdesc>
<content type="string" default="${OCF_RESKEY_restore_command_default}" />
</parameter>

<parameter name="archive_cleanup_command" unique="0" required="0">
<longdesc lang="en">
archive_cleanup_command for recovery.conf.
This is used for replication and is optional.
</longdesc>
<shortdesc lang="en">archive_cleanup_command</shortdesc>
<content type="string" default="${OCF_RESKEY_archive_cleanup_command_default}" />
</parameter>

<parameter name="recovery_end_command" unique="0" required="0">
<longdesc lang="en">
recovery_end_command for recovery.conf.
This is used for replication and is optional.
</longdesc>
<shortdesc lang="en">recovery_end_command</shortdesc>
<content type="string" default="${OCF_RESKEY_recovery_end_command_default}" />
</parameter>

<parameter name="master_ip" unique="0" required="0">
<longdesc lang="en">
Master's floating IP address to be connected from hot standby.
This parameter is used for "primary_conninfo" in recovery.conf.
This is required for replication.
</longdesc>
<shortdesc lang="en">master ip</shortdesc>
<content type="string" default="${OCF_RESKEY_master_ip_default}" />
</parameter>

<parameter name="repuser" unique="0" required="0">
<longdesc lang="en">
User used to connect to the master server.
This parameter is used for "primary_conninfo" in recovery.conf.
This is required for replication.
</longdesc>
<shortdesc lang="en">repuser</shortdesc>
<content type="string" default="${OCF_RESKEY_repuser_default}" />
</parameter>

<parameter name="primary_conninfo_opt" unique="0" required="0">
<longdesc lang="en">
primary_conninfo options of recovery.conf except host, port, user and application_name.
This is optional for replication.
</longdesc>
<shortdesc lang="en">primary_conninfo_opt</shortdesc>
<content type="string" default="${OCF_RESKEY_primary_conninfo_opt_default}" />
</parameter>

<parameter name="restart_on_promote" unique="0" required="0">
<longdesc lang="en">
If this is true, RA deletes recovery.conf and restarts PostgreSQL
on promote to keep Timeline ID. It probably makes fail-over slower.
It's recommended to set on-fail of promote up as fence.
This is optional for replication.
</longdesc>
<shortdesc lang="en">restart_on_promote</shortdesc>
<content type="boolean" default="${OCF_RESKEY_restart_on_promote_default}" />
</parameter>

<parameter name="replication_slot_name" unique="0" required="0">
<longdesc lang="en">
Set this option when using replication slots.
Can only use lower case letters, numbers and underscore for replication_slot_name.

When the master node has 1 slave node,one replication slot would be created with the name "replication_slot_name".
When the master node has 2 or more slave nodes,the replication slots would be created for each node, with the name adding the node name as postfix.
For example, replication_slot_name is "sample" and 2 slaves which are "node1" and "node2" connect to
their slots, the slots names are "sample_node1" and "sample_node2".
If the node name contains a upper case letter, hyphen and dot, those characters will be converted to a lower case letter or an underscore.
For example, Node-1.example.com to node_1_example_com.

pgsql RA doesn't monitor and delete the repliation slot.
When the slave node has been disconnected in failure or the like, execute one of the following manually.
Otherwise it may eventually cause a disk full because the master node will continue to accumulate the unsent WAL.
1. recover and reconnect the slave node to the master node as soon as possible.
2. delete the slot on the master node by following psql command.
$ select pg_drop_replication_slot('replication_slot_name');
</longdesc>
<shortdesc lang="en">replication_slot_name</shortdesc>
<content type="string" default="${OCF_RESKEY_replication_slot_name_default}" />
</parameter>

<parameter name="tmpdir" unique="0" required="0">
<longdesc lang="en">
Path to temporary directory.
This is optional for replication.
</longdesc>
<shortdesc lang="en">tmpdir</shortdesc>
<content type="string" default="${OCF_RESKEY_tmpdir_default}" />
</parameter>

<parameter name="xlog_check_count" unique="0" required="0">
<longdesc lang="en">
Number of checks of xlog on monitor before promote.
This is optional for replication.
</longdesc>
<shortdesc lang="en">xlog check count</shortdesc>
<content type="integer" default="${OCF_RESKEY_xlog_check_count_default}" />
</parameter>

<parameter name="crm_attr_timeout" unique="0" required="0">
<longdesc lang="en">
The timeout of crm_attribute forever update command.
Default value is 5 seconds.
This is optional for replication.
</longdesc>
<shortdesc lang="en">The timeout of crm_attribute forever update command.</shortdesc>
<content type="integer" default="${OCF_RESKEY_crm_attr_timeout_default}" />
</parameter>

<parameter name="stop_escalate_in_slave" unique="0" required="0">
<longdesc lang="en">
Number of seconds to wait for stop (using -m fast) before resorting to -m immediate
in slave state.
This is optional for replication.
</longdesc>
<shortdesc lang="en">stop escalation_in_slave</shortdesc>
<content type="integer" default="${OCF_RESKEY_stop_escalate_in_slave_default}" />
</parameter>

<parameter name="check_wal_receiver" unique="0" required="0">
<longdesc lang="en">
If this is true, RA checks wal_receiver process on monitor
and notifies its status using "(resource name)-receiver-status" attribute.
It's useful for checking whether PostgreSQL (hot standby) connects to primary.
The attribute shows status as "normal" or "normal (master)" or "ERROR".
Note that if you configure PostgreSQL as master/slave resource, then
wal receiver is not running in the master and the attribute shows status as
"normal (master)" consistently because it is normal status.
</longdesc>
<shortdesc lang="en">check_wal_receiver</shortdesc>
<content type="boolean" default="${OCF_RESKEY_check_wal_receiver_default}" />
</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"/>
<action name="monitor" depth="0" timeout="30" interval="29" role="Master" />
<action name="promote" timeout="120" />
<action name="demote" timeout="120" />
<action name="notify"   timeout="90" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="5" />
<action name="methods" timeout="5" />
</actions>
</resource-agent>
EOF
}


#
#   Run the given command in the Resource owner environment...
#
runasowner() {
    local quietrun=""
    local loglevel="-err"
    local var

    for var in 1 2
    do
        case "$1" in
            "-q")
                quietrun="-q"
                shift 1;;
            "warn"|"err")
                loglevel="-$1"
                shift 1;;
            *)
                ;;
        esac
    done

    ocf_run $quietrun $loglevel $SU $OCF_RESKEY_pgdba -c "cd $OCF_RESKEY_pgdata; $*"
}

#
#       Shell escape
#
escape_string() {
    echo "$*" | sed -e "s/'/'\\\\''/g"
}


#
# methods: What methods/operations do we support?
#

pgsql_methods() {
    cat <<EOF
    start
    stop
    status
    monitor
    promote
    demote
    notify
    methods
    meta-data
    validate-all
EOF
}


# Execulte SQL and return the result.
exec_sql() {
    local sql="$1"
    local output
    local rc

    output=`$SU $OCF_RESKEY_pgdba -c "cd $OCF_RESKEY_pgdata; \
                $OCF_RESKEY_psql $psql_options -U $OCF_RESKEY_pgdba \
                -Atc \"$sql\""`
    rc=$?

    echo $output
    return $rc
}


#pgsql_real_start: Starts PostgreSQL
pgsql_real_start() {
    local pgctl_options
    local postgres_options
    local rc

    if pgsql_status; then
        ocf_log info "PostgreSQL is already running. PID=`cat $PIDFILE`"
        if is_replication; then
            return $OCF_ERR_GENERIC
        else
            return $OCF_SUCCESS
        fi
    fi

    # Remove postmaster.pid if it exists
    rm -f $PIDFILE

    # Remove backup_label if it exists
    if [ -f $BACKUPLABEL ] && ! is_replication; then
        ocf_log info "Removing $BACKUPLABEL. The previous backup might have failed."
        rm -f $BACKUPLABEL
    fi

    # Check if we need to create a log file
    if ! check_log_file $OCF_RESKEY_logfile
    then
        ocf_exit_reason "PostgreSQL can't write to the log file: $OCF_RESKEY_logfile"
        return $OCF_ERR_PERM
    fi

    # Check socket directory
    if [ -n "$OCF_RESKEY_socketdir" ]
    then
        check_socket_dir
    fi

    if [ "$OCF_RESKEY_rep_mode" = "slave" ]; then
        rm -f $RECOVERY_CONF
        make_recovery_conf || return $OCF_ERR_GENERIC
    fi

    # Set options passed to pg_ctl
    pgctl_options="$OCF_RESKEY_ctl_opt -D $OCF_RESKEY_pgdata -l $OCF_RESKEY_logfile"

    # Set options passed to the PostgreSQL server process
    postgres_options="-c config_file=${OCF_RESKEY_config}"

    if [ -n "$OCF_RESKEY_pghost" ]; then
        postgres_options="$postgres_options -h $OCF_RESKEY_pghost"
    fi
    if [ -n "$OCF_RESKEY_start_opt" ]; then
        postgres_options="$postgres_options $OCF_RESKEY_start_opt"
    fi

    # Tack pass-through options onto pg_ctl options
    pgctl_options="$pgctl_options -o '$postgres_options'"

    # Invoke pg_ctl
    runasowner "unset PGUSER; unset PGPASSWORD; $OCF_RESKEY_pgctl $pgctl_options start"

    if [ $? -eq 0 ]; then
        # Probably started.....
        ocf_log info "PostgreSQL start command sent."
    else
        ocf_exit_reason "Can't start PostgreSQL."
        return $OCF_ERR_GENERIC
    fi

    while :
    do
        pgsql_real_monitor warn
        rc=$?
        if [ $rc -eq $OCF_SUCCESS -o $rc -eq $OCF_RUNNING_MASTER ]; then
            break;
        fi
        sleep 1
        ocf_log debug "PostgreSQL still hasn't started yet. Waiting..."
    done

    # create replication slot on the master and slave nodes.
    # creating slot on the slave node is in preparation for failover.
    if use_replication_slot; then
        create_replication_slot
        if [ $? -eq $OCF_ERR_GENERIC ]; then
            ocf_exit_reason "PostgreSQL can't create replication_slot."
            return $OCF_ERR_GENERIC
        fi
    fi

    ocf_log info "PostgreSQL is started."
    return $rc
}

pgsql_replication_start() {
    local rc

    # initializing for replication
    change_pgsql_status "$NODENAME" "STOP"
    delete_master_baseline
    exec_with_retry 0 $CRM_MASTER -v $CAN_NOT_PROMOTE
    rm -f ${XLOG_NOTE_FILE}.* $REP_MODE_CONF $RECOVERY_CONF
    if ! make_recovery_conf || ! delete_xlog_location || ! set_async_mode_all; then
        return $OCF_ERR_GENERIC
    fi

    if [ -f $PGSQL_LOCK ]; then
        ocf_exit_reason "My data may be inconsistent. You have to remove $PGSQL_LOCK file to force start."
        return $OCF_ERR_GENERIC
    fi

    # start
    pgsql_real_start
    if [ $? -ne $OCF_SUCCESS ]; then
        return $OCF_ERR_GENERIC
    fi
    change_pgsql_status "$NODENAME" "HS:alone"
    return $OCF_SUCCESS
}

#pgsql_start: pgsql_real_start() wrapper for replication
pgsql_start() {
    if ! is_replication; then
        pgsql_real_start
        return $?
    else
        pgsql_replication_start
        return $?
    fi
}

#pgsql_promote: Promote PostgreSQL
pgsql_promote() {
    local target
    local rc

    if ! is_replication; then
        ocf_exit_reason "Not in a replication mode."
        return $OCF_ERR_CONFIGURED
    fi
    rm -f ${XLOG_NOTE_FILE}.*

    for target in $NODE_LIST; do
        [ "$target" = "$NODENAME" ] && continue
        change_data_status "$target" "DISCONNECT"
        change_master_score "$target" "$CAN_NOT_PROMOTE"
    done

    ocf_log info "Creating $PGSQL_LOCK."
    touch $PGSQL_LOCK
    show_master_baseline

    if ocf_is_true ${OCF_RESKEY_restart_on_promote}; then
        ocf_log info "Restarting PostgreSQL instead of promote."
        #stop : this function returns $OCF_SUCCESS only.
        pgsql_real_stop slave
        rm -f $RECOVERY_CONF
        pgsql_real_start
        rc=$?
        if [ $rc -ne $OCF_RUNNING_MASTER ]; then
            ocf_exit_reason "Can't start PostgreSQL as primary on promote."
            if [ $rc -ne $OCF_SUCCESS ]; then
                change_pgsql_status "$NODENAME" "STOP"
            fi
            return $OCF_ERR_GENERIC
        fi
    else
        runasowner "$OCF_RESKEY_pgctl -D $OCF_RESKEY_pgdata promote"
        if [ $? -eq 0 ]; then
            ocf_log info "PostgreSQL promote command sent."
        else
            ocf_exit_reason "Can't promote PostgreSQL."
            return $OCF_ERR_GENERIC
        fi

        while :
        do
            pgsql_real_monitor warn
            rc=$?
            if [ $rc -eq $OCF_RUNNING_MASTER ]; then
                break;
            elif [ $rc -eq $OCF_ERR_GENERIC ]; then
                ocf_exit_reason "Can't promote PostgreSQL."
                return $rc
            fi
            sleep 1
            ocf_log debug "PostgreSQL still hasn't promoted yet. Waiting..."
        done
        ocf_log info "PostgreSQL is promoted."
    fi

    change_data_status "$NODENAME" "LATEST"
    exec_with_retry 0 $CRM_MASTER -v $PROMOTE_ME
    change_pgsql_status "$NODENAME" "PRI"
    return $OCF_SUCCESS
}

#pgsql_demote: Demote PostgreSQL
pgsql_demote() {
    local rc

    if ! is_replication; then
        ocf_exit_reason "Not in a replication mode."
        return $OCF_ERR_CONFIGURED
    fi

    exec_with_retry 0 $CRM_MASTER -v $CAN_NOT_PROMOTE
    delete_master_baseline

    if ! pgsql_status; then
        ocf_log info "PostgreSQL is already stopped on demote."
    else
        ocf_log info "Stopping PostgreSQL on demote."
        pgsql_real_stop master
        rc=$?
        if [ "$rc" -ne "$OCF_SUCCESS" ]; then
            change_pgsql_status "$NODENAME" "UNKNOWN"
            return $rc
        fi
    fi
    change_pgsql_status "$NODENAME" "STOP"
    return $OCF_SUCCESS
}

#pgsql_real_stop: Stop PostgreSQL
pgsql_real_stop() {
    local rc
    local count
    local stop_escalate

    if ocf_is_true ${OCF_RESKEY_check_wal_receiver}; then
        attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -D -q
    fi

    if ! pgsql_status
    then
        #Already stopped
        return $OCF_SUCCESS
    fi

    stop_escalate=$OCF_RESKEY_stop_escalate
    if [ "$1" = "slave" ]; then
        stop_escalate="$OCF_RESKEY_stop_escalate_in_slave"
    fi
    # adjust stop_escalate time when it is longer than the timeout
    if [ -n "$OCF_RESKEY_CRM_meta_timeout" ] && \
        [ "$stop_escalate" -ge $((OCF_RESKEY_CRM_meta_timeout/1000)) ]; then
        stop_escalate=$(((OCF_RESKEY_CRM_meta_timeout/1000) - 10))
        ocf_log info "stop_escalate(or stop_escalate_in_slave) time is adjusted to ${stop_escalate} based on the configured timeout."
    fi

    # Stop PostgreSQL, do not wait for clients to disconnect
    if [ $stop_escalate -gt 0 ]; then
            runasowner "$OCF_RESKEY_pgctl -W -D $OCF_RESKEY_pgdata stop -m fast"
    fi

    # stop waiting
    count=0
    while [ $count -lt $stop_escalate ]
    do
        if ! pgsql_status
        then
            #PostgreSQL stopped
            break;
        fi
        count=`expr $count + 1`
        sleep 1
    done

    if pgsql_status
    then
        #PostgreSQL is still up. Use another shutdown mode.
        ocf_log info "PostgreSQL failed to stop after ${stop_escalate}s using -m fast. Trying -m immediate..."
        runasowner "$OCF_RESKEY_pgctl -W -D $OCF_RESKEY_pgdata stop -m immediate"
    fi

    while :
    do
        pgsql_real_monitor
        rc=$?
        if [ $rc -eq $OCF_NOT_RUNNING ]; then
            # An unnecessary debug log is prevented.
            break;
        fi
        sleep 1
        ocf_log debug "PostgreSQL still hasn't stopped yet. Waiting..."
    done

    # Remove postmaster.pid if it exists
    rm -f $PIDFILE

    if  [ "$1" = "master" -a "$OCF_RESKEY_CRM_meta_notify_slave_uname" = " " ]; then
        ocf_log info "Removing $PGSQL_LOCK."
        rm -f $PGSQL_LOCK
    fi
    return $OCF_SUCCESS
}

pgsql_replication_stop() {
    local rc

    exec_with_retry 5 $CRM_MASTER -v $CAN_NOT_PROMOTE
    delete_xlog_location

    if ! pgsql_status
    then
        ocf_log info "PostgreSQL is already stopped."
        change_pgsql_status "$NODENAME" "STOP"
        return $OCF_SUCCESS
    fi

    pgsql_real_stop slave
    rc=$?
    if [ $rc -ne $OCF_SUCCESS ]; then
        change_pgsql_status "$NODENAME" "UNKNOWN"
        return $rc
    fi

    change_pgsql_status "$NODENAME" "STOP"
    set_async_mode_all
    delete_master_baseline
    return $OCF_SUCCESS
}

#pgsql_stop: pgsql_real_stop() wrapper for replication
pgsql_stop() {
    if ! is_replication; then
        pgsql_real_stop
        return $?
    else
        pgsql_replication_stop
        return $?
    fi
}

#
# pgsql_status: is PostgreSQL up?
#

pgsql_status() {
     if [ -f $PIDFILE ]
     then
         PID=`head -n 1 $PIDFILE`
         runasowner "kill -s 0 $PID >/dev/null 2>&1"
         return $?
     fi

     # No PID file
     false
}

pgsql_wal_receiver_status() {
    local PID
    local receiver_parent_pids
    local pgsql_real_monitor_status=$1

    PID=`head -n 1 $PIDFILE`
    receiver_parent_pids=`ps -ef | tr -s " " | grep "[w]al receiver process" | cut -d " " -f 3`

    if echo "$receiver_parent_pids" | grep -q -w "$PID" ; then
        attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -v "normal" -q
        return 0
    fi

    if [ $pgsql_real_monitor_status -eq "$OCF_RUNNING_MASTER" ]; then
        attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -v "normal (master)" -q
        return 0
    fi

    attrd_updater -n "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -v "ERROR" -q
    ocf_log warn "wal receiver process is not running"
    return 1
}

#
# pgsql_real_monitor
#

pgsql_real_monitor() {
    local loglevel
    local rc
    local output

    # Set the log level of the error message
    loglevel=${1:-err}

    if ! pgsql_status
    then
        ocf_log info "PostgreSQL is down"
        return $OCF_NOT_RUNNING
    fi

    if is_replication; then
        #Check replication state
        output=`exec_sql "${CHECK_MS_SQL}"`
        rc=$?

        if [ $rc -ne  0 ]; then
            report_psql_error $rc $loglevel "Can't get PostgreSQL recovery status."
            return $OCF_ERR_GENERIC
        fi

        case "$output" in
            f)  ocf_log debug "PostgreSQL is running as a primary."
                if [ "$OCF_RESKEY_monitor_sql" = "$OCF_RESKEY_monitor_sql_default" ]; then
                    return $OCF_RUNNING_MASTER
                fi
                ;;

            t)  ocf_log debug "PostgreSQL is running as a hot standby."
                return $OCF_SUCCESS;;

            *)  ocf_exit_reason "$CHECK_MS_SQL output is $output"
                return $OCF_ERR_GENERIC;;
        esac
    fi

    OCF_RESKEY_monitor_sql=`escape_string "$OCF_RESKEY_monitor_sql"`
    runasowner -q $loglevel "$OCF_RESKEY_psql $psql_options \
                  -c '$OCF_RESKEY_monitor_sql'"
    rc=$?
    if [ $rc -ne  0 ]; then
        report_psql_error $rc $loglevel "PostgreSQL $OCF_RESKEY_pgdb isn't running."
        return $OCF_ERR_GENERIC
    fi

    if is_replication; then
        return $OCF_RUNNING_MASTER
    fi
    return $OCF_SUCCESS
}

pgsql_replication_monitor() {
    local rc

    rc=$1
    if [ $rc -ne $OCF_SUCCESS -a $rc -ne "$OCF_RUNNING_MASTER" ]; then
        return $rc
    fi
    # If I am Master
    if [ $rc -eq $OCF_RUNNING_MASTER ]; then
        change_data_status "$NODENAME" "LATEST"
        change_pgsql_status "$NODENAME" "PRI"
        control_slave_status || return $OCF_ERR_GENERIC
        if [ "$RE_CONTROL_SLAVE" = "true" ]; then
            sleep 2
            ocf_log info "re-controlling slave status."
            RE_CONTROL_SLAVE="none"
            control_slave_status || return $OCF_ERR_GENERIC
        fi
        return $rc
    fi

    # I can't get master node name from $OCF_RESKEY_CRM_meta_notify_master_uname on monitor,
    # so I will get master node name using crm_mon -n
    print_crm_mon | tr -d "\t" | tr -d " " | grep -q "^${RESOURCE_NAME}[(:].*[):].*Master"
    if [ $? -ne 0 ] ; then
        # If I am Slave and Master is not exist
        ocf_log info "Master does not exist."
        change_pgsql_status "$NODENAME" "HS:alone"
        have_master_right
        if [ $? -eq 0 ]; then
            rm -f ${XLOG_NOTE_FILE}.*
        fi
    else
        output=`exec_with_retry 0 $CRM_ATTR_FOREVER -N "$NODENAME" \
                -n "$PGSQL_DATA_STATUS_ATTR" -G -q`
        if [ "$output" = "DISCONNECT" ]; then
            change_pgsql_status "$NODENAME" "HS:alone"
        fi
    fi
    return $rc
}

#pgsql_monitor: pgsql_real_monitor() wrapper for replication
pgsql_monitor() {
    local rc

    pgsql_real_monitor
    rc=$?

    if ocf_is_true ${OCF_RESKEY_check_wal_receiver}; then
        pgsql_wal_receiver_status $rc
    fi

    if ! is_replication; then
        return $rc
    else
        pgsql_replication_monitor $rc
        return $?
    fi
}

# pgsql_post_demote
pgsql_post_demote() {
    DEMOTE_NODE=`echo $OCF_RESKEY_CRM_meta_notify_demote_uname | sed "s/ /\n/g" | head -1 | tr '[A-Z]' '[a-z]'`
    ocf_log debug "post-demote called. Demote uname is $DEMOTE_NODE"
    if [ "$DEMOTE_NODE" != "$NODENAME" ]; then
        if ! echo $OCF_RESKEY_CRM_meta_notify_master_uname | tr '[A-Z]' '[a-z]' | grep $NODENAME; then
            show_master_baseline
            change_pgsql_status "$NODENAME" "HS:alone"
        fi
    fi
    return $OCF_SUCCESS
}

pgsql_pre_promote() {
    local master_baseline
    local my_master_baseline
    local cmp_location
    local number_of_nodes

    # If my data is newer than new master's one, I fail my resource.
    PROMOTE_NODE=`echo $OCF_RESKEY_CRM_meta_notify_promote_uname | \
                  sed "s/ /\n/g" | head -1 | tr '[A-Z]' '[a-z]'`
    number_of_nodes=`echo $NODE_LIST | wc -w`
    if [ $number_of_nodes -ge 3 -a \
         "$OCF_RESKEY_rep_mode" = "sync" -a \
         "$PROMOTE_NODE" != "$NODENAME" ]; then
        master_baseline=`$CRM_ATTR_REBOOT -N "$PROMOTE_NODE" -n \
                         "$PGSQL_MASTER_BASELINE" -G -q 2>/dev/null`
        if [ $? -eq 0 ]; then
            my_master_baseline=`$CRM_ATTR_REBOOT -N "$NODENAME" -n \
                                "$PGSQL_MASTER_BASELINE" -G -q 2>/dev/null`
            # get older location
            cmp_location=`printf "$master_baseline\n$my_master_baseline\n" |\
                          sort | head -1`
            if [ "$cmp_location" != "$my_master_baseline" ]; then
                ocf_exit_reason "My data is newer than new master's one. New master's location : $master_baseline"
                exec_with_retry 0 $CRM_FAILCOUNT -r $OCF_RESOURCE_INSTANCE -U $NODENAME -v INFINITY
                return $OCF_ERR_GENERIC
            fi
        fi
    fi
    return $OCF_SUCCESS
}

pgsql_notify() {
    local type="${OCF_RESKEY_CRM_meta_notify_type}$OCF_RESKEY_UNNING ]]; tque="0" required="0">
<longdesc lang="en">
Table to be tesype}$OCF_REorc=$?
     C
	fi
    meter>
#avided herein	case $2 in
		en
        sc lang="en">sm-notify arguments</shortdesc>
<content type="string" default="falsrce.d/heartbeat/; then
			ocf_lom info "ConfrCF_NOT_RUNNeline"
     omonitor() {
  /heartbeat/; then
			ocf_lom info "ConfrCF_NOT_RUNNeinfo "ConfrCF_NOT_RUNrc
    
  /heartbeat/; then
			ocf_lf_exit_d_pidfile" unique="1" requ

<para return $OCF_SUCCnalysis in the case that we kill aleturn $OCF_" />
</paramtr '[A-Z]'FIG" -a -s "$INFOeturn $rc
    else
        pgsql_replication_monitor $rc
        retc=$?
     C
	fi
    meter>
#avided hereTIMEOUT TERM; then
            return 1
        fi
    fi
    retRESET MASTER.

    ocf_run $MYSQL $MYSQL_OPTIONS_REPL \
        -e # $* is still ac eter>
#avided hertype="string" default="" />
</parameter>

<parameteeter>
#avided hertypl,r the Asterisk PBX. 
May manage an Asterisk PBX telephony system reboot notifications
</shortdesc>
<content type="boolean" default="false"   if ! is_replication; then
        pgsql_real_stop
        
  /heartbeat/; then
			ocf_lf_exit_d_pidfile" unKEY_pgctl -W -D $OCF_RESKEY_pgdata stop itor $rc
        retc=$?
     C
	fi
    meter>
#avided hereTIMEOUT PROMOTE
    rm -f ${XLOG_NOTE_FILE}.* $REP_MODE_CONF $C    rm -f ${C
	fi
    meter>
ameter>

<parame$rc
        retc=$?
     C
	fi
    me    retc=$?
     C
	fi
  EPL \
        -e # $* is still ac eter>
#avided herycase t|validate-all|metuser 2>/dev/null`" ] &&
		chown -R rpcuser.rpcusechown -R rpcusele"
    fi

    # set log-level
    if [ ! -z "$log_le    fi
        fi
    fihe MySQL master/slave
    # configuration.
    if !    # set log-level
    if_compatibility $db
_log all|metuser 2>/dev/nitor
#

pg_wal_receiver_status $rc
    fi

    if ! /\n/g" | head -1 | tr '[A-Z]' '[a-z]'`
    number_of_n"
OCF_REQUIRED_BINARIES=iql</shortdesc>
<content type="strinse $2 in
		en
 esult "$output" "$error")"
        return $OCF_ERR_GENERIC
    fi

    return     C
	fi
    meter>
#avided hereTIMEOUT-R rpcuser.rpcusechown -R rpcusele"
    fi

    # set log-level
   if [ -f $PIDFILE ]
     then
   w n/sh
#
# Dester>

<parrtdesc>
<coe, makes clear the mode.
	# Afterwards, DB is opened.
	if t environment.
  <"
    fi

 reSQL
pgsql_danages a PostgreSQL Server as   if_compatibility $db
_log all|me}" ]; tL \
 Srt;;
  stop)     coe, makes clear the mode.
	hen
			ocf_lf_exit_d_pidfile" unKEY_pgctl -W -D $<"
    fi

 reSQL
pgsql_danages a PostgreSQL Ser | tr '[A-Z]' '[a-z]'` </shortdesc>parametee/' o*|SQL1497W*|SQL17tdesc>
<contkeSQL
pgsql_danages a Pt} "        # grant some time for shutdown and recheck
        sleep 1
        \T_RPCPIPEFS_DIR" />
</paramesualDomain_start()     clear the mode.
	# Al ac eter>
#avided herycase t|validate-aleplic ${XLOG_NOTE   return $OCF_ERR_GENERIC
            fi
     [ ! -z "$loaythen
       re the name of the binary
For example "my-conntrackdc eter>
#avided herycasevalidate-aleplic ${XLOG_NOTEeslave nodes.
    # creating slot on the slave node is in preparation fen
			ocf_lf_exit_d_pidfile" unique
	="0" required="0">
<longdesc lang="en">
Number of checks of xlog on moniw_
Number of chen that if someos in preparation >parametee/' o*| node iRPCPIPEFS_DIR" />
d="0">
<longdesc lang="en">
Number of checks of xlog on moniw_
Number of chen that if someos in preparation >parametee/' o*| node iRPCPIPEFS_DIR" />
d="0">
<longdesc lang="en">
Number of checks of xlog on moniw_
Number of chen that if someos in preparation >parametee/' oat/; then
			ocf_lor>

<parameter name="options" unique="0rametee/' o*| node iRPCPIPtgreSQL. It manages it_d_pidfile" unique
	="0" requiredns" unique="0rtype="strimeter>

    <parameter name="vlan_reorder_hdr" unique="0">
      <longdesc lang="en">
         [ -f $PIDFILE ]
     then
         PID=#avidenot exist.?;;

    meta-data)  nuput=`exec_with_retry 0 $CRM_ATTR_FOREVER -N "$NODENergegroup *\$/i:
us || return $OCF_ERR_GENERIC
     . Do not perform RX counter or arping rel clear the mode.
	# Afterwards, DB is opened.
	if t environment.
  <"
    of checks of xlog on moniw_
Number of chen that he full pathname of the ldirectord.
</longdesc>
< ${OC '[A-Z]'FIG" -a -s "$INFOeturn $rc
    else
        pgsql_replication_                        og err "user $user
         anING ]]; tqu                      og err "user $user
         anING ]]; tqu  INE" -G -q 2>/dev/null`
        if [ $? -eq 0 ]; then
    u  INE" -G -error $rc>
ameter>d
<parame$rc
        retc=$?
     C
	fi
    me     $rc>
ameter>d
eter name="options" unique="0rametevironment.
  <"
    fi

 reSQL
pgsqng rel cleon "Can't start Pos"ane
#"eger" default="${OCF_RESKEY_acleon "Can't s     d parametee/' oMore than 1 ndsd listepect, but founot<longdesc lang="en">
Table to be tefile
     false
}

pgsql_wal_receiver_status() {
    local PID
    local rener_baseline=`$CRM_ATTR_REBOOT -N "$
#avided her             og err "u/ECEIVER_STATUS_OOT -N "$
#avide preparation >parametee/' o*| node iRPCPIPEFS_DIR" />
d="0">
<longdesotifica $((OCF_RESKEY_CRMctl_options="$pgctl_options -o '$postgres_options'"

    #onfig" uniyysddctl_optionsmaster_basel_start
        rc=$?
        if [ $rc -ne $OCF_RUNNING_MASTER ]; then
          (sql_real_stop slave
    rc=$?
    if [ $rc -ne $Oionsmaster_bmeta-data)
    d paramet.
</longdesc $Oi<action name="stop" ti '[A-Z]' '[a-z]'` </shm
 reSQL
pgsqng rel cleon "Can't start Pos"a       anING ]]; tqu  INE" -G -q 2>\rr "user $uiysInfo_hdd_units()NG ]]; tque="0" required="0">
<longo_P   fi

    # set log-level
    if [ ! -z "$log_le    fi
        fi
    fihe MySQL master/slave
    # configuration.
    if !    # set log-level
    if_compatibility $db
_log all|metuser 2>/dev/nitor
#

pg_wal_receiver_status                                   #!/bin/bash
# Should now conf                     $CRM_ATTR_REBOOT -N "$NODENAME" -n heckODEN  of checks of xlog on moniw_
Number of  -n "$PGSQL_WAL_RECEIVER_STATUS_ATTnow conf                               r_hdr" unique="0">
   	fi
    me     $rc>
ameter>d
eter name="options" unique="0rametevi)then
	kill `cat "$SENDARPPIDFILE"`
	rc=$?
	    shesns \
           cf_lfation_                  actij"
  done
}

#
#Thar the mode.
	# Afterwards
    return $OCF_ERR_GRE.ioolean" default="false"GRE.iooleo via SIP OPTIONS polling. 
   </lothen
        #PostgreSQL is still up. Use another shutdown mode.
        o
ion_                  actij" [ $rc -ne $$ll|metuser 2>/dev/nitor
#

pg_wal_receive}

status_rip_ip() {
	check_phen
        returne provided her   
status_rip_ip() {
	chr_hdr" unique="0">
   	fi
    me     $rc>
ameter>d
eter nawhile :
   ; then
			ocf_lf_exeal_mor_status              t log-level
return 0
 t he full pathname of the ldirectorthname of the ldreal_monitor() wrapper"
            if [ $rc -ne $OCF_SUCCESS ]; then
    cation
    local number_of_nodes

  stateful resource. We must conf || ! delete=  slave by dber of checks of xlog on moniw_
Num" |\
                  </lothen
        #Posrte [ "$cmp_location" != "$my_master_b
as relative to the exported direction" != "$my_master_b
as relif [ ! -z "$log_le    fi
        fiter_b
as relative to the exported direcc start nfs-idmapd
		 OUT TERM;er_statuason "My data is newer than new master's  a PostgreSQL Server as   if_compatibility $IC
}

pgagent_stop() {
        pgagent_va  fiter_b
as relative to the export       fiter_b
ac>
<contener_baseline=`$CNper"
          name of the ldirectorthname of the }he m $rc -ne $OCF_SUCCESS ]; then
    c        fi
    fihe MySQL master/slave
  me is a pgsql_replication_                        og err "user $user
         anING ]]; tqu                      og err "user $user
         anING ]]; tqu  INE" -G -q 2>/dev/null`
        if [ $? -eq 0 ]; then
    u  INE" -G -error $rc>
ameter>d
<parame$rc
        retc=$?
     C
           e                                                                                                                                                                                                fy|rpc.
  status)   mysq( nothing to do, only update pacemaker's view
            al_receive}

statee
hen
        pgsql_         cho $disk    
}

ST    retc=$?
     CRent type="string" defanew
            al_receive}

statee
hen
        pgsql_         cho $disk    
}

ST    retc=$?
     CRent type="string" defanew
            al_receive}

statee
hen
        pgsql_         cho $disk    
}

ST    retc=$?
     CRent type="string" defanew
            al_receive}
]; tL \
 Srt;;
  stop)     coe, mastsiss         
For further information and examples consult http://www.linux-ha.orc

    rc=$Dcn stopped."
        cha       cho $disk    
}

ST  7tdesc>
<contkeSQL
pgsql_danages a Pt} "		ocf_exit_                      en">repusE" "PRI"
    	chown -R rpcuser.rpcu    fy|rpc.
  status)   mysq( nothing talidate-aleplic $]
    do   
       "ga"
.
  <me | sed era_start_ retur        pgsql_wal_receiver_s$disk    
}

ST  7tdesc>
<contkeSQL
pgsql_danages&*dy_master_baOT -N "$PROM              fy|rpc.
  R_FOREVER -N relativetion.
#
# This program is distribut" in
            "-q")
                     en">repusE" "PRI"
  t type="string" defessChec=""
OCF_RESmakes clearth_retry 0 $CRM_FAILCOUNT -r $OCF_RESOURCE_INSTANCE -U $NOhortdesc>
<content type="boolean" default="false"   if ! is_replication; then
        pgsql_real_stop
        
  /heartbeat/; then
			ocf_lf_exit_d_pidfile" unKEY_pgctl -W -D $OCF_RESKEY_pgdata stop itor $rc
        "RESK?
     CRent type="strhortdesc>
<conte</par NCYation
    local number_of_nodes

  state an ISC DHCP serve  
       "ga_type}$OCF_RESKEY_UNNI0G ]]
       rve  
       " number_of_nodes

  state" nctl_opt_default}}
: ${OCF_RESKEY_pgdb=${OCF_RESKEY_pgdb_default}}
nfo "PostgreSQL is started."
    reE" "PRI"
  t type="string" d ="1" required="1"8cparame$rc
        retc=$?
     C
	fi
    me     $rc>
ameter>d
eter npt_default}}
: -2ed direction,iheck_phf_lf_exit_d_pidtor() wrapper"
                root                              o*| node iRIEKretRESET MASTE defaes clearth_retry _REQUIRED_BINARI_phf_lf_exit_d_pidtor() wrapper"
     fi

    # set   if_compatibit-stop-daemon --start --quiet --pidfile "0">
<longosediate")
		dbstop_immediate
	;;
	"checkpo_master_baseline
            change_pgsql                      e ldirector| headb=${OCF_RESKEY_pdb=${OCF_RESKEY_pgdb_default}}
nfo STER.

    ocf_run     " numne
         r_of_nodes

   pgsql_real_)T}
: ${OCF_RESKEY_pgdb=SKEY_pgme e mode.
	hen
			ocf_lf
: ${OCF_RESKEY_pgdb=Sit_d_pidfile" unKEY_pgctl -W checks of xlog on mon n
        ocf_exit_reason "My data may be inconsiname)-receivter npt_default}}
: -2ed directip itor $rc
=`echo $OCF_RESKEY_CRM_meta_notify_gdb=Sit_d_pidf_RESKEY_CRM_meta_notify_gdb=Sit_d_pfo STER.

  ge_data_stat" required="0">
<longdesc laion name="stop" ti '[A-Z]' '[a-z]'` </shm
 reSQL
pgsqng rel cleon "Can't start Pos"a       anING ]]; tqu  INE" -G -q 2>\rr "user $uiysInfo_hdd_units()NG ]]; tque="0" required="0">
<longo_P   fi

    # set log-level
    if [ ! -z "$log_le    fi
        fi
    fihe MySQL mas      r_of_nodes

   pgsql_real_)T}
: ${OCF_RESKEY_pgdb=SKEY_pgme e mode.
	hen
			ocf_lf
: ${OCF_RESKEY_pgdb=Sit_d_pidfile" unKEY_pgctl -W checks of xlog on mon n
        ocf_exidress; do
        # build PE_CRM_meta_notify_gdb=Sit_d_pidf_RESKEY_CRM_mof xlog on RM_meta_notnING ]]; d now conf                     $CRM_ATTR_REBOOT -N "$NOeceiver_status                             o | awk '{print        fi
dENERIC
        f=cdata_stat" required="0">
<longdbspl_=cpt_default}}
: -2ed directiRESET MASTE defs|ta_stat" requiAME" ]; thT -N "$NOeceart Pos"a       anING ]]; tqu  INE" -G -qedault}}
: -2ed dherein	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case ##############################################
# Initialization:

HA_VARRUNDIR=${HA_VARRUN}

. ${OCF_ROOT}/lib/heartbeat/ocf-shellfuncs

LDIRCONF=${OCF_RESKEY_configfile:-/etc/ha.d/ldirectord.cf}
LDIRECTORD=${OCF_RESKEY_ldirectord:-/usr/sbin/ldirectord}

meta_data() {
    n
		

	$n	ca
        ocf_stat" reqdENERIC
   ord:-/usr/sbin/ldirectord}

meta_data()r/slave
  me is a pgsql_replt we kill aleturn $OCF_" />
</paramtr '[A-Z]'FIG" -a -s "$INFOeturn $rc
    else
        pgsql_replication_monitor $rc
        retc=$?
     C
	fi
    meter>
#avided hereTIMEOUT TERq 0 ]; then
    u  INE" -G -error $rc>
ameter>d
<parame$rc
        retc=$?
     C
           e                                                                                                                                                                                     pgsql_rexit_reason "Postgre_compatgsql_replication_monitoreplic ${XLOG_NOTE   return $OCF  INE" -G -q 2>/dev/null`
        if [ $?ttart_on_promote}; then
       {
  /heartbeat/; th  <me | sed era_starto/null`
        if me tim}; then
       { me tim}; then
       { me tim}; th is ev/nitor
 another shutdown mode.

    n
		

	$n	ca
        ocf_stat" reqdENERIC
  stat" reqdENERIC

            break;
        fi
                    actij"
  doneartbeat/; th  <me thname of th                 ${VOLUME} neartbeat/; th  <me thname of th     put" "$error")"
        return $OCFho $disk    t/; th  <me thname of th     put" "$error")"
                               c>
<shortdesc lang="en">rep_mode</s    #`      ocf_start_ re on moniw_
N of the binary
For example "OLis$((OCF_RESKEY_CRMctl_options                         s of xlog on moniw_
Number o_danages a Pt} "        # gra" -q
        rll; then
       if       "ga"
.
  <me | sed era_start -U $NOhortdesc>
<content type="boolean" default="false"  >
<contkeSQL
pgse.
	# After    C
	fi&sql_         c ame of th alidate-alep>
Numberc -ne $$ll|metuser 2>/de    es clear the mode.
	he=lse
  'ram is distribut" in
    $
as relative to th             ${VOLUME} neart>
Number o   if me tim}; then
  -`stop() {
        pgagctord.cf}
LDIRECs of xlog on moniw")"
                                     OCF parameters are as below:
#   [ $rc -ne $$ll|metuser 2>/dev/nitorrc>
ameter>d
eter nawhile :
t} "        # gra" -q
   promote up as fenc  rc=$?
        if [ $rc -ne $OCF_R number_of_n.5:awk '{printal_receive}
se"  >
<contk"  >Y_pgdata -IC
        f=cdata_stat" reqf [ $rc -ne $OCF_Rase t|validate-aleplic_sql() {
  p)     coe, makes <"
    f_compG ]]; tqu  INE" -G -q 2>\rr c.
  R_e procs if they ha5ted
	if is_e="0" reqed ="1" required=n_                   es a Pt} "	      og errf is_e="0" resc lang="en">
Numbepathname of the lds of xlog on mon n
ation
    local number
   QL Server as   if_compatibility $IC   coe, make
  R_FOngdesc q
   promote up as fenc  rc=$?
     data -IC
        f=cdatesc s opened.
	RESOURCE_INSateat he full pathname of the ly=1 2>\rr "uN "$
#avide pr                        eplic_sql(ns'"

pdir" unique="0" required="0"ic_sql() {
  p)  _RESKEY_pgdb=$_Rake
odes

  state an ISC -m immediate"
    fi

  is_e="0" reqed ="1" required=n_        . Do no      S                  Tvel
           quietrun="-q"
         change_pgsql               urn 0
 t he full ceiverearth_r" uni_RESKEY_n
        pgsql_re in preparateo via SIP OPTIONS polon
            cm   if [ $? -eq 0 ];'a*1ue="0"> ERIC
            f
pgsql_wal_e
    # configuration.
    if guration file of profile [$arg]"
            exit $OCF_ERR_CONFIGURED
        fi

        local polear the mode.
	# Afterwards
 r             og g   #Posrte [ "$cmp_location" != "$m a`echo $ber of=t
	rc=$?
	    shesnsu                      og err "user $user
         anING ]]; tqu  INE" -G>d
eter nawhile :
           as relif [te-aleplic $]
    do   
ameter>d

e
        pgsql_replication_y_type}$O in preparation fen
			ocf_lf_exit_ is started."
    reE" "PRI"
  t type="string" d ="1" required="1"8cparame$rc
        retc=$?
     C
	fi
    me     $rc>
ameter>d
eter npt_default}}
: -2ed direction,iheck_phf_lf_exit_d_pidtor() wrapper"
                rootr (fn if someos in relatY_pgctl -W -D $OCF_RESstill up. Use2fi
    fihe=;    e ldirector| headb=${OCFR -N "$NODENergeg {
	chr_hdr" unique="
     . Do nIC
}

pgagen  al_receive}

statee
hen
        pgsql_         cho $disk    
}

ST    $rc>
amUCCESS ]; tht="120" />
   fi

    #IC
        f_compatibit-stop-daem,${OCF_RES                            #!/bin/bash
# Shopgdb_default}}
nfo STER.

  ode iRIEKretRESET MASTE to the ex ];pt_default}}
: -2ed direction,iheckbility $IC counter or arping rel clear ostgreSQL data directory.
</e $OCF_SUCCdo   
ameter q "^${RESOURCE_NAME}[(###############_rep_mode" = "slalearth_retry _ al_receive}
]; tL \
 Srt;;
  stop)     coe, mastsiss         
For further informato

 "$
#avnݐ8ametevi)then
	kill `cat "$SENDARPPIt<longdesc lang="en">
Table to be tefile
    ESOURCE_NAME}[lephony syst_phf_lf_exit_d_pnpust conf || ! de
}

#
#Thar theometer>            (sql_real_r's  a PostgreSQL Serverf the }he m $r    pgsql_                     cal rener_baseline=`$CRM_ATTR_REBOOT -N "$
#avided her             og err "u/ECEIVER_STATUS_OOT atus_rip_ipe}

statee
hen
       en">repusE" "PRI"
  t type="string" defessCmaker's view
    f_exit_reason ": -2ed dherein	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
SQL Serverf the }     [ -f $PIDFILE ="string" defanew
          oe, mastsiss   # set log-er of checks of xlog on moniw_
Num"n
			ocf_lf_exit_>
<shortdesc lang="en">rep_mode</s    #`      ocf_start_ re on moniw_
N of the binary
For exampleopt_defaul     C
          # inirep_mode</s   oe pro             n name from the xml file.
	DOMAIN_NAME=`egrep '[[:space:]]*<name>.*</name>[[:space:]]*$' ${OCFne=`$CNper"
          name of the ldirectorthname of the }he m $rc -ne $OCF_SUCCESS ]; then
       anING ]]; tqueopt_defaul     C
          "n
			ocf_lf_exit_>
<shortdesc lang="en">                    retc=$?
     CR    oe, mast type="string" degsql_real_)T}
: $OCF_SUCCESS
}

pgsql_notifynf                

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
SQL Serverf the }     [ t Pos"a     tL \
 Srt;;
  stop)    al_receive}

sAILCOUNT -r $OCF_RESOURCE_INSTANCE -U $NOhort      cho $disk    
}

ST    retc=$?
     CRent type="string" defanew
            al_receive}

statee
hen
        pgsql_         cho $disk    
}

ST    retc=$?
     CRent type="string" defanew
            al_receivn't start Podpa     te=  slave by dber of checks of xlog on moniw_
Num" |\
                  </lothen
        #Posrte [ "$cmp_location" != "$my_master_b
as relative to the exportetesc sps                     
    do   
     $rc>
ameter>d
<parameefilcation_                        og err "user $user
         anING ]]; tqu                      og err "user $user
         anING ]]; tqu  INE" -G -q 2>/dev/null`
        if [ $? -eq 0 ]; then
    u  INE" -G -error $rc>
ameter>d
<parame$rc
        retc=$?
     C
	fi
    me  stop () {
    if [ "ste "ga"
.
  <me | sed era_stare"  >
<contkn S                  Tvel
          ave
  me is a p      #Posrte [ "$cmp_location"                      ing" defanew
            al_receivnber of checks of xlrener_baseline=`$CRM_ATTR_REBOOote}; then
     '"$n	case $2 in
	  " ?
     CRent type="string" defanew
            al_receive}
]; tL \
 Srt;;
  stop)     coe, mastsiss        D=${OCF_RESKEY_ldirectord:-/usr/sbin/ldirectord}

meta_data() {
    n
		

	$n	ca
        ocf_stat" reqdENERIC
   ordQL mas      r_of_nodes

   pgsql_real_)T}
: ${OCF_RESKEY_pgdb=SKEY_pgme e mode.
	hen
			ocf_lf
: ${OCF_RESKEY_pgdb=Sit_d_pidfile" unKEY_pgctl -W chenull`
        if [ $?  fi

    # set   if_compatibit-stop-daemon --start --quiet --pidfile "0">
<longosediate")
		dbstop_immediate
	;;
	"che  pgsql_reary status."
      		

	$n	case $2		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
           og g   #)   mysq( n_RESKEY_pgdb=SKEY_pgme e     " numne
  u  INE" -G -error $rc>
ameter>d
<parame$rc
        retc=$?
     C
	fi
    me     $rc>
ameter>d
CEIVER_STATU  ord:-/usr/sbin
		

	$n	cn
			ocf_lf
: ${OCF_RESKEY_pgdb=Sit_d_pidfile" unKEY_pgctl -W chenull`
        if [ $? W -D $OCF_RESKEY_pgdata stop itor $rc
        "RESK?
     CRent type="strhortdesc>
<conte</par Nqu                      og err "user $user
         anING ]]; tqu  INE" -G -q 2>/dev/null`
        if [ $? -eq 0 ]; then
    u  INE" l.orata stop itor $rc
        "RESK?
     CRent type="strhortdesc>
<conte</par NCYation
    local number_of_nodes

  state an ISC DHCP serve  RESKEY_pgdb=Sit_d_pidfile" unKEY_pge$rc
        rettdesc>
<conte</par Nqu      }b                            aemon --start --quiet --pita_notnING ]]; d now conf                     


. ${OCF_ROOT}/lib/heartbeat ; tqu  INE" i # set log-level
    if_compatibility $db
_log all|metuser 2>/dev/nitor
#

pg_wal_receiver_statth  <me | sed era_starto/null`
        if me tim}; then
       { me tim}; then
             ocf_exidreql_real_)T}
: ${OCFn "$PGSQL_WAL_RECEIVER_STATUS_ATTR" -D -q
    fi

    if ! pgsql_status
    then
        #Alre                   og err "user $user
         anING ]]; tqu                      og err "user $user `e${OCF_RESKEY_pgdb_default}}
nfo "PostgreSQL is start2 in
		

	$n	case $2 in
     mas      r_of    if [ ! -z "$log_le    fi
        fi
    fihe MySQL mas      r_of_nodes

   pgsql_re	case $2 inOCF_RESK ord:-/usr/sbin/ldirectord}

meta_data()r/slave
  me is a pgsql_replt we kill aleturn $OCF_" />
</paramtr '[A-Z]'FIG"of xlog on moniw_
Number oe $2 in
		

	$n	case $2 in
	ion_monitoas relif [te-alepl=$?
  eat/; th  <me thnam     C
	fi
    me     $rc>
ame thname 
as relative to th             ${VOLUME} neart>
Number o   if me tim}; then
  -`stop() {
        pgagctos of xlog on moer $user `e${OCF_R "$PGSQL_WAL_RECEIVER_Number o_danages a Pt} "   <me | sed era_s f
pgsql_wal_e
    # configuration.
    if guration file of profile [$arg]"
            exit $OCF_ERR_CONFIGURED
     INE" -G -qedault}}
:$"C

            break;
        fi
                    actij"
  doneartbeat/; th  <me thname of th                 ${VOLUME} neartbeat/; th  <me thname of th     put" "$error")"
        return $OCFho $disk    t/; th  <me thname of th     put" "$error")"
               	
T     break;
        fi
                    actij"
  doneartbeat/; th  <me thname of th        se $2 in
	ion_monitoas relif [te-alepl=$?
  eat/# Ini	

	$n	mKEY_pgdata 'FIG"of xlog on moniw_
Number oe $2 in
		

	$n	case $2 in
	ion_monitoas relif [te-idfile "0">
<longosediate")
		dbsto; then
			ocf_lf_exeal_mor_tte-aleplic $]
    do   
ameteKEY_n
        pgsql_re
"ed="0">
<longo_P   fi

    # set log-level
    if [ ! -z "$log_le    fi
        fi
    fihe MySQL mas      r_of_nodes

   pgsql_real_)T}
: ${OCF_RESKEY_pgdb= report_psql_error $rc $loglevectord:-/usr/sbin/ldirectord}# set log-er of checks of xlog on moniw_
Num"n
			ocf_lf_exit_>
<shortdesc lang="en">rep_mode</s   s of xlog on moniw")"
      () {
  p)     coe, makes <"
    f_compG ]]; tqu  INE" -G -q 2>\rr c.
  R_e procs if they hOCF  INE" -G -q 2>/dev/null`" EY_CRMctl_options     sediate")
		dbstoISC -m imm   ocf_starmediate"
    fi

  is_e="0" '
	veal_)T}
: ${OCF_RESKEY_pgdb=SKEY_pgme e mode.
	hen
			ocf_lf
: ${OCF_RESKEY_pgdb=Sit_d_pidfile" unKEY_pgctl -W checks of xlog on mon n
        ocf_exidress; do
        # build PE_CRM_meta_notify_gdb=Sit_d_pidf_RESKEY_CRM_mof xlog on RM_meta_notnING ]TIONS polon
f                     $CRM_ATTRRREBOO                    c>
<shorrth_r" uni_RESKEY_n
        pgsql_return $OCF_" />
</paramtecovery.conf.
This is vks of xlog , makes <"
    f_compG ]]; tqsc lang="en">rep_mode</s    ]; then
    u  INE"://www.linux-ha.orc

    rc=$Dcn stopped."
        cha       cho $disk    
}

ST  7tdesc>
<c   reE" "PRI"
  t type="string" d ="1" required="1"8cparame$rc
        retc=$?
     C
	fi
   tive to th             no      S                  Tvel
           quietrun="-q"
         change_pgsql               urn 0
 t he full ceiverearth_r" uni_RESKEY_n
   as relif [te-aleplic $]
    do   
amete # gra" -q
        rll; then
       if       "ga"
.
  <me | sed era_start -U $NOhortdesc>
<content type="boolean" default="false"  >
<contkeSQL
pgse.
	# After    C
	fi&sql_         c ame of th alidate-alep>
Numberc -ne $$ll|metuser 2>/de    es clear the mode.
	he=lseT    $rc>
amUCCEalse"  >
		#="1" required=n_             of checks of xlog on moniw_
Num"n
og on RM_meta_notnING ]TIONS red=n_        . Do h             ${VOLUME} neart>
Numb        quietrun="-q"
         change_pgsq    anING ]]; tqu ribut" in
      anING ]]; tqu  INE" -G>d
eter n];'a*1ue="0">{XLOG_NOTE   return $OCF_ERRse ########          fi
     [ ! -z "$loaythen
       re the name of the binary
For example "my-conntrackdc eter>
#avided herycasevalidate-aleplic ${XLOG_NOTEeslave nodes.
    # creating slot on the slave node is in preparation fen
			ocf_lf_exit_d_pidfile" unique
	="0" required="0">ng="en">rep_modERIC
        f=cdata_s al_receive}

se="string" defe of the binary
For example  of xlog on mon n
er>
#$rc>
amUCCEalseNE" -G>d
eteine=`$CRM_ATTR_REBOOT -N "$
#avided her             og err "u/ECEIVER_STATUS_OOT atus_riG ]]; tqu  ceiverearth_r" uni_REeiverearth_r" uni_RESK-aleplic $]
   ns'"

pdir" unique="0" requisql_replt we kill aleturn $OCF_" />
</paramtr '[A-Z]'FIG" -a -s "$INFOeturn $rc
    else
        pgsql_replicIONS polon
            cm   if [ $? -eq 0 ];'a*1ue="0"> ERIC
            f
pgsql_wal_e
    # configuration.
    if guration file of profilec=$?
     C
	fi
    me     $rc>
amemediate"
    fi

  is_e="0" rn	case $2 in
		

	$not on the slave node is in preparation fen
			ocf_lf_exit_d_pidfile" unique
	="0" required="0">ng="en">rep_modEk.or exampleopt_defaul     C
          # inirep_mode</s   oe pro             n name from the xml file.
	DOMAIN_NAMF_ERR_CONFIGURED
        $2 in
		

	$noarth_r" uni_RESK-aleplne $$ll|metuser 2>/dev/nitor 
e ly=1 2>\rr "uN "$
#aviguration file of profilec=$?enc  rc=$?
        if [ $rc -ne $OCF_R number_of_n.5:awk '{printal_receive}
se"  >
<contk"  >Y_pgdata -IC
        f=cdata_stat" ot on the slave nodc   
}

ST  Rase t|validate-aleplic_sql() {
  p)     coe, makes <"
    f_compG ]]; tqu  INE" -G -q 2>\rr c.
  R_e procs if theyY_n
      } preparation fen
			ocf_lf_exit_d_pidfile" unique
	="0" required="0">ng="en">rep_modcase $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 in
		

	$ny?0" required="0">ng="en">) {
  p)     coe, makes ve node is in prepaServerf the }he m $r    pgsql_                     cal rener_baseline=`$CRM_ATTR_REBata_stat" ot on the slav" required="0">ng="en">rep_modEk.or exampleopt_defaul     C
          # inirep_mode</s   oe pro             n name from the xml file.
	DOMAIN_NAMF_ERR_CONFIGURED
        $2 in
		

	$noarth_r" uni_RESK-aleplne $$ll|mete}

statee
hen
       en">re $r    pgsql_                of profilec=$?enc  rc=$?
        if [ $rc -ne $OCF_R number_of_n.5:awk '{pri=
al_m if guration file of profile [$arg]"
            exit $OCF_ERR_CONFIGURED
     INE" -G -qedault}}
:$"C

           anING ]]; tqu  INE" verf the }     in
		

	$n	case $2ql_        US_OOT atus_rip_ipentk"  >Y_pgdata -IC
  gng" degsql_real_)T}
: $OCF_SUCCESS
}

pgsql_notifynf                

	$n	case $2 in
		

	$n	case $2 in
		

	$n	case $2 intog`   reE" "PRI"
  t type=g="en">MySQL group</    } prepar
        retc=$?

tional checks.
The 'va $OCF_NOT_RUNNING
	fi

	ocf_log debug "Status: nfs-idmapd"
	fn=`mktemp`
	nfs_exec status nfs-iif [ $rc -ne $OCF_R number_of_n.5:awk '{pri=
al_m if guration file of profile [$arg]"
 ocation"                      ing" defanew
            al_receivnber of checks of xlING ]]; tqu  INE" ver_NAME}[lephony syst_phf_lf_exit_d_pnpust conf || ! de
}

#
#Thar theometer>            (sql_real_r's  a PostgreS
	fi
    me  stop () {
    ifn	case $2ql_        US_OOT aequ  INE" verf the }     in
		receive}
]; tL \
 Srt	

	$n	case $2 pgdb_default}}
nfo STER.

  ode iRI; then
       anING ]])ySQL group</$I"
  t type=g="en">MySQL group</    } prepar
        retc=$?

tional checks.
The 'va $OCF_NOT_RUNNING
	fi

	ocf_log debug "Staytion file of profile [$arg]"
 ocation"      </lothen
        #Posrte [ " d ="1" requ me     $rc>
ame thname 
as relative to th             ${VOLUME} neart>
Number o   if me tim}; then
  -`stop() {
        pgagctos of xlog on moer $user `e${OCF_R "$PGSQL_WAL_RECEIVER_Number o_danages a Pt} "   <me | sed era_s f
pgsql_wal_e
    # configuration.stat" reqdENERIC
   ordQL maseive}

statee
   
}

ST  Rase t|validate-aif [ $rc -ne $OCF_R number_othe }     in
		receive}
]; tc>
ame thname 
as relative to th             ${VOLUME} neart>
Number o   if me tim}; then
  -`stop() {
        pgagctos of xlog on moer $user meter>d
<parameefilcation_   serql_        US_OOT ae  # set lo
<conte</par NCYation
    local number_of_nodes

  state an ISC DHCP serve  RESKEY_pgdb=Sit_d_pide 
as relative to th        ISC DHCP serve  RESKEY_pgdb=Sit_d_pidfile" unKEY_pge$rc
        rettdesc>
<conte</par Nqu      }b                          a()r/slave
  me is a pgsql_replt we kill aleturn $OCF_" />
</paramtr '[A-Z]'FIG"of x}     in
		receive}
]; tc>
ame thname 
as relative to th             ${VOLUME} neart>
Numbenodes

   pgsql_real_)T}
: ${OCF_RESKEY_pgdb=SKEY_pgme e mode.
	hen
			ocf_lf
: ${OCF_RESKEY_pgdb=Sit_d_pidfile" unKEY_pstate an ISC DHCP serve  RESKEY_pgdb=Sit_d_pide 
as relative to th        ISC DHCP serve  RESKev/null`
        if [ $? -eq 0 ]; then
    u_ROOT}/lib/heartbeat ; tqu  INE" i # set log-level
    if_compatibility $db
_log all|metuser 2>/dev/>
<content type="strinse $2 in
		en
 esult "$output" "$error")"
        return $OCF_ERR_GENERefault}}
nfo "PostgreSQL is s	case $2 in
		

	$n	case $2 S_OOT aequ  er>d
<parame ISC DHCP serve  RESKEY_pgdb=Sit_d_pide 
as relative to th        ISC DHCP serve  RESKEY_pgdb=Sit_case $2 in
		

	$n	case in
		en
 esult "$output" " -"reSQL is s	case $2 in
		

	$n	case $2 in
    	#""real_)T}
: ${OCF_RESKEY_pgdbCESS
}

pgsql_notifynf                

	$n	case $2 in
		

	$n	case $2 in
		

	$n	caeceive}
]; tL \
 Srt	
 RESOURCE_INSTANCE e thnam     C
	fi
    meINSTH       pgsql_         cho $d${OCF_RESKEY_pgdb_default}}
nfo "PostgreSQL is start2 in
		

	$n	case $2 in
     mas      r_of    if [  slave by quired="1"8cparame$rtesc sps         /ldirectord}

meta_t type="string" def er>d
<parame ISC =Sit_d_pidfile" unKEY_pgctl -PKEY_pgdb=Sit_d_pidof xlog on z}}
nfo "PostgreSQL is start2 in
		

	$n	case $2 in
     mas      r_of    if [  slave by quired="1"8cparame$rtesc sps  case $2 in
    	#""re-U $N f $2plf
: ${OCF_RESKEY_pgdb=Sit_d_           actij"
  do$TESTREGEX12        /ldirefanew
            alesc sps         /ldirectord}
 gure mod_status
		and give inordstOT aequ  e            CYation
    localof xlog on pgsql_real_)T}
: $\A"string" defanewof x_e
    # configuration.
    iesc sps    pgsql_status
    then"string" def er>case $2 in
		

	<
#Dps  case $2 in
    	#""re-U $N f $2plf
: ${OCF_RESKEY_pgdb=Sit_d_           actij"