Results 1 to 4 of 4
Hi,
Please can anyone give me an idea or script for to check a logfile in frequently and the script need to send an email alert9to external email id's) incase ...
- 07-12-2010 #1Just Joined!
- Join Date
- Jun 2007
- Posts
- 36
Shell Script for Send a EMail
Hi,
Please can anyone give me an idea or script for to check a logfile in frequently and the script need to send an email alert9to external email id's) incase of any new error in the log file.
Expectinig the valuable reply from LF members.
TIA.
- 07-13-2010 #2Just Joined!
- Join Date
- Sep 2008
- Posts
- 20
Hi skj,
the job may be very simple and may be done with a large variety of programs/commands...
Let's see ..
Suppose we want to trace only one log and only when some errors are present (say, we look for "RAID error #" into an ipotetic log /var/log/storages.log).
We want to check this every 15 minutes and send the eventual error(s) notice by mail to admin@privatemail.net.
Let's proceed
FOA, we need a script to do this. Say, we name it '/usr/local/bin/hd-check.sh'
I paste it here under (with comments inside the source)
Now we must schedule the script, say, every 15 minutes.Code:#!/bin/bash LOG2CHECK="/var/log/storages.log" PATTERN2CHECK="Raid error #" # I create some temporaneous resources TMPDIR=`mktemp -d /tmp/hdcheck_XXXXXX` TMP1="$TMPDIR/tmp_1" # Now try to search only for pattern we are interested in grep "$PATTERN2CHECK" "$LOG2CHECK" > "$TMP1" # ... now if "$TMP" is size>0 .. means we have some errors found. # in this case we want to be alerted by mail if [ -s "$TMP1" ]; then # send mail with mailx command (mailx2 version), which is able to modify # also "Return-Path" parameter with "-r" flag (mutt unfortunately cannot) mailx -s "Some errors in RAID device occured" -r "sender@mysite.com" admin@privatemail.net < "$TMP1" # ... you may also want to send error log as attachment.. in this case you should # issue a mail command like the following # # echo "This is the body. The error log is in attachment" | \ # mailx -a "$TMP1" -s "Some errors in RAID device occured" \ # -r "sender@mysite.com" admin@privatemail.net # (NOTE: tailing backslach ("\") means that the command continues in the next line.) fi # remove all temp folder rm -rf $TMPDIR
let's insert a cron job, with root user, by the command
like this oneCode:crontab -e
That's it!Code:*/15 * * * * /usr/local/bin/hd-check.sh
Considerations:
- In the system, you must have a running MTA (postfix, sendmail .. or whatever)
- I used mailx (mailx2) command present in recent version of openSuse distro, some other mail commands (such as mail) or mailx of other distributions (eg. CentOs) may have different flags and options.
- Some mail command (like mutt or system mail) could not have the ability to set the Return-Path header correctly. In this case, some external recipient's domain could not accept your incoming mail because of a wrong FQDN. In this case, adjust your command accordingly.
- You may also want to use "sendmail" standard command to send mail like the following:
.. but keep in mind that in this case, you must format the mail header correctly (From: To:,Subject:,etc.) in the file's head before sending. With this way, the Return-Path is well customized by the -f parameter.Code:/usr/sbin/sendmail -f "sender@mysite.com" admin@privatemail.net < $TMP1
That's all
Do not hesitate to write me back for any other question.
- 07-13-2010 #3
A lot depends on what is the format of your log file. I supposed that every error is logged as a single line containing the word "ERROR". The following script works for me:
Note that you can most probably just replace msmtp with any other sendmail-like program; it's just that I only have experience with msmtp.Code:#!/bin/bash tail -f -n0 <log-filename> | while read line ; do if [ $(echo $line | grep -i 'ERROR' | wc -l) != 0 ]; then (echo "Subject: Something Bad Has Happened"; echo; echo $line) | msmtp <email@address> fi done
I feel it necessary to remark that this script will not need to be run by a cron daemon. It is supposed to sit in the background and check every new piece information arriving in the file.Last edited by unlimitedscolobb; 07-13-2010 at 04:35 PM. Reason: Remark that no cron daemon is necessary.
- 07-13-2010 #4Just Joined!
- Join Date
- Jul 2010
- Location
- Florida
- Posts
- 1
Another short example
Here's perhaps a simpler example to follow...
NOTIFY_LIST is a variable in the script containing whom to email.
I grep the installer.log looking for something bad (*FAILED*)
If I find it, I email out the entire log to the NOTIFY_LIST.
You could put this in a script in conjunction with the crontab suggestion above...
Code:NOTIFY_LIST="email@address.com,email@address2.com" # Now let's verify result and email out if we failed... alert=`grep "*FAILED*" /tmp/installer.log -c` if [ $alert -ne 0 ]; then cat /tmp/installer.log | mailx /tmp/installer.log -s *FAILED*installfailed $NOTIFY_LIST fi


Reply With Quote
