實現inotify
內核支持
內核是否支持inotify
Linux支持inotify的內核最小版本為 2.6.13,參看man 7 inotify
#列出下面的文件,說明服務器內核支持inotify
[root@centos8 ~]#ls -l /proc/sys/fs/inotify
-rw-r--r-- 1 root root 0 Dec 7 10:10 max_queued_events
-rw-r--r-- 1 root root 0 Dec 7 10:10 max_user_instances
-rw-r--r-- 1 root root 0 Dec 6 05:54 max_user_watches
[root@centos8 ~]#cat /proc/sys/fs/inotify/max_queued_events
16384
[root@centos8 ~]#cat /proc/sys/fs/inotify/max_user_instances
128
[root@centos8 ~]#cat /proc/sys/fs/inotify/max_user_watches
8192
inotify內核參數說明:
- max_queued_events:inotify 事件隊列最大長度,如值太小會出現 Event Queue Overflow 錯誤,默認值:16384
- max_user_instances:每個用戶創建inotify實例最大值,默認值:128
- max_user_watches:可以監視的文件數量(單進程),默認值:8192
范例:
[root@data-centos8 ~]#vim /etc/sysctl.conf
fs.inotify.max_queued_events=66666
fs.inotify.max_user_watches=100000
[root@centos8 ~]#sysctl -p
fs.inotify.max_queued_events = 66666
fs.inotify.max_user_watches = 100000
[root@centos8 ~]#cat /proc/sys/fs/inotify/*
66666
128
100000
inotify-tools工具
inotify-tools參考文檔:https://github.com/rvoicilas/inotify-tools/wiki
安裝inotify-tools:基于epel源
[root@data-centos8 ~]# yum -y install inotify-tools
inotify-tools包主要工具:
- inotifywait: 在被監控的文件或目錄上等待特定文件系統事件(open ,close,delete等)發生,常用于實時同步的目錄監控
- inotifywatch:收集被監控的文件系統使用的統計數據,指文件系統事件發生的次數統計
inotifywait 命令常見選項
- -m, –monitor 始終保持事件監聽
- -d, –daemon 以守護進程方式執行,和-m相似,配合-o使用
- -r, –recursive 遞歸監控目錄數據信息變化
- -q, –quiet 輸出少量事件信息
- –exclude
指定排除文件或目錄,使用擴展的正則表達式匹配的模式實現 - –excludei
和exclude相似,不區分大小寫 - -o, –outfile
打印事件到文件中,相當于標準正確輸出,注意:使用絕對路徑 - -s, –syslogOutput 發送錯誤到syslog相當于標準錯誤輸出
- –timefmt
指定時間輸出格式 - –format
指定的輸出格式;即實際監控輸出內容 - -e 指定監聽指定的事件,如果省略,表示所有事件都進行監聽
inotifywait 的–timefmt
參考 man 3 strftime
- %Y 年份信息,包含世紀信息
- %y 年份信息,不包括世紀信息
- %m 顯示月份,范圍 01-12
- %d 每月的第幾天,范圍是 01-31
- %H 小時信息,使用 24小時制,范圍 00-23
- %M 分鐘,范圍 00-59
- %S 秒,范例 0-60
范例:
--timefmt "%Y-%m-%d %H:%M:%S"
inotifywait 的 –format
- %T 輸出時間格式中定義的時間格式信息,通過 –timefmt option 語法格式指定時間信息
- %w 事件出現時,監控文件或目錄的名稱信息,相當于dirname
- %f 事件出現時,將顯示監控目錄下觸發事件的文件或目錄信息,否則為空,相當于basename
- %e 顯示發生的事件信息,不同的事件默認用逗號分隔
- %Xe顯示發生的事件信息,不同的事件指定用X進行分隔
范例:
--format "%T %w%f event: %;e"
--format '%T %w %f'
inotifywait -e 選項指定的事件類型
- create 文件或目錄創建
- delete 文件或目錄被刪除
- modify 文件或目錄內容被寫入
- attrib 文件或目錄屬性改變
- close_write 文件或目錄關閉,在寫入模式打開之后關閉的
- close_nowrite 文件或目錄關閉,在只讀模式打開之后關閉的
- close 文件或目錄關閉,不管讀或是寫模式
- open 文件或目錄被打開
- moved_to 文件或目錄被移動到監控的目錄中
- moved_from 文件或目錄從監控的目錄中被移動
- move 文件或目錄不管移動到或是移出監控目錄都觸發事件
- access 文件或目錄內容被讀取
- delete_self 文件或目錄被刪除,目錄本身被刪除
- unmount 取消掛載
范例:
-e create,delete,moved_to,close_write, attrib
范例:使用inotifywait
#監控一次性事件
inotifywait /data/www
Setting up watches.
Watches established.
/data/www/ CREATE f1.txt
#持續前臺監控
inotifywait -mrq /data/www --exclude=".*\.swp"
/data/www/ OPEN f1.txt
/data/www/ ACCESS f1.txt
/data/www/ CLOSE_NOWRITE,CLOSE f1.txt
#持續后臺監控,并記錄日志
inotifywait -o /root/inotify.log -drq /data/www --timefmt "%Y-%m-%d %H:%M:%S" --format "%T %w%f event: %e"
#持續前臺監控特定事件
inotifywait -mrq /data/www --timefmt "%F %H:%M:%S" --format "%T %w%f event: %;e" -e create,delete,moved_to,close_write,attrib
本文鏈接:http://www.thecarconnectin.com/36019.html
網友評論comments