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使用新浪微博api上传图片到微博示例
Jan 10 Python
python pcm音频添加头转成Wav格式文件的方法
Jan 09 Python
python使用KNN算法识别手写数字
Apr 25 Python
Python pandas用法最全整理
Aug 04 Python
如何为Python终端提供持久性历史记录
Sep 03 Python
如何在sublime编辑器中安装python
May 20 Python
Pytorch实现将模型的所有参数的梯度清0
Jun 24 Python
Linux安装Python3如何和系统自带的Python2并存
Jul 23 Python
一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃
Nov 02 Python
Python 虚拟环境工作原理解析
Dec 24 Python
利用python实现后端写网页(flask框架)
Feb 28 Python
图文详解matlab原始处理图像几何变换
Jul 09 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实现Ftp用户的在线管理的代码
2007/03/06 PHP
PHP截取汉字乱码问题解决方法mb_substr函数的应用
2008/03/30 PHP
php中的boolean(布尔)类型详解
2013/10/28 PHP
php插入排序法实现数组排序实例
2015/02/16 PHP
Yii实现Command任务处理的方法详解
2016/07/14 PHP
php语言注释,单行注释和多行注释
2018/01/21 PHP
在IE模态窗口中自由查看HTML源码的方法
2007/03/08 Javascript
js中的referrer返回上一页使用介绍
2013/09/26 Javascript
jQuery中bind与live的用法及区别小结
2014/01/27 Javascript
js 针对html DOM元素操作等经验累积
2014/03/11 Javascript
js时间日期格式化封装函数
2014/12/02 Javascript
Js控制滑轮左右滑动实例
2015/02/13 Javascript
JS+CSS实现分类动态选择及移动功能效果代码
2015/10/19 Javascript
JavaScript trim 实现去除字符串首尾指定字符的简单方法
2016/12/27 Javascript
Ajax跨域实现代码(后台jsp)
2017/01/21 Javascript
nodejs模块nodemailer基本使用-邮件发送示例(支持附件)
2017/03/28 NodeJs
node版本管理工具n包使用教程详解
2018/11/09 Javascript
JavaScript 面向对象基础简单示例
2019/10/02 Javascript
uni-app自定义导航栏按钮|uniapp仿微信顶部导航条功能
2019/11/12 Javascript
原生js实现二级联动菜单
2019/11/27 Javascript
对python requests的content和text方法的区别详解
2018/10/11 Python
Python修改文件往指定行插入内容的实例
2019/01/30 Python
使用Python实现画一个中国地图
2019/11/23 Python
CSS3中几个新增加的盒模型属性使用教程
2016/03/01 HTML / CSS
Spartoo芬兰:欧洲最大的网上鞋店
2016/08/28 全球购物
GE设备配件:GE Appliance Parts(家电零件、配件和滤水器)
2018/11/28 全球购物
手工制作的意大利皮革运动鞋:KOIO
2020/01/05 全球购物
全球性的众包图形设计市场:DesignCrowd
2021/02/02 全球购物
如何实现jdbc性能优化
2012/07/30 面试题
夜大毕业生自我评价分享
2013/11/10 职场文书
中学生自我评价范文
2014/02/08 职场文书
公立医院改革实施方案
2014/03/14 职场文书
植树造林的宣传标语
2014/06/23 职场文书
后进生评语大全
2015/01/04 职场文书
《英雄联盟》2022日蚀、月蚀皮肤演示 黑潮亚索曝光
2022/04/13 其他游戏
详解Go语言中Get/Post请求测试
2022/06/01 Golang