#!/bin/bash
# Script to check real memory usage
# L.Gill 02/05/06 - V.1.0
# ------------------------------------------
# ########  Script Modifications  ##########
# ------------------------------------------
# Who	 When	   What
# ---    ----      ----
# LGill	 17/05/06  "$percent" lt 1% fix - sed edits dc result beggining with "."
# 
#
#!/usr/bin/bash

solaris_shared() {
	typeset -i shmtot
	shmtot=0
	for segsz in `ipcs -m -b | grep "^m" | awk '{print $7}'` ; do
		shmtot=$(($shmtot + $segsz))
	done
	echo $(($shmtot / 1048576))
}

solaris_cache() {
	typeset -i zfscache
	zfscache=`kstat zfs:0:arcstats:size | grep size | awk '{print $2}'`
	echo $(($zfscache / 1048576))
}

USAGE="`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>"
THRESHOLD_USAGE="WARNING threshold must be greater than CRITICAL: `basename $0` $*"
calc=/tmp/memcalc
percent_free=/tmp/mempercent
critical=""
warning=""
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# print usage
if [[ $# -lt 4 ]]
then
	echo ""
	echo "Wrong Syntax: `basename $0` $*"
	echo ""
	echo "Usage: $USAGE"
	echo ""
	exit 0
fi
# read input
while [[ $# -gt 0 ]]
  do
        case "$1" in
               -w|--warning)
               shift
               warning=$1
        ;;
               -c|--critical)
               shift
               critical=$1
        ;;
        esac
        shift
  done
# verify input
if [[ $warning -eq $critical || $warning -lt $critical ]]
then
	echo ""
	echo "$THRESHOLD_USAGE"
	echo ""
        echo "Usage: $USAGE"
	echo ""
        exit 0
fi

typeset -i total
typeset -i used
typeset -i free
typeset -i shared
typeset -i buffers
typeset -i cached
os=`uname -s`
case $os in
	Linux)
		memoutput=`free -m | head -2 | tail -1`
		total=`echo $memoutput | gawk '{print $2}'`
		used=`echo $memoutput | gawk '{print $3}'`
		free=`echo $memoutput | gawk '{print $2-$3}'`
		shared=`echo $memoutput | gawk '{print $5}'`
		buffers=`echo $memoutput | gawk '{print $6}'`
		cached=`echo $memoutput | gawk '{print $7}'`
		;;
	SunOS)
		top=""
		which top | grep "^/" > /dev/null 2> /dev/null
		if [ $? -eq 0 ] ; then
			top=`which top`
		elif [ -x /usr/local/bin/top ] ; then
			top="/usr/local/bin/top"
		else
			echo "top not found!"
			exit $STATE_UNKNOWN
		fi
		topout=`$top -n | grep "^Memory"`
		total=`echo $topout | awk '{print $2}' | sed -e 's/M$//'`
		free=`echo $topout | awk '{print $5}' | sed -e 's/M$//'`
		used=$(($total-$free))
		shared=`solaris_shared`
		cached=`solaris_cache`
		buffers=0
		;;
	*)
		echo "The operating system $os is not supported"
		exit $STATE_UNKNOWN
		;;
esac

# normal values
#echo "$total"MB total
#echo "$used"MB used
#echo "$free"MB free

# make it into % percent free = ((free mem / total mem) * 100)
echo "5" > $calc # decimal accuracy
echo "k" >> $calc # commit
echo "100" >> $calc # multiply
echo "$free" >> $calc # division integer
echo "$total" >> $calc # division integer
echo "/" >> $calc # division sign
echo "*" >> $calc # multiplication sign
echo "p" >> $calc # print
echo "q" >> $calc # exit
percent=`dc $calc|sed 's/^\./0./'|tr "." " "|awk {'print $1'}`
#percent1=`/usr/bin/dc $calc`
rm -f $calc
#echo "$percent1"
if [[ $percent -le  $critical ]]
	then
    string="CRITICAL"
    result=2
elif [[ $percent -le  $warning ]]
        then
    string="WARNING"
    result=1
else
    string="OK"
    result=0
fi

case $os in
	Linux)
		echo "$string - $free / $total MB ($percent%) Free Memory, Used: $used MB, Shared: $shared MB, Buffers: $buffers MB, Cached: $cached MB | total="$total"MB free="$free"MB used="$used"MB shared="$shared"MB buffers="$buffers"MB cached="$cached"MB"
		;;
	SunOS)
		if [ $cached -gt 0 ] ; then
			echo "$string - $free / $total MB ($percent%) Free Memory, Used: $used MB, Shared: $shared MB, Cached: $cached MB | total="$total"MB free="$free"MB used="$used"MB shared="$shared"MB cached="$cached"MB"
		else
			echo "$string - $free / $total MB ($percent%) Free Memory, Used: $used MB, Shared: $shared MB | total="$total"MB free="$free"MB used="$used"MB shared="$shared"MB"
		fi
		;;
esac
exit $result
