#!/bin/sh
#
# External STONITH module for DRAC5 adapters.
#
# Author:  Jun Wang
# License:      GNU General Public License (GPL)
#

#DEBUGF=/var/log/ha-drac.log
#touch $DEBUGF
#chmod 600 $DEBUGF

#exec 2>>$DEBUGF

#function debug() {
#	cat >&2
#}

#echo "started with: $@" | debug

outf=/var/run/heartbeat/drac-$$
touch $outf
if [ ! -f $outf ]
then
	echo "cannot create temporary file $outf"
	exit 1
fi
trap "cat $outf >&2; rm -f $outf" EXIT

function sshlogin() {
	if [ x = "x$ipaddr" -o x = "x$userid" ]
	then
		echo "ipaddr or userid missing; check configuration"
		return 1
	fi

	test -f /etc/ha.d/drac.debug && set -x
	@SSH@ -q -x -n $userid@$ipaddr racadm serveraction "$1" >$outf 2>&1
}

function drac_reset() {
	sshlogin hardreset
}

function drac_on() {
	sshlogin poweron
}

function drac_off() {
	sshlogin poweroff
}

function drac_status() {
	sshlogin powerstatus
}

case $1 in
gethosts)
	echo $hostname
	;;
on)
	drac_poweron
	;;
off)
	drac_poweroff
	;;
reset)
	drac_reset
	;;
status)
	drac_status
	;;
getconfignames)
	for i in hostname ipaddr userid; do
		echo $i
	done
	;;
getinfo-devid)
	echo "DRAC5 STONITH device"
	;;
getinfo-devname)
	echo "DRAC5 STONITH device"
	;;
getinfo-devdescr)
	echo "DRAC5 host reset/poweron/poweroff"
	;;
getinfo-devurl)
	echo "http://www.dell.com"
	;;
getinfo-xml)
	cat <<EOF
<parameters>

<parameter name="hostname" unique="1">
<content type="string" />
<shortdesc lang="en">
Hostname
</shortdesc>
<longdesc lang="en">
The hostname of the host to be managed by this STONITH device
</longdesc>
</parameter>

<parameter name="ipaddr" unique="1">
<content type="string" />
<shortdesc lang="en">
IP Address
</shortdesc>
<longdesc lang="en">
The IP address of the STONITH device
</longdesc>
</parameter>

<parameter name="userid" unique="1">
<content type="string" />
<shortdesc lang="en">
Login
</shortdesc>
<longdesc lang="en">
The username used for logging in to the STONITH device
</longdesc>
</parameter>

</parameters>
EOF
	;;
*)
	exit 1
	;;
esac
