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抓取淘宝下拉框关键词的方法
Jul 08 Python
学习python之编写简单简单连接数据库并执行查询操作
Feb 27 Python
教你用一行Python代码实现并行任务(附代码)
Feb 02 Python
python 将字符串转换成字典dict的各种方式总结
Mar 23 Python
对python调用RPC接口的实例详解
Jan 03 Python
Python实现京东秒杀功能代码
May 16 Python
Python之虚拟环境virtualenv,pipreqs生成项目依赖第三方包的方法
Jul 23 Python
PyCharm专业最新版2019.1安装步骤(含激活码)
Oct 09 Python
Flask中endpoint的理解(小结)
Dec 11 Python
解决jupyter notebook 前面书写后面内容消失的问题
Apr 13 Python
Python如何实现FTP功能
May 28 Python
字典算法实现及操作 --python(实用)
Mar 31 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
phpMyAdmin 链接表的附加功能尚未激活的问题
2010/08/01 PHP
PHP远程采集图片详细教程
2014/07/01 PHP
php中使用gd库实现下载网页中所有图片
2015/05/12 PHP
php学习笔记之mb_strstr的基本使用
2018/02/03 PHP
PHP实现打包zip并下载功能
2018/06/12 PHP
PHP中遍历数组的三种常用方法实例分析
2019/06/24 PHP
PHP函数用法详解【初始化、嵌套、内置函数等】
2020/06/02 PHP
PHP7 字符串处理机制修改
2021/03/09 PHP
提高代码性能技巧谈—以创建千行表格为例
2006/07/01 Javascript
学习ExtJS 访问容器对象
2009/10/07 Javascript
今天是星期几的4种JS代码写法
2013/09/17 Javascript
JavaScript调用后台的三种方法实例
2013/10/17 Javascript
JavaScript的arguments对象应用示例
2014/09/15 Javascript
JavaScript+html5 canvas绘制缤纷多彩的三角形效果完整实例
2016/01/26 Javascript
动态加载JavaScript文件的两种方法
2016/04/22 Javascript
Javascript 跨域知识详细介绍
2016/10/30 Javascript
输入框点击时边框变色效果的实现方法
2016/12/26 Javascript
bootstrap table表格插件使用详解
2017/05/08 Javascript
JavaScript高阶函数_动力节点Java学院整理
2017/06/28 Javascript
微信小程序 如何引入外部字体库iconfont的图标
2018/01/31 Javascript
详解redis在nodejs中的应用
2018/05/02 NodeJs
JavaScript实现更换背景图片
2019/10/18 Javascript
javascript实现简单打字游戏
2019/10/29 Javascript
微信小程序实现Swiper轮播图效果
2019/11/22 Javascript
vue+Element中table表格实现可编辑(select下拉框)
2020/05/21 Javascript
在vue项目中 实现定义全局变量 全局函数操作
2020/10/26 Javascript
原生JS运动实现轮播图
2021/01/02 Javascript
Python中bisect的用法
2014/09/23 Python
Python读取配置文件(config.ini)以及写入配置文件
2020/04/08 Python
用于ETL的Python数据转换工具详解
2020/07/21 Python
秋季运动会广播稿大全
2014/02/17 职场文书
终止劳动合同协议书
2014/04/14 职场文书
五年级学生评语大全
2014/12/26 职场文书
论文答谢词
2015/01/20 职场文书
中考百日冲刺决心书
2015/09/22 职场文书
搞笑欢迎词大全
2015/09/30 职场文书