Linux下使用inotify-tools工具監控文件

如果想在Linux上監控文件系統的變化,如訪問屬性、讀寫屬性、權限屬性、刪除創建、移動等操作。可以考慮使用inotify-tools 工具,inotify-tools 是一個C庫和一組命令行的工作提供Linux下inotify的簡單接口。下面對inotify-tools講解。

inotify-tools

inotify是一個API,需要通過開發應用程序進行調用,對於大多數用戶來講這有着許多不便,inotify-tools的出現彌補了這一不足。inotify-tools是一套組件,它包括一個C庫和幾個命令行工具,這些命令行工具可用於通過命令行或腳本對某文件系統的事件進行監控

inotifywait命令 可以用來收集有關文件訪問信息,Linux發行版一般沒有包括這個命令,需要安裝inotify-tools,這個命令還需要將inotify支持編譯入Linux內核,好在大多數Linux發行版都在內核中啓用了inotify。

inotifywatch命令 用於收集關於被監視的文件系統的統計數據,包括每個 inotify 事件發生多少次。

開始之前需要檢測系統內核是否支持inotify,使用命令如下uname -a

在這裏插入圖片描述用uname -a命令檢查Linux內核,如果低於2.6.13,就需要重新編譯內核加入inotify的支持。使用ll /proc/sys/fs/inotify命令,是否有以下三條信息輸出,如果沒有表示不支持。

在這裏插入圖片描述

如果想要在Linux發行版上,查看是否安裝inotify-tools,使用命令如下。

rpm -qa inotify-tools

如果沒有安裝,可以使用yum install inotify-tools -y

yum -y install inotify-tools

在這裏插入圖片描述
看到到上面的信息,說明安裝成功。安裝成功後notify-tools提供的兩個命令行工具:

inotifywait:通過inotify API等待被監控文件上的相應事件並返回監控結果,默認情況下,正常的結果返回至標準輸出,診斷類的信息則返回至標準錯誤輸出。它可以在監控到對應監控對象上指定的事件後退出,也可以進行持續性的監控。
inotifywatch:通過inotify API收集被監控文件或目錄的相關事件並輸出統計信息。

inotifywait使用介紹

inotifywait 僅執行阻塞,等待 inotify 事件,你可以使用它來監控任何一組文件和目錄,或監控整個目錄樹(目錄、子目錄、子目錄的子目錄等等),並且可以結合 shell 腳本,更好的使用 inotifywait。來看看inotifywait如何使用:

inotifywait [-hcmrq] [-e ] [-t ] [–format ] [–timefmt ] [ … ]

inotifywait -h

inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ … ]
Options:
-h|–help Show this help text.
@ Exclude the specified file from being watched.
–exclude
Exclude all events on files matching the
extended regular expression .
–excludei
Like --exclude but case insensitive.
-m|–monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|–daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|–recursive Watch directories recursively.
–fromfile
Read files to watch from or `-’ for stdin.
-o|–outfile
Print events to rather than stdout.
-s|–syslog Send errors to syslog rather than stderr.
-q|–quiet Print less (only print events).
-qq Print nothing (not even events).
–format Print using a specified printf-like format
string; read the man page for more details.
–timefmt strftime-compatible format string for use with
%T in --format string.
-c|–csv Print events in CSV format.
-t|–timeout
When listening for a single event, time out after
waiting for an event for seconds.
If is 0, inotifywait will never time out.
-e|–event [ -e|–event … ]
Listen for specific event(s). If omitted, all events are
listened for.
Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.
Events:
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in
writeable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted

實例

inotifywait 實時監控目錄

在命令行上運行inotifywait,等待“/home/minger/share/tencent/rand/”目錄中的任何文件被訪問。運行inotifywait後,如果改變/home/minger/share/tencent/rand/目錄下的文件大小,則會彈出:

在這裏插入圖片描述

實時對 /data/web 目錄進行監控

實現對 /data/web 目錄進行監控,監控文件刪除,修改,創建和權限相關事件,並且要求將監控信息寫入/var/log/web_watch.log。要求日誌條目要清晰明瞭,能突顯文件路徑、事件名和時間。

#!/bin/bash
inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format  '%T %w%f %e' --event delete,modify,create,attrib  /data/web | while read  date time file event
  do
      case $event in
          MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
                  echo $event'-'$file'-'$date'-'$time >> /var/log/web_watch.log
              ;;

          MOVED_FROM|MOVED_FROM,ISDIR|DELETE|DELETE,ISDIR)
                  echo $event'-'$file'-'$date'-'$time /var/log/web_watch.log
              ;;
      esac
  done

運行輸出:

在這裏插入圖片描述
從上面的信息來看,如果在/data/web目錄上刪除,修改,創建文件等操作,則會打印信息到/var/log/web_watch.log中。

在這裏插入圖片描述

總結

inotify-tools是爲linux下inotify文件監控工具提供的一套C的開發接口庫函數,同時還提供了一系列的命令行工具,這些工具可以用來監控文件系統的事件。 inotify-tools是用C編寫的,除了要求內核支持inotify外,不依賴於其他。inotify-tools提供兩種工具,一是inotifywait,它是用來監控文件或目錄的變化,二是inotifywatch,它是用來統計文件系統訪問的次數。更多的使用方式,請查看 man手冊。

在這裏插入圖片描述
歡迎關注公衆號【程序猿編碼】,添加本人微信號(17865354792),回覆:領取學習資料。或者回復:進入技術交流羣。網盤資料有如下:

在這裏插入圖片描述

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