Shell script for alert


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for alert
# 1  
Old 07-02-2015
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.
# 2  
Old 07-02-2015
Hi there,

you'll want something like this:
Code:
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
This User Gave Thanks to Smiling Dragon For This Post:
# 3  
Old 07-03-2015
shell script for alert

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 ,

Code:
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

Last edited by Don Cragun; 07-03-2015 at 04:43 AM.. Reason: Add CODE and ICODE tags.
# 4  
Old 07-03-2015
That check is included in -s . You can test this yourself by using an imaginary file name.
# 5  
Old 07-03-2015
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:
Code:
#!/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:
Code:
host=$(hostname)

to:
Code:
host=`hostname`

These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-03-2015
shell script for alert

Thank you so much Don , Really this is very much helpful,Smilie


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
# 7  
Old 07-04-2015
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filesystem alert shell script not working!!

Hi All, My below shell script is not capturing %used value in the filesystem alert in the subject of the mail alert: Code: #!/bin/bash export DBALIST="abc@xyz.com" df -k /oradata/xyz/archive > dfk.result archive_capacity=`awk -F" " '{ print $5 }' dfk.result|grep -i %| cut -c 1-4` if... (6 Replies)
Discussion started by: harveyclayton
6 Replies

2. UNIX for Beginners Questions & Answers

Shell script to send mail alert

Hi I have below shell script to send mail alert , however I want to add more functionality in this script and that is , script should only check that file between 9 am to 5pm , and if there is no activity in this time 9 am to 5 pm for 2hours then it should give me mail alert, please help... (2 Replies)
Discussion started by: scazed
2 Replies

3. Shell Programming and Scripting

Shell script to send mail alert

HI Guys, I am writing one shell script to send the mail alert to some email id's if the file not modified in last 10 mins but its not working, I believe MTIME is null string is wrong . can you please assist me on this. script :- filename="abc.txt" echo "Filename is $filename"... (1 Reply)
Discussion started by: abhigrkist
1 Replies

4. Shell Programming and Scripting

shell script to alert if a file has been modified

Hi , I want a script who will send alert the moment someone edit any file in a directory in LINUX. Can some one throw some light on this please.!! (4 Replies)
Discussion started by: d8011
4 Replies

5. Shell Programming and Scripting

shell script to alert cpu memory and disk usage help please

Hi all can any one help me to script monitoring CPU load avg when reaches threshold value and disk usage if it exceeds some % tried using awk but when df -h out put is in two different lines awk doesnt work for the particular output in two different line ( output for df -h is in two... (7 Replies)
Discussion started by: robo
7 Replies

6. Shell Programming and Scripting

Shell script to capture ORA errors from Alert Log

Hi, as the title says, I am after a simple script, which will open the Alert log from an 11.2.0.1 Linux environment and mail the error message and description to a recipient email address. I can then schedule this job via cron and let it run every 15 minutes. I have searched online... (16 Replies)
Discussion started by: jnrpeardba
16 Replies

7. Shell Programming and Scripting

Unix Shell Script to automate email alert

Hi all, I have a task on my plate which is of high priority. I need an automated email alert that checks FTP notices subdirectory on a daily basis and forwards any word files to a group of people. This word files gets created whenever there is an issue with FTP connectivity. Please help...... (1 Reply)
Discussion started by: stunnerz_84
1 Replies

8. Shell Programming and Scripting

Filesystem alert shell script not working!!

Hi All, My below shell script is not capturing %used value in the filesystem alert in the subject of the mail alert: #!/bin/bash export DBALIST="abc@xyz.com" df -k /oradata/xyz/archive > dfk.result archive_capacity=`awk -F" " '{ print $5 }' dfk.result|grep -i %| cut -c 1-4` if ] then... (5 Replies)
Discussion started by: a1_win
5 Replies

9. Shell Programming and Scripting

shell script not getting current error messages with time from alert.log

Hi All, I need to get current error messages with time from alert.log.Below is my shell script but it's not working to meet this objective. could anyone pls share on the above issue for resolution: #################################################################### ## ckalertlog.sh ##... (2 Replies)
Discussion started by: a1_win
2 Replies

10. Solaris

Shell script to send email alert for core dump

Friends, I am in search for a shell script that is capable of running as a cronjob and have to send out an email when ever there is a CORE DUMP. Please post the hints to achieve my goal. Thanks in advance. (1 Reply)
Discussion started by: rtatineni
1 Replies
Login or Register to Ask a Question