send mail once string is seen

I need to write a script that when it sees the following string in a log file, it emails a group of people

SQLException: ORA-12157: TNS:internal network communication error

im a newb to scripting, any ideas would be appreciated

Just use grep for the string in the log.. if you find it.. send out a mail

yeah but how do i go about doing that, i also need to only send an email if it's todays date...

Please elaborate your requirement..

im a script newb so i dont even know where to begin. I need a script that will constantly run and grep a log for the above string, and send an email if that string is found.... only on todays date...

no one.....?

Hey if you give can give what format the date(say dd-mm-yy or mon-dd-yy) is in the log, then it can be done.

Here's where you begin, but still, there are more things to consider...

ERROR=$(grep ORA-12157 /your/log/file.txt | wc -l)
if (( ${ERROR} > 0 )); then
mailx -s "SQLException: ORA-12157: TNS:internal network communication error" \ <email alias for multiple users>
fi

Save that in a script and put it in your cron tab.
Create a mail group in sendmail, and that's that.
That'll do what you are asking

BUTthere are other considerations.
Do you want to send the email every time the error occurs?
Are you just checking every so often, like say, every day to see if such error occured?
How often does the log file rotate? You don't want to be sending multiple e-mails for a single event, right?

I gave this my own shot

# vi check_ora
"check_ora" 14 lines, 302 characters
#program to grep orac error and email admin
#5-15-06
#Chris Saunders

if grep ORA-12157 /u01/AIDMS/log/AIDMS_default_island_1/aidms.log
then

echo "Oracle issues on cluster" | mailx -s "SQLException: ORA-12157: TNS:internal network communication error" saundersc
@hostname

fi

echo $?

but i cant get the email. this is just a starting point... i posted in the solaris forum on why i cant receive the email...

i want to send the email everytime it occurs on the current days date.. so I dont get an email from an old error...

the log file does not rotate often

i want this to run every 30 seconds... but i know how to do that via cron..

You are not getting anything because your "if" statement has no validation.
In layman's terms, your statement reads:
"if I look in this file for this information, then..."
It should read:
"if I look in this file for this information AND I find it, then..."

The snippet that I posted before should work. Obvioulsy is not the solution, since you want to get an e-mail every time you get the alarm, and the snippet I posted will e-mail you as long as there is 1 entry anywhere in your log file.

Now, are the entries in your log file time stamped?

yes there is a time stamp in this format, 2006-05-08 16:09:59,734

but i have to disagree with you, my program seems to be working...

if i dont echo the body of the text to mailx, then it waits for me to type it, once i run the script.... therefore im thinking the grep executes and finds that string and executes the then...

What shells are available? A quick, non-robust solution in ksh / pdksh might be as follows:

#! /bin/ksh
file=/u01/AIDMS/log/AIDMS_default_island_1/aidms.log

tail -n0 -f $file |&
while read -p line; do [[ $line == *ORA-12157* ]] && {
  print $line # To stdout
  mail_code_goes_here
 }
done

This will follow the file (tail -f), so it only counts messages received while it's running, so you'll have to find a way to make sure it runs reliably (if this is just a band-aid, nohup might be good enough.)

HTH

There is nothing wrong with code like:
if grep string filename ; then echo string was found ; fi

grep sets its exit code to indicate whether or not it found anything. Output may be sent to stdout which is the only concern. Usually you would use -q or redirect the output to /dev/null.

if grep string filename >/dev/null ; then echo string was found ; fi

solaris 9 box, so ksh, bash, etc..

hmm, good idea, but i kind of liked the code i wrote, maybe you guys could help me with that, i guess it will give more of a sense of acomplishment, if my code were to work, here it is again, i need to somehow only send email if the "ora" occurs on the current day..

#program to grep orac error and email admin
#5-15-06
#Chris Saunders

if grep ORA-12157 /u01/AIDMS/log/AIDMS_default_island_1/aidms.log
echo $?
then

echo "Oracle issues on cluster" | mailx -s "SQLException: ORA-12157: TNS:internal network communication error" saundersc
@hostname

fi