#! /bin/sh


##########################################################################################################################################
#                                                                                                                                        #
#       AUTHER  : MAHENDRA B. PATIL                                                                                                      #
#       DATE    : 03-DEC-2012                                                                                                            #
#       PURPOSE : To monitor Load Avarage and inform    		                                                                 #
#		  if Load Avarage > 60% of processer units then it will alert i.e. send mail to respective people			 #
#																	 #
#																	 #
##########################################################################################################################################


ALERT_EMAIL_TO="naaptoltech@gmail.com;monitoring.alert@naaptol.com;"
ALERT_EMAIL_SUBJECT="Health Checkup : Load Average"
ALERT_EMAIL_FROM="tech@naaptol.com"

LOAD_AVARAGE_MAX_PERCENT="60"

SERVER_IP_MONITOR="$1"


#############################################ALL Required Functions defined Here START#####################################
function print_start_header()
{
  setCurrentHome
  echo -e "\n\n######################LOAD AVERAGE CHECK START ON DATE - $(date +%Y-%m-%d-%H-%M-%S)##############################################\n\n"

}

function print_end_header()
{
  cleanup
  echo -e "\n\n######################LOAD AVERAGE CHECK END ON DATE - $(date +%Y-%m-%d-%H-%M-%S)##############################################\n\n"
}

#Any clean up activity which must happen at the last should go here
function cleanup()
{
    echo -e "Clean up!!!"
}
. /opt/anm/scripts/alert/alert.sh
#############################################ALL Required Functions defined Here END#####################################

print_start_header

if [ -z "${SERVER_IP_MONITOR}" ]; then
   ERROR_MSG="Invalid server ip ${SERVER_IP_MONITOR}"
   inform_error
fi

totalprocessor=`cat /proc/cpuinfo | grep processor | wc -l`
LOAD_AVARAGE_MAX=`echo "scale=2;($totalprocessor*$LOAD_AVARAGE_MAX_PERCENT)/100"|bc`

loadavg=`uptime | cut -d ',' -f4,5,6|cut -d ':' -f2|sed 's/ //g'`
currentloadavg=`echo $loadavg|awk -F, '{print $1}'`
loadavg_critical=`echo "$currentloadavg > $LOAD_AVARAGE_MAX"|bc`


if [ "${loadavg_critical}" == "1" ]; then
  
	echo -e "Health Checkup : Load Average : on server ${SERVER_IP_MONITOR} : $currentloadavg"
	
	ALERT_EMAIL_SUBJECT="$ALERT_EMAIL_SUBJECT : Critical on ${SERVER_IP_MONITOR}"
	ALERT_EMAIL_MESSAGE="Critical Load Avarage on server ${SERVER_IP_MONITOR} is $currentloadavg . Please check asap."
	inform

fi


print_end_header
