#! /bin/sh


##########################################################################################################################################
#                                                                                                                                        #
#       AUTHER  : MAHENDRA B. PATIL                                                                                                      #
#       DATE    : 03-DEC-2012                                                                                                            #
#       PURPOSE : To monitor NFS and inform    		                                                                 #
#		  if NFS is not running or mounted 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 : NFS"
ALERT_EMAIL_FROM="tech@naaptol.com"

SERVER_IP_MONITOR="$1"
SERVER_NFS_MONITOR="$2"


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

}

function print_end_header()
{
  cleanup
  echo -e "\n\n######################NFS 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_MONITOR : ${SERVER_IP_MONITOR}"
   inform_error
fi

if [ -z "${SERVER_NFS_MONITOR}" ]; then
   ERROR_MSG="Invalid SERVER_NFS_MONITOR : ${SERVER_NFS_MONITOR}"
   inform_error
fi


nfs_commond_check="/usr/sbin/showmount -e ${SERVER_NFS_MONITOR}"

nfs_details=`${nfs_commond_check}`

nfs_status=`echo "${nfs_details}" | grep -i "Export list for ${SERVER_NFS_MONITOR}" | wc -l`

if [ "${nfs_status}" == "0" ]; then
  
	ALERT_EMAIL_SUBJECT="$ALERT_EMAIL_SUBJECT : Critical on ${SERVER_IP_MONITOR}"
	ERROR_MSG="Following NFS Check Command Failed <BR/> ${nfs_commond_check}"
	inform_error

fi


nfs_commond_check="/usr/sbin/rpcinfo -u ${SERVER_NFS_MONITOR} mountd"

nfs_details=`${nfs_commond_check}`

nfs_status=`echo "${nfs_details}" | grep -i "ready and waiting" | wc -l`

if [ "${nfs_status}" == "0" ]; then
  
	ALERT_EMAIL_SUBJECT="$ALERT_EMAIL_SUBJECT : Critical on ${SERVER_IP_MONITOR}"
	ERROR_MSG="Following NFS Check Command Failed <BR/> ${nfs_commond_check}"
	inform_error

fi


nfs_commond_check="/usr/sbin/rpcinfo -u ${SERVER_NFS_MONITOR} nfs"

nfs_details=`${nfs_commond_check}`

nfs_status=`echo "${nfs_details}" | grep -i "ready and waiting" | wc -l`

if [ "${nfs_status}" == "0" ]; then
  
	ALERT_EMAIL_SUBJECT="$ALERT_EMAIL_SUBJECT : Critical on ${SERVER_IP_MONITOR}"
	ERROR_MSG="Following NFS Check Command Failed <BR/> ${nfs_commond_check}"
	inform_error

fi


nfs_commond_check="mount -v "

nfs_details=`${nfs_commond_check}`

nfs_status=`echo "${nfs_details}" | grep -i "${SERVER_NFS_MONITOR}" | grep -i "nfs"  | wc -l`

if [ "${nfs_status}" == "0" ]; then
  
	ALERT_EMAIL_SUBJECT="$ALERT_EMAIL_SUBJECT : Critical on ${SERVER_IP_MONITOR}"
	ERROR_MSG="Following NFS Check Command Failed <BR/> ${nfs_commond_check}"
	inform_error

fi


echo -e "NFS from ${SERVER_NFS_MONITOR} Server running Successfully!!!"

print_end_header
