1833870 Members
1585 Online
110063 Solutions
New Discussion

Help on Shell script

 
SOLVED
Go to solution
SUDHAKARAN.
Occasional Advisor

Help on Shell script

Hello all,
Can you please help me in writing a shell script for continously monitoring the syslog file for certain errors and if that error occurs then execute a particular command.
Thanks
Sudhakaran.S
Unix Administrator
5 REPLIES 5
Scott Van Kalken
Esteemed Contributor

Re: Help on Shell script

The only way I know of to do this reliably is to schedule something via cron.

You can use tail -f but when the log file is rolled or a new one is created, the tail stops working.

I prefer a cron job that executes at n times per day and then just greps for the errors.

For example, my backup write this into the syslog:

Feb 12 02:01:48 melhpor1 syslog: INFO: LVM backups for host finished.
..
Michael Tully
Honored Contributor

Re: Help on Shell script

Hi,

You need to write a script that looks
at the /var/adm/syslog/syslog.log
file. In doing so you will need to
describe the errors or warnings that
you may be looking for. So at given
times of the day using cron, use
tail -f xxxx lines and grep for the
errors you might be looking for.

-Michael
Anyone for a Mutiny ?
Scott Van Kalken
Esteemed Contributor
Solution

Re: Help on Shell script

oops - lo9st half the post - too busy watching a purge of omniback....


anyway....

an exmaple would be:

#!/bin/sh

SCRIPT=${0##*/}
PATH=/bin:/sbin:/usr/bin:/whatever

if grep -q ; then mailx -s "oh my god there's an error" root@machine.com


if you're feeling artistic, put the error lines into a file and do something like this:


#!/bin/sh

#########################################################
#
# Short script to check for oracle databases
# Nothing fancy
# Has $basename.conf file to add database name to
#
#
# v1.00 22/5/01
#
# Scott van Kalken
#########################################################

######################################################
# Couple of variables - only the essentials man #
######################################################

SCRIPT=${0##*/}
CONF=/opt/admin/morning_checks/$SCRIPT.conf
PATH=/usr/bin:/opt/admin/morning_checks


#####################################################
# Functions
#
# Function go checks everything that needs to be
#
#####################################################


go(){
while read database
do
dbup=`ps -ef | grep -v grep | grep -c ora_pmon_$database`
if [ $dbup -eq 0 ]
then
echo $database IS NOT UP
else
echo $database IS UP
fi
done
}

###########################################################
#
# Main Script - if no conf file then blat out
#
###########################################################

if [ ! -f $CONF ] ; then echo "No Conf File" ; exit 1; fi
go < $CONF


This is a script to check whether or not oracle databases are up in the morning after cold backup.

Although we're moving to hot backup, it's still nice to see that they're at least up.

I'm thinking of something neat to test test for actual functionality in version 59.1 :)

Anyhow, hope this helps get you started.

Scott.
Peter Kloetgen
Esteemed Contributor

Re: Help on Shell script

Hi Sudahkaran,

the following script could do the job for you:

tail -n 5 /var/adm/syslog/syslog.log > /path_to_logfile
# output of tail is written into logfile

grep 'error_message' /path_to_logfile

#grep looks for error_message

if test $? -eq 0
then
command_to_be_done_in_this_case
fi

# if found, then command is executed

You simply have to put this script as cronjob, done as often as you need!


Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Bill Thorsteinson
Honored Contributor

Re: Help on Shell script

Consider installing logcheck. It scans the log periodically reporting all messages not excluded. You can flag certain messages as violations and they will be reported at the head of the list.