#!/bin/sh
#
# Title: check_3gstore_outlet
# Author: Eric Loyd (eloyd@everwatch.global, eric@bitnetix.com)
# Version: 1.0
# Purpose:
#	Sends a CGI command off to a 3gstore IP controlled power outlet and returns status for outlet X
#	It assumes "on" is OKAY, and anything else is CRITICAL
#
# Notes:  Need a remote IP-based power switch?  http://3gstore.com/product/6081_2_outlet_ip_switch.html

hostname=""
username="admin"
password="admin"
outlet=1
command=""
outlet_status=0

print_help() {
  cat <<__HELP_EOF__
Usage:	$0 -H hostname -u username -p password [-o outlet] [-c command]

Notes:
	This plugin checks a 3GStore 2-outlet IP-based power switch and optionally turns on/off/toggles/resets one of the two power outlets.  The device can be purchased at http://3gstore.com/product/6081_2_outlet_ip_switch.html

Options:
	-H hostname	Hostname of the device (ex: outlet.domain.com)
	-u username	Username to connect with (default=$username)
	-p password	Password to connect with (default=$password)
	-o outlet	Outlet number to use (1 or 2, default=$outlet)
	-c command	Rather than check an outlet, set it to a specific state:
			ON - Turn it on
			OFF - Turn it off
			TOGGLE - Toggle its current state (on->off or off->on)
			RESET - Turn off, then on, then activate UIS Reset function*

By default, the plugin checks the state of the outlet and returns OK if it is powered on or CRITICAL if it is powered off.

*See product documentation
__HELP_EOF__
  exit
}

while [ -n "$1" ]; do
  case "$1" in
    -h) print_help; exit;;
    -H) hostname="$2"; shift 2;;
    -c) command="$2"; shift 2;;
    -u) username="$2"; shift 2;;
    -p) password="$2"; shift 2;;
    -o) outlet="$2"; shift 2;;
    *) shift 1;;
  esac
done

[ -z "$hostname" ] && echo "UNKNOWN: no hostname specified." && exit 3
[ -z "$username" ] && echo "UNKNOWN: no username specified." && exit 3
[ -z "$password" ] && echo "UNKNOWN: no password specirfid." && exit 3
if [ "$outlet" -ne "1" -a "$outlet" -ne "2" ]; then
  echo "UNKNOWN: Invalid outlet number (${outlet})."
  exit 3
fi

function outlet_status() {
  outlets=`curl -s --user ${username}:${password} http://${hostname}/xml/outlet_status.xml | grep -oPm1 "(?<=<outlet_status>)[^<]+"`
  outlet1=`echo $outlets | awk -F, '{print $1}'`
  outlet2=`echo $outlets | awk -F, '{print $2}'`
  [ "$outlet" = "1" ] && outlet_status="$outlet1"
  [ "$outlet" = "2" ] && outlet_status="$outlet2"
  return "$outlet_status"
}

# Instead of checking, if command is not blank, then we're changing the state
if [ -z "$command" ]; then
  outlet_status $outlet
  case "$?" in
    1) echo "OK: Outlet $outlet is powered up"; exit 0;;
    *) echo "CRITICAL: Outlet $outlet is not powered up"; exit 2;;
  esac
else
  control=""
  [ "$command" = "OFF" ] && control=0
  [ "$command" = "ON" ] && control=1
  [ "$command" = "TOGGLE" ] && control=2
  [ "$command" = "RESET" ] && control=3
  if [ -z "$control" ]; then
    echo "UNKNOWN: Invalid command ($command). Should be ON, OFF, RESET, or TOGGLE."
    exit 3
  fi
  output=`curl -s --user ${username}:${password} "http://${hostname}/cgi-bin/control.cgi?outlet=${outlet}&control=${control}"`
  outlet_status $outlet
  case "$?" in
    1) echo "OK: $outlet is powered up"; exit 0;;
    *) echo "OK: $outlet is not powered up"; exit 2;;
  esac
fi
