什么是auditThe 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_fileauditctl -w /tmp -p e -k webserver_watch_tmp-w 监控文件路径 /etc/passwd,-p 监控文件筛选 r(读) w(写) x(执行) a(属性改变)-k 筛选字符串,用于查询监控日志auditctl -a exit,never -S mountauditctl -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不过完全找不到文档。我也觉得这个东西用得上的时候不多。除非.....
什么是inotifyinotify 是文件系统事件监控机制,是细粒度的、异步的机制。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/Inotifyinotifywait 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与inotifyinotify有两个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
importpyinotify
importos
importsys
classWatchLogProceesing(pyinotify.ProcessEvent):
defprocess_IN_CLOSE_WRITE(self, event):
print"processing %s"% (event.pathname)
defmonitor_dir(directory):
wmn = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wmn, WatchLogProceesing())
wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)
notifier.loop()
monitor_dir("/home/ottocho")
pyinotifypyinotify是更出名功能更全面的库。以下是一个超级简单的示例。
#!/usr/bin/env python
importpyinotify
importos
importsys
classWatchLogProceesing(pyinotify.ProcessEvent):
defprocess_IN_CLOSE_WRITE(self, event):
print"processing %s"% (event.pathname)
defmonitor_dir(directory):
wmn = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wmn, WatchLogProceesing())
wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)
notifier.loop()
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