1. 程式人生 > >Python pyinotify檔案系統監控

Python pyinotify檔案系統監控

Pyinotify是一個Python模組,用來監測檔案系統的變化。 Pyinotify依賴於Linux核心的功能—inotify(核心2.6.13合併)。 inotify的是一個事件驅動的通知器,其通知介面通過三個系統呼叫從核心空間到使用者空間。pyinotify結合這些系統呼叫,並提供一個頂級的抽象和一個通用的方式來處理這些功能。

  • pyinotify 說百了就是通過 呼叫系統的inotify來實現通知的
  • inotify 既可以監視檔案,也可以監視目錄
  • Inotify 使用系統呼叫而非 SIGIO 來通知檔案系統事件。

Inotify 可以監視的檔案系統事件包括:

Event Name Is an Event
Description
IN_ACCESS Yes file was accessed.
IN_ATTRIB Yes metadata changed.
IN_CLOSE_NOWRITE Yes unwrittable file was closed.
IN_CLOSE_WRITE Yes writtable file was closed.
IN_CREATE Yes file/dir was created in watched directory.
IN_DELETE Yes file/dir was deleted in watched directory.
IN_DELETE_SELF Yes 自刪除,即一個可執行檔案在執行時刪除自己
IN_DONT_FOLLOW No don’t follow a symlink (lk 2.6.15).
IN_IGNORED Yes raised on watched item removing. Probably useless for you, prefer instead IN_DELETE*.
IN_ISDIR No event occurred against directory. It is always piggybacked to an event. The Event structure automatically provide this information (via .is_dir)
IN_MASK_ADD No to update a mask without overwriting the previous value (lk 2.6.14). Useful when updating a watch.
IN_MODIFY Yes file was modified.
IN_MOVE_SELF Yes 自移動,即一個可執行檔案在執行時移動自己
IN_MOVED_FROM Yes file/dir in a watched dir was moved from X. Can trace the full move of an item when IN_MOVED_TO is available too, in this case if the moved item is itself watched, its path will be updated (see IN_MOVE_SELF).
IN_MOVED_TO Yes file/dir was moved to Y in a watched dir (see IN_MOVE_FROM).
IN_ONLYDIR No only watch the path if it is a directory (lk 2.6.15). Usable when calling .add_watch.
IN_OPEN Yes file was opened.
IN_Q_OVERFLOW Yes event queued overflowed. This event doesn’t belongs to any particular watch.
IN_UNMOUNT Yes 宿主檔案系統被 umount

通過pyinotify來實現對檔案系統的監控非常簡單

#!/usr/bin/env python
# encoding:utf-8
 
import os
from  pyinotify import  WatchManager, Notifier, \
ProcessEvent,IN_DELETE, IN_CREATE,IN_MODIFY
 
class EventHandler(ProcessEvent):
    """事件處理"""
    def process_IN_CREATE(self, event):
        print   "Create file: %s "  %   os.path.join(event.path,event.name)
 
    def process_IN_DELETE(self, event):
        print   "Delete file: %s "  %   os.path.join(event.path,event.name)
 
    def process_IN_MODIFY(self, event):
            print   "Modify file: %s "  %   os.path.join(event.path,event.name)
 
def FSMonitor(path='.'):
        wm = WatchManager() 
        mask = IN_DELETE | IN_CREATE |IN_MODIFY
        notifier = Notifier(wm, EventHandler())
        wm.add_watch(path, mask,rec=True)
        print 'now starting monitor %s'%(path)
        while True:
                try:
                        notifier.process_events()
                        if notifier.check_events():
                                notifier.read_events()
                except KeyboardInterrupt:
                        notifier.stop()
                        break
 
if __name__ == "__main__":
    FSMonitor()