#! /bin/sh


##########################################################################################################################################
#                                                                                                                                        #
#       AUTHER  : MAHENDRA B. PATIL                                                                                                      #
#       DATE    : 03-DEC-2012                                                                                                            #
#       PURPOSE : To monitor Disk Usage and inform    				                                                         #
#		  if any partition > 70% of its total size 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 : Disk Usage"
ALERT_EMAIL_FROM="tech@naaptol.com"

DISK_USAGE_LIMIT="70"

SERVER_IP_MONITOR="$1"


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

}

function print_end_header()
{
  cleanup
  echo -e "\n\n######################DISK USAGE 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

function generate_STATUS_REPORT_html()
{
	STATUS_REPORT="$STATUS_REPORT <HTML> "
	STATUS_REPORT="$STATUS_REPORT <HEAD> "
	STATUS_REPORT="$STATUS_REPORT <TITLE>Health Checkup : Disk Usage</TITLE> "
	
	STATUS_REPORT="$STATUS_REPORT  <style> "
	STATUS_REPORT="$STATUS_REPORT  .report "
	STATUS_REPORT="$STATUS_REPORT  { "
	STATUS_REPORT="$STATUS_REPORT     border-top:1px solid black; "
	STATUS_REPORT="$STATUS_REPORT     border-left:1px solid black; "
	STATUS_REPORT="$STATUS_REPORT  } "
	STATUS_REPORT="$STATUS_REPORT  .report TD "
	STATUS_REPORT="$STATUS_REPORT  { "
	STATUS_REPORT="$STATUS_REPORT     border-right:1px solid black; "
	STATUS_REPORT="$STATUS_REPORT     border-bottom:1px solid black; "
	STATUS_REPORT="$STATUS_REPORT  } "
	STATUS_REPORT="$STATUS_REPORT  </style> "

	STATUS_REPORT="$STATUS_REPORT </HEAD> "

	STATUS_REPORT="$STATUS_REPORT <BODY> "
	STATUS_REPORT="$STATUS_REPORT <TABLE width='100%' bgcolor='#00000' cellspacing='0' cellpadding='1' class=report> "
	STATUS_REPORT="$STATUS_REPORT <TR> "
	STATUS_REPORT="$STATUS_REPORT 	<TD colspan='4' bgcolor='#FFFFFF'><center><h1>Health Checkup : Disk Usage</h1></center></TD>	 "
	STATUS_REPORT="$STATUS_REPORT </TR> "


	STATUS_REPORT="$STATUS_REPORT <TR> "
	STATUS_REPORT="$STATUS_REPORT <TD colspan=2 width='30%' bgcolor=#FFFFFF><b>Date&nbsp;:&nbsp;</b>$(date +%Y-%m-%d-%H-%M-%S)</TD> "
	STATUS_REPORT="$STATUS_REPORT <TD colspan=2  bgcolor=#FFFFFF><b>Server&nbsp;:&nbsp;</b>$SERVER_IP_MONITOR</TD> "
	STATUS_REPORT="$STATUS_REPORT </TR> "

	
	STATUS_REPORT="$STATUS_REPORT <TR> "
	STATUS_REPORT="$STATUS_REPORT 	<TD colspan='4' bgcolor="#FFFFFF">Note: Following partitions are above $DISK_USAGE_LIMIT% usage,Please clean up asap.</TD>	 "
	STATUS_REPORT="$STATUS_REPORT </TR> "

	STATUS_REPORT="$STATUS_REPORT <TR> "
	STATUS_REPORT="$STATUS_REPORT 	<TD colspan='3' bgcolor="#FFFFFF"><b>Partition Name</b></TD> "
	STATUS_REPORT="$STATUS_REPORT 	<TD bgcolor="#FFFFFF"><b>Usage(%)</b></TD> "
	STATUS_REPORT="$STATUS_REPORT </TR> "


	STATUS_REPORT="$STATUS_REPORT $ALERT_EMAIL_CONTENT "

	STATUS_REPORT="$STATUS_REPORT </TABLE> "
	
	STATUS_REPORT="$STATUS_REPORT </BODY> "
	STATUS_REPORT="$STATUS_REPORT </HTML> "
}

#############################################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

disk_details=`df -h | grep -v Filesystem|grep % |awk '{print $(NF - 1)  " " $NF }' | awk 'x=$1{gsub("%","",x) ;if((x+0)>70){ print $2 "=" $1}}' `

if [ "${disk_details}" != "" ]; then

  echo -e "Health Checkup : Disk Usage : on server ${SERVER_IP_MONITOR} : partitions Above $DISK_USAGE_LIMIT% Usage are \n ${disk_details}"

  ALERT_EMAIL_CONTENT=""

  for partition in ${disk_details}
  do
    partition_name=`echo $partition|awk -F= '{print $1}'`
    partition_usage=`echo $partition|awk -F= '{print $2}'`

	ALERT_EMAIL_CONTENT="$ALERT_EMAIL_CONTENT <TR> "
	ALERT_EMAIL_CONTENT="$ALERT_EMAIL_CONTENT   <TD colspan='3' bgcolor='#FFFFFF'>$partition_name</TD> "
	ALERT_EMAIL_CONTENT="$ALERT_EMAIL_CONTENT   <TD bgcolor='#FFFFFF'>$partition_usage</TD> "
	ALERT_EMAIL_CONTENT="$ALERT_EMAIL_CONTENT </TR> "

  done

  generate_STATUS_REPORT_html

  ALERT_EMAIL_SUBJECT="$ALERT_EMAIL_SUBJECT : Critical on ${SERVER_IP_MONITOR}"
  ALERT_EMAIL_MESSAGE="${STATUS_REPORT}"
  inform

else 
  echo -e "Health Checkup : Disk Usage : on server ${SERVER_IP_MONITOR} : All partitions below $DISK_USAGE_LIMIT% Usage"
fi

print_end_header
