#!/bin/ksh
#
# check_errpt
#
# Changelog:
#
# XARY aka aryzhov@spasu.net Thu Sep  3 13:44:19 CEST 2009
# Ignore ERRPT coredumps from OmniVision
#
# XARY aka aryzhov@spasu.net Wed Jan 21 08:26:03 CET 2009
# Ignore ERRPT coredumps, if it's caused by oracle or emagent or dbsp
#
# XARY aka aryzhov@spasu.net Mon Aug 25 09:51:04 MEST 2008
# Introduced LASTSEQNF to trach the sequence number
# and avoid repeating messages
#
# XARY aka aryzhov@spasu.net Wed Feb  6 12:18:08 MET 2008
# Polished the Subject: field and added "Recent important .."
# to the mail body.
#
# XARY aka aryzhov@spasu.net Mon Feb  4 12:10:11 MET 2008
# Changed to $SEQNUM processing instead of $LASTEDATE
#
# XARY aka aryzhov@spasu.net Fri Feb  1 15:07:36 CET 2008
# One message per invocation only. See comment at "exit 1" below
#
# XARY aka aryzhov@spasu.net Thu Jan 31 13:57:55 CET 2008
# LASTEDATE recovery from garbage in LASTEFILE
#

[ x`uname` != xAIX ] && exit 0

SCRIPTNAME=`basename $0`
LASTEFILE=/var/tmp/$SCRIPTNAME.lasterr
LASTSEQNF=/var/tmp/$SCRIPTNAME.lastseq
EXCLFILE=$0.excludemsg

PROGNAME_EREGEX="oracle|emagent|dbspi"

# recover the timestamp of the last reported error
touch $LASTEFILE 2>/dev/null # to fool the housekeeping cronjobs
LASTEDATE=$(cat -q $LASTEFILE)
[ -z "$LASTEDATE"   -o   "$LASTEDATE" = Unable ]  &&  LASTEDATE=0101000000

#
# list all errors since the last timestamp
# and weed out the header and the last reported message
# retain only messages not in the exclude list
#
EXCLREGX="^IDENTIFIER|$LASTEDATE|`<$EXCLFILE awk '/^[0-9a-fA-F]/ {printf(\"%s|\", $1)}' | sed 's/[|]$//'`"

errpt -s $LASTEDATE 2>/dev/null | egrep -v "$EXCLREGX" | sort -k 2 | while read IDENTIFIER TIMESTAMP  T C RESOURCE_NAME  DESCRIPTION
do

  [ x"`echo $TIMESTAMP | sed 's/[0-9]//g'`" = x ] || {
    echo `date` - $IDENTIFIER $TIMESTAMP  $T $C $RESOURCE_NAME  $DESCRIPTION >>/var/tmp/errpt.BadEntry
    continue # Bad errlog entry
  }


  ELABEL=`errpt -a -s $TIMESTAMP -j $IDENTIFIER 2>/dev/null | awk '$1=="LABEL:"   {print $2}' | head -1`
  SEQNUM=`errpt -a -s $TIMESTAMP -j $IDENTIFIER 2>/dev/null | awk '$1=="Sequence" {print $3}' | head -1`
  LASTSQ=`echo \`cat $LASTSEQNF\``

  echo $SEQNUM | egrep -s '[0-9]' || SEQNUM=0
  echo $LASTSQ | egrep -s '[0-9]' || LASTSQ=1

  [ $LASTSQ -eq $SEQNUM ] && continue # we get repeating messages sometimes

  echo $SEQNUM      >$LASTSEQNF
  echo "$TIMESTAMP" >$LASTEFILE

  #
  # Ignore oracle and emagent coredumps (and OmniVision)
  #
  [ x"$IDENTIFIER" = xC69F5C9B ] && {
    PROGNAME=`errpt -a -l $SEQNUM | awk '/PROGRAM NAME/ {printf("\n")}; /^PROGRAM/,/PROCESSOR/ {printf(" %s ", $0)}' | awk '!/^$/ {print $3}'`
    echo "$PROGNAME" | egrep -s "$PROGNAME_EREGEX" && continue
  }

  #
  # Ignore oradata FS full
  #
  [ x"$IDENTIFIER" = xF7FA22C9 ] && {
    errpt -a -l $SEQNUM | tail -1 | egrep -s /export/oradata/ && continue
  }

  MSGTXT=`errpt -a -l $SEQNUM 2>/dev/null  | awk '/^Description/,/^Detail|Recommended/' | awk '!/^Description|^Detail|Recommended/' | sed 's/^Probable.*$/-/'`

  echo "`echo $MSGTXT | cut -b1-40`..."
  echo "`echo $MSGTXT`"
  echo
  errpt -a -l $SEQNUM 2>/dev/null
  echo "\n=== Recent important messages (in reversed chronological order) ==="
  errpt -s $LASTEDATE 2>/dev/null | egrep -v "$EXCLREGX|$TIMESTAMP" | sort -k 2 | head -10
  echo
  sleep 1

	      #
	      # XARY aka aryzhov@spasu.net Fri Feb  1 15:07:36 CET 2008
	      #
  exit 1      # If we continue looping here,
	      # we'll see only the latest error in Subject:
	      # which not necessarily is the most important one.
	      # So, deliver only 1 messages at script invocation.
	      #
	      # Chances are quite low that multiple *important*
	      # unfiltered events will happen wihin one iteration
	      #
done

#
# All latest errpt entries processed, nothing to report
#
echo "errpt OK"; exit 0

