NRPE – Check_memory script

this script is intended to “mime” the NSClient++ checkMem option on linux. Most check_mem scripts i have found where rather “minimal” so i ended up writing my own one. Feel free to test, use, alter, redis or what ever with itšŸ™‚

Here is a new version. Improvements.

– Var footprints reduced to a bare minimum.
– Free command is now only executed once.
– Corrected the swap validation that was using the wrong vars.
– Added small corrections like adding ; within “ execs to mark end of command etc.

Here you gošŸ™‚

    #!/bin/bash
#
#
# @Type 	: Bash
# @Date 	: 19-05-2009
# @Author 	: Chris Gralike
# @Category	: Nagios Monitoring/SLA
# @Category	: MemoryCheck Linux, All memory spaces
# @Company	: AMIS Services BV.
# @License	: GNU_Public
# @ToDo		: See Comments in script. Suggestions are welcome... 
##

## You are free to alter, modify and distribute the content of this file
 # This file is offered "as is", and we offer no guarantee on functionality
 # and or outcome of this check. Neither can the author nor AMIS be
 # responsible for any damages caE by this script. 
 #
 # Have fun with it, and we hope it proves usefull
 # Script is based on precentage orientated monitoring / SLAs
 ##

#Get Options
while getopts 'u:v:w:x:y:z:hpef' OPT; do
  case $OPT in
    u) U=$OPTARG;;
    v) V=$OPTARG;;
    w) W=$OPTARG;;
    x) X=$OPTARG;;
    y) Y=$OPTARG;;
    z) Z=$OPTARG;;
    e)  E="yes";;
    f)  FR="yes";;
    p)  P="yes";;
    h)  H="yes";;
    *)  unknown="yes";;
  esac
done
#Help Output
USAGE="
	usage: $0 [ -option [value]]
	
	[options:]
		-h		->	Show this help page
		-p		->	Show performance options
		-e		->	Warn/Crit based on usage precentage, warn/crit based on usage orientation
		-f		->	Warn/Crit based on free precantage, warn/crit based on free space orientation
		-u [valeu]	->	Physical warning treshold (in %)
		-v [value]	->	Physical critical treshold (in %)
		-w [value]	->	Virtual waring treshold (in %)	
		-x [value]	->	Virtual critical treshold (in%)
		-y [value]	->	Swap warning value (in%)
		-z [value]	->	Swap critical value (in%)
