cancel
Showing results for 
Search instead for 
Did you mean: 

I need a script ....

SOLVED
Go to solution
Voda
Advisor

I need a script ....

Can anyone help me on making a script which makes possible to compare the time from a log with the real time.

The log file looks like:
18:26:50 Maximum server connections 32...
and I want to compare it with the actual time and if it differ more than 5 minutes it sends me an email or a message to the screen. I have an HP-ux and Informix DB which the log is taken. It hangs every three weeks and it doesn't show nothing else that only stop to write on the log file. If you have another idea how to notice or you have a similar experience that the DB act like in this case you are pleased to help me on this.
I have tried everything like updating the DB, OS with no results.


Thank in advance to all who can help me

7 REPLIES
john korterman
Honored Contributor
Solution

Re: I need a script ....

Hi Voda,

sounds a bit like symptom treatment, but
try the attached script, using the logfile as $1

But before that, modify the mail address in the script.


regards,
John K.
it would be nice if you always got a second chance
Victor Fridyev
Honored Contributor

Re: I need a script ....

#!/bin/sh
LOGF=/tmp/dbmon.log
if [ -s $LOGF ] ; then
PREV=$(cat $LOGF)
else
PREV=0
fi
CURR=$(cat /var/adm/syslog/syslog.log|
grep "Maximum server connections" |wc -l)
if [ $CURR -le $PREV ]; then
echo ALERT|mailx -s "DB HANGS" your@mail
fi
echo $CURR > $LOGF


Run this script from crontab each 5 minutes.
The script counts strings with the needed pattern. If this number does not differ in 5 minute, you receive mail

HTH
Entities are not to be multiplied beyond necessity - RTFM
Hein van den Heuvel
Honored Contributor

Re: I need a script ....

John,

Your script will use the time from the 10th before last line, as that is the tail default.

Also, why not make awk do all the hard work while it has the data?!

eg:

TOTAL_LOG_SECONDS=$(tail -1 $1 | awk -F"[: ]" '{print $1*3600 + $2*60 + $3}')

or by having awk also find the end:

TOTAL_LOG_SECONDS=$(awk -F"[: ]" 'END{print $1*3600 + $2*60 + $3}' $1)


And finally here is a tweak which remembers the last piece of string that looked like a timestamp in the beginning of a line and then uses that at the END.

TOTAL_LOG_SECONDS=$(awk '/^..:..:/{t=$1} END{split(t,a,":"); print a[1]*3600 + a[2]*60 + a[3]}' $1)

awk could also do the current time, and perl even more easily so.

Cheers,
Hein.








Hein van den Heuvel
Honored Contributor

Re: I need a script ....


Voda,

Here is an other approach

You could create a reference file with the same time as last log file modification time.

Then when the script runs again you expect to see a more recent file.
If not, sound the bell.

In psuedo code:

touch -r $logdir/log $logdir/ref
while (1) {

wait 10 minutes

if (find $logdir -name log -newer ref)
then {
# all is well, update reference file
touch -r $logdir/log $logdir/ref
} else {
tail $logdir/log | mail ...
}

}
john korterman
Honored Contributor

Re: I need a script ....

Hein,

you are absolutely right that my suggestion does not read the correct line - thanks for pointing that out: I feel embarrassed.
And if I could handle awk as well as you, I would probably have done that...

Voda,
you can remedy my prior suggestion by changing the line
tail $1 | read a b
to
tail -1 $1 | read a b

However, finding the root cause would be better.

regards,
John K.
it would be nice if you always got a second chance
Voda
Advisor

Re: I need a script ....

Hi Guys,
Thanks for your help. The script is working great.

Regards
Endrin
Voda
Advisor

Re: I need a script ....

I found the answer