#!/bin/sh
#
#       ActCheck OCF RA. Does nothing but call hb_actmon_client
#       at start operation, with resource id as 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) 2009 NIPPON TELEGRAPH AND TELEPHONE CORPORATION
#
# OCF parameters:
#  OCF_RESKEY_cmd_opt - Additional options for hb_actmon_client (-C, -V).
#
#######################################################################
# Initialization:

. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

unset LC_ALL; export LC_ALL
unset LANGUAGE; export LANGUAGE

ACTCHECK=/usr/lib/heartbeat/hb-actmonitor/hb_actmon_client

usage() {
    cat <<END
    usage: $0 {start|stop|monitor|meta-data}
END
}

meta_data() {
    cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="actcheck">
<version>1.0</version>

<longdesc lang="en">
This is a resource script for ActCheck. It does absolutely nothing except
keep track of whether its running or not, but it called hb_actmon_client
at start operation.
</longdesc>
<shortdesc lang="en">actcheck resource agent</shortdesc>

<parameters>
<parameter name="cmd_opt" unique="0" required="0">
<longdesc lang="en">
Additional hb_actmon_client options (-C, -V). Default is "".
</longdesc>
<shortdesc lang="en">cmd_opt</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="600s" />
<action name="stop" timeout="10s" />
<action name="monitor" depth="0" timeout="10s" interval="3600s" start-delay="0" />
<action name="meta-data" timeout="5s" />
</actions>
</resource-agent>
END
}


#
# START: called hb_actmon_client.
#
actcheck_start() {
    ocf_log info "actcheck_start: started..."

    actcheck_monitor
    if [ $? = $OCF_SUCCESS ]; then
	return $OCF_SUCCESS
    fi

    touch ${OCF_RESKEY_state}

    if [ -z "$OCF_RESKEY_CRM_meta_clone" ]; then
	$ACTCHECK ${OCF_RESKEY_cmd_opt} -R ${OCF_RESOURCE_INSTANCE}
    else
	$ACTCHECK ${OCF_RESKEY_cmd_opt} -R ${OCF_RESOURCE_INSTANCE%:${OCF_RESKEY_CRM_meta_clone}}
    fi

    if [ $? -ne 0 ]; then
	return $OCF_ERR_GENERIC
    fi

    ocf_log info "actcheck_start: complete."
    return $OCF_SUCCESS
}

#
# STOP
#
actcheck_stop() {
    ocf_log info "actcheck_stop: started..."

    actcheck_monitor
    if [ $? = $OCF_SUCCESS ]; then
	rm ${OCF_RESKEY_state}
    fi
    ocf_log info "actcheck_stop: complete."
    return $OCF_SUCCESS
}

#
# MONITOR
#
actcheck_monitor() {
    ocf_log debug "actcheck_monitor: started..."

    RCODE=$OCF_NOT_RUNNING

    if [ -f ${OCF_RESKEY_state} ]; then
	RCODE=$OCF_SUCCESS
    fi
    ocf_log debug "actcheck_monitor: complete."
    return $RCODE
}

#
# main process
#

if [ $# -ne 1 ]; then
    usage
    exit $OCF_ERR_ARGS
fi

: ${OCF_RESKEY_state=${HA_RSCTMP}/actcheck-${OCF_RESOURCE_INSTANCE}.state}

OP=$1

# These operations do not require instance parameters
case $OP in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
usage)		usage
		exit $OCF_SUCCESS
		;;
start)		actcheck_start;;
stop)		actcheck_stop;;
monitor)	actcheck_monitor;;
*)		usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $OP : $rc"
exit $rc
