Help me in finding logic


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help me in finding logic
# 1  
Old 04-06-2009
Bug Help me in finding logic

I am searching for some logic which will help me to introduce quick action for the errors. We have application server and we need to check the bunch of information for every 10 minutes and alert thru mail. I wrote a script in which i am tailing last 1000 lines and counting one exception then sending mail like below

count=`tail -n 2000 /logsTrace.log | grep -c 'TimeOutException'`
if [ $count -ne 0 ]
then
echo "Error! $server is thrown TimeOutException $(date +"%D %T")" >> temp.txt
cat temp.txt | mail -s "Error! $server is thrown TimeOutException $(date +"%D %T")" $mail_id

i need to change my logic with the awk command where i can take like this for all exception tailing at one time

tail -n 1000 /logs/Trace.log | awk 'BEGIN{Jms = 0; OOM = 0}
/TimeOutException/{Jms++}
/OutOfMemoryException/{OOM = 1}
END{printf "%-10s%-10s",Jms,OOM;}'

like wise i can add for all the exception by tailing one time the log so that i can minimize the use of tailing. But how do i can send mail. Is there any way to do this please help me. i am stuck in that.

Thanks
Senthilkumar AK
# 2  
Old 04-06-2009
assuming the awk works .... I fixed a few errors may have missed one.

Code:
while true 
do
  tail -n 1000 /logs/Trace.log | awk '  BEGIN{Jms = 0; OOM = 0}
    {
      /TimeOutException/{Jms++}
      /OutOfMemoryException/{OOM++}
    }
    END{printf ("jms= %-10s OOM= %-10s",Jms,OOM;)}' > somefile
   if [[ -s somefile ]] ; then 
      cat somefile | /usr/bin/mailx -s 'errors'  somebody@somewhere.com 
   fi
# 10 minutes wait
   sleep 600
done

# 3  
Old 04-06-2009
Jim thanks,

But i am looking for a script which can send a mail with some messages, for example if the OOM is 1 then its send a message that we got the Out of Memory if the Jms =1 then i need to send the message like Time out exception... Do i can use more if loop and user the outside variable mail id in the awk ..?

Same like i need to check in the systemut.log and systemerr.log log.. do i make this as a function and reuse it more often.

thanks
Senthilkumar ak.
# 4  
Old 04-06-2009
Hi!

Perhaps you are thinking in a too complicated way? If I understood your problem correctly, main thing is to receive e-mail in case of an error, not for collecting statistics every 10 minutes?

Why wouldn't you just do something like

tail -f message_log_file | \
awk '/OOM/ { printf "OOM" }
/JAM/ { printf "JAM" } ' | while read LINE
do
# and the mailing part comes here
done

(Or even faster, use sed's regexps).

I haven't tested that, but it should work - and is LOTS lighter than polling the last 2000 lines of a file. Plus, this informs you instantly (well, one second after the file is changed, to be precise).

Regards,

pen
# 5  
Old 04-06-2009
Bug

Thanks Pen,

But my requirement is modifying the cron job which is already running there for a ten minutes. I need to tune the script to obtain more accuracy. The earlier script is very huge and they use tail -2000 for each every exception. We have almost 21 exception to catch and they are doing tail -2000 21 times for the same log. I thought of modifiying it awk statement and asking for help. Jim has given a hint to my idea but still i need some clarification on the mailing parts. In your logic if i get two exception i will get same mail not different mail. still i am require your expertise on this.

thanks
Senthilumar ak.
# 6  
Old 04-06-2009
Hi!

OK, you didn't mention about the cron job earlier :-(

Then we should go back to Jim's drawing board.

Code:
tail -n 2000 log_file | awk ' 
    /Match_1/ { arr[$0]++ } 
    /Match_2/ { arr[$0]++ }
    # and so on
    END { for (f in arr) printf "%s\n", f } ' | while read LINE
do
    # mail with subject $LINE
end

This way you'll receive one e-mail per each exception. The idea is to use the fact that all arrays in awk are assosiative, ie. indexed by strings. You can also group the matches, like
Code:
/Match_1|Match_2/ { ... }

but the code may get difficult to read.

pen

Last edited by pen; 04-06-2009 at 07:31 PM.. Reason: Prettyprinted the code
# 7  
Old 04-06-2009
Thanks Pen, thats my mistake.

But if i need add some lines and not only the exception then how can i add that and how i decide about the subject line of the mail content. I will be common for all the exception and not specific. I asked the same query to Jim also. I know i am expecting more, but that way we give a good script.

tail -n 2000 log_file | awk '
/Match_1/ { arr[$0]++; sub="We got OOM error" }
/Match_2/ { arr[$0]++; sub="We got JMS error" }

and use the sub at the end in the mail subject and arr in the mail content. Please correct me if i am wrong.

Thanks
Senthilkumar.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question