1833800 Members
2557 Online
110063 Solutions
New Discussion

Re: Script for checking

 
ust3
Regular Advisor

Script for checking

I have below script to check the word in the driectory /ora_tmp , there are many files in this directory , if the word "error" is exist in any file within 5 day , then send the mail to ora-usr@mydomain.com about which file have this word and the "error" statement , now , if I want

1. if there is no "error" word existed in any file , then DO NOT send the mail ;
2. send "error" statement to user one time only , that mean , only inform user the "error" statement one time , if there are same "error" statement within 5 days , send the mail to user one time only ( the user need to know the same error one time only ) . can advise how can I change it ? thx.

FILE=/tmp/error ; export FILE


mailfile=/tmp/mailfile ; export mailfile


find /ora_tmp -type f -mtime -5 -maxdepth 1 | xargs grep -i "error" > $FILE

cat $FILE >> $mailfile

echo $mailfile
mail ora-usr@mydomain.com < $mailfile
10 REPLIES 10
Dennis Handly
Acclaimed Contributor

Re: Script for checking

One way would be to have 6 files. Each day you would move the files one older:
mv ${FILE}.4 ${FILE}.5
...
mv ${FILE}.0 ${FILE}.1

(To prime the pump, create 5 empty files.)

Then do the find ... grep to ${FILE}.0

After this step, you would remove all lines that were in ${FILE}.1 ... ${FILE}.5
sort -u ${FILE}.[1-5] > ${FILE}.ALL
fgrep -v -f ${FILE}.ALL ${FILE}.0 > $mailfile
rm -f ${FILE}.ALL

(I'm assuming ${FILE}.ALL isn't too big or the performance of the script isn't important.)

Note: There is no need to export FILE and mailfile if you aren't reading them in some other script.

If the same error re-occurs after 5 days, it will be re-sent. If it goes away and then comes back in two days, it won't be re-sent.

1) only send if $mailfile has errors
if [ -s $mailfile ]; then
mailx -s "Error messags" ora-usr@mydomain.com < $mailfile
fi
ust3
Regular Advisor

Re: Script for checking

thx reply ,

I modified the script as below , it can send nodify mail only when there is error ,
but I am not too understand the suggestion above , my requirement is " to send "error" statement to user one time only , that mean , only inform user the "error" statement one time , if there are same "error" statement within 5 days , send the mail to user one time only ( the user need to know the same error one time only ) . can advise ? thx


FILE=/tmp/error ; export FILE
mailfile=/tmp/mailfile ; export mailfile
find /ora_tmp -type f -mtime -5 -maxdepth 1 | xargs grep -i "error" > $FILE
if [ -s $FILE ] ; then
cat $FILE >> $mailfile
echo $mailfile
mail ora-usr@mydomain.com < $mailfile
Dennis Handly
Acclaimed Contributor

Re: Script for checking

>I modified the script as below, it can send modify mail only when there is error,

(I assume there is a "fi" to go with the "if".)

>my requirement is "to send error statement to user one time only", that mean, only inform user the "error" statement one time, if there are same "error" statement within 5 days, send the mail to user one time only (the user need to know the same error one time only).

Currently my suggested code sends the error only once, in 5 days. If you want to turn the error back on after 5 days, you would have to fiddle with the 5 error "caches".

Basically, you need more words to describe what you want. I.e. a concrete example.

You could explain what you want for all 2**5 == 32 cases of one error either there or not, for 5 days. :-)
But I don't need that many cases.
ust3
Regular Advisor

Re: Script for checking

thx reply,

what my requirement is find the word "error" in the directory ( I will schedule it to run 3 hour/day ) , if there is "error" within 5 days then send the mail ( includes the file name ) to inform the administrator , BUT only send the same file name to admin one time .

for example , the oracle db generate the error to /tmp time by time , I would like to fix the error once it is happened , but if the error is not fixed yet over 3 hours , then NO NEED to send the same error to administrator , that mean administrator only receive the same error one time . can advise what can i do ? thx
ust3
Regular Advisor

Re: Script for checking

in the directory , there are many files , each file have many lines , if the applicatin have bug , it will generate the error to file , so some files have a word "error" ,
ust3
Regular Advisor

Re: Script for checking

thx all ,

please ignore the problem .
ust3
Regular Advisor

Re: Script for checking

sorry to ask again.

I would like to state my question again , the oracle is continuely generate some file to /tmp , some of file have error statement in it as below.

$ls /tmp
file1 file2 file3

$vi /tmp/file1
error:the amount is not correct
error:integer problem

$vi /tmp/file2
error:update problem

$vi /tmp/file3
error:exceed no. of input

what I want is to find which files have error in it and send a mail to administrator to inform the error is exist ( the mail have file name and the error statement ) , I will set a crontab job to check it in every 3 hours , but only send the mail one time if the same error, that mean only send a same error to administratior one time , can advise what can i do ? thx

Dennis Handly
Acclaimed Contributor

Re: Script for checking

# find which files have error in it
grep -ni error /tmp/file* > ${FILE}.0

# remove errors from before, includes file, line # and message
touch ${FILE}.ERR
fgrep -v -f ${FILE}.ERR ${FILE}.0 > $mailfile
# save new errors
sort -u -o ${FILE}.ERR ${FILE}.ERR ${FILE}.0
rm -f ${FILE}.0
# only send if $mailfile has errors
(copy from above)
Hein van den Heuvel
Honored Contributor

Re: Script for checking

>> but only send the mail one time if the same error, that mean only send a same error to administratior one time

One time per check cycle?
One time per file checked?
One time for all files checked?

Anyway, please find below a working skeleton perl solution which surely can readily be adapted to more detailed needs.
Give it a whirl?

[hint: after a first round of testing there will be a 'marker' file. So to re-test you may want to 'touch' a few error files.]

Enjoy,
Hein.

#!/usr/bin/perl

my $DIR = "/tmp/hein";
my $MARK = $DIR."/error_check_marker";
my $OPER = "hein";

use strict;
use warnings;

my $file;
my %errors_seen;

my $last_time = -M $MARK;
$last_time = 9999 unless $last_time;
open (MARK, ">$MARK") or die "Could not touch $MARK";
close MARK; # new modified time stamp

foreach $file (glob $DIR."/*") {
next if $last_time < -M $file; # been there done that?
if (open (FILE, "<$file")) {
$file =~ s/$DIR\///;
while () {
chomp;
next unless /^error:/;
if (defined $errors_seen{$'}) {
$errors_seen{$'} .= ",$file";
} else {
$errors_seen{$'} = $file;
}
}
close FILE;
} else {
print STDERR "Could not open $file $!\n";
}
}

if (%errors_seen) {
open (MAIL, "| mailx -s \"Error report\" $OPER") or die "Could not create Email";
printf MAIL "Found the following errors in recent file in $DIR\n";
foreach (sort keys %errors_seen) {
printf MAIL "%25s : %s\n", $_, $errors_seen{$_};
}
close MAIL;
}
Hein van den Heuvel
Honored Contributor

Re: Script for checking

btw, my scripts ignores the 5 day requirement, replacing with a 'new' error in this cycle. It Assumes the error files are created from scratch. If there are appended to then it gets trickier as an old message with remain. You would almost need to remember file, and line number for an error seen, to do that properly. Something similar (or the same as) Dennis's approach would be better for that, as with can remember whcih file, which line.
My approach could be augmented by making the 'marker file' retain a list of which errors seen and when.
On startup read the file and discard any marker error older that 5 days. Then only stash those errors in the errors_seen array which were not still in the marker file.
Update the marker fiel from the old data plus the fresh errors.
SMOP!

Hein.