I want my script to NOT to send an e-mail if it finds the same keyword more than twice.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I want my script to NOT to send an e-mail if it finds the same keyword more than twice.
# 8  
Old 11-27-2014
I guess I still don't understand your requirements. You show four lines above and say you only want one message for those four lines (not four messages),

Pls check the attachment for the detailed error messages
Exact situation: There was a problem and the server is reporting it every second and it was friday, my weekend.
So after checking my mails on monday i., 2 days later, I received
60X60X60=216000 !! e-mails for the SAME message.
I do not want same email these many times. If it is different messages, it is absolutely fine.

but none of the keywords in either of the egrep commands in your script are in those four lines. So, why should any message be sent?

This is an example only not the exact situation. You can consider "JS_callout as a keyword here in this example"

Let me see if I understand what you're trying to do. You're running your log processor every minute, and you're only looking at new messages in the log file each minute. So, are you saying you want a mail message each minute of the day in which that error is reported?

Yes, my script runs every minute and checks the NEW lines in the database server errorlog for the errors (keywords) and send send an e-mail to my outlook account IF ANY.

With the sixteen keywords in your 1st egrep (there are only fourteen in the 2nd egrep) do I correctly understand that you're OK getting 16 messages each minute for each minute of the day (for a maximum of 23,040 messages in your mailbox each day) as long as no two messages sent by any invocation of your log checker contain the same keyword? Are the lines that you show above supposed to generate mail messages or not?

NO, I do not want emails for recurring messages within small duration
I'm ok if it sends the same error message (if received) for every hour

Why are you counting sixteen keywords as errors but only sending e-mail for fourteen of those keywords?[/QUOTE]
You can ignore the keywords part here. It is working fine and as per requirement
# 9  
Old 11-27-2014
I guess your process has a faulty design from the beginning.

Running the log check every minute makes sense only if you react within the minute. Estimate your reaction time and reduce the job's frequency adequately.

Composing the grep ... command pipe will trigger one single mail if any of the keywords is found any no. of times in the NEW part of the logfile, which requires the new lines to be identified correctly. You may want to reconsider that.

Creating a sensible schedule and setup for the log check, you should receive, say, 48 mails for the weekend, unless you reduce the frequency even further for out-of-office hours.
# 10  
Old 11-27-2014
Rudic,

I could not understand your statement "I guess your process has a faulty design from the beginning."

Running the log check every minute makes sense only if you react within the minute. Estimate your reaction time and reduce the job's frequency adequately.

This is meaningful and i will consider to change the frequency of the CRON job

Composing the grep ... command pipe will trigger one single mail if any of the keywords is found any no. of times in the NEW part of the logfile, which requires the new lines to be identified correctly. You may want to reconsider that.

I'm already aware of this.

Creating a sensible schedule and setup for the log check, you should receive, say, 48 mails for the weekend, unless you reduce the frequency even further for out-of-office hours.

You mean running the job for every hour? I will think on this.

Can you answer the following situation:

Imagine the CRON job is scheduled for every 10 minutes

The following are the new entries in the errorlog
JS: failed to receive jsagent response
Job Scheduler Task lost its Agent connection (Error 0)
Job Scheduler Task connected with Agent on port 4903

I will receive only 1 email for this. Agreed and accepeted.

Now, at next run(20 min later) I received the new and same error messages int he errorlog
JS: failed to receive jsagent response
Job Scheduler Task lost its Agent connection (Error 0)
Job Scheduler Task connected with Agent on port 4903

And again in the next run, same scenario and soon

Can you let me know how can we stop the script sending e-mails for the second run and thereafter (for same error messges scenario).

This is what exactly im trying to find out.

Hope you understand my question

-
Rajesh
# 11  
Old 11-27-2014
How about doing a sort | uniq -c on your new grepped results and the old extract (saved somewhere), and if the count is = 1, mail the error?
That would suppress identical lines; but a deviation of the slightest iota would issue a mail.
# 12  
Old 11-27-2014
Proposed solution:
Code:
#! /bin/sh

HOST=`uname -n`
MAILTO=rajeshneemkar@gmail.com

LOGFILE=/sybase/B10/ASE-15_0/install/B10.log
LOGBASE=`basename ${LOGFILE} .log`
LOGSENT=${LOGBASE}.notified
LOGLAST=${LOGBASE}.lastline

LOOKFOR="corrupt"
LOOKFOR="${LOOKFOR}|critical"
LOOKFOR="${LOOKFOR}|deadlock"
LOOKFOR="${LOOKFOR}|error"
LOOKFOR="${LOOKFOR}|fail"
LOOKFOR="${LOOKFOR}|infected"
LOOKFOR="${LOOKFOR}|logsegment"
LOOKFOR="${LOOKFOR}|no_log"
LOOKFOR="${LOOKFOR}|problem"
LOOKFOR="${LOOKFOR}|severity"
LOOKFOR="${LOOKFOR}|signal"
LOOKFOR="${LOOKFOR}|stacktrace"
LOOKFOR="${LOOKFOR}|suspect"
LOOKFOR="${LOOKFOR}|threshold"

IGNORED="background task error"

#-------------------------------------------------------------------------------
# initialize notification and lastline files

touch ${LOGSENT}

if [ ! -f ${LOGLAST} ]; then # start at the beginning
  echo 0 > ${LOGLAST}
fi

LASTPREV=`cat ${LOGLAST}`
LASTLINE=`wc -l < ${LOGFILE}`
echo ${LASTLINE} > ${LOGLAST}

if [ ${LASTLINE} -lt ${LASTPREV} ]; then # file has been trimmed
  LASTPREV=0
fi

#-------------------------------------------------------------------------------
# extract lines of interest

TMPFILE=`mktemp`
NEWFILE=`mktemp`

trap "rm -f ${TMPFILE} ${NEWFILE}" EXIT

if [ -f ${LOGFILE} ]; then
  tail -n +$(( ${LASTPREV} + 1 )) ${LOGFILE} \
  | egrep -i  "${LOOKFOR}" \
  | egrep -iv "${IGNORED}" \
  | sort -u
else
  echo ${HOST}:${LOGFILE}: no such file or directory
fi > ${TMPFILE}

comm -23 ${TMPFILE} ${LOGSENT} > ${NEWFILE} # new error messages

if [ -s ${NEWFILE} ]; then # send out new messages
  sort -o ${LOGSENT} ${LOGSENT} ${NEWFILE} # update message cache
  mailx -s "Errors - ${LOGFILE##*/} on ${HOST}" < ${NEWFILE}
fi

Some details:
Code:
LOGFILE=/sybase/B10/ASE-15_0/install/B10.log
LOGBASE=`basename ${LOGFILE} .log`
LOGSENT=${LOGBASE}.notified
LOGLAST=${LOGBASE}.lastline

The notified file keeps track of error messages that have been detected. This file needs to be removed once errors have been cleared.
Remove the lastline file if you want the rescan the entire logfile.
Code:
LOOKFOR="corrupt"
LOOKFOR="${LOOKFOR}|critical"
    ....snip....
IGNORED="background task error"

Just to make it easier to keep track of what is looked for and what is ignored.
Code:
tail -n +$(( ${LASTPREV} + 1 )) ${LOGFILE} \
  | egrep -i  "${LOOKFOR}" \
  | egrep -iv "${IGNORED}" \
  | sort -u

Get the unique error messages that have logged from the line after the previous last line of the logfile.
Code:
comm -23 ${TMPFILE} ${LOGSENT} > ${NEWFILE}

List of new error messages

Last edited by derekludwig; 11-30-2014 at 05:32 AM.. Reason: LOGLAST => LASTPREV (wrong variable)
# 13  
Old 11-29-2014
Code:
LOOKFOR="\
corrupt
critical
deadlock
error
fail
infected
logsegment
no_log
problem
severity
signal
stacktrace
suspect
threshold"

and a grep should work as well.
# 14  
Old 11-29-2014
MiG - good alternative. But I was not sure which version of *IX being used.

Had I thought about this longer, I would use two files:
Code:
if [ -f ${LOGFILE} ]; then
 tail -n +$(( ${LASTPREV} + 1 )) ${LOGFILE} \
 | grep -Eif "${LOOKFORS}" \
 | grep -Eivf "${IGNORES}" \
 | sort -u
else

${LOOKFORS} is a file that would list what is being looked for:
Code:
\<corrupt\>
\<critical\>
  ... snip ...
\<suspect\>
\<threshold\>

and ${IGNORES} would contain what can be ignored:
Code:
background task error

Command line options should also be used to select email address, lookfors file, ignores files, etc., should also be used (imho). Something like:
Code:
logwatch [-e emiladdr] [-l lookfors] [-i ignores] [-n notified] [-l lastline] logfile

but this will be left as an exercise for the reader.

---------- Post updated at 07:41 AM ---------- Previous update was at 05:36 AM ----------

Rajeshneemkar,

You should also consider having a second cronjob that runs daily or weekly that removes the ${LOGSENT} file. This would ensure that you get notified at least once daily of a reoccurring problem.

- DL

---------- Post updated at 07:55 AM ---------- Previous update was at 07:41 AM ----------

I also missed the attached logfile, so please change:
Code:
tail -n +$(( ${LASTPREV} + 1 )) ${LOGFILE} \
| egrep -i  "${LOOKFOR}" \

to:
Code:
tail -n +$(( ${LASTPREV} + 1 )) ${LOGFILE} \
| sed -e 's/^[^>]*> //' \
| egrep -i  "${LOOKFOR}" \

which will strip off the timestamps.

Last edited by derekludwig; 11-30-2014 at 05:34 AM.. Reason: LOGLAST => LASTPREV (wrong variable)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies

2. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

3. Shell Programming and Scripting

Ksh: Send a mail in case grep finds something

I want to search a file if it contains special strings and if yes, the records found should be mailed. I can either do it with a temporary file: /usr/bin/grep somestring somefile > /tmp/tempfile && /usr/bin/mail -s "Found something" email@mycomp.com < /tmp/tempfile... or by running the grep... (10 Replies)
Discussion started by: Cochise
10 Replies

4. Shell Programming and Scripting

Script Contain Send Mail via Telnet

Dear Masters, I am using Solaris 10 and bash shell. I make a script for checking if a file is exists on the specified folder or not from 5th day of the month until the end of the month. If the file doesn't exist, the script will send email to me. The script ran perfectly when I execute it on... (6 Replies)
Discussion started by: kris.adrianto
6 Replies

5. Shell Programming and Scripting

not able to send the mail through perl script

I have written small piece of code to send mail through perl script.Below is the code.I have executed it as # perl perlmail.pl and the code got executed with no errors. Still have not received any mail. Also I have installed Mail::send module from CPAN directly but no luck. Could any mail... (2 Replies)
Discussion started by: giridhar276
2 Replies

6. Shell Programming and Scripting

script to send mail !!

Hi Experts.. i have created a table with the fields, empid name mailid 1 raja raja@xy.com and entered the values of all persons who are in that file... i have a .csv file date shift1 shift2 6/6/2011 ram raja Now i want a script that could fetch the data in (input file .csv file) and... (3 Replies)
Discussion started by: cratercrabs
3 Replies

7. Shell Programming and Scripting

Help-send mail script

Hi, I have written one script for sending mails with attachment. currently its working for only one recipient. I want to send mails to n number of users by taking user input i.e number of users. Output of current script: Enter how many files : 1 Enter First Name : kiran E-Mail... (2 Replies)
Discussion started by: kiran_j
2 Replies

8. UNIX for Advanced & Expert Users

need to configure mail setting to send mail to outlook mail server

i have sun machines having solaris 9 & 10 OS . Now i need to send mail from the machines to my outlook account . I have the ip adress of OUTLOOK mail server. Now what are the setting i need to do in solaris machines so that i can use mailx or sendmail. actually i am trying to automate the high... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

9. Shell Programming and Scripting

Script to send a mail in UNIX

Hi, I need to write one unix script gor sending a mail notification. I have to pass the followinf as arguments,from ,to,subject,messege body Can i use mailx....Please provide the code Thanks in advance. (1 Reply)
Discussion started by: sudhi
1 Replies

10. Shell Programming and Scripting

Send e-mail in Shell script

How to send an error mail from a shell script e.g. mail destination_adr@blabla.int "Message : here an error message " thanks, Ann. (1 Reply)
Discussion started by: annelisa
1 Replies
Login or Register to Ask a Question