Visit Our UNIX and Linux User Community


script to grep only the lasts errors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to grep only the lasts errors
# 1  
Old 06-07-2011
script to grep only the lasts errors

Hi,

sorry if there already a thread about this, I did a little bit of digging but haven't found exactly what I want.
I have a java application on a glassfish server who crash from time to time Smilie

I need a script to allert me if there's a error like "java heappspace" or "out of memory" in the server.log

for now I've done that :
Code:
#!/bin/sh
DIR=/opt/appli/glassfish.prod/domains/domain1/logs
MAIL=/usr/ucb/mail
DEST_MAIL=mail@me.com
SUBJ_MAIL="alert java  heapspace blablabla"
/usr/xpg4/bin/grep -q 'Java heap space'  $DIR/server.log
if [ $? -eq 0 ]
    then echo "alerte java heapspace was found" | $MAIL -s "$SUBJ_MAIL" $DEST_MAIL
fi

it works fine but the thing is, the log roll is every 10Mo and I don't want to change that, and the script will continue to allerte me even if the error was taking care of (until the server.log is archive and a new one is created)

So I need to find a way to grep this type of error, but to detect if this error has already bin found
I really don't know how to start....if there's any idea?

I'm on a Soalris 10

thanks

Last edited by Franklin52; 06-07-2011 at 07:35 AM.. Reason: Please use code tags
# 2  
Old 06-07-2011
create a file called /tmp/count and enter 0

Code:
DIR=/opt/appli/glassfish.prod/domains/domain1/logs
MAIL=/usr/ucb/mail
DEST_MAIL=mail@me.com
SUBJ_MAIL="alert java heapspace blablabla"
################added newly##############
err_count_file="/tmp/count"
error_count=`cat $err_count_file`
count=`/usr/xpg4/bin/grep -c 'Java heap space' $DIR/server.log`
if [ "$count" -gt "$error_count" ]
then
  echo "alerte java heapspace was found" | $MAIL -s "$SUBJ_MAIL" $DEST_MAIL
  echo $count > $err_count_file
else
 exit 0
fi

# 3  
Old 06-07-2011
well thanks, but doesn't work :-/ .... no email is sent....

but anyway, the condition is not satisfying ..... let's admit that I have an old java heap space in my server.log (who has already been treated), the grep will continue to count it, and the variable $count will still be greater than $error_count

maybe by just checking the log from the last XX minutes..... or something like that
# 4  
Old 06-07-2011
check the "mail" if any errors in sending the mail.

Add one more echo line inside "if" and direct the output to a log file to debug further.

check /tmp/count file as well ( whether does it have a new value )
# 5  
Old 06-07-2011
ok my bad, there was a quote missing ^^

so the script is working fine, but only until the log is archive and a new one is created
if a new log file is created, the old count stays in the "count" file .....and the script doesn't put 0 when it doesn't find anymore error ....
# 6  
Old 06-09-2011
ok, I finally did what I want

I made some adjustments to the previous script so if there's a lot of java heap space I don't receive a lot of mail^^

for info, here it is :

Code:
#!/bin/sh
DIR=/opt/prod/glassfish/domains/domain1/logs
DIR_COUNT=/opt/prod/exploit/domain1/scripts/
MAIL=/usr/ucb/mail
DEST_MAIL=mails@me.com
SUBJ_MAIL="[ALERTE]Java  heapspace was found"
START=0
err_count_file=$DIR_COUNT/count
error_count=`cat $DIR_COUNT/count`
/usr/xpg4/bin/grep -q 'Java heap space'  $DIR/server.log
if [ $? -ne 0 ]
    then echo $START > $DIR/count
fi
count=`grep -c 'Java heap space' $DIR/server.log`
if [ "$count" -gt "$error_count" ]
then
        echo "Java heap space found " | $MAIL -s "$SUBJ_MAIL" $DEST_MAIL &&  echo $count > $err_count_file

else 
	echo $count > $err_count_file && exit 0
fi


Last edited by pludi; 06-09-2011 at 08:27 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #15
Difficulty: Easy
UDP is a connection-oriented protocol.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to grep logs for Errors

Hi Guys, I want to write a script which can grep the logs (server.log) from a file for Error String and output to a other file. Problems: How to know about the errors only between the current restart and not in previous as server.log has earlier restarts also? thanks for the help! Much... (5 Replies)
Discussion started by: ankur328
5 Replies

2. Shell Programming and Scripting

How to grep logs for errors and receive specific additional lines?

Hi there, I have a script that I've used to find errors in my Minecraft Server logs. But I'd like to refine that script to be more useful. Here is the script: grep -n "SEVERE" /minecraft/server.log | awk -F":" '{print $1-2 "," $1+10 "p"}' | xargs -t -i sed -n {} /minecraft/server.log >>... (15 Replies)
Discussion started by: nbsparks
15 Replies

3. UNIX for Dummies Questions & Answers

How to ignore errors in script

I have a simple script that processes files. Here's a simplified example of what I'm doing: foreach t (web.*) mv $t dnw$t:e.log end foreach t (card.*) mv $t card$t:e.log end The problem is that sometimes there is no web.* file. In that case, I get an error "foreach: No match" and... (4 Replies)
Discussion started by: software5723
4 Replies

4. Shell Programming and Scripting

Help with execution errors in script

solution found.... Please use tags for scripts, listings, and console output (2 Replies)
Discussion started by: audiolord
2 Replies

5. Shell Programming and Scripting

purge logs, keep the 30 lasts

Hi, I have to make a script to purge logs in a directory (this script will run automatically every day) and this script has to keep just the 30 last files. So I want it to count all the files in the directory, find the 30 most recents and delete the others. I just started shell scripting and... (4 Replies)
Discussion started by: jblecrou
4 Replies

6. Shell Programming and Scripting

ftp script : list 4 lasts files

Hi, At work we have backups on a ftp. I want to view 4 last files saved (their names, dates, and weight). how can i achieve this goal using simplest way ? Thank's. (3 Replies)
Discussion started by: simon974
3 Replies

7. Shell Programming and Scripting

Help with shell script errors

hey watsup guys i am new in the shell script world. so i need help fom you guys, i have written these two codes and they both give the same errors( expr : syntax error). Code 1 : #! /bin/sh # count1 appends an increment to a file 200 times # note that a file called numbers must be... (5 Replies)
Discussion started by: surubi_abada
5 Replies

8. Shell Programming and Scripting

Script to capture errors

Hello; I'm trying to write a script to capture any hardware error from logs/syslog on my SUSE 10 servers so i can be notified if we have any hardware issues such a bad fan or battery, etc.. Thanks in advance for any help (2 Replies)
Discussion started by: Katkota
2 Replies

9. Shell Programming and Scripting

Remove lasts characters from a string

Hi all, Consider i have a directory /tmp/test and inside this directory i have the following files: 1.svf.tmp 2.svf.tmp 3.svf.tmp How can i remove the last four characters of every file in irder for the directory to be as: 1.svf 2.svf 3.svf I use the following command but id doesn't... (6 Replies)
Discussion started by: chriss_58
6 Replies

10. Shell Programming and Scripting

errors using grep

heres the line i think is giving me the error. any help is appreciated. code: if or posibly; while || error : usage: grep -e pattern_list... usage: grep -f... (2 Replies)
Discussion started by: pbonilla
2 Replies

Featured Tech Videos