#!/bin/sh
#
# External STONITH module for APC Switched Rack PDU (software version 3.x)
#
# Copyright (c) 2007 Sergey Maznichenko <msergeyb@gmail.com>
#
# 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.
#

OID=".1.3.6.1.4.1.318.1.1.4.4.2.1.3."

SWITCH_ON="1"
SWITCH_OFF="2"
SWITCH_RESET="3"

COMMAND="snmpset -v1 -c $community $host $OID$outlet i "

hostlist=`echo $hostlist | tr ',' ' '`

case $1 in
gethosts)
	echo $host
	exit 0
	;;
on)
	$COMMAND$SWITCH_ON
	exit 0
	;;
off)
	$COMMAND$SWITCH_OFF
	exit 0
	;;
reset)
	$COMMAND$SWITCH_RESET
	exit 0
	;;
status)
        if [ -z "$host" ]; then
    	    exit 1
	fi
	if ping -w1 -c1 "$host" 2>&1 | grep "unknown host"; then
    	    exit 1
	else
	    exit 0
	fi
	;;
getconfignames)
	echo "host community outlet"
	exit 0
	;;
getinfo-devid)
	echo "rackpdu STONITH device"
	exit 0
	;;
getinfo-devname)
	echo "rackpdu STONITH external device"
	exit 0
	;;
getinfo-devdescr)
	echo "APC Switched Rack PDU AP7952"
	exit 0
	;;
getinfo-devurl)
	echo "http://www.apc.com/products/family/index.cfm?id=70"
	exit 0
	;;
getinfo-xml)
	cat << PDUXML
<parameters>
<parameter name="host" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Name or IP address of Rack PDU host.
</shortdesc>
<longdesc lang="en">
Name or IP address of Rack PDU host.
</longdesc>
</parameter>
<parameter name="community" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Name of write community.
</shortdesc>
<longdesc lang="en">
Name of write community.
</longdesc>
</parameter>
<parameter name="outlet" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Number of outlet
</shortdesc>
<longdesc lang="en">
Number of outlet.
</longdesc>
</parameter>
</parameters>
PDUXML
	exit 0
	;;
*)
	exit 1
	;;
esac
