Wednesday, October 15, 2014

Nagios Plugin for Monitor Windows Virtual Machine CPU by WMI

#!/bin/bash
#Purpose         : To compute WINDOWS VM CPU using WMI
#Authors         : Ranjith Kumar R
#Date            : 10th Oct 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 -I SAMPLE Interval in seconds -S No of Samples -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 "VM CPU plugin for WINDOWS"
    echo ""
    support
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 7 ]; 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
            ;;
        -I)
            INTERVAL=$2
            shift
            ;;
        -S)
            SAMPLE=$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/"

PROCESSOR_COUNT=`/usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS -c CheckWMI -a "Query=Select NumberOfLogicalProcessors from win32_Processor" | tr -d [NumberOfLogicalProcessors] | cut -d'|' -f1 | sed 's/=/ /g'`
PROCESSOR_SPEED=`/usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS -c CheckWMI -a "Query=Select HostProcessorSpeedMHz from Win32_PerfRawData_vmGuestLib_VCPU" | cut -d '|' -f1 | tr -cd [:digit:]`

VM_SPEED=0
EFFECTIVE_VM_SPEED=0

for i in `seq $SAMPLE`

do

VM_SPEED=`/usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS -c CheckWMI -a "Query=Select EffectiveVMSpeedMHz from Win32_PerfRawData_vmGuestLib_VCPU" | cut -d '|' -f1 | tr -cd [:digit:]`
EFFECTIVE_VM_SPEED=$(($EFFECTIVE_VM_SPEED + $VM_SPEED))

sleep $INTERVAL

done

EFFECTIVE_VM_SPEED=`echo "scale=2; $EFFECTIVE_VM_SPEED / $SAMPLE" | bc`

ADDCOUNT=0

for count in `echo $PROCESSOR_COUNT`

do

ADDCOUNT=`expr $count + $ADDCOUNT`

done

PROCESSOR_COUNT=`echo $ADDCOUNT`

TOTAL_PROC_SPEED=$( echo "$PROCESSOR_COUNT * $PROCESSOR_SPEED" | bc -l )

#TOTAL_PROC_SPEED=`echo "scale=2; $PROCESSOR_COUNT * $PROCESSOR_SPEED" | bc`

CPU_UTILIZATION=$( echo "$EFFECTIVE_VM_SPEED / $TOTAL_PROC_SPEED" | bc -l )

#CPU_UTILIZATION=`echo "scale=2; $EFFECTIVE_VM_SPEED / $TOTAL_PROC_SPEED" | bc`

CPU_UTILIZATION_IN_PER=$( echo "$CPU_UTILIZATION *100" | bc -l )

#CPU_UTILIZATION_IN_PER=`echo "scale=2; $CPU_UTILIZATION *100" | bc`

INTEGER=`echo $CPU_UTILIZATION_IN_PER | cut -d'.' -f1`    

EFFECTIVE_VM_SPEED=$( echo "$EFFECTIVE_VM_SPEED * 1000 * 1000" | bc )
PROCESSOR_SPEED=$( echo "$PROCESSOR_SPEED * 1000 * 1000" | bc )
        if [ "$warning" -ge "$critical" ]; then

                $ECHO "CRITICAL : Warning threshold should be less than critical"

                exitstatus=$STATE_CRITICAL

                exit $exitstatus
        fi


        if [ "$INTEGER" -ge "$critical" ]; then
        if [ "$INTEGER" -ge "100" ]; then
        $ECHO "CRITICAL : High CPU utilization "100%"|CPU_UTILIZATION="100%";$warning;$critical;EFFECTIVE_VM_SPEED=$EFFECTIVE_VM_SPEED"Hz";NO_OF_vCPUs=$PROCESSOR_COUNT;HOST_PROCESSOR_SPEED=$PROCESSOR_SPEED"Hz""

        exitstatus=$STATE_CRITICAL

                exit $exitstatus
else

        $ECHO "CRITICAL : High CPU utilization $CPU_UTILIZATION_IN_PER"%"|CPU_UTILIZATION=$CPU_UTILIZATION_IN_PER"%";$warning;$critical;EFFECTIVE_VM_SPEED=$EFFECTIVE_VM_SPEED"Hz";NO_OF_vCPUs=$PROCESSOR_COUNT;HOST_PROCESSOR_SPEED=$PROCESSOR_SPEED"Hz""

        exitstatus=$STATE_CRITICAL

                exit $exitstatus
        fi
        fi

        if [ "$INTEGER" -ge "$warning" ]; then

$ECHO "WARNING : CPU utilization $CPU_UTILIZATION_IN_PER"%"|CPU_UTILIZATION=$CPU_UTILIZATION_IN_PER"%";$warning;$critical;EFFECTIVE_VM_SPEED=$EFFECTIVE_VM_SPEED"Hz";NO_OF_vCPUs=$PROCESSOR_COUNT;HOST_PROCESSOR_SPEED=$PROCESSOR_SPEED"Hz""
                exitstatus=$STATE_WARNING

                exit $exitstatus
        fi

        if [ "$INTEGER" -lt "$warning" ]; then

$ECHO "OK : CPU utilization $CPU_UTILIZATION_IN_PER"%"|CPU_UTILIZATION=$CPU_UTILIZATION_IN_PER"%";$warning;$critical;EFFECTIVE_VM_SPEED=$EFFECTIVE_VM_SPEED"Hz";NO_OF_vCPUs=$PROCESSOR_COUNT;HOST_PROCESSOR_SPEED=$PROCESSOR_SPEED"Hz""
                exitstatus=$STATE_OK

                exit $exitstatus

        else

$ECHO "UNKNOWN : CPU utilization $CPU_UTILIZATION_IN_PER"%"|CPU_UTILIZATION=$CPU_UTILIZATION_IN_PER"%";$warning;$critical;EFFECTIVE_VM_SPEED=$EFFECTIVE_VM_SPEED"Hz";NO_OF_vCPUs=$PROCESSOR_COUNT;HOST_PROCESSOR_SPEED=$PROCESSOR_SPEED"Hz""
`/bin/echo "Please contact Ranjith or Babu" | /bin/mail -s "Please verify check_WIN_VM_CPU.sh plugin output for the host $HOSTADDRESS" ranjith.rajendran\@test.com babu.dhinakaran\@test.com -- -f no_reply_nagios\@symphonyits.com`;
                exitstatus=$STATE_UNKNOWN

                exit $exitstatus

        fi
        fi


No comments:

Post a Comment