文件监控与通知机制 audit inotify

为了满足这样的需求:记录文件变化、记录用户对文件的读写,甚至记录系统调用,文件变化通知。


本文介绍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等)


日志查询

设置了监控后,会在/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 是文件系统事件监控机制,是细粒度的、异步的机制。
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事件发生的时候阻塞,使之便于脚本应用。
简单的示例:
#!/bin/bash
inotifywait -mrq --event create,delete,modify,move --format '%w %e' /path | while read w e; do
    if [ "$e" = "IGNORED" ]; then
        continue
    fi
    rsync -az --delete $w username@ip:$w
done


另外一个例子:

#!/bin/sh
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %f' \
    -e close_write /home/ottocho | while read date time file; 
do
    rsync /home/ottocho/${file} ottocho::backup 
    echo "Inof: ${date} ${time}, ${file} backed up"
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

以下是一个源自源代码的例子,非常简易,注释就不必了吧
#!/usr/bin/env python 

import pyinotify
import os
import sys

class WatchLogProceesing(pyinotify.ProcessEvent):
    def process_IN_CLOSE_WRITE(self, event):
        print "processing %s" % (event.pathname)
 
def monitor_dir(directory):
    wmn = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wmn, WatchLogProceesing())
    wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)
    notifier.loop()
        
monitor_dir("/home/ottocho")


pyinotify

pyinotify是更出名功能更全面的库。以下是一个超级简单的示例。更多详情:http://pyinotify.sourceforge.net/
#!/usr/bin/env python 

import pyinotify
import os
import sys

class WatchLogProceesing(pyinotify.ProcessEvent):
    def process_IN_CLOSE_WRITE(self, event):
        print "processing %s" % (event.pathname)
 
def monitor_dir(directory):
    wmn = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wmn, WatchLogProceesing())
    wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)
    notifier.loop()
        
monitor_dir("/home/ottocho")




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值