Coloring alert.log Output via Tail and Less

To make my day-to-day job easier and more convenient, I like having some utilities and aliases.
We, the DBA nation, need to look at the alert.log file frequently. So, basically, I did the following:

  • Created a script to tail my alert.log file
  • Colored the important words

Added an alias to the script



$ vi tail_alert.sh

#!/bin/bash

export SCRIPT_DIR=”$( cd “$( dirname “${BASH_SOURCE[0]}” )” && pwd )”
source ${SCRIPT_DIR}/platform.env

ECHO_RED=”E[1;40;31m”
ECHO_STD=”E[1;40;37m”

LIGHT_RED=`echo -e ’33[1;40m33[1;31m’`
RED=`echo -e ’33[1;40m33[0;31m’`
LIGHT_PURPLE=`echo -e ’33[1;40m33[0;35m’`
GREEN=`echo -e ’33[1;40m33[32m’`
NORMAL=`echo -e ’33[0m’`

if [ “$1” = “” ] then
ALERT_FILE=${ORACLE_BASE}/diag/${DIR_TYPE}/${ORACLE_UNQNAME,,}/${ORACLE_SID}/trace/alert_${ORACLE_SID}.log
if [ ! -s ${ALERT_FILE} ] then
echo
echo -e “${ECHO_RED}please provide alert.log file name${ECHO_STD}”
echo
exit
fi
else
ALERT_FILE=”$1″
fi
WARNING_PATTERN=”ORA-|annot| not|WARNING|cannot allocate|not complete|LGWR: Attempting destination|network reconnect|LGWR: Destination LOG_ARCHIVE_DEST_|is UNSYNCHRONIZED|All online logs need archiving|Examine archive trace files for archiving errors|hung|Killing|Terminating|WARN:|Possible network disconnect|Archive log rejected|request rejected|dead|failed|shutting down|shutdown|disabled”
ERROR_PATTERN=”ORA-[^:][^ ]*|TNS-[^:][^ ]*|Error|Archival stopped|Fatal”
GOOD_PATTERN=”continu|STARTING|Completed:|COMPLETE|Starting ORACLE instance|Success|succeeded”

tail -n30 -f ${ALERT_FILE} | ${SED} “s/${WARNING_PATTERN}/${LIGHT_PURPLE}&${NORMAL}/gi;s/${ERROR_PATTERN}/${LIGHT_RED}&${NORMAL}/gi;s/${GOOD_PATTERN}/${GREEN}&${NORMAL}/gi”

To make it the easiest way to run, the last step is adding an alias to the script:
alias talert='/home/oracle/scripts/tail_alert.sh'

It’s the same trick for just viewing the file via less:

$ vi less_alert.sh
#!/bin/bash
export SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source ${SCRIPT_DIR}/platform.env
ECHO_RED="E[1;40;31m"
ECHO_STD="E[1;40;37m"
LIGHT_RED=`echo -e '33[1;40m33[1;31m'`
RED=`echo -e '33[1;40m33[0;31m'`
LIGHT_PURPLE=`echo -e '33[1;40m33[0;35m'`
GREEN=`echo -e '33[1;40m33[32m'`
NORMAL=`echo -e '33[0m'`

if [ “$1” = “” ] then
ALERT_FILE=${ORACLE_BASE}/diag/${DIR_TYPE}/${ORACLE_UNQNAME,,}/${ORACLE_SID}/trace/alert_${ORACLE_SID}.log
if [ ! -s ${ALERT_FILE} ] then
echo
echo -e “${ECHO_RED}please provide alert.log file name${ECHO_STD}”
echo
exit
fi
else
ALERT_FILE=”$1″
fi

WARNING_PATTERN=”ORA-|annot| not|WARNING|cannot allocate|not complete|LGWR: Attempting destination|network reconnect|LGWR: Destination LOG_ARCHIVE_DEST_|is UNSYNCHRONIZED|All online logs need archiving|Examine archive trace files for archiving errors|hung|Killing|Terminating|WARN:|Possible network disconnect|Archive log rejected|request rejected|dead|failed|shutting down|shutdown|disabled”
ERROR_PATTERN=”ORA-[^:][^ ]*|TNS-[^:][^ ]*|Error|Archival stopped|Fatal”
GOOD_PATTERN=”continu|STARTING|Completed:|COMPLETE|Starting ORACLE instance|Success|succeeded”

cat ${ALERT_FILE} | ${SED} “s/${WARNING_PATTERN}/${LIGHT_PURPLE}&${NORMAL}/gi;s/${ERROR_PATTERN}/${LIGHT_RED}&${NORMAL}/gi;s/${GOOD_PATTERN}/${GREEN}&${NORMAL}/gi” | less -R

And again adding an alias to the script:
alias alert=’/home/oracle/scripts/less_alert.sh’

Sample output:

Enjoy.