Generate a mail when you add info to a txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate a mail when you add info to a txt file
# 1  
Old 11-03-2010
Generate a mail when you add info to a txt file

Hi,

I have an application's log file:

/var/log/logfile which is feeded from time to time due to an application. This file contains data, what I want is:

-Whenever some new data is copied to /var/log/logfileI want to generate an email to root BUT only with the new added data in the body. Is it possible?

Thanks.
# 2  
Old 11-03-2010
Yes.

You need to provide exact examples of the data and what you expect to end up in the email
# 3  
Old 11-03-2010
Hi Jim,

The data added to log file are audit logs on an AIX server, here's an example"

USER_Create user user Wed Nov 03 11:20:17 OK mkuser
israel
USER_Remove user user Wed Nov 03 11:20:20 OK rmuser
israel

The source application is:
Code:
/usr/sbin/auditstream | /usr/sbin/auditselect -m -e "event== S_ENVIRON_WRITE || event== USER_Create || event== USER_Remove || event== USER_Change || event== GROUP_Create || event== GROUP_Remove || event== GROUP_Change || event== PASSWORD_Change " | /usr/sbin/auditpr -h elrtRc -t0 -v >> /var/log/logfile

So, the beginning of the file is always any event inside this code.

Thanks
Israel
# 4  
Old 11-04-2010
You will need to keep track of the lines already emailed. I nominated /var/log/logfile.notify. Script could be run from cron every Xmins. Note if file is truncated (eg by log rotate) the logfile.notify linecount will need to be updated, otherwise no emails till the log passes the high watermark stored in the notify file.

Code:
NOTIFY_FILE=/var/log/logfile.notify
LOGFILE=/var/log/logfile
WHO=root@localhost
LAST=0
[ -f $LOGFILE ] || exit
[ -f $NOTIFY_FILE ] && LAST=$(cat $NOTIFY_FILE)
NOW=$(wc -l $LOGFILE | cut -f1 -d' ')
 
if [ $LAST -lt $NOW ]
then
    ( if [ $LAST -gt 0 ] 
      then
          sed "1,${LAST}d" $LOGFILE
      else
          cat $LOGFILE
      fi ) | mailx -s "New data for $LOGFILE" $WHO
    echo $NOW > $NOTIFY_FILE
fi

# 5  
Old 11-04-2010
Hi Chubler,

Look the error it shows when you run the scritp:

Code:
user@server: /var/tmp # emailnotify.ksh
emailnotify.ksh[11]: test: 0403-004 Specify a parameter with this command.
Null message body; hope that's ok

If /var/tmp/logfile is empty it always send a blank mail to root. Any idea why?

FYI: I'm running the code on ksh

Really appreciate your help.
Thanks.

---------- Post updated at 04:08 AM ---------- Previous update was at 03:12 AM ----------

well, the issue was logfile.notify needs to have data. I did:

Code:
echo 1 >> /var/log/logfile.notify

Now the script is working... thanks a lot!!!

Regards
Israel.
# 6  
Old 11-04-2010
Quote:
Originally Posted by iga3725
Hi Chubler,
well, the issue was logfile.notify needs to have data. I did:

Code:
echo 1 >> /var/log/logfile.notify

Now the script is working... thanks a lot!!!

Regards
Israel.
Funny the following code should have set LAST to zero if the notify file wasn't there. I tested this before posting. did you modify this part of the code?

Code:
LAST=0
[ -f $LOGFILE ] || exit
[ -f $NOTIFY_FILE ] && LAST=$(cat $NOTIFY_FILE)

# 7  
Old 11-05-2010
Hi,

No I haven't changed anything.. dis you test your code on ksh? I'm running ksh on AIX.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to store info from a txt file into a hash?

I'm trying to make a perl script using the "open" command to open and read a file, storing the information in said file into a hash structure. This is what is inside my file- Celena Standard F 01/24/94 Cancer Jeniffer Orlowski F 06/24/86 None Brent Koehler M 12/05/97 HIV Mao Schleich... (4 Replies)
Discussion started by: Eric1
4 Replies

2. Shell Programming and Scripting

Generate fixed length txt file

hi, i am using below query to generate the fixed length txt file. this sql is being called from shell script. This is supposed to be a fixed record file with the below definitions. There must be 2 byte filler after the CAT_ID AND each line should have total of 270 bytes. field ... (1 Reply)
Discussion started by: itzkashi
1 Replies

3. Shell Programming and Scripting

Unable to attach a .txt file or .log file to mail and mailx command

Hi, I am trying to attach a .log file or .txt file to mail command to send an email once my ksh script executed. I am unable to use mutt command as it has been not installed and i am not supposed to install it. I have tried many ways by googling which has not helped me to succeed. Here is my... (5 Replies)
Discussion started by: Samah
5 Replies

4. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

5. UNIX for Dummies Questions & Answers

how to generate random number as as the first column of a txt file

Dear all, I have a question. I have a txt file say 4000 rows X 1800 Column. I 'd like to creat a new column as the first column which is a column of random numbers (n=4000) thanks a lot! Lin (2 Replies)
Discussion started by: forevertl
2 Replies

6. Shell Programming and Scripting

problem with sending mail from txt file having HTML code via sendmail -t

Hi, i have the following code in shell named as test3.sh.. #!/bin/sh . /home/<user>/.profile export dt=`date "+%d%b%y"` export tim=`date "+%d%b%y %HM:%MM"` cd export WD=`pwd` SID="<sid>" export SID export ORACLE_SID=$SID export ORACLE_HOME=/oracle/$SID/102_64 export... (4 Replies)
Discussion started by: jassi10781
4 Replies

7. Shell Programming and Scripting

How to monitor log file for a Error and generate the e-mail ( Please help)

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:... (7 Replies)
Discussion started by: hishanms
7 Replies

8. 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

9. Emergency UNIX and Linux Support

Generate Info From a Unix Binary

We have a situation where a binary from our build system is failing in one specific flow but the same from dev works fine. We are sure that build system picks up the right libs and code, however we see a difference of 6000 bytes in both. (build - 6000 bytes = size of dev binary) Hence we would... (7 Replies)
Discussion started by: uunniixx
7 Replies

10. Shell Programming and Scripting

Need to attach a txt file while sending mail

Hi, I need to attach a txt file while sending mail thru Unix. Please help me out in this.. -Charan. (9 Replies)
Discussion started by: charan81
9 Replies
Login or Register to Ask a Question