Monitor a file and send mail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Monitor a file and send mail
# 1  
Old 05-08-2018
Monitor a file and send mail

I want to monitor the maillog file in real time and send a mail when a certain grep condition is met. Every time the grep condition is met a mail will be sent. I wish to ignore all grep conditions 30 mins after each mail and thereafter continue monitoring.

For example:
  1. Condition is met, mail is sent
  2. 30 mins no mail even if condition is met.
  3. After 30 mins again condition is met and a mail is triggered.
  4. Again for next 30 mins no mail even if condition is met.

and so on.

I am doing the following:

Code:
tail -F -n0 /var/log/maillog | \
 grep -v --line-buffered 'status=sent' | \
 grep --line-buffered -v 'relay=local' | \
 grep --line-buffered 'relay=' | \
 while read line
 do
    echo "$line" | \
    mail -s Test mail@group.com
 done

Now i am not sure how to prevent mail sending 30 mins after the event is triggered.

Last edited by rbatte1; 05-10-2018 at 08:22 AM.. Reason: Formatted numbered list with LIST=1 tags and broke up one huge line to make it more readable.
# 2  
Old 05-08-2018
How about we remember where we are upto in the maillog and only print new stuff.
That way we can sleep for 30mins and the resume checking:

Code:
UPTO=0
MAILFILE=/var/log/maillog
while [ -f "$MAILFILE" ]
do
    FSZ=$(wc -l < "$MAILFILE")

    if [ $FSZ -lt $UPTO ]
    then
        # File has shrunk!
        # most likely been truncated - start from beginning again.
        UPTO=0
    fi

    found=$( awk -v START=$UPTO -v FINISH=$FSZ '
       NR <= START || NR > FINISH { next }
       /status=sent/ || /relay=local/ { next }
       /relay=/ { print }' "$MAILFILE" )

    if [ -z "$found" ]
    then
       # Wait for a minute for maillog to grow a bit
       sleep 60
    else
       echo "$found" | mail -s Test mail@group.com

       # No more emails for 30mins
       sleep 1800

       # If you don't want to be notified about any other
       # occurances within this 30min period uncomment below
       # FSZ=$(wc -l /var/log/"$MAILFILE")
    fi
    UPTO=$FSZ
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies

2. Shell Programming and Scripting

Monitor file if match then send mail

Hi I want to monitor a file even if the file rotate. When a text occurs I want to send a mail. Something like this but it's not working correctly: tail -F mylog.log | grep 'MatchMe' | while read line do echo $(date +"%Y-%m-%d %H:%M:%S") MatchMe occurs | mail -s "MatchMe"... (1 Reply)
Discussion started by: chitech
1 Replies

3. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

4. Shell Programming and Scripting

Basic script for monitor send mail service

Hi All Need help Can any one share a basic script that is used for monitor sendmail service whether online, offline.etc in solaris Thanks in advance Zimmy (5 Replies)
Discussion started by: zimmyyash
5 Replies

5. Shell Programming and Scripting

Looking for shell script to monitor CPU utilization and send mail once exceed 75%

Dear Group, I'm look for shell script to Monitor CPU usage and send mail once it exceed 75% I'm running Suse10.4. (3 Replies)
Discussion started by: clfever
3 Replies

6. Shell Programming and Scripting

Monitor log file for a Error and generate the e-mail.

This is my log file and this is live log. Any abnormal error other than following I need to generate the email. Log path : /DER/app/admin/ABC/bdump/erg.log Current log# 2 seq# 103046 mem# 0: /ora2/oradata/ABC/redo02a.log Current log# 2 seq# 103046 mem# 1:... (1 Reply)
Discussion started by: hishanms
1 Replies

7. Homework & Coursework Questions

monitor log entries and send e-mail

I am stuck and worried. My assignment was due a day ago and I was too busy completing other assignments due during the same time. I worry that not completing this assignment will fail me. Need urgent help in completing the script asap. I dont want to sound pushy to get the answer but just wanted to... (10 Replies)
Discussion started by: vin8465
10 Replies

8. Emergency UNIX and Linux Support

monitor log entries and send e-mail

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! I am stuck and worried. My assignment was due a day ago and I was too busy completing other assignments due during the same time. I worry that not completing this assignment... (1 Reply)
Discussion started by: vin8465
1 Replies

9. UNIX for Dummies Questions & Answers

How to send html file in a mail not as an attachment but it should display in the mail in table for

Hi The below script working when we are sending the html as attachment can u please guide how to send thesmae data in table form direct in the mail and not in mail attachment . cat Employee.sql SET VERIFY OFF SET PAGESIZE 200 SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON - HEAD... (0 Replies)
Discussion started by: mani_isha
0 Replies

10. Shell Programming and Scripting

Pull E-mail address from file, send e-mail

Hello, I am new to perl and need to create a script that will read a file and pull a name from the file and send e-mail. How can I use the following awk statement in a perl script? grep UNIXadmins /root/mail.conf | awk '{ print $2}' and use the output to send a e-mail. Any help would... (1 Reply)
Discussion started by: DC Heard
1 Replies
Login or Register to Ask a Question