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

if [ -z "$outlet_config" ]; then
    outlet_config="/etc/ha.d/rackpdu.conf"
fi

OID=".1.3.6.1.4.1.318.1.1.4.4.2.1.3."

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

function SendCommand() {

    host=$1
    switch_cmd=$2

    for h in $hostlist
    do
        if [ "$h" != "$host" ]; then
		continue
        fi
	    
        outlet=`grep $host $outlet_config | tr -d ' ' | cut -f2 -d'='`
	snmpset -v1 -c $community $pduip $OID$outlet i $switch_cmd

	return 1
    done
    return 0
}

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

case $1 in
gethosts)
	for h in $hostlist ; do
            echo $h
	done
	exit 0
	;;
on)
	if
	    SendCommand $2 $SWITCH_ON
	then
	    exit 0
	else
	    exit 1
	fi
	;;
off)
	if
	    SendCommand $2 $SWITCH_OFF
	then
	    exit 0
	else
	    exit 1
	fi
	;;
reset)
	if
	    SendCommand $2 $SWITCH_RESET
	then
	    exit 0
	else
	    exit 1
	fi
	;;
status)
        if [ -z "$pduip" ]; then
    	    exit 1
	fi

	if ping -w1 -c1 $pduip >/dev/null 2>&1; then
    	    exit 1
	else
	    exit 0
	fi
	;;
getconfignames)
	echo "hostlist pduip community outlet_config"
	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="hostlist" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Hostlist
</shortdesc>
<longdesc lang="en">
The list of hosts that the STONITH device controls
</longdesc>
</parameter>
<parameter name="pduip" 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_config" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Configuration file
</shortdesc>
<longdesc lang="en">
Configuration file which contains
node_name=outlet_number
strings.
by default is /etc/ha.d/rackpdu.conf
</longdesc>
</parameter>
</parameters>
PDUXML
	exit 0
	;;
*)
	exit 1
	;;
esac
