
####################### Author : Navneet Chola 			####################
####################### Company Name : Wipro Infotect,Mumbai,India #############
####################### Usage :  error_checker.sh [no of days from today ] #####
####################### Purpose : This shell script Checks the ASE errorlog ####
####################### And report the Errors 				     ########
####################### By default it will check the current days log  #########
####################### Second paramter is the no of days back till    #########  
#######################                   today you want to check      #########  
####################### Dependencies :						########
####################### Following variables should be set for the login ########
#######################    $SYBASE, $SYBASE_ASE, $DSQUERY               ########
#######################   Read permission for the user on the Errorlog file  ###
###############################################################################

################################################################################

###Checking the No of the Parameters ##############

n=$#
if [ $n -gt 0 ]
then
n=$1
fi

if [ $n -gt 0 ]
then
	echo "Checking the error log for Last "$1" Days "
else
	echo "Checking for Today "
fi

yy=`date +%Y`
mm=`date +%m`
dd=`date +%d`

################################################################################
### Substracting the No of days from Current days  ############################ 
################################################################################

if [ $n -gt 0 ]
then

# Set the current month day and year.            
month=`date +%m`                                 
day=`date +%d`                                   
year=`date +%Y`                                  
                                                 
# Add 0 to month. This is a                      
# trick to make month an unpadded integer.       
month=`expr $month + 0`                          
                                                 
# Subtrace n from the current day.               
day=`expr $day - $n`                             
                                                 
# While the day is less than or equal to         
# 0, deincrement the month.                      
while [ $day -le 0 ]                             
do                                               
  month=`expr $month - 1`                        
                                                 
  # If month is 0 then it is Dec of last year.   
  if [ $month -eq 0 ]; then                      
    year=`expr $year - 1`                        
    month=12                                     
  fi                                             
                                                 
  # Add the number of days appropriate to the    
  # month.                                       
  case $month in                                 
    1|3|5|7|8|10|12) day=`expr $day + 31`;;      
    4|6|9|11) day=`expr $day + 30`;;             
    2)                                           
      if [ `expr $year % 4` -eq 0 ]; then        
        if [ `expr $year % 400` -eq 0 ]; then    
          day=`expr $day + 29`                   
        elif [ `expr $year % 100` -eq 0 ]; then  
          day=`expr $day + 28`                   
        else                                     
          day=`expr $day + 29`                   
        fi                                       
      else                          
        day=`expr $day + 28`        
      fi                            
    ;;                              
  esac                              
done                                

yy=`echo $year`
mm=`echo $month`
dd=`echo $day`

if [ $day -lt 10 ]
then
dd=`echo "0"$day`
fi
                                    
if [ $month -lt 10 ]
then
mm=`echo "0"$month`
fi


fi

ser_string=`echo $yy"\/"$mm"\/"$dd`

echo "sed -e '/"$ser_string"/,\$!d' "$SYBASE"/"$SYBASE_ASE"/"install"/"$DSQUERY".log" > /tmp/errorsh$LOGNAME

############################################################################################
######### sed script to find the errors is written to /tmp/errorsh
############################################################################################

sh /tmp/errorsh$LOGNAME  | egrep -ie '(warning|severity|fail|unmirror|mirror exit|not enough|error|suspect|corrupt|correct|deadlock|critical|allow|infect|error|full|problem|unable|not found|threshold|couldn|not valid|invalid|NO_LOG|logsegment|syslogs|stacktrace)'

############################################################################################
#########  Finally  finding the Errors from the Errorlog file      
############################################################################################
