Shell script for alert

Hi Experts,

Im new in shell script , please help to achieve the below requirement, We have some replication setup in unix server, in that if there is any exception or error occurs immediately the rep_exception.log will have the exception detail, this log will be updated if any error occurs no other information will not be there. in this scenario , we need to send an alert mail , like whenever this is file updating , the content of this file will be send as part of mail body. then we have to clean up this file.

Or every 5 mins once we have to check this log file if any contents stored then we have to send an email then have to make the file empty. simply if any words or lines in the file we have to send an email.

please help on that, if its a basic one , apologize.

Hi there,

you'll want something like this:

while true
do
  if [ -s "rep_exception.log]
  then
    mail -s "Exception found for replication on `hostname`" $targetemailaddress < rep_exception.log && cat /dev/null > rep_exception.log
  fi
  sleep $SecondsBetweenChecks
done

Not tested

1 Like

Thanks a lot its working fine , Also is it possible to check the below condition as well

before checking the rep_exception.log file content , can we check whether this file is already available then proceed if [ -s "rep_exception.log" ] .

if the log file is not there then no need to proceed.

trying the below one ,

while true
do
  if [ -f "rep_exception.log] && [ -s "rep_exception.log]
  then
    mail -s "Exception found for replication on `hostname`" $targetemailaddress < rep_exception.log && cat /dev/null > rep_exception.log
  fi
  sleep $SecondsBetweenChecks
done

please confirm is this fine

That check is included in -s . You can test this yourself by using an imaginary file name.

We need to know more about what is writing data to your log file and how it is writing data to your log file. If the log file is being held open constantly by the writer, then catting /dev/null to your log file won't work; you'll have to force the writer to close and reopen the log file. (That may mean you have to kill and restart the writer.)

With the code you have, you'll have problems because:

  1. you have mismatched quotes,
  2. there has to be space between the test operands and the closing ] ,
  3. you'll lose log data if a log entry is made between the time when you send the email and the time when you clear your log file with cat , and
  4. although the log file isn't cleared if the mail fails, there is no backup plan for sending any notification if mail fails.

If log entries are written individually with the log file being created (if it didn't already exist), or appended to (if it did exist) for each log entry (as assumed by your current code), then your life if MUCH simpler with something like:

#!/bin/ksh
lf="rep_exception.log"
host=$(hostname)
SecondsBetweenChecks=300
TargetEmailAddress="someone@somewhere.com"
while true
do	if [ -s "$lf" ]
	then	mv "$lf" "lf.$$"
		mail -s "Exception found for replication on $host" \
			"$TargetEmailAddress" < "lf.$$" || cat "$lf.$$"
		rm -f "lf.$$"
	fi
	sleep $SecondsBetweenChecks
done

If mail fails, this will just cat the log data for that round to standard output. You can replace that cat with some other backup notification plan if you want to. This was written and tested with a Korn shell, but will work with any shell that conforms to minimal POSIX shell syntax (such as ash , bash , ksh , ,ksh , zsh , etc.).

With a pure, historical Bourne shell (such as /bin/sh on Solaris systems), you would need to change the line:

host=$(hostname)

to:

host=`hostname`
2 Likes

Thank you so much Don , Really this is very much helpful,:b:

As you suggested, We can append to for each log entry in the file. Here the concern is , if the appended entries is exceeded the maximum allowed mail size there will be a problem.

Our configuration is like , when ever err log updated then the same error log details with key values automatically stored in the database errlog table , We can refer that details for troubleshooting if some of the log data is not there in the body of the alert mail , here we need just an alert with some limited data in the alert mail as for reference.

also we can say in the mail , "Exception found for replication on $host, Check the errlog table for more detail and troubleshoot the issue"

So can you please suggest can we send only first 500/1000 lines from the log file. So once alert mail is received the team will check the errlog table in the database. thanks

As Don said :
You can replace that cat with some other backup notification plan if you want to.

What unix utility would you use instead of cat to display first 500 or 1000 or last 500 or 1000 lines ?

Regards
Peasant.

Note also that we're talking about more data being generated in 5 minutes than can be mailed. If there is data in your log file, it is mailed to you and the log file is cleared in 5 minutes or less.

How much data are you logging every 5 minutes? How big of a mail message can you send?

Thanks Don and peasant , its rare scenario for big log file generation , for example ,

if one record is failed in database there will be 10 lines in the log file , some times 100000 records also will be failed, that time the log file will have
100000 * 10 lines.

In this case if the mail body is have any restriction to hold 100000*10 lines then there will be a problem. please suggest . we can use tivoli tool also to trigger the mail.