文件監控與通知機制 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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章