#!/bin/sh
#
# ## Plugin for Nagios to monitor the memory usage of a windows process
# ## with the check_nrpe of nsclient++
# ## Written by Guenther Orth (http://www.enbiz.de/)
# ##
# ## - 20091109 coded and tested for Linux
#
#
# ## You are free to use this script under the terms of the Gnu Public License.
# ## No guarantee - use at your own risc.
#
# ## Installation guidlines:
# ## needed software: check_nrpe for the nagios-host (linux)
# ##                  nsclient++ for windows with activatet dll:
# ##			- NRPEListener.dll
# ##			- CheckExternalScripts.dll
# ##			
# ## First start to edit the nsc.ini on the windows server and look for the
# ## wanted process:
# ## check_win_proc=cmd /c tasklist /FI "IMAGENAME eq <process.exe>"
# ## Start the nsclient++ on the windows server
# ## On the nagios server ...
#
# ## Todo:
# ## - errorhandling
# ## - check range and value of parameters
#
# Usage: ./check_winproc_ram -H <hostname or ip> -w <warn> -c <crit> -s <external script on windows
# 	e.g check_win_proc>
#
# ## Description:
#
# ## Output:
# 
#
# Exit Codes
# 0 OK       the ram usage of this process is ok
# 1 Warning  the ram usage of this process is above "warning" threshold
# 2 Critical the ram usage of this process is  above "critical" threshold
# 3 Unknown  Invalid command line arguments or could not reach the nsclient++
#
# Example: check_winproc_ram -H windows01 -w 7000 -c 7500 -s check_win_proc
#
# - ok		(exit code 0)
#  - warning	(exit code 1)
#  - critical	(exit code 2)

# Paths to commands used in this script. These
# may have to be modified to match your system setup.

GREP="/bin/grep"
CUT="/usr/bin/cut"
AWK="/usr/bin/awk"
SED="/bin/sed"
NRPE="/usr/local/nagios/libexec/check_nrpe"


PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="Revision 1.0"
AUTHOR="(c) 2009 Guenther Orth (http://www.enbiz.de/)"

# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

print_revision() {
    echo "$REVISION $AUTHOR"
}

print_usage() {
    echo "Usage: $PROGNAME [-H <hostname or ip>] [-w|--warning <warn in KB>] [-c|--critical <crit in KB>] [-s <external alias>]"
    echo "Usage: $PROGNAME -h|--help"
    echo "Usage: $PROGNAME -V|--version"
    echo ""
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    echo "Memory usage monitor plugin for windows procs for Nagios"
    echo "Description of the parameters:"
    echo " -H: Hostname or ipaddress"
    echo " -w|--warning: threshold for warning memory usage in KB"
    echo " -c|--critical: threshold for warning memory usage in KB"
    echo " -s external script alias on the windows host"
    echo ""
    print_usage
    echo ""
}

# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 1 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

# Grab the command line arguments

thresh_warn=""
thresh_crit=""
externalias=""
HOSTNAME=""

exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
           ;;
	-H)
	    HOSTNAME=$2
	    shift
	    ;;
        --warning)
            thresh_warn=$2
            shift
            ;;
        -w)
            thresh_warn=$2
            shift
            ;;
        --critical)
            thresh_crit=$2
            shift
            ;;
        -c)
            thresh_crit=$2
            shift
            ;;
        -s)
            externalias=$2
	    shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

##### Get memory usage
NRPERUN=`$NRPE -H $HOSTNAME -c $externalias|$GREP K`
WERT=`echo $NRPERUN|$SED 's/\.//2'|$AWK '/[ 0-9\. ]/ {print $5}'`
SERVICENAME=`echo $NRPERUN|$CUT -d " " -f 1`
result="ok"
exitstatus=$STATE_OK


##### Compute the thresholds and value for the statistic
let WERT2=$WERT*1024
let thresh_warn_KB=$thresh_warn*1024
let thresh_crit_KB=$thresh_crit*1024

##### Compare with thresholds

if [ "$thresh_warn" != "" ]; then
    if [ $WERT -ge $thresh_warn ]; then
        result="warning"
        exitstatus=$STATE_WARNING
    fi
fi
if [ "$thresh_crit" != "" ]; then
    if [ $WERT -ge $thresh_crit ]; then
        result="critical"
        exitstatus=$STATE_CRITICAL
    fi
fi

echo "$SERVICENAME: $WERT KB - $result|'ram'=$WERT2;$thresh_warn_KB;$thresh_crit_KB"
exit $exitstatus