Python log Monitoring System processing log (Pyinotify)

Source: Internet
Author: User

Objective

Recently the project encountered a Python package for monitoring log files pyinotify, combined with their own project experience and some information on the web to summarize, the overall principle is to use the Pyinotify Module Monitoring log folder, the arrival of the logs, trigger the corresponding function to process, The process of deleting log files after processing is finished, the following focuses on the following pyinotify

Pyinotify

Pyinotify is a python module that monitors changes to file systems. Pyinotify relies on the functionality of the Linux kernel-inotify (kernel 2.6.13 merging). INotify is an event-driven notifier that notifies the interface through three system calls from kernel space to user space. Pyinotify combines these system calls and provides a top-level abstraction and a common way to handle these features.

    • Pyinotify said hundred is by invoking the system's inotify to realize the notification
    • INotify can either monitor files or monitor directories
    • Inotify uses system calls rather than SIGIO to notify file system events.
Inotify file system events that can be monitored include:
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 is created in watched directory.
In_delete Yes File/dir is deleted in watched directory.
In_delete_self Yes Self-deletion, that is, an executable file deletes itself when executed
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's 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 Self-moving, that is, an executable file moves itself at execution time
In_moved_from Yes File/dir in a watched dir is moved from X. Can trace the full move of a item when in_moved_to are available too, in the If the MOVED item is itself watched, it s path would be updated (see IN_MOVE_SELF).
In_moved_to Yes File/dir is 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 The host file system is Umount
In_access, that is, the file is accessed In_modify, the file is Writein_attrib, the file attributes are modified, such as chmod, Chown, Touch and other in_close_write, the writable file is Closein_close_ Nowrite, non-writable files are closein_open, files are openin_moved_from, files are moved, such as mvin_moved_to, files are moved, such as MV, Cpin_create, create new file In_delete, Files are deleted, such as rmin_delete_self, self-deletion, that is, an executable file in the execution of the deletion of its own in_move_self, self-moving, that is, an executable file in the execution of the mobile In_unmount, the host file system is Umountin_close, File is closed, equivalent to (In_close_write | In_close_nowrite) In_move, file is moved, equivalent to (In_moved_from | IN_MOVED_TO)
Pyinotify Use Example
#!/usr/bin/python# coding:utf-8import osfrom pyinotify import WatchManager, Notifier,processevent,in_delete, In_create,in_modify class EventHandler (processevent): "" "Event Handling" "" 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 M Onitor%s '% (path) while True:try:notifier.process_events () if Notifier.check_events (): Notifier.read_ev Ents () except KeyboardInterrupt:notifier.stop () break if __name__ = = "__main__": Fsmonitor ('/root/softpython/a Pk_url ') 

Python log Monitoring System processing log (Pyinotify)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.