#!/bin/sh
#
#      The failcount of the pg-rex resource that is the running by
#      the node specified by the argument is changed to INFINITY. 
#      When the failcount cannot be changed, fence the node
#      specified by argument.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  
# 02110-1301, USA.
#
# Copyright (c) 20011 NIPPON TELEGRAPH AND TELEPHONE CORPORATION
#
###############################################################################

#
# Initialization
#
COMMAND_NAME=`basename $0`
HOST_NAME=`uname -n`
CIBADMIN="/usr/sbin/cibadmin"
CRM_RESOURCE="/usr/sbin/crm_resource"
CRM_FAILCOUNT="/usr/sbin/crm_failcount"
CRM_FENCE="/usr/sbin/crm -F node fence"
AGENT_TYPE="pg-rex"
FAILCOUNT="INFINITY"
DEBUG_LOG=0

#
# log: log line is output.
#
log() {
    loglevel=$1
    shift

    [ $loglevel = "debug" -a $DEBUG_LOG -eq 0 ] && return 0

    dist=1
    if [ $loglevel = "error" ]; then
        dist=2
    fi
    echo "`date "+%b %d %T"` $HOST_NAME $COMMAND_NAME: [$$]: $loglevel: $*" >&$dist
}

#
# get_primitive_id: get the primitive id for the pg-rex resource.
#
get_primitive_id() {
    line=`($CIBADMIN -Q | grep "<primitive" | grep "type=\"$AGENT_TYPE\"") 2>/dev/null`
    if [ $? -ne 0 ]; then
        return 1
    fi
    primitive_id=`(echo "$line" | sed 's/^.*id="\([^"]*\)".*$/\1/') 2>/dev/null`
    echo $primitive_id
    return 0
}

#
# get_clone_max: get the "clone-max" value of pg-rex resource.
#
get_clone_max() {
    clone_max=`$CRM_RESOURCE -r $1 -g clone-max --meta 2>/dev/null`
    if [ $? -ne 0 ]; then
        return 1
    fi
    echo $clone_max
    return 0
}

#
# resource_failed: make the pg-rex resource break down.
#
resource_failed() {
    $CRM_FAILCOUNT -r $1 -U $2 -v INFINITY >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        return 1
    fi
    return 0
}

#
# fence_node: fence the node.
#
fence_node() {
    $CRM_FENCE $1
    if [ $? -ne 0 ]; then
        return 1
    fi
    return 0
}

#
# Main
#
if [ `id -u -n` != root ]; then
    log error "$COMMAND_NAME must be run as root."
    exit 1
fi

if [ $# -ne 1 ]; then
    log error "A node isn't specified."
    log info "Usage: $COMMAND_NAME <node>"
    exit 1
fi
node=$1
log debug "node=$node"

if ! primitive_id=`get_primitive_id`; then
    log error "Couldn't get the primitive id of pg-rex resource."
    exit 1
fi
log debug "primitive id=$primitive_id"

if ! clone_max=`get_clone_max $primitive_id`; then
    log error "Couldn't get the 'clone-max' value of the pg-rex resource."
    exit 1
fi
log debug "clone-max=$clone_max"

index=0
while [ $index -lt $clone_max ]
do
    resource_id=$primitive_id:$index
    log debug "resource_id=$resource_id"

    if resource_failed $resource_id $node; then
        log notice "The failcount of $resource_id on $node was changed to INFINITY."
    else
        log error "The failcount of $resource_id on $node couldn't be changed to INFINITY."
        if fence_node $node; then
            log notice "$node was fenced."
        else
            log error "$node couldn't be fenced."
            exit 1
        fi
        break
    fi

    index=`expr $index + 1`
done

exit 0
