Wednesday, October 15, 2014

Nagios Plugin for Windows Virtual Machine Memory

#!/bin/bash
#Purpose         : To pull VM Memory from the Windows performance counter
#Authors         : Ranjith Kumar R
#Date            : 25th Sep 2014
#Version         : V1.0


PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="V1.0"
ECHO="/bin/echo"
STATE_UNKNOWN=3
STATE_OK=0
STATE_CRITICAL=2
STATE_WARNING=1
TAIL="/usr/bin/tail"
print_usage() {
    echo "Usage: $PROGNAME -H Hostaddress -w warning percentage -c critical percentage"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}
print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
    echo "Bandwidth monitor plugin for Nagios"
    echo ""
    support
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 3 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi
# Grab the command line arguments

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 $REVISION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        --hostaddress)
            HOSTADDRESS=$2
            shift
            ;;
        -H)
            HOSTADDRESS=$2
            shift
            ;;
        --warning)
            warning=$2
            shift
            ;;
        -w)
            warning=$2
            shift
            ;;
--critical)
            critical=$2
            shift
            ;;
        -c)
            critical=$2
            shift
            ;;
        -x)
            exitstatus=$2
            shift
            ;;
        --exitstatus)
            exitstatus=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

WORKINGDIR="/usr/local/nagios/libexec"
TOTALMEM=`$WORKINGDIR/check_nrpe -H $HOSTADDRESS -c CheckCounter -a "\VM Memory\Memory Mapped in MB"  ShowAll | cut -d'=' -f2`
WARNINGMEM=$( echo "$TOTALMEM * $warning" | bc )
WARNINGMEM=$( echo "$WARNINGMEM / 100" | bc )
CRITICALMEM=$( echo "$TOTALMEM * $critical" | bc )
CRITICALMEM=$( echo "$CRITICALMEM / 100" | bc )
WARNINGMEM=$( echo "$WARNINGMEM * 1024 * 1024" | bc )
CRITICALMEM=$( echo "$CRITICALMEM * 1024 * 1024" | bc )

OUTPUT=`$WORKINGDIR/check_nrpe -H $HOSTADDRESS -c CheckCounter -a "\VM Memory\Memory Active in MB" MaxWarn=$WARNINGMEM MaxCrit=$CRITICALMEM ShowAll`
RESULT=`echo $?`
OUTPUT_VALUE=`echo $OUTPUT | cut -d'|' -f1  | tr -cd [:digit:]`

PERCENTAGE=$( echo "$OUTPUT_VALUE / $TOTALMEM" | bc -l )
PERCENTAGE=$( echo "$PERCENTAGE * 100" | bc -l )
OUTPUT1=`echo $OUTPUT | cut -d'|' -f1`
OUTPUT2=`echo $OUTPUT | cut -d'=' -f2 | cut -d';' -f1`
OUTPUT2=$( echo "$OUTPUT2 * 1024 * 1024" | bc -l )
if [ "$RESULT" -eq "0" ]; then

echo "$OUTPUT1,TOTALMEM=$TOTALMEM | VM Memory Active in bytes=$OUTPUT2"bytes";$WARNINGMEM;$CRITICALMEM,PER_USED%=$PERCENTAGE%;$warning;$critical"

exit $RESULT

fi

if [ "$RESULT" -eq "1" ]; then

echo "$OUTPUT1,TOTALMEM=$TOTALMEM | VM Memory Active in bytes=$OUTPUT2"bytes";$WARNINGMEM;$CRITICALMEM,PER_USED%=$PERCENTAGE%;$warning;$critical"

exit $RESULT

fi

if [ "$RESULT" -eq "2" ]; then

echo "$OUTPUT1,TOTALMEM=$TOTALMEM | VM Memory Active in bytes=$OUTPUT2"bytes";$WARNINGMEM;$CRITICALMEM,PER_USED%=$PERCENTAGE%;$warning;$critical"

exit $RESULT

else

echo "UNKNOWN: $OUTPUT1,TOTALMEM=$TOTALMEM | VM Memory Active in bytes=$OUTPUT2"bytes";$WARNINGMEM;$CRITICALMEM,PER_USED%=$PERCENTAGE%;$warning;$critical"

exit $RESULT

fi
fi

No comments:

Post a Comment