#!/bin/bash

#
# check for digits only
#
function is_digit
{
echo $1|gawk ' { if ($0 ~ /^[0-9]+$/)
                    print 1
                  else
                    print 0 } '
} # end is_digit


#
# determine the ORACLE_HOME for the SID passed in.
#   * RAC test for root of SID removing last digit
#

orasid=$1
export ORACLE_SID=$orasid
_orasid0=${orasid:0:${#orasid}-1}
_orasid1=${orasid:${#orasid}-1:${#orasid}}
OH=`/usr/local/bin/dbhome $orasid`
if [ "$?" != 0 ] && [ $(is_digit $_orasid1) == 1 ]
then
        OH=`/usr/local/bin/dbhome $_orasid0`
        if [ "$?" != 0 ]
        then
                echo "error: cannot find ${orasid} or ${_orasid0} in oratab file."
                exit
        else
                echo "using ${OH} for SID ${_orasid0} found in oratab."
                export ORACLE_HOME=$OH
        fi
else
        echo "using ${OH} for SID ${orasid} found in oratab."
        export ORACLE_HOME=$OH
fi


script_log=/var/log/oracle/error_chk_${orasid}.log
alert_log=/var/log/oracle/alertlog_${orasid}.rpt
NOW=$(date +"%Y-%m-%d")

# added for easyagent filemonitor check
ea_filemon_log=/var/log/oracle/alertlog_${orasid}.err
ea_filemon_heartbeat=/var/log/oracle/alertlog_${orasid}.hb
# end easyagent stuff

lowersid=$(echo $orasid | tr '[:upper:]' '[:lower:]')
adrci_homepath="diag/rdbms/$lowersid/$orasid"

date > $script_log

        echo 'checking for errors in the alert log : '  $adrci_home >> $script_log
        $ORACLE_HOME/bin/adrci exec="set homepath ${adrci_homepath}; show alert -term -p \\\"message_text like '%ORA-%' \\\"" >> $script_log
        /bin/grep -A 2 $NOW $script_log > $alert_log
	
# added for easyagent filemonitor check
	if [ -s $alert_log ]; then
		cp $alert_log $ea_filemon_log
	else
		rm $ea_filemon_log
	fi
	touch $ea_filemon_heartbeat
# end easyagent stuff

exit
