September 22nd, 2003, 04:00 PM
How to monitor a file for changes?
I am trying to write a script that will monitor a file for changes, and then send an email with the changes. I am unsure on how to proceed. Here is some meta-code of what I am trying to do:
reading the file and sending an email is easy, I am just not sure how I would watch for the file to be changed.
#watch for changes in file
#if change, read line of file, send email
September 22nd, 2003, 04:47 PM
Check the file's date/time perhaps?
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
September 22nd, 2003, 05:04 PM
You want the stat() command in the os module:
My guess is that you probably want one of those.
September 22nd, 2003, 05:08 PM
I have to agree with scorp, you'll need to keep an eye on the last modifide time for the file, you can get this using the os.path.getmtime() function.. i'd sugest you take a look at the os.path module for more details.
If your on *ix you could use cron to run the script every 5 min or so, if not your gonna have to loop, it may not be the most eligant way to do things but it work's
You can then use the smtplib module to email the file as an attachment (or in the emails body) to wherever you need to. Pretty cool
If you need any more help i'd be happy to lend a hand
September 22nd, 2003, 06:00 PM
Thanks, stat() was all I need to get going. Right now the program sets itself up as a daemon, then I have a loop that keeps the script going. I use time.sleep(5) so it checks the file's modification time every 5 seconds.
I'll put a link to the first version of a completed script when I am done, if anyone is interested.
September 22nd, 2003, 06:15 PM
You got my interest! Let us know when you done or if you need anything else..
September 23rd, 2003, 01:51 AM
I'd be interested, too, so feel free to post it as soon as you're done.
September 23rd, 2003, 02:34 PM
September 23rd, 2003, 04:19 PM
Here is my first version of the log watching script. Right now, it is set up to monitor my php error log and email me on php parse or fatal error. It isn't very configurable yet, but if anyone has ideas for making it better, send them my way. This is mainly an excersise for learning python syntax, so I'm sure the script could be improved.
""" Become a daemon"""
if os.fork(): os._exit(0)
sys.stdin = sys.__stdin__ = open('/dev/null','r')
sys.stdout = sys.__stdout__ = open('/dev/null','w')
sys.stdout = sys.__stderr__ = os.dup(sys.stdout.fileno())
import time, os, re, smtplib
""" Configs. Should probably move these to a config file? or take args? """
log_filename = '/var/log/php_errors.log'
search_keywords = ['PHP Parse error', 'PHP Fatal error']
mailserver = 'localhost'
From = 'firstname.lastname@example.org'
To = 'email@example.com'
Subj = 'Log Watcher Alert'
""" Open File, get last mod time """
file = open(log_filename, 'r')
watcher = os.stat(log_filename)
this_modified = last_modified = watcher.st_mtime
""" Go to the end of the file """
""" Main Loop """
if this_modified > last_modified:
last_modified = this_modified
""" File was modified, so read new lines, look for error keywords """
line = file.readline()
if not line: break
for keyword in search_keywords:
if re.search(keyword, line):
text = ('From: %s\nTo: %s\nSubject: %s\n' % (From, To, Subj)) + line
server = smtplib.SMTP(mailserver)
failed = server.sendmail(From, To, text)
watcher = os.stat(log_filename)
this_modified = watcher.st_mtime
Last edited by Stew_McGruff; September 23rd, 2003 at 06:05 PM.