Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 22nd, 2003, 03:00 PM
Stew_McGruff's Avatar
Stew_McGruff Stew_McGruff is offline
PHP Wacko
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2003
Location: Washington DC
Posts: 991 Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 7 h 38 m 9 sec
Reputation Power: 70
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:
Code:
while 1:
    #watch for changes in file
    
    #if change, read line of file, send email

reading the file and sending an email is easy, I am just not sure how I would watch for the file to be changed.

Reply With Quote
  #2  
Old September 22nd, 2003, 03:47 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #3  
Old September 22nd, 2003, 04:04 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
You want the stat() command in the os module:

Quote:
stat(path)
Perform a stat() system call on the given path. The return value is an object whose attributes correspond to the members of the stat structure, namely: st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (time of most recent content modification or metadata change).


My guess is that you probably want one of those.

Reply With Quote
  #4  
Old September 22nd, 2003, 04:08 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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

Have fun,
Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #5  
Old September 22nd, 2003, 05:00 PM
Stew_McGruff's Avatar
Stew_McGruff Stew_McGruff is offline
PHP Wacko
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2003
Location: Washington DC
Posts: 991 Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 7 h 38 m 9 sec
Reputation Power: 70
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.

Reply With Quote
  #6  
Old September 22nd, 2003, 05:15 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
You got my interest! Let us know when you done or if you need anything else..

Mark.

Reply With Quote
  #7  
Old September 23rd, 2003, 12:51 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
I'd be interested, too, so feel free to post it as soon as you're done.

Reply With Quote
  #8  
Old September 23rd, 2003, 01:34 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
This script, by A.M. Kuchling, might be of interest.

Reply With Quote
  #9  
Old September 23rd, 2003, 03:19 PM
Stew_McGruff's Avatar
Stew_McGruff Stew_McGruff is offline
PHP Wacko
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2003
Location: Washington DC
Posts: 991 Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level)Stew_McGruff User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 7 h 38 m 9 sec
Reputation Power: 70
1st draft

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.
Code:
#!/usr/local/bin/python2.2
def daemonize():
	""" Become a daemon"""
	import os,sys
	if os.fork(): os._exit(0)
	os.setsid()
	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())

def daemonize_log_watcher():
	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 = 'py_log_watch@example.com'
	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 """
	file.seek(0,2)	
	
	""" Main Loop """
	while 1:
		if this_modified > last_modified:
			last_modified = this_modified
			""" File was modified, so read new lines, look for error keywords """
			while 1:
				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)
						server.quit()
							
		
		watcher = os.stat(log_filename)
		this_modified = watcher.st_mtime
		time.sleep(1)

if __name__=='__main__':
	daemonize()
	daemonize_log_watcher()

Last edited by Stew_McGruff : September 23rd, 2003 at 05:05 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to monitor a file for changes?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway