Monitor Oracle Processes
#!/bin/ksh
############################################################################
#
# Color Codes
#
############################################################################
ESC_SEQ="\033["
NORM=$ESC_SEQ"0m"
RED=$ESC_SEQ"0;31m"
LRED=$ESC_SEQ"1;31m"
LGREEN=$ESC_SEQ"1;32m"
############################################################################
#
# Create errcheck function
#
############################################################################
function errcheck {
if [[ $1 = 0 ]]
then return
fi
case $1 in
2) echo "ORACLE_MONITOR:ERROR:2:ORACLE_PROCESS_OFFLINE"
echo "---------------------------------------------"
echo "The following processes have been detected as OFFLINE..."
echo -e ${LRED}""
crscheck |grep -i off |egrep -v "${SDS_SI_IGNORE}"
echo -e ${NORM}""
echo -e ${LGREEN}"Action:${NORM} Contact a DBA to investigate the cause of this failure."
echo "---------"
echo ""
echo ""
SDS_ERRORS=`expr $SDS_ERRORS + $SDS_ERR`;;
4) echo "ORACLE_MONITOR:ERROR:4:ORACLE_PROCESS_NOT_RUNNING"
echo "------------------------------------------------------"
echo "The following errors have been detected..."
echo -e ${LRED}""
echo -e "Oracle Instance: $f, has no $n process running."
echo -e ${NORM}""
echo -e ${LGREEN}"Action:${NORM} Contact a DBA to investigate the cause of this failure."
echo "---------"
echo ""
echo ""
if [[ $SDS_ERRORS -eq 4 ]] || [[ $SDS_ERRORS -eq 6 ]]
then :
else SDS_ERRORS=`expr $SDS_ERRORS + $SDS_ERR`
fi;;
8) echo "ORACLE_MONITOR:ERROR:8:ASM_PROCESS_NOT_RUNNING"
echo "------------------------------------------------------"
echo "The following errors have been detected..."
echo -e ${LRED}""
echo -e "$SDS_ASM has no $n process running."
echo -e ${NORM}""
echo -e ${LGREEN}"Action:${NORM} Contact a DBA to investigate the cause of this failure."
echo "---------"
echo ""
echo ""
if [[ $SDS_ERRORS -eq 8 ]] || [[ $SDS_ERRORS -eq 10 ]] || [[ $SDS_ERRORS -eq 14 ]]
then :
else SDS_ERRORS=`expr $SDS_ERRORS + $SDS_ERR`
fi;;
16) echo "ORACLE_MONITOR:ERROR:16:GRID_LISTENER_NOT_RUNNING"
echo "------------------------------------------------------"
echo "The following errors have been detected..."
echo -e ${LRED}""
echo -e "There is no listener process running."
echo -e ${NORM}""
echo -e ${LGREEN}"Action:${NORM} Contact a DBA to investigate the cause of this failure."
echo "---------"
echo ""
echo ""
SDS_ERRORS=`expr $SDS_ERRORS + $SDS_ERR`;;
32) echo "Not set yet";;
64) echo "Not set yet";;
128) echo "Not set yet";;
*) echo "ORACLE_MONITOR:ERROR:999:SCRIPT_FAILURE_UNKNOWN_ERROR_CODE"
echo "----------------------------------------------------------"
echo -e ${LGREEN}"Action:${NORM} Contact a DBA to investigate the cause of this failure."
exit 999;;
esac
}
############################################################################
#
# Create crscheck function
#
############################################################################
function crscheck {
RSC_KEY=$1
AWK=/bin/awk # if not available use /usr/bin/awk
# Table header:echo ""
$AWK \
'BEGIN {printf "%-45s %-10s %-18s\n", "HA Resource", "Target", "State";
printf "%-45s %-10s %-18s\n", "-----------", "------", "-----";}'
# Table body:
$GRID_HOME/bin/crsctl status resource | $AWK \
'BEGIN { FS="="; state = 0; }
$1~/NAME/ && $2~/'$RSC_KEY'/ {appname = $2; state=1};
state == 0 {next;}
$1~/TARGET/ && state == 1 {apptarget = substr($2,index($2,"ZZZZZ"),7); state=2;}
$1~/STATE/ && state == 2 {appstate = $2; state=3;}
state == 3 {printf "%-45s %-10s %-18s\n", appname, apptarget, appstate; state=0;}'
}
############################################################################
#
# SCRIPT START
#
############################################################################
############################################################################
#
# Script : oracle_monitor.ksh
#
# Date : 12th Jan 2011
#
# Author : Mark Ramsay
#
# Copyright: www.markramsay.com 2011
#
# Description : Checks to make sure various processes are running
#
# Parameters : None
#
# History Date Name Reason
# ---- ---- ------
# dd/mm/yy xxx yyyyyyy
#
############################################################################
#
# Configure Variables
#
export SDS_COMPONENTS="pmon smon dbw lgwr"
export SDS_ASM_COMPONENTS="pmon smon dbw lgwr"
export SDS_GRID_LISTENER=lsnr
export SDS_RAC_IGNORE=""
export SDS_SI_IGNORE="ora.diskmon|ora.ons"
export SDS_ERRORS=0
export SDS_ERR=0
#
# Testing Variables Only - Unhash for Use.
#
#export SDS_SI_IGNORE="ora.dskmon|ora.ons"
#export SDS_COMPONENTS="pon son dbw lgr"
#export SDS_ASM_COMPONENTS="pon smon dbw lgwr"
#export SDS_GRID_LISTENER=lnr
#
# Check health using crs
#
crscheck |grep -i off |egrep -v "${SDS_SI_IGNORE}" >/dev/null
if [[ $? -eq 0 ]]
then SDS_ERR=2
errcheck $SDS_ERR
fi
#
# Get a list of DB instances
#
SDS_LIST=`egrep -v '(^M.....)00' /etc/oratab |egrep '(^M.....)'|awk -F\: '{print $1}'` #Line will need to be updated for the relevant database.
#
# Open loop for instances
#
for f in $SDS_LIST
do
#
# Open loop for components
#
for n in $SDS_COMPONENTS
do
#
# Check DB process is running for the given instance
#
ps -ef |grep $f |grep $n |grep -v grep >/dev/null
if [[ $? -eq 1 ]]
then SDS_ERR=4
errcheck $SDS_ERR
fi
#
# End loop for components
#
done
#
# End loop for instances
#
done
#
# Check ASM processes
#
SDS_ASM=`grep ^\+ASM /etc/oratab | awk -F\: '{print $1}'`
#
# Open loop for ASM components
#
for n in $SDS_ASM_COMPONENTS
do
#
# Check to see if ASM component is running
#
ps -ef |grep $SDS_ASM |grep -v grep |grep $n >/dev/null
if [[ $? -eq 1 ]]
then SDS_ERR=8
errcheck $SDS_ERR
fi
#
# End loop for ASM components
#
done
#
# Check listener
#
ps -ef |grep $SDS_GRID_LISTENER |grep -v grep >/dev/null
if [[ $? -eq 1 ]]
then SDS_ERR=16
errcheck $SDS_ERR
fi
#
# Exit script with error code
#
exit $SDS_ERRORS