#!/bin/sh

# Copyright (c) 2012 ken.naruo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

## global.conf load
LANG=C;export LANG
SCRIPT_DIR=`dirname $0`
. ${SCRIPT_DIR}/../global.conf

## process check
if [ -f /tmp/ecron_monitord_lock ];then
  exit 0
else
 touch /tmp/ecron_monitord_lock
fi


## param setting
IFCONFIG=/usr/sbin/ifconfig
LINUX=`uname -a | grep Linux`

if [ "${LINUX}" ];then
  IFCONFIG=/sbin/ifconfig
fi

## global.conf load
startup () {
  . ${ROOT}/global.conf
}
trap startup HUP
startup


## monitoring servers
while true
do
 . ${SCRIPT_DIR}/../global.conf
 UNAME=`uname -n`
 ERROR_QUE=${ROOT}/que/daemon_error/${UNAME}_`date +%Y%m%d`

 while read RHOST
 do

 SERVER=`echo ${RHOST} | awk  -F":" '{print $1}'`
 SSH_PORT=`echo ${RHOST} | awk  -F":" '{print $2}'`


 SERVER_STATUS_QUE=${ROOT}/que/server_status/${SERVER}
 IP_ADDR=`dig ${SERVER} | grep -A 1 "ANSWER SECTION" | grep -v "ANSWER SECTION" | awk '{print $5}'`
 LOCAL_CHECK=`${IFCONFIG} -a | grep ${IP_ADDR}`

 if [ "${LOCAL_CHECK}" = "" ];then

    ping -c 3 ${SERVER}  > /dev/null 2>&1

    if [ $? != 0 ];then

      echo "`date +%Y/%m/%d` `date +%H:%M:%S` ${SERVER} ERROR live_check_failed" >  ${SERVER_STATUS_QUE}
      if [  ! -f ${ROOT}/que/daemon_error/${SERVER}_monitor_error ];then

        sleep 30
        ping -c 3 ${SERVER}  > /dev/null 2>&1
        SERVER_CHECK=$?

        if [ "${SERVER_CHECK}" != 0 ];then

          touch ${ROOT}/que/daemon_error/${SERVER}_monitor_error
          echo "`date +%Y/%m/%d` `date +%H:%M:%S` ecron_monitord: ${SERVER} live check has failed" >>  ${ERROR_QUE}

          if [ "${MAIL_ADDR}" ];then
            echo "${SERVER} live check has failed"|mailx -s "ERROR ${UNAME} ecron_monitord" ${MAIL_ADDR}
          fi

        fi

      fi

    else

       echo "`date +%Y/%m/%d` `date +%H:%M:%S` ${SERVER} SUCCESS live_check_normal_end" >  ${SERVER_STATUS_QUE}
       if [ -f ${ROOT}/que/daemon_error/${SERVER}_monitor_error ];then
          rm ${ROOT}/que/daemon_error/${SERVER}_monitor_error
          echo "`date +%Y/%m/%d` `date +%H:%M:%S` ecron_monitord: ${SERVER} recovered" >>  ${ERROR_QUE}
       fi
    fi
 else
    echo "`date +%Y/%m/%d` `date +%H:%M:%S` ${SERVER} SUCCESS live_check_normal_end" >  ${SERVER_STATUS_QUE}
 fi

 done < ${ROOT}/map.conf

 sleep 60

done