"
#Show usage if requested
if [ "$H" = "yes" -o $# -lt 1 ]; then
	echo "$USAGE"
	exit 0
fi
#Check if all the thresholds are defined.
if [ "$W" = "" -o "$X" = "" -o "$U" = "" -o "$V" = "" -o "$Y" = "" -o "$Z" = "" ]; then
	echo "All the [u,v,w,x,y,z] options should be defined! \r\n"
	exit 0
fi
#Check if all the thresholds are integers
# if syntax is $0 -e -u 10 -v 10 -w 10 -x 10 -y 10 -z 10 -p things are allright
# if syntax is $0 -e -u 10 -v 10 -w 10 -x 10 -y 10 -z -p A bunch of errors arise because " " is not equal too ""
# and failes the check above.
# This typo needs to be fixed someday...
#Check the orientation
if [ "$E" = "yes" -a "$FR" = "yes" ]; then
	echo "You cant use both the free, as E orientation for the tresholds. Please select -e or -f as orientation"
	exit 0
elif [ "$E" = "yes" ]; then
	# Check the critical / warning thresholds
	if [ "$W" -gt "$X" -o "$U" -gt "$V" -o "$Y" -gt "$Z" ]; then
		echo "Warning: Warning > Critical while using usage orientation!"
	fi	

elif [ "$FR" = "yes" ]; then
	# Check the warning / Critical thresholds
	if [ "$W" -lt "$X" -o "$U" -lt "$V" -o "$Y" -lt "$Z" ]; then
                echo "Warning: Warning < Critical while using free space orientation!"
        fi
else
	echo "Please define an orientation using either the -e or the -f option"
	exit 0;
fi

#Get physiscal numbers 
FRE=`free -m`

PT=`echo "$FRE" | grep Mem | awk '{ print $2; exit;}';`
PU=`echo "$FRE" | grep Mem | awk '{ print $3; exit;}';`
FP=$(($PT-$PU));
#Get virtual memory numbers
VU=`echo "$FRE" | grep buffers/cache | awk '{ print $3; exit;}';`
VF=`echo "$FRE" | grep buffers/cache | awk '{ print $4; exit;}';`
VT=$(($VU+$VF));
#Get Swapped memory numbers
ST=`echo "$FRE" | grep Swap | awk '{ print $2; exit;}';`
SU=`echo "$FRE" | grep Swap | awk '{ print $3; exit;}';`
SF=$(($ST-$SU));

#still needs to be done in an nice syntax.
#Start validating our numbers.
if [ "$E" = "yes" ]; then
	#calculate E precentages
	PPF=`echo "scale=5; (100/$PT)*$PU" | bc -l | awk -F '.' '{ print $1; exit; }'`
	VPF=`echo "scale=5; (100/$VT)*$VU" | bc -l | awk -F '.' '{ print $1; exit; }'`
	SPF=`echo "scale=5; (100/$ST)*$SU" | bc -l | awk -F '.' '{ print $1; exit; }'`
	#still needs to be done in an nice syntax.
	if [ "$P" = "yes" ]; then
        	#not sure about the syntax...
		PD="|Ph=$PPF%;$U;$V; Vi=$VPF%;$W;$X; Sw=$SPF%;$Y;$Z;"
	else
        	PD=""
	fi

	MSG="Ph:$PU/$PT($PPF%), Vi:$VU/$VT($VPF%), Sw:$SU/$ST($SPF%) $PD"

	#Validate Physical Memory
	if [ "$PPF" -gt "$U" -a "$PPF" -gt "$V" ]; then
		NPE=2
	elif [ "$PPF" -gt "$U" -a "$PPF" -lt "$V" ]; then
		NPE=1
	elif [ "$PPF" -lt "$U" -a "$PPF" -lt "$V" ]; then
		NPE=0
	else
		#unknown crit and warn the same?
		NPE=3
	fi 
	#Validate Virtual Memory
	if [ "$VPF" -gt "$W" -a "$VPF" -gt "$X" ]; then
       	        NVE=2
        elif [ "$VPF" -gt "$W" -a "$VPF" -lt "$X" ]; then
       		NVE=1
   	elif [ "$VPF" -lt "$W" -a "$VPF" -lt "$X" ]; then
               	NVE=0
       	else
               	#unknown crit and warn the same?
               	NVE=3
       	fi
	#Validate Swap memory
	if [ "$SPF" -gt "$W" -a "$SPF" -gt "$X" ]; then
                NSE=2
        elif [ "$SPF" -gt "$W" -a "$SPF" -lt "$X" ]; then
                NSE=1
        elif [ "$SPF" -lt "$W" -a "$SPF" -lt "$X" ]; then
                NSE=0
        else
                #unknown crit and warn the same?
                NSE=3
        fi
	#We only want to see the most critical,...
	if [ "$NPE" -eq "2" -o "$NVE" -eq "2" -o "$NSE" -eq "2" ]; then
		echo "Critical - $MSG"
		exit 2
	elif [ "$NPE" -eq "1" -o "$NVE" -eq "1" -o "$NSE" -eq "1" ]; then
		echo "Warning - $MSG"
		exit 1
	elif [ "$NPE" -eq "0" -a "$NVE" -eq "0" -a "$NSE" -eq "0" ]; then
                echo "Ok - $MSG"
                exit 0
	else
		echo "Unknown - $MSG"
		exit 3
	fi
elif [ "$FR" = "yes" ]; then
	#calculate free precentages
	PPF=`echo "scale=5; (100/$PT)*$FP" | bc -l | awk -F '.' '{ print $1; exit; }'`
	VPF=`echo "scale=5; (100/$VT)*$VF" | bc -l | awk -F '.' '{ print $1; exit; }'`
	SPF=`echo "scale=5; (100/$ST)*$SF" | bc -l | awk -F '.' '{ print $1; exit; }'`
	
	#still needs to be done in an nice syntax.
	if [ "$P" = "yes" ]; then
			#not sure about the syntax...
			PD="|Ph=$PPF%;$U;$V; Vi=$VPF%;$W;$X; Sw=$SPF%;$Y;$Z;"
	else
			PD=""
	fi

	MSG="Ph:$FP/$PT($PPF%), Vi:$VF/$VT($VPF%), Sw:$SF/$ST($SPF%) $PD"

        #Validate Physical Memory
        if [ "$PPF" -lt "$U" -a "$PPF" -lt "$V" ]; then
                NPE=2
        elif [ "$PPF" -lt "$U" -a "$PPF" -gt "$V" ]; then
                NPE=1
        elif [ "$PPF" -gt "$U" -a "$PPF" -gt "$V" ]; then
                NPE=0
        else
                #unknown crit and warn the same?
                NPE=3
        fi
        #Validate Virtual Memory
        if [ "$VPF" -lt "$W" -a "$VPF" -lt "$X" ]; then
                NVE=2
        elif [ "$VPF" -lt "$W" -a "$VPF" -gt "$X" ]; then
                NVE=1
        elif [ "$VPF" -gt "$W" -a "$VPF" -gt "$X" ]; then
                NVE=0
        else
                #unknown crit and warn the same?
                NVE=3
        fi
        #Validate Swap memory
        if [ "$SPF" -lt "$W" -a "$SPF" -lt "$X" ]; then
                NSE=2
        elif [ "$SPF" -lt "$W" -a "$SPF" -gt "$X" ]; then
                NSE=1
        elif [ "$SPF" -gt "$W" -a "$SPF" -gt "$X" ]; then
                NSE=0
        else
                #unknown crit and warn the same?
                NSE=3
        fi
        #We only want to see the most critical,...
        if [ "$NPE" -eq "2" -o "$NVE" -eq "2" -o "$NSE" -eq "2" ]; then
                echo "Critical - $MSG"
                exit 2
        elif [ "$NPE" -eq "1" -o "$NVE" -eq "1" -o "$NSE" -eq "1" ]; then
                echo "Warning - $MSG"
                exit 1
        elif [ "$NPE" -eq "0" -a "$NVE" -eq "0" -a "$NSE" -eq "0" ]; then
                echo "Ok - $MSG"
                exit 0
        else
		 echo "Unknown - $MSG"
                exit 3
        fi
else
	echo "Something is very off..... please review all the settings, or report this as a bug"
	exit 3
fi

Rgrds Chris

About Chris Gralike

Momenteel ben ik manager van de afdeling business continuity bij de zakelijke IT dienstverlener AMIS Services BV. Sinds 2003 ben ik actief in de ICT branche. Tussen 2003 en nu heb ik verschillende rollen vervuld. In de rollen: systeem- en netwerkbeheer, system engineer, servicemanager en nu practice manager ben ik in contact gekomen met uiteenlopende technologieƫn, methodologieƫn, ideeƫn, oplossingen en innovaties. Een rijke ervaring waarmee ik de klanten van Conclusion en AMIS elke dag probeer te ondersteunen. Mijn credo: 'Altijd opzoek een win-win tussen business en technologie.'

Posted on May 19, 2009, in centreon, Linux, Linux Commands, Nagios / Centreon, Network Monitoring and tagged , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: