為了滿足這樣的需求:記錄文件變化、記錄用戶對文件的讀寫,甚至記錄系統調用,文件變化通知。
本文介紹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來記錄更多信息。
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 事件發生的時候阻塞,使之便於腳本應用。
簡單的示例:
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.
3.
import pyinotify
4.
import os
5.
import sys
6.
7.
class WatchLogProceesing(pyinotify.ProcessEvent):
8.
def process_IN_CLOSE_WRITE(self, event):
9.
print "processing %s" % (event.pathname)
10.
11.
def monitor_dir(directory):
12.
wmn = pyinotify.WatchManager()
13.
notifier = pyinotify.Notifier(wmn, WatchLogProceesing())
14.
wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)
15.
notifier.loop()
16.
17.
monitor_dir("/home/ottocho")
pyinotify
pyinotify是更出名功能更全面的庫。以下是一個超級簡單的示例。更多詳情:http://pyinotify.sourceforge.net/
1.
#!/usr/bin/env python
2.
3.
import pyinotify
4.
import os
5.
import sys
6.
7.
class WatchLogProceesing(pyinotify.ProcessEvent):
8.
def process_IN_CLOSE_WRITE(self, event):
9.
print "processing %s" % (event.pathname)
10.
11.
def monitor_dir(directory):
12.
wmn = pyinotify.WatchManager()
13.
notifier = pyinotify.Notifier(wmn, WatchLogProceesing())
14.
wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)
15.
notifier.loop()
16.
17.
monitor_dir("/home/ottocho")