#!/bin/sh
#
# External STONITH module for IPMI.

RESET="power reset"
POWEROFF="power off"
POWERON="power on"
STATUS="power status"
IPMITOOL=`which ipmitool`

function have_ipmi() {
	test -x "${IPMITOOL}"
}

function do_ipmi() {
	have_ipmi || {
		echo "ipmitool not installed"
		return 1
	}
	if [ -z "${ipaddr}" -o -z "${userid}" -o -z "${passwd}" ]; then
		echo "ipaddr, userid or password missing; check configuration"
		return 1
	fi

	${IPMITOOL} -I lan -H ${ipaddr} -U ${userid} -P ${passwd} ${1} || {
		echo "error executing ipmi command"
		return 1
	}
}

# Rewrite the hostname to accept "," as a delimeter for hostnames too.
hostname=`echo ${hostname} | tr ',' ' '`

case ${1} in
gethosts)
	for h in ${hostname} ; do
		echo ${h}
	done
	exit 0
	;;
on)
	res=1
	for h in ${hostname}; do
		if [ "${h}" != "${2}" ]; then
			continue
		fi
		do_ipmi "${POWERON}"
		res=$?
	done
	exit ${res}
	;;
off)
	res=1
	for h in ${hostname}; do
		if [ "${h}" != "${2}" ]; then
			continue
		fi
		do_ipmi "${POWEROFF}"
		res=$?
	done
	exit ${res}
	;;
reset)
	res=1
	for h in ${hostname}; do
		if [ "${h}" != "${2}" ]; then
			continue
		fi
		do_ipmi "${RESET}"
		res=$?
	done
	exit $res
	;;
status)
	do_ipmi "${STATUS}"
	exit $?
	;;
getconfignames)
	for i in hostname ipaddr userid passwd; do
		echo $i
	done
	exit 0
	;;
getinfo-devid)
	echo "IPMI STONITH device"
	exit 0
	;;
getinfo-devname)
	echo "IPMI STONITH external device"
	exit 0
	;;
getinfo-devdescr)
	echo "IPMI-based host reset"
	exit 0
	;;
getinfo-devurl)
	echo "http://ipmitool.sf.net/"
	exit 0
	;;
getinfo-xml)
	cat << IPMIXML
<parameters>
<parameter name="hostname" unique="1">
<content type="string" />
<shortdesc lang="en">
Hostname
</shortdesc>
<longdesc lang="en">
The name 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>

<parameter name="passwd" unique="1">
<content type="string" />
<shortdesc lang="en">
Password
</shortdesc>
<longdesc lang="en">
The password used for logging in to the STONITH device
</longdesc>
</parameter>
</parameters>
IPMIXML
	exit 0
	;;
*)
	exit 1
	;;
esac
