[python]文件系统监控
来源:互联网 发布:java可变参数传入数组 编辑:程序博客网 时间:2024/06/02 02:17
Pyinotify是一个Python模块,用来监测文件系统的变化。 Pyinotify依赖于Linux内核的功能—inotify(内核2.6.13合并)。 inotify的是一个事件驱动的通知器,其通知接口通过三个系统调用从内核空间到用户空间。pyinotify结合这些系统调用,并提供一个顶级的抽象和一个通用的方式来处理这些功能。
- pyinotify 说百了就是通过 调用系统的inotify来实现通知的
- inotify 既可以监视文件,也可以监视目录
- Inotify 使用系统调用而非 SIGIO 来通知文件系统事件。
Inotify 可以监视的文件系统事件包括:
Event NameIs an EventDescriptionIN_ACCESSYesfile was accessed.IN_ATTRIBYesmetadata changed.IN_CLOSE_NOWRITEYesunwrittable file was closed.IN_CLOSE_WRITEYeswrittable file was closed.IN_CREATEYesfile/dir was created in watched directory.IN_DELETEYesfile/dir was deleted in watched directory.IN_DELETE_SELFYes自删除,即一个可执行文件在执行时删除自己IN_DONT_FOLLOWNodon’t follow a symlink (lk 2.6.15).IN_IGNOREDYesraised on watched item removing. Probably useless for you, prefer instead IN_DELETE*.IN_ISDIRNoevent occurred against directory. It is always piggybacked to an event. The Event structure automatically provide this information (via .is_dir)IN_MASK_ADDNoto update a mask without overwriting the previous value (lk 2.6.14). Useful when updating a watch.IN_MODIFYYesfile was modified.IN_MOVE_SELFYes自移动,即一个可执行文件在执行时移动自己IN_MOVED_FROMYesfile/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_TOYesfile/dir was moved to Y in a watched dir (see IN_MOVE_FROM).IN_ONLYDIRNoonly watch the path if it is a directory (lk 2.6.15). Usable when calling .add_watch.IN_OPENYesfile was opened.IN_Q_OVERFLOWYesevent queued overflowed. This event doesn’t belongs to any particular watch.IN_UNMOUNTYes宿主文件系统被 umount通过pyinotify来实现对文件系统的监控非常简单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/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()
参考资料:
http://pyinotify.sourceforge.net/
- Python pyinotify文件系统监控
- [python]文件系统监控
- Python pyinotify文件系统监控
- python watchdog监控文件系统变动
- python watchdog:监控文件系统事件的Python库
- python watchdog:监控文件系统事件的Python库
- 文件系统监控
- 监控文件系统
- 关于linux文件系统监控
- fanotify 监控文件系统
- Java文件系统监控
- Pyinotify文件系统监控
- inotify 文件系统监控
- Pyinotify文件系统监控
- 文件系统实时监控
- fanotify 监控文件系统
- Pyinotify文件系统监控
- inotify - 监控文件系统
- 软件层的特征
- 数据库插入数据(包括Identity列)
- 解释一下SetItemData
- Applet与javacript通信
- miniupnpc的主要函数介绍
- [python]文件系统监控
- android下提取PPS,SPS
- shell 脚本实现的守护进程
- vi命令大全
- 浅析快速嵌入式MCU设计仿真性能
- Git Stash用法
- 【五一呈献】Java 全能高效 MVC & REST 开发框架 Portal-Basic v3.1.1 正式发布
- iOS 扫描wifi热点那些事儿
- 检查内存泄露(NSZombieEnabled,Analyze,Instruments的leaks)