#! /bin/sh


##########################################################################################################################################
#                                                                                                                                        #
#       AUTHER  : MAHENDRA B. PATIL                                                                                                      #
#       DATE    : 03-DEC-2012                                                                                                            #
#       PURPOSE : To monitor Solr Connection Timeout and inform		                                                                 #
#		  if Solr Connection Timeout > 0 in last 10 minutes on Specified Jboss then it will alert				 #
#                 i.e. send mail to respective people											 #
#																	 #
#																	 #
##########################################################################################################################################

SERVER_IP_MONITOR="$1"
NODE_NAME="$2"

ALERT_EMAIL_TO="naaptoltech@gmail.com;monitoring.alert@naaptol.com;"
ALERT_EMAIL_SUBJECT="Health Checkup : Solr Connection Timeout : on ${SERVER_IP_MONITOR} JBOSS ${NODE_NAME}"
ALERT_EMAIL_FROM="tech@naaptol.com"



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

}

function print_end_header()
{
  cleanup
  echo -e "\n\n######################SOLR CONNECTION TIMEOUT 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 "${NODE_NAME}" ]; then
   ERROR_MSG="Invalid jboss node ${NODE_NAME}"
   inform_error
fi


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

if [ ! -d "/opt/jboss-4.0.2.${NODE_NAME}/server/default/log/" ]; then
   ERROR_MSG="Invalid jboss node ${NODE_NAME}"
   inform_error
fi

CURRENT_DATE_TIME=`echo $(date -d '10 minute ago'  +%Y-%m-%d' '%H:%M)`
CHECK_DATE_TIME=`echo ${CURRENT_DATE_TIME} | awk '{print $1 " " substr($2,0,4)}'`

SOLR_CONNECTION_ERROR_COUNT=`cat /opt/jboss-4.0.2.${NODE_NAME}/server/default/log/jbossserver.log | grep "${CHECK_DATE_TIME}" | grep "SolrServerException: org.apache.commons.httpclient.ConnectTimeoutException" | wc -l`

if [ "${SOLR_CONNECTION_ERROR_COUNT}" -gt "0" ]; then
	
	echo -e "Health Checkup : Solr Connection Timeout : on ${SERVER_IP_MONITOR} JBOSS ${NODE_NAME} : ${SOLR_CONNECTION_ERROR_COUNT}"
		
	ALERT_EMAIL_MESSAGE="Connection Time out in last 10 mins are ${SOLR_CONNECTION_ERROR_COUNT} on ${SERVER_IP_MONITOR} JBOSS ${NODE_NAME} . Please check asap."
	inform
fi

print_end_header

