#!/bin/bash

this_script=`basename $0`

#
# check_youless
#
# Author: Christiaan van der Veer (cvdveer@foxlair.nl)
#
# Based on check_TEMP by Magnus Luebeck


STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4


function print_version () {
    cat <<EOF
$this_script v0.3

0.1 Initial release. Get wattage from youless device.
0.2 Added performance data for pnp4nagios use.
0.3 Added a new first argument to switch between Watt or kWh
0.4 Made the watt or kwh check insensitive
1.0 first public release

This Nagios plugin comes with no warranty. You can use and distribute
it under terms of the GNU General Public License Version 2 (GPL V2) or 
later.
EOF
}

function print_help () {
    print_version
    cat <<EOF

Plugin for Nagios checking Watt usage hrough
a YouLess device.

Call this script with the parameters:

Device Watt Warning_watt Critical_watt
or
Device kWh Warning_kWh Critical_kWh

EXAMPLE: check_youless 192.168.0.14 Watt 1000 2000
may result in
kWh OK - Watt: 791 | 'Watt'=791;1000;2000

EXAMPLE: check_youless 192.168.0.14 kWh 60000 80000
may result in
Wattage OK - kWh: 52792.812 | 'kWh'=52792.812;60000;80000


EOF
    print_usage

cat <<EOF

Options:
 -h, --help
    Print detailed help screen

 -V, --version
    Print version information
EOF
}

function print_usage () {
    cat <<EOF
Usage: $this_script -h, --help
       $this_script -V, --version     
       $this_script DEVICE_ADDRESS USAGE WARNING_TRESHOLD CRITICAL_TRESHOLD
       where USAGE is either Watt or kWh
EOF
}

case "$1" in
        --help|-h)
	    print_help
            exit 0
        ;;
        --version|-V)
	    print_version
            exit 0
	;;
	-*)
	    print_usage
	    exit $STATE_UNKNOWN
	;;
esac

if [[ $# -ne 4 && $5 != "debug" ]] ; then
    print_usage
    exit $STATE_UNKNOWN
fi

shopt -s nocasematch
if
[[ "$2" =~ "Watt" ]];
then USEMATCH="Watt"
elif
[[ "$2" =~ "kWh" ]];
then USEMATCH="kWh"
else
print_usage
exit $STATE_UNKNOWN
fi
shopt -u nocasematch


URL=$1
USE=$USEMATCH
WARNING_WATT=$3
CRITICAL_WATT=$4

CUR_WATT=`wget http://$URL/a -q -O - |grep $USE|sed s/\ $USE//|sed s/\,/\./|sed s/^\ //`

# bash doesn't do floating point. for comparison strip after the .
CALC_KWH=`echo $CUR_WATT | cut -d. -f1`

if [ -z "$CUR_WATT" ]
then
  echo -n " CRITICAL - NO RESPONSE FROM THE DEVICE"
  echo
  exit $STATE_CRITICAL
fi
#-----------------------------------------------------
# 4) Finally echo the exit code
#-----------------------------------------------------

if [ $CALC_KWH -lt $WARNING_WATT ]
then
  echo -n "$USE OK - "
  echo -n "$USE: $CUR_WATT | '$USE'=$CUR_WATT;$WARNING_WATT;$CRITICAL_WATT" 
  echo
  exit $STATE_OK
fi

if [ $CALC_KWH -lt $CRITICAL_WATT ]
then
  echo -n "WARNING - $USE TOO HIGH - "
  echo -n "$USE: $CUR_WATT | '$USE'=$CUR_WATT;$WARNING_WATT;$CRITICAL_WATT"
  echo
  exit $STATE_WARNING
else
  echo -n "CRITICAL - WATT TOO HIGH - "
  echo -n "$USE: $CUR_WATT | '$USE'=$CUR_WATT;$WARNING_WATT;$CRITICAL_WATT"
  echo
  exit $STATE_CRITICAL
fi

