Python pyinotify日志監(jiān)控系統(tǒng)處理日志的方法
前言
最近項目中遇到一個用于監(jiān)控日志文件的Python包pyinotify,結(jié)合自己的項目經(jīng)驗(yàn)和網(wǎng)上的一些資料總結(jié)一下,總的原理是利用pyinotify模塊監(jiān)控日志文件夾,當(dāng)日志到來的情況下,觸發(fā)相應(yīng)的函數(shù)進(jìn)行處理,處理完畢后刪除日志文件的過程,下面就著重介紹下pyinotify
pyinotify
Pyinotify是一個Python模塊,用來監(jiān)測文件系統(tǒng)的變化。 Pyinotify依賴于Linux內(nèi)核的功能—inotify(內(nèi)核2.6.13合并)。 inotify的是一個事件驅(qū)動的通知器,其通知接口通過三個系統(tǒng)調(diào)用從內(nèi)核空間到用戶空間。pyinotify結(jié)合這些系統(tǒng)調(diào)用,并提供一個頂級的抽象和一個通用的方式來處理這些功能。
- pyinotify 說百了就是通過 調(diào)用系統(tǒng)的inotify來實(shí)現(xiàn)通知的
- inotify 既可以監(jiān)視文件,也可以監(jiān)視目錄
- Inotify 使用系統(tǒng)調(diào)用而非 SIGIO 來通知文件系統(tǒng)事件。
Inotify 可以監(jiān)視的文件系統(tǒng)事件包括:
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 | 自刪除,即一個可執(zhí)行文件在執(zhí)行時刪除自己 |
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 | 自移動,即一個可執(zhí)行文件在執(zhí)行時移動自己 |
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 | 宿主文件系統(tǒng)被 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,創(chuàng)建新文件
IN_DELETE,文件被刪除,如rm
IN_DELETE_SELF,自刪除,即一個可執(zhí)行文件在執(zhí)行時刪除自己
IN_MOVE_SELF,自移動,即一個可執(zhí)行文件在執(zhí)行時移動自己
IN_UNMOUNT,宿主文件系統(tǒng)被umount
IN_CLOSE,文件被關(guān)閉,等同于(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')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 如何使用Sentry 監(jiān)控你的Spring Boot應(yīng)用
- vue項目前端錯誤收集之sentry教程詳解
- python實(shí)時監(jiān)控logstash日志代碼
- 淺析springcloud 整合 zipkin-server 內(nèi)存日志監(jiān)控
- 使用python3調(diào)用wxpy模塊監(jiān)控linux日志并定時發(fā)送消息給群組或好友
- 關(guān)于Oracle Dataguard 日志傳輸狀態(tài)監(jiān)控問題
- PHP簡單實(shí)現(xiàn)定時監(jiān)控nginx日志文件功能示例
- Java實(shí)時監(jiān)控日志文件并輸出的方法詳解
- Sentry錯誤日志監(jiān)控使用方法解析
相關(guān)文章
Python利用PyQt5制作一個獲取網(wǎng)絡(luò)實(shí)時數(shù)據(jù)NBA數(shù)據(jù)播報GUI功能
現(xiàn)在NBA聯(lián)賽也進(jìn)行到半決賽了,我們怎么樣才能以更快的方法獲取NBA的數(shù)據(jù)呢?這里我們就自己來做一個數(shù)據(jù)播報的程序2021-07-07Python 使用 multiprocessing 模塊創(chuàng)建進(jìn)程池的操作方法
在現(xiàn)代計算任務(wù)中,尤其是處理大量數(shù)據(jù)或計算密集型任務(wù)時,使用并行處理可以顯著提升程序性能,Python的multiprocessing模塊提供了創(chuàng)建進(jìn)程池的功能,通過預(yù)先創(chuàng)建的進(jìn)程來并發(fā)執(zhí)行任務(wù),避免了頻繁的進(jìn)程創(chuàng)建和銷毀,感興趣的朋友一起看看吧2024-10-10使用Keras實(shí)現(xiàn)Tensor的相乘和相加代碼
這篇文章主要介紹了使用Keras實(shí)現(xiàn)Tensor的相乘和相加代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python常見加密模塊用法分析【MD5,sha,crypt模塊】
這篇文章主要介紹了Python常見加密模塊用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了MD5,sha與crypt模塊加密的相關(guān)實(shí)現(xiàn)方法與操作技巧,需要的朋友可以參考下2017-05-05python matplotlib 繪圖 和 dpi對應(yīng)關(guān)系詳解
這篇文章主要介紹了python matplotlib 繪圖 和 dpi對應(yīng)關(guān)系詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03PyQt5簡單讀取以及顯示圖片的應(yīng)用實(shí)例
我們在進(jìn)行圖像處理時,經(jīng)常會用到讀取圖片并顯示出來這樣的操作,下面這篇文章主要給大家介紹了關(guān)于PyQt5簡單讀取以及顯示圖片應(yīng)用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05分析Python中解析構(gòu)建數(shù)據(jù)知識
本篇文章給大家講述一下Python中解析構(gòu)建數(shù)據(jù)知識的相關(guān)內(nèi)容,有需要的朋友跟著學(xué)習(xí)下。2018-01-01