Difference between revisions of "RepServer Errorlog checker"

From SybaseWiki
Jump to: navigation, search
m
m
Line 6: Line 6:
 
#
 
#
 
# Script: rs_log_checker.sh
 
# Script: rs_log_checker.sh
# Author: Bob Holmes - email: cambob@gmail.com)
+
# Author: Bob Holmes - email: cambob@gmail.com
 
# Date  : 08/05/2008
 
# Date  : 08/05/2008
 
# Version: 1.1
 
# Version: 1.1

Revision as of 20:35, 18 February 2009

The script below is for monitoring the repserver errorlog - it works with any version of repserver and does not need to write to the errorlog itself in order to monitor it. However, since it doesn't write to the errorlog, it does need a $ADMINDIR/control_files/ directory which you may wish to create as a non-transient file (rserrlogmarker.dat) is maintained there. Also, as per the script notes, you can easily modify this script to monitor *any* log; be sure to give rserrlogmarker.dat and the other files in the config section different names or keep them elsewhere so that there is no conflict.

#!/usr/bin/ksh
#
# Script: rs_log_checker.sh
# Author: Bob Holmes - email: cambob@gmail.com
# Date  : 08/05/2008
# Version: 1.1
# Usage : rs_log_checker.sh [v|V] (verbose mode)
# Description: Script for checking the repserver errorlog
# --------------------------
#    ***    Variables for customisation can be found by searching for "!!" ***
# --------------------------
# Modification history:
#
# --------------------------
# Enhancement notes:
# BobH: V1.0: Modified to re-create rserrlogmarker file if not found, then re-run automatically.
# BobH: V1.1: Updated for detecting a new log file and resetting rserrlogmarker if so.
# --------------------------
# Comments: This script can easily be modified to monitor any log, including ASE.
#           The log itself is never written to so this script maintains a control
#           file to keep track of the last line it checked.

##############################################################################
#                           setup environment                                #
##############################################################################
HOSTNAME=`hostname`

##############################################################################
#                           config variables                                 #
##############################################################################
# config
RSLOGFILE="/PATH_TO_ERRORLOG_HERE/servername_rs.log"        # !! modify for your environment
ADMINDIR="/opt/home/sybase/admin"                           # !! modify for your environment

# static
RSLOGMARKER="$ADMINDIR/control_files/rserrlogmarker.dat"    # !! need $ADMINDIR/control_files/ directory
TEMPFILE1="$ADMINDIR/rslogchecker1.tmp"                     # transient file
TEMPFILE2="$ADMINDIR/rslogchecker2.tmp"                     # transient file
MAILFILE="$ADMINDIR/rslogchecker.mail"                      # transient file
RECIPIENTS="user@domain.dom"                                # !! enter email address config here 
                                                            #   (separate with commas for multiple addresses)
SCRIPT_NAME=`basename $0`

##############################################################################
#                             main program                                   #
##############################################################################

# check location of errorlog
if [ ! -f $RSLOGFILE ]
then
   echo RS errorlog file not found. Please check config.
   exit
fi

# get log marker value from file (or otherwise create it)
# V1.1: marker is now a line number
if [ ! -f $RSLOGMARKER ]
then
   MARKER=$(cat -n $RSLOGFILE | tail -1 | awk '{print $1}')
   echo $MARKER > $RSLOGMARKER
   #echo "rs log marker reset"
   $SCRIPT_NAME # must re-run after marker file reset
   exit
else
   MARKER=$(cat $RSLOGMARKER)
fi

# Compare number of lines in log file with current marker value to check
# if a new log has been created/repserver restarted.
if [ $(wc -l $RSLOGFILE | awk '{print $1}') -lt $MARKER ]
then
   # new log file created/repserver restarted
   printf "${SCRIPT_NAME}: New log file detected\n" > $TEMPFILE2
   RESTARTDT=$(head -1 $RSLOGFILE | awk '{print $2,$3}')
   printf "${SCRIPT_NAME}: Repserver restarted at: ${RESTARTDT}\n\n" >> $TEMPFILE2
   printf "Errors/Warnings from startup follow:\n" >> $TEMPFILE2
   printf "------------------------------------\n" >> $TEMPFILE2
   # pick out any errors/or warnings from log file:
   egrep "^E. |^W. " $RSLOGFILE >> $TEMPFILE2
   mailx -s "$HOSTNAME: Repserver restarted!" $RECIPIENTS < $TEMPFILE2
   rm $RSLOGMARKER # force reset of marker file
   rm $TEMPFILE2
   exit
fi


if [ "$1" = "V" ] || [ "$1" = "v" ] # verbose (diag) mode
then
  echo Current marker value is: $MARKER
fi

# increment the marker (line number) to avoid re-reading same last line
((MARKER=MARKER+1))
sed -n "$MARKER,$ p" $RSLOGFILE > $TEMPFILE1
######### next executable line checks for errors #########
######### add your search terms to the egrep below #######
EMSGS=$(egrep -c "^E\. |^W\. " $TEMPFILE1)
if [ $EMSGS -ne 0 ]
then
  if [ "$1" = "V" ] || [ "$1" = "v" ] # verbose (diag) mode
  then
     echo Error message found: $HOSTNAME: $EMSGS messages
     cat $TEMPFILE1
     echo END OF REPORT
  else
     cp $TEMPFILE1 $MAILFILE
     printf "-------CALLOUT SYBASE DBA - ON CALL----------------\n" >> $MAILFILE   # !! optional
     mailx -s "$HOSTNAME: $EMSGS errors found in repserver errorlog" $RECIPIENTS < $MAILFILE
  fi
fi

#update marker:
# Note: use line count from TEMPFILE1 + (MARKER-1) to avoid re-scanning the whole errorlog;
# cat/cp'ing large log files can cause load issues so we don't do that!
LINECOUNT=$(wc -l $TEMPFILE1 | awk '{print $1}')
((MARKER=MARKER+LINECOUNT-1))
echo $MARKER > $RSLOGMARKER
if [ "$1" = "V" ] || [ "$1" = "v" ] # verbose (diag) mode
then
  echo New marker value is: $MARKER
fi

# housekeeping
if [ -e $TEMPFILE1 ]
then
  rm $TEMPFILE1
fi
if [ -e $TEMPFILE2 ]
then
  rm $TEMPFILE2
fi

#end script