shell script not showing output

manna787

New Member
Joined
Feb 26, 2024
Messages
4
Reaction score
0
Credits
48
I constucted the below script. This script should read oracle alert log by ADRCI tool and then send out email of any errors and warnings. I debugged the script and it does not have any errors but the script does not show any output. I appreciate if someone can help .

####################################################################################################

#!/bin/bash
#########################################################################################
# Description: Read each Oracle Home directory. Run adrci matching for problems
#
#
# crontab : # Check Alert Log 30.03.2014
# 00,30 * * * * /home/oracle/tmp/adrci_alert.sh > /home/oracle/tmp/adrci.cron.log 2>&1
#
#########################################################################################
# Which HOME?
export ORACLE_HOME=/orahome/app/oracle/product/19.0.0/dbhome
export DIAG_LOC=/orahome/app/oracle/diag/rdbms
# Who gets the alert?
export RECIPIENT='*******@adp.com'
# Other Variables
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export HOST=hostname
export PATH=$ORACLE_HOME/bin:$PATH
export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
export SUBJECT="Oracle ALERTS on ${HOST} OK"
export LOG=/tmp
export ALERT=$LOG/error.txt

# Write the alert log message header for the email
echo "${HOST} date +%Y-%m-%d.%H:%M:%S-%Z" > ${ALERT}
echo "All alerts in ADRCI Alert log for the last 30 minutes" >> ${ALERT}
echo "THIS ALERT WILL NOT BE REPEATED!!! TAKE ACTION NOW!!!" >> ${ALERT}
echo "Follow-up on this email and check the alert log on ${HOST}" >> ${ALERT}

# find out the homes
adrci_homes=( $(adrci exec="show homes" | grep -e rdbms -e asm))

# run through Each home found and examine the alert log
# Here we are looking for ORA- messges, Deadlock, anything which raises an incident or anything which is instance-level
# IN THE LAST 30 MINUTES (1/48), so we need to run this code every 30 minutes or we may miss something.
for adrci_home in ${adrci_homes[@]}
do
echo "Checking: ${adrci_home}" >> ${ALERT}
echo $adrci_home' Alert Log' >> ${ALERT}
adrci exec="set home ${adrci_home} ; show alert -p \\\"(message_text like '%ORA-%' or message_text like '%WARNING%' or message_text like '%instance%' or message_text like '%incident%') and originating_timestamp>=systimestamp-(1/48) \\\"" -term >>${ALERT}
done
# count the errors. This is a good place to exclude specific errors you wish to ignore with a -v match.
# note - your grep must be aligned with the pattern match above for this to work
num_errors=grep -c -e 'TNS' -e 'ORA' -e 'WARNING' -e 'instance' -e 'incident' ${ALERT} | grep -v 'ORA-28'

# If there are any errors, lets email the alert information to someone
if [ $num_errors -gt 0 ]
then
SUBJECT="ERROR in Oracle ALERT log on ${HOST}"
mail -s "${SUBJECT}" ${RECIPIENT} < ${ALERT}
fi
 


Someone with more scripting ability will be along shortly, I hope!

In the meantime, I suggest folks at least do a cursory check with ShellCheck. Here's an article on the subject:

 
HOST Variable: You have export HOST=hostname. If hostname is a command to get the hostname of the machine, it should be enclosed in backticks or $(). So it should be export HOST=$(hostname).

num_errors Variable: The line num_errors=grep -c -e 'TNS' -e 'ORA' -e 'WARNING' -e 'instance' -e 'incident' ${ALERT} | grep -v 'ORA-28' seems to have an issue. The grep -c command will return the count of matching lines, and then you pipe this to another grep command. This will not work as expected. If you want to exclude ‘ORA-28’ from the count, you should do the exclusion before counting. Like this:
num_errors=$(grep -e 'TNS' -e 'ORA' -e 'WARNING' -e 'instance' -e 'incident' ${ALERT} | grep -v 'ORA-28' | wc -l)
 
HOST Variable: You have export HOST=hostname. If hostname is a command to get the hostname of the machine, it should be enclosed in backticks or $(). So it should be export HOST=$(hostname).

num_errors Variable: The line num_errors=grep -c -e 'TNS' -e 'ORA' -e 'WARNING' -e 'instance' -e 'incident' ${ALERT} | grep -v 'ORA-28' seems to have an issue. The grep -c command will return the count of matching lines, and then you pipe this to another grep command. This will not work as expected. If you want to exclude ‘ORA-28’ from the count, you should do the exclusion before counting. Like this:
num_errors=$(grep -e 'TNS' -e 'ORA' -e 'WARNING' -e 'instance' -e 'incident' ${ALERT} | grep -v 'ORA-28' | wc -l)
Thanks I will try.
 
Top