#!/bin/bash
#
# NAME
# ----
# - xymon_cbw_relay.sh
#
# DESCRIPTION
# -----------
# - Simple Bash shell script to control the two relays on a ControlByWeb
#   4-temp/2-relay module
#
# - The most current version of this script may be found
#   at http://www.revpol.com/xymon_cbw_relay_script
#
# - Instructions to integrate this with a Xymon Monitoring server may also
#   be found at the above URL
#
# - *NOTE* - This script has the added "bonus" of also dealing with relays
#            that are normally ON that you want Xymon to turn OFF on alert
#            and then back ON when the alert is recovered. Why you would want
#            to use this "reverse logic" is unknown to this author, but the
#            option is available. :)
#
#          - Also, the modules accept a TOGGLE option. Why you might want to
#            use this mode with a Xymon alert script is also unknown, but the
#            option is provided just in case.
#
# - If you find this script useful, I'd love to know. Send me an email!
#
# William A. Arlofski
# Reverse Polarity, LLC
# 860-824-2433 Office
# http://www.revpol.com/
#
# HISTORY
# ------
# - 20100325 - Initial version release
#            - Major same-day "logic" rewrite after Ryan from ControlByWeb
#              provided module-specific information for controlling the relays
# - 20100326 - Fixed glaring problem with restoring relay to "normal" condition
#              when script was called during an alert recovery. This was due
#              to the major logic changes made previously
# - 20100331 - Minor modifications to some of the comments
#
###############################################################################
#
# Copyright (C) 2010 William A. Arlofski - waa-at-revpol-dot-com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or visit http://www.gnu.org/licenses/gpl.txt
#
###############################################################################
#
# Set some variables
# ------------------
# Local System Binaries
# ---------------------
TR="/usr/bin/tr"
CUT="/usr/bin/cut"
CURL="/usr/bin/curl"


# Location/device specific variables
# ----------------------------------
#
# FQDN or IP address of the ControlByWeb
# temperature monitor  and user login
# and password (User should be blank)
#---------------------------------------
HOST="temp.revpol.com"
USER=""
PASS="password"


# If the Xymon alert was configured to pulse
# the relay, do we also pulse on a recovery?
# ------------------------------------------
PULSEONRECOV="0"


###############################################################################
# --------------------------------------------------
# Nothing should need to be modified below this line
# --------------------------------------------------
###############################################################################


if [ -z "$RCPT" ] || [ -z "$RECOVERED" ]; then
 echo ""
 echo "*NOTE* Must be called by Xymon with RCPT, and RECOVERED environment variables set"
 echo "        RCPT must be in the form X:MODE"
 echo "        where X is 1 or 2 and"
 echo "        MODE is ON, OFF, PULSE, or TOGGLE"
 echo ""
 exit
fi


# Get Relay and Mode from environment
# -----------------------------------
RELAY=`echo "$RCPT" | "$CUT" -d':' -f1`
MODE=`echo "$RCPT" | "$CUT" -d':' -f2 | "$TR" [a-z] [A-Z]`


# Verify that an appropriate avalue was set for the relay number
# --------------------------------------------------------------
if [ ! "$RELAY" == "1" ] && [ ! "$RELAY" == "2" ] ; then
  echo "Relay must be one of either 1 or 2"
  exit 1
fi


# Determine the MODE (ON, OFF, PULSE or TOGGLE) and convert
# the mode to its numeric value
#
# Here we also decide what to do if we were called due to a
# recovery of an alert:
# If we are recovering from an alert, Xymon calls the same
# script with the same command line variables, but sets the
# environment variable $RECOVERED to "1" otherwise $RECOVERED
# is set to "0" when the script is called for an alert
#
# The special case of "pulse on recovery" is also tested
# -----------------------------------------------------------
case "$MODE" in

        ON   )
          if [ "$RECOVERED" == "1" ] ; then
            MODE="0"
              else
                MODE="1"
          fi
          ;;

        OFF  )
          if [ "$RECOVERED" == "1" ] ; then
            MODE="1"
              else
                MODE="0"
          fi
          ;;

       PULSE )
          # If this is not a recovery alert, just pulse the correct relay
          # OR
          # If this is a recovery of an alert, AND we want to "pulse on recovery"
          # then pulse the relay, otherwise we are done and just exit
          # ---------------------------------------------------------------------
          if [ "$RECOVERED" == "0" ] || [ "$RECOVERED" == "1" -a "$PULSEONREC" == "1" ] ; then
           MODE="2"
            else
              exit 1
          fi
          ;;

     TOGGLE )
          MODE="5"
          ;;

        *   )
         echo "No mode, or improper mode given... use ON, OFF, PULSE, or TOGGLE"
         exit
         ;;
esac


# Send command to the module using curl
# -------------------------------------
"$CURL" -s -u "$USER:$PASS" "$HOST/state.xml?relay$RELAY""State=$MODE"
