文件监控与通知机制 audit inotify

什么是audit
The Linux Audit Subsystem is a system to Collect information regarding events occurring on the system(s)
Kernel events (syscall events)
User events (audit-enabled programs)
syslog会记录系统状态(硬件警告、软件的log), 但syslog属于应用层, log归咎与软件, 并不会记录所有动作. 于是audit来记录更多信息。

audit命令

auditctl audit系统管理工具,用来获取状态,增加删除监控规则。
ausearch 查询audit log工具
aureport 输出audit系统报告


auditctl示例

auditctl -w /etc/passwd -p war -k password_file
auditctl -w /tmp -p e -k webserver_watch_tmp
-w 监控文件路径 /etc/passwd,
-p 监控文件筛选 r(读) w(写) x(执行) a(属性改变)
-k 筛选字符串,用于查询监控日志
auditctl -a exit,never -S mount
auditctl -a entry,always -S all -F pid=1005
-S 监控系统调用
-F 给出更多监控条件(pid/path/egid/euid等)
auditctl -D 删除所有的rule



日志查询

设置了监控后,会在/var/log/audit/audit.log里出现日志。
可以用此命令查看日志:
ausearch -f /etc/passwd -x rm
-k  利用auditctl指定的key查询
-x  执行程序
# ausearch -ts today -k password-file
# ausearch -ts 3/12/07 -k password-file
-ts 指定时间后的log (start time)
-te 指定时间前的log (end time)


audit库

libaudit和libaudit-python
不过完全找不到文档。我也觉得这个东西用得上的时候不多。除非.....


什么是inotify
inotify 是文件系统事件监控机制,是细粒度的、异步的机制。
inotify is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, dnotify, which had similar goals.


原理和实现

http://www.ibm.com/developerworks/cn/linux/l-inotifynew/
http://en.wikipedia.org/wiki/Inotify


inotifywait in shell

此命令会在inotify事件发生的时候阻塞,使之便于脚本应用。
简单的示例

  1. #!/bin/bash  

  2. inotifywait -mrq --event create,delete,modify,move --format '%w %e' /path | while read w e; do  

  3.    if [ "$e" = "IGNORED" ]; then  

  4.        continue  

  5.    fi  

  6.    rsync -az --delete $w username@ip:$w  

  7. done  



另外一个例子:

  1. #!/bin/sh  

  2. inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %f' \  

  3.    -e close_write /home/ottocho | while read date time file;  

  4. do  

  5.    rsync /home/ottocho/${file} ottocho::backup  

  6.    echo "Inof: ${date} ${time}, ${file} backed up"  

  7. done  




inotify与rsync

很多人利用inotify和rsync实现实时同步文件,而基于inotify API和rsync command的sersync解决了这个问题。  
http://code.google.com/p/sersync/


python与inotify

inotify有两个python库,pyinotify和inofityx.区别在inotifyx的官网上作者如是说道:
inotifyx是C的拓展,没有使用ctypes,因此速度更快、在inotify的API变化时会更少出现问题。inotifyx是inotify更轻的封装。pyinotify更复杂,它提供了很多inotifyx没有的特性,可这些特性在大多数情况下未必需要。inotifyx提供的API是基本不变的简易的。
http://www.alittletooquiet.net/software/inotifyx/
http://pyinotify.sourceforge.net/


inotifyx

以下是一个源自源代码的例子,非常简易,注释就不必了吧

  1. #!/usr/bin/env python

  2. import pyinotify  

  3. import os  

  4. import sys  

  5. class WatchLogProceesing(pyinotify.ProcessEvent):  

  6. def process_IN_CLOSE_WRITE(self, event):  

  7. print"processing %s" % (event.pathname)  

  8. def monitor_dir(directory):  

  9.    wmn = pyinotify.WatchManager()  

  10.    notifier = pyinotify.Notifier(wmn, WatchLogProceesing())  

  11.    wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)  

  12.    notifier.loop()  

  13. monitor_dir("/home/ottocho")  



pyinotify

pyinotify是更出名功能更全面的库。以下是一个超级简单的示例。

  1. #!/usr/bin/env python

  2. import pyinotify  

  3. import os  

  4. import sys  

  5. class WatchLogProceesing(pyinotify.ProcessEvent):  

  6. def process_IN_CLOSE_WRITE(self, event):  

  7. print"processing %s" % (event.pathname)  

  8. def monitor_dir(directory):  

  9.    wmn = pyinotify.WatchManager()  

  10.    notifier = pyinotify.Notifier(wmn, WatchLogProceesing())  

  11.    wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)  

  12.    notifier.loop()  

  13. monitor_dir("/home/ottocho")  




附:

今天有一个文件总是被修改,为了找出此原因故用了此方法进行查看此文件是怎么被修改的

先设定被监控的文件

auditctl -w /usr/local/apache/htdocs/index.php war -k index_php

经过几分钟里面的内容被 修改

查看方法ausearch -ts today -k index_php

也可以把此文件输出到一个文件进行查看

ausearch -ts today -k index_php >> /tmp/logs/index_php


查看被监控的文件方法为 auditctl -l

删除所有 的文件监控 auditctl -D





你可能感兴趣的:(programs,occurring,regarding)