#!/usr/bin/python # # max inotify childs to small for midsize/large environments # import os import sys import pyinotify import time class EventHandler(pyinotify.ProcessEvent): def __init__(self, wm, watches): super(EventHandler, self).__init__() self.wm = wm self.watches = watches def process_IN_ACCESS(self, event): return print "Read %s" % (event.pathname,) def process_IN_ATTRIB(self, event): print "Attributed %s" % (event.pathname,) def process_IN_CLOSE_NOWRITE(self, event): return print "Closed nowrite %s" % (event.pathname,) def process_IN_CLOSE_WRITE(self, event): print "Closed write %s" % (event.pathname,) def process_IN_CREATE(self, event): print "Created %s" % (event.pathname,) if event.mask & pyinotify.IN_ISDIR: dirpath = event.pathname wdd = self.wm.add_watch(dirpath, pyinotify.ALL_EVENTS) if wdd[dirpath] < 0: print dirpath, wdd else: self.watches.update(wdd) def process_IN_DELETE(self, event): print "Deleted %s" % (event.pathname,) try: if event.mask & pyinotify.IN_ISDIR: wid = self.watches[event.pathname] self.wm.rm_watch(wid) except Exception, ex: print ex def process_IN_DELETE_SELF(self, event): print "Deleted self %s" % (event.pathname,) def process_IN_MODIFY(self, event): print "Modified %s" % (event.pathname,) def process_IN_MOVE_SELF(self, event): print "Moved self %s" % (event.pathname,) def process_IN_MOVED_FROM(self, event): print "Moved from %s" % (event.pathname,) def process_IN_MOVED_TO(self, event): print "Moved to %s" % (event.pathname,) def process_IN_OPEN(self, event): return print "Opened %s" % (event.pathname,) def process_default(self, event): print "Default %s %s" % (event.pathname, event.maskname) def main(): watches = {} wm = pyinotify.WatchManager() handler = EventHandler(wm, watches) #notifier = pyinotify.Notifier(wm, handler) notifier = pyinotify.ThreadedNotifier(wm, handler) notifier.daemon = True notifier.start() count = 0 for dirpath, dirnames, _filenames in os.walk('/'): if dirpath in ('/dev', '/proc', '/sys', '/var/cache/pbuilder/btrfs', '/var/log'): del dirnames[:] continue wdd = wm.add_watch(dirpath, pyinotify.ALL_EVENTS) if wdd[dirpath] < 0: print count, dirpath, wdd #break else: watches.update(wdd) count += 1 #while notifier.check_events(): # notifier.read_events() # notifier.process_events() #notifier.loop() print "Waiting for %d/%d events" % (count, len(watches)) sys.stdin.read() if __name__ == '__main__': main()