#!/bin/sh
#
# Copyright (c) 2008, 2009 NTT COMWARE CORPORATION
# All rights reserved.
# Version 1.1 (2009/11/30)
#
# 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.
#

#######################################################################
# Initialization:

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

: ${PING6:=ping6}

#######################################################################

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

<longdesc lang="en">
This is a VIPcheck Resource Agent.
</longdesc>
<shortdesc lang="en">VIPcheck resource agent</shortdesc>

<parameters>
<parameter name="target_ip" unique="1" required="1">
<longdesc lang="en">
ping target VIP address.
</longdesc>
<shortdesc lang="en">target ip</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="count" unique="1">
<longdesc lang="en">
repeat times
</longdesc>
<shortdesc lang="en">repeat times</shortdesc>
<content type="integer" default="1" />
</parameter>

<parameter name="wait" unique="1">
<longdesc lang="en">
wait times
</longdesc>
<shortdesc lang="en">wait times</shortdesc>
<content type="integer" default="10" />
</parameter>
</parameters>

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

#######################################################################

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

Expects to have a fully populated OCF RA-compliant environment set.
END
}

VIPcheck_validate_all() {
	check_binary $PING
	check_binary $PING6

	case $OCF_RESKEY_target_ip in
		"")	ocf_log err "Required parameter OCF_RESKEY_target_ip is missing"
			return $OCF_ERR_CONFIGURED;;
		*)	: OK;;
	esac

	iplist=`echo $OCF_RESKEY_target_ip | tr ',' ' '`
	if [ x"`echo $iplist`" = "x" ]; then
		# 値がスペース、カンマのみの場合
		ocf_log err "parameter OCF_RESKEY_target_ip is invalid"
		return $OCF_ERR_CONFIGURED
	fi
	for vip in $iplist; do
		if [ x`echo ${vip%%\%*}` = "x" ]; then
			# 値が「%<インタフェース>」形式の場合
			ocf_log err "parameter OCF_RESKEY_target_ip is invalid"
			return $OCF_ERR_CONFIGURED
		fi
	done
	return $OCF_SUCCESS
}

VIPcheck_start() {
	VIPcheck_monitor
	if [ $? = $OCF_SUCCESS ]; then
		return $OCF_SUCCESS
	fi 

	iplist=`echo $OCF_RESKEY_target_ip | tr ',' ' '`
	for vip in $iplist; do
		if [ x`echo ${vip} | grep '%'` != "x" ]; then
			if [ x`echo ${vip##*\%}` != "x" ]; then
				# -I オプション部を生成
				if_option=`echo -I${vip##*\%}`
			fi
		fi

		target_ip=`echo ${vip%%\%*}`
		if [ x`echo ${target_ip} | grep ':'` != "x" ]; then
			cmd=$PING6
		else
			cmd=$PING
		fi

		cmdl=`echo $cmd -c${OCF_RESKEY_count} -w${OCF_RESKEY_wait} $if_option -- $target_ip`
		ocf_log debug "execute: $cmdl"
		stderr=`$cmdl 2>&1 >/dev/null`
		prc=$?
		ocf_log debug "$cmd return code = $prc"

		if [ $prc = 0 ]; then 
			# pingが通った。--> ERROR
			return $OCF_ERR_GENERIC
		elif [ $prc != 1 ]; then
			# pingコマンドがエラーの場合
			# -> 複数行のエラーメッセージは「. 」で連結してログ出力
			msg=`echo -n "$stderr" | awk '{printf("%s. ", $0);}'`
			ocf_log err "$cmd command failed($prc): ${msg%\. }"
			return $OCF_ERR_GENERIC
		fi

		# 変数クリア
		if_option=
	done

	# 全てのアドレスにpingが通らなかった。--> 成功
	touch ${OCF_RESKEY_state}
	return $OCF_SUCCESS
}

VIPcheck_stop() {
	VIPcheck_monitor
	if [ $? = $OCF_SUCCESS ]; then
		rm ${OCF_RESKEY_state}
	fi
	return $OCF_SUCCESS
}

VIPcheck_monitor() {
	if [ -f ${OCF_RESKEY_state} ]; then
		return $OCF_SUCCESS
	fi
	if false ; then
		return $OCF_ERR_GENERIC
	fi
	return $OCF_NOT_RUNNING
}

: ${OCF_RESKEY_state=${HA_RSCTMP}/VIPcheck-${OCF_RESOURCE_INSTANCE}.state}
: ${OCF_RESKEY_count=1}
: ${OCF_RESKEY_wait=10}

case $__OCF_ACTION in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
validate-all)	VIPcheck_validate_all;;
start)		VIPcheck_validate_all && VIPcheck_start;;
stop)		VIPcheck_stop;;
monitor)	VIPcheck_monitor;;
usage|help)	VIPcheck_usage
		exit $OCF_SUCCESS
		;;
*)		VIPcheck_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc
