#!/bin/ksh

# File now runs without user input and is going to be used to page when errors are encountered in the alert_log

# (and in other files as well, any file can be specified, so there can be multiple versions of this file to search

# any given filename.

#

# AUTHOR:  GIL STANDEN 09/15/2003

#

# Put all your ORA- error strings to search for in the "$PAGE_CONFIG"/strings file.

# DO NOT ! rm "$PAGE_CONFIG"/strings

#

# IMPORTANT:  SET xx FOR THE DATABASE INSTANCE ON WHICH YOU WILL RUN SCRIPT !!

xx=$1

#

PAGE_CONFIG=/home/orapf"$xx"/scripts/paging/config

PAGE_ALERTS=/home/orapf"$xx"/scripts/paging/pages

PAGE_DATA=/home/orapf"$xx"/scripts/paging/rawdata

PAGE_LOG=/home/orapf"$xx"/scripts/paging/log

PAGE_TEMP=/home/orapf"$xx"/scripts/paging/temp

# Put all your ORA- error strings to search for in the "$PAGE_CONFIG"/strings file.

# DO NOT ! rm "$PAGE_CONFIG"/strings

cp "$PAGE_CONFIG"/strings "$PAGE_CONFIG"/strings.bak

touch "$PAGE_TEMP"/strings1

rm "$PAGE_TEMP"/strings1

touch "$PAGE_TEMP"/strings2

rm "$PAGE_TEMP"/strings2

touch "$PAGE_DATA"/message.txt

rm "$PAGE_DATA"/message.txt

touch "$PAGE_DATA"/message1.txt

rm "$PAGE_DATA"/message1.txt

touch "$PAGE_TEMP"/lines1

rm "$PAGE_TEMP"/lines1

touch "$PAGE_TEMP"/lines2

rm "$PAGE_TEMP"/lines2

touch "$PAGE_TEMP"/mf_temp

#

#ext=`date +%y%m%d%H%M`

#

# read filenames?"Filename: "

uname -a | cut -f2 -d' ' | read servername

echo /mnt/pfcs/pfcs"$xx"/pfcs"$xx"db/8.1.7/admin/pfcs"$xx" | read DBADMIN

echo $DBADMIN/bdump/alert_pfcs"$xx".log | read filenames

#echo /home/orapf"$xx"/scripts/paging/log | read DBADMIN

#echo $DBADMIN/alert_pfcs"$xx".log | read filenames

echo /home/orapf"$xx"/scripts | read pgrhm

# read s?"Number of Grep Strings: "

# let s=2

more "$PAGE_CONFIG"/strings | wc -l | read s

#echo "s="$s

let i=1

# while [ "$i" -le "$s" ]

#    do

#    read string?"Search String $i: "

#    echo 'ORA-' | read string

#    echo $string':$string'$i >> "$PAGE_CONFIG"/strings

#    let i=$i+1

#    done

# exit

# read n?" Lines Before: "

let n=1

# read m?"  Lines After: "

let m=0

let i=1

let p=1

let alert=1

touch "$PAGE_TEMP"/messagefile1

export green=;

echo $filenames | cut -f"$p" -d' ' | read filename

#echo "filename="$filename

while [ -z "$green" ]

     do

     more "$PAGE_CONFIG"/strings > "$PAGE_TEMP"/strings1

     let i=1

#    echo "i="$i

#    echo "s="$s

     while [ "$i" -le "$s" ]

          do

#       echo "string1=string"$i

        grep 'string'$i "$PAGE_TEMP"/strings1 | cut -f1 -d':' | read string

#       echo "string="$string

        grep -n "$string" "$filename" | wc -l | read counter

#       echo 'counter_start='$counter

        grep -n "$string" "$filename" | cut -f1 -d':' | grep -n '' > "$PAGE_TEMP"/lines1

        more "$PAGE_TEMP"/strings1 | head -1 | read deleteid1

#       echo $deleteid1

        more "$PAGE_TEMP"/strings1 | sed "/$deleteid1/d" > "$PAGE_TEMP"/strings2

        more "$PAGE_TEMP"/strings2 > "$PAGE_TEMP"/strings1

          while [ $counter -gt 0 ]

               do

               more "$PAGE_TEMP"/lines1 | cut -f2 -d':' | head -1 | read lineno

               let increment=1

               let datefound=1

             while [ $datefound -ne 0 ]

                    do

                    let LB=$lineno-$increment

                    let UB=$lineno+$m

#                   echo "alert="$alert

                    grep -n '' "$filename" | sed -n "$LB"','"$UB"'p' | egrep 'Mon|Tue|Wed|Thu|Fri|Sat|Sun' >/dev/null

                    let datefound=$?

#                   echo "datefound="$datefound

                    let increment=$increment+1

                    done

#        let LB=$lineno-$n

#        let UB=$lineno+$m

         grep -n '' "$filename" | sed -n "$LB"','"$UB"'p' >> "$PAGE_TEMP"/messagefile"$p"

#        grep -n '' "$filename" | sed -n "$LB"','"$UB"'p' | sed -e 's/^[ \t]*//' > "$PAGE_TEMP"/mf"$alert"

    more "$filename" | grep  '' | sed -e 's/^[ \t]*//' | grep -n '' | sed -n "$LB"','"$UB"'p' > "$PAGE_TEMP"/mf"$alert"

       echo 'Above entries from: '$servername':'$filename | sed s/.log/./ | read file_loc

       echo $file_loc$2 >> "$PAGE_TEMP"/mf"$alert"

       echo $pgrhm'/paging/pages/mf'$alert >> "$PAGE_TEMP"/mf"$alert"

       wc -l "$PAGE_TEMP"/mf"$alert" | cut -f1 -d' ' | read lines_in_mf

#     echo ' '

#     echo 'lines_in_mf'$alert' outside of loop='$lines_in_mf

#     echo ' '

#     more "$PAGE_TEMP"/mf"$alert"

#     let looplimit=$lines_in_mf-1

       if [ $lines_in_mf -gt 4 ]

       then

#     cp "$PAGE_TEMP"/mf"$alert" "$PAGE_TEMP"/mf_temp_debug

       mv "$PAGE_TEMP"/mf"$alert" "$PAGE_TEMP"/mf_temp

#     let alertsave=$alert

#      echo "$PAGE_TEMP"/mf"$alert" >> "$PAGE_TEMP"/mf_temp

       grep $string "$PAGE_TEMP"/mf_temp | wc -l | read looplimit

       grep $string "$PAGE_TEMP"/mf_temp | cut -f1 -d':' | head -1 | read grab_line

       more "$PAGE_TEMP"/mf_temp | cut -f1 -d':' | head -1 | read dateline

       grep $string "$PAGE_TEMP"/mf_temp | head -1 | cut -f1 -d':' | read nexthit

       if [ $nexthit -eq $dateline+1 ]

       then

       while [ $lines_in_mf -gt 4 ]

       do

       grep -n $string "$PAGE_TEMP"/mf_temp | head -1 | cut -f1 -d':' | read grab_rel_line

       more "$PAGE_TEMP"/mf_temp | sed "$grab_rel_line"d > "$PAGE_TEMP"/mf_temp2

       mv "$PAGE_TEMP"/mf_temp2 "$PAGE_TEMP"/mf_temp

       wc -l "$PAGE_TEMP"/mf_temp | cut -f1 -d' ' | read lines_in_mf

       if [ $lines_in_mf -eq 4 ]

       then

       mv "$PAGE_TEMP"/mf_temp "$PAGE_TEMP"/mf"$alert"

       let alert=$alert+1

       fi

       done

       else

       let j=0

       while [ $j -lt $looplimit ]

            do

            grep -n $string "$PAGE_TEMP"/mf_temp | cut -f1 -d':' | read grab_rel_line

            head -1 "$PAGE_TEMP"/mf_temp > "$PAGE_TEMP"/mf"$alert"

            head -"$grab_rel_line" "$PAGE_TEMP"/mf_temp | tail -1 >> "$PAGE_TEMP"/mf"$alert"

            tail -2 "$PAGE_TEMP"/mf_temp >> "$PAGE_TEMP"/mf"$alert"

            grep $string "$PAGE_TEMP"/mf_temp | cut -f1 -d':' | read grab_line

            more "$PAGE_TEMP"/mf_temp | sed "/$grab_line/d" > "$PAGE_TEMP"/mf_temp2

            mv "$PAGE_TEMP"/mf_temp2 "$PAGE_TEMP"/mf_temp

            let j=$j+1

            let alert=$alert+1

#           echo ' '

#           echo 'lines_in_mf'$alert' inside of loop='$lines_in_mf

#           echo ' '

            done

       fi

            let alert=$alert-1

            fi

       let alert=$alert+1

#        echo ' '

#        echo 'after the loop alert='$alert

       more "$PAGE_TEMP"/lines1 | head -1 | read deleteid2

         more "$PAGE_TEMP"/lines1 | sed "/$deleteid2/d" > "$PAGE_TEMP"/lines2

         more "$PAGE_TEMP"/lines2 > "$PAGE_TEMP"/lines1

         wc -l < "$PAGE_TEMP"/lines1 | read counter

#        echo "counter="$counter

         done

         let i=$i+1

     done

     sort -uno"$PAGE_TEMP"/file"$p" "$PAGE_TEMP"/messagefile"$p"

     echo ' ' >> "$PAGE_TEMP"/file"$p"

     echo 'Above entries from ' >> "$PAGE_TEMP"/file"$p"

     echo $filename >> "$PAGE_TEMP"/file"$p"

     echo ' ' >> "$PAGE_TEMP"/file"$p"

     cat "$PAGE_TEMP"/file"$p" >> "$PAGE_DATA"/message.txt

     rm "$PAGE_TEMP"/messagefile"$p"

     rm "$PAGE_TEMP"/file"$p"

     let p=$p+1

     let q=$p-1

     echo $filenames | cut -f"$p" -d' ' | read filename

     grep "$filename" "$PAGE_DATA"/message.txt | read green

     if [ -z "$filename" ]

     then

     rm "$PAGE_TEMP"/lines1

     rm "$PAGE_TEMP"/lines2

#    rm "$PAGE_CONFIG"/strings

     rm "$PAGE_TEMP"/strings1

     rm "$PAGE_TEMP"/strings2

     cut -f2-20 -d: "$PAGE_DATA"/message.txt > "$PAGE_DATA"/message1.txt

     exit

     fi

     echo $filenames | cut -f"$q" -d' ' | read filename

     if [ $filename = $green ]

     then

     rm "$PAGE_TEMP"/lines1

     rm "$PAGE_TEMP"/lines2

     #rm "$PAGE_CONFIG"/strings

     rm "$PAGE_TEMP"/strings1

     rm "$PAGE_TEMP"/strings2

     cut -f2-20 -d: "$PAGE_DATA"/message.txt > "$PAGE_DATA"/message1.txt

     touch "$PAGE_TEMP"/mf_temp

     rm "$PAGE_TEMP"/mf_temp

     #rm "$PAGE_DATA"/message.txt "$PAGE_DATA"/messages.found."$2"

     #rm "$PAGE_DATA"/message1.txt "$PAGE_DATA"/messages1.found."$2"

     rm "$PAGE_DATA"/message.txt

     rm "$PAGE_DATA"/message1.txt

     exit

     fi

done