To exclude a string from monitoring

Hi below is my current scripting which will monitor for any errors in the application

logfile="/tmp/application.log"
output=$(grep "ERROR" $logfile)
if [ -n "$output" ]
then
    echo "found- send email" 
else
    echo "not found"
fi

Now I wanted the script not to send the email for one of the occurence which is a known issue
Could you please help me as how can I set this up
The error string I wanted to exclude from monitoring is below

"   ERROR  - Error while reading from socket
java.net.SocketException: Connection reset "

You don't need to measure grep's output to determine if it's found a string, it outputs an error code that you can use to plug into if directly.

If you want to test for one thing and exclude another though, I'd use awk. Much better than piling on pipe after pipe.

logfile="/tmp/application.log"

if awk '/ERROR/ && !/Error while reading from socket/ { exit 0 } END { exit 1}' $logfile
then
        echo "found- send email"
else
        echo "not found"
fi
1 Like

Hi,

Thanks for your quick reply
Could you please let me know as { exit 0 } END { exit 1} means?
are they error codes?

'exit 0' will cause the 'true' section of the if statement to occur. The instant it finds a matching line, it will return 0.
'exit 1' will cause the 'false' section of the if statement to occur. If awk finds no matching lines, it will end up returning 1 (once it runs the END section).

It is the code that awk returns to the shell. Every program returns a code, 0 for success, anything else(1-255) for errors.

1 Like

Hi Thanks again, I tested the script however its not working as expected :frowning:
There was an ERROR string apart from the "Error while reading from socket" and the script was not able to catch that error and send email

Sorry incase if you have got my question wrong- basically I wanted to EXCLUDE (not to send the email which is in my original post incase if you have missed) "Error while reading from socket" from the monitoring

had the issue of the 'return value' of the 'exit' in the past - Solaris' awk-s.

logfile="/tmp/application.log"  
if awk '/ERROR/ && !/Error while reading from socket/ { _e=1;exit } END { exit _e}' $logfile; then
         echo "found- send email" 
else         
         echo "not found"
fi
1 Like

Hi the platform is linux!

Hi,

Thanks for your reply
Linux platform
Is there any other way of getting there ?

Thanks for sharing!
Issue resolved I assume?

Show more of your input please, including the error it failed to catch.

Hi issue is not resolved yet!

The error its not catching is below

[dbupdate] ERROR  - ReaderException while receiving directives :
com.workbrain.clocks.server.wbsynch.CommunicationException: ; nested exception is:

---------- Post updated 03-07-13 at 09:19 AM ---------- Previous update was 03-06-13 at 06:21 PM ----------

Hi All,

Could some one please help me to fix this issue? thanks in advance

---------- Post updated at 09:53 AM ---------- Previous update was at 09:19 AM ----------

Hi,

The script also failed because it actually catches the exception
"Error while reading from socket"

Basically I want to ignore this error "Error while reading from socket" and the script should alert me for any other errors apart from this

---------- Post updated at 10:42 AM ---------- Previous update was at 09:53 AM ----------

Moving the thread to top

Do not bump posts.

Do not double post.

If we do not reply to your thread immediately, wait! We are not 'on call'.

vgersh's solution was working backwards I think, and had an unneeded 'exit' which stopped it from working -- otherwise his idea is sound:

logfile="/tmp/application.log"
if awk '/ERROR/ && !/Error while reading from socket/ { _e=1 } END { exit !_e}' $logfile
         echo "found- send email"
else
         echo "not found"
fi
1 Like

Sorry about that, perfectly worked!!! thanks so much again!!