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开发之基于thread线程搜索本地文件的方法
Nov 11 Python
python实现可以断点续传和并发的ftp程序
Sep 13 Python
python 计算文件的md5值实例
Jan 13 Python
Python使用requests及BeautifulSoup构建爬虫实例代码
Jan 24 Python
Python设置在shell脚本中自动补全功能的方法
Jun 25 Python
Python字符串的全排列算法实例详解
Jan 07 Python
Django uwsgi Nginx 的生产环境部署详解
Feb 02 Python
详解Python使用Plotly绘图工具,绘制甘特图
Apr 02 Python
Python提取PDF内容的方法(文本、图像、线条等)
Sep 25 Python
python不同系统中打开方法
Jun 23 Python
python的数学算法函数及公式用法
Nov 18 Python
python学习之panda数据分析核心支持库
May 07 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
杏林同学录(八)
2006/10/09 PHP
PHP 循环列出目录内容的函数代码
2010/05/26 PHP
php生成Android客户端扫描可登录的二维码
2016/05/13 PHP
浅谈php数组array_change_key_case() 函数和array_chunk()函数
2016/10/22 PHP
PHP实现小程序批量通知推送
2018/11/27 PHP
PHP中数组转换为SimpleXML教程
2019/01/27 PHP
PHP生成随机密码4种方法及性能对比
2020/12/11 PHP
Javascript select控件操作大全(新增、修改、删除、选中、清空、判断存在等)
2008/12/19 Javascript
js关闭子窗体刷新父窗体实现方法
2012/12/04 Javascript
JS在textarea光标处插入文本的小例子
2013/03/22 Javascript
第十章之巨幕页头缩略图与警告框组件
2016/04/25 Javascript
js/jquery控制页面动态加载数据 滑动滚动条自动加载事件的方法
2017/02/08 Javascript
微信小程序列表渲染功能之列表下拉刷新及上拉加载的实现方法分析
2017/11/27 Javascript
JavaScript 中定义函数用 var foo = function () {} 和 function foo()区别介绍
2018/03/01 Javascript
apicloud拉起小程序并传递参数的方法示例
2018/11/21 Javascript
vue实现文件上传读取及下载功能
2020/11/17 Javascript
python翻译软件实现代码(使用google api完成)
2013/11/26 Python
Python中针对函数处理的特殊方法
2014/03/06 Python
提升Python程序运行效率的6个方法
2015/03/31 Python
python复制与引用用法分析
2015/04/08 Python
Python在Windows和在Linux下调用动态链接库的教程
2015/08/18 Python
Python 列表(List) 的三种遍历方法实例 详解
2017/04/15 Python
python pygame模块编写飞机大战
2018/11/20 Python
使用Python和Scribus创建一个RGB立方体的方法
2019/07/17 Python
python 字符串追加实例
2019/07/20 Python
OpenCV里的imshow()和Matplotlib.pyplot的imshow()的实现
2019/11/25 Python
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
2019/12/02 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
2020/03/25 Python
详解Python GUI编程之PyQt5入门到实战
2020/12/10 Python
大学生毕业求职简历的自我评价
2013/10/24 职场文书
解除劳动合同协议书
2014/04/14 职场文书
文员转正自我鉴定怎么写
2014/09/29 职场文书
大学生违纪检讨书300字
2014/10/25 职场文书
护士医德医风心得体会
2016/01/25 职场文书
[有人@你]你有一封绿色倡议书,请查收!
2019/07/18 职场文书
CSS font-variation 可变字体的魅力(实例详解)
2022/03/03 HTML / CSS