Python pyinotify日志监控系统处理日志的方法


Posted in Python onMarch 08, 2018

前言

最近项目中遇到一个用于监控日志文件的Python包pyinotify,结合自己的项目经验和网上的一些资料总结一下,总的原理是利用pyinotify模块监控日志文件夹,当日志到来的情况下,触发相应的函数进行处理,处理完毕后删除日志文件的过程,下面就着重介绍下pyinotify

pyinotify

Pyinotify是一个Python模块,用来监测文件系统的变化。 Pyinotify依赖于Linux内核的功能—inotify(内核2.6.13合并)。 inotify的是一个事件驱动的通知器,其通知接口通过三个系统调用从内核空间到用户空间。pyinotify结合这些系统调用,并提供一个顶级的抽象和一个通用的方式来处理这些功能。

  1. pyinotify 说百了就是通过 调用系统的inotify来实现通知的
  2. inotify 既可以监视文件,也可以监视目录
  3. 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

IN_ACCESS,即文件被访问

IN_MODIFY,文件被write

IN_ATTRIB,文件属性被修改,如chmod、chown、touch等

IN_CLOSE_WRITE,可写文件被close

IN_CLOSE_NOWRITE,不可写文件被close

IN_OPEN,文件被open

IN_MOVED_FROM,文件被移走,如mv

IN_MOVED_TO,文件被移来,如mv、cp

IN_CREATE,创建新文件

IN_DELETE,文件被删除,如rm

IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己

IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己

IN_UNMOUNT,宿主文件系统被umount

IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)

IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)

pyinotify使用例子

#!/usr/bin/python
# coding: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,auto_add=True,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('/root/softpython/apk_url')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
举例讲解Python中的Null模式与桥接模式编程
Feb 02 Python
Python中字符串的格式化方法小结
May 03 Python
python基础教程之Filter使用方法
Jan 17 Python
Python实现简单的多任务mysql转xml的方法
Feb 08 Python
python logging日志模块的详解
Oct 29 Python
教你用 Python 实现微信跳一跳(Mac+iOS版)
Jan 04 Python
在python中只选取列表中某一纵列的方法
Nov 28 Python
深入浅析Python科学计算库Scipy及安装步骤
Oct 12 Python
Python Celery多队列配置代码实例
Nov 22 Python
Python计算IV值的示例讲解
Feb 28 Python
关于python3.7安装matplotlib始终无法成功的问题的解决
Jul 28 Python
简单的命令查看安装的python版本号
Aug 28 Python
TensorFlow模型保存和提取的方法
Mar 08 #Python
火车票抢票python代码公开揭秘!
Mar 08 #Python
Python实现定时备份mysql数据库并把备份数据库邮件发送
Mar 08 #Python
python实现12306抢票及自动邮件发送提醒付款功能
Mar 08 #Python
TensorFlow模型保存/载入的两种方法
Mar 08 #Python
python2.7 json 转换日期的处理的示例
Mar 07 #Python
教你用Python创建微信聊天机器人
Mar 31 #Python
You might like
PHP与MySQL开发中页面乱码的产生与解决
2008/03/27 PHP
php && 逻辑与运算符使用说明
2010/03/04 PHP
PHP获取音频文件的相关信息
2015/06/22 PHP
深入解析PHP的Yii框架中的event事件机制
2016/03/17 PHP
用js 让图片在 div或dl里 居中,底部对齐
2008/01/21 Javascript
javascript 自定义事件初探
2009/08/21 Javascript
jquery 利用show和hidden实现级联菜单示例代码
2013/08/09 Javascript
js利用prototype调用Array的slice方法示例
2014/06/09 Javascript
PhotoShop给图片自动添加边框及EXIF信息的JS脚本
2015/02/15 Javascript
JS常用函数和常用技巧小结
2016/10/15 Javascript
jQuery中table数据的值拷贝和拆分
2017/03/19 Javascript
js中document.write和document.writeln的区别
2018/03/11 Javascript
基于Vue实现关键词实时搜索高亮显示关键词
2018/07/21 Javascript
详解基于Vue,Nginx的前后端不分离部署教程
2018/12/04 Javascript
使用JavaScript解析URL的方法示例
2019/03/01 Javascript
Vue3 中的数据侦测的实现
2019/10/09 Javascript
[29:10]Ti4 冒泡赛第二天 NEWBEE vs Titan 3
2014/07/15 DOTA
Python入门篇之函数
2014/10/20 Python
python中kmeans聚类实现代码
2018/02/23 Python
python实现微信自动回复功能
2018/04/11 Python
python检测文件夹变化,并拷贝有更新的文件到对应目录的方法
2018/10/17 Python
浅谈Django中view对数据库的调用方法
2019/07/18 Python
python实现邮件自动发送
2019/08/10 Python
python基于json文件实现的gearman任务自动重启代码实例
2019/08/13 Python
python通过实例讲解反射机制
2019/10/17 Python
浅析Django中关于session的使用
2019/12/30 Python
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
2020/02/25 Python
python线程池如何使用
2020/05/28 Python
浅谈Html5中视频 音频标签 进度条的问题
2016/07/26 HTML / CSS
业务内勤岗位职责
2014/04/30 职场文书
机电系毕业生求职信
2014/07/11 职场文书
关于成绩下滑的自我检讨书
2014/09/20 职场文书
教师素质教育心得体会
2016/01/19 职场文书
Redis如何一键部署脚本
2021/04/12 Redis
vue报错function () { [native code] },无法出现我们想要的内容 Unknown custom element
2022/04/11 Vue.js
详细介绍Next.js脚手架完整搭建封装
2022/04/26 Javascript