使用inotify實現linux系統下的目錄變化監控


近期,有個項目須要及時刪除Nginx服務生成的緩存文件,因為不是非常了解Nginx緩存生成的策略,在網上也沒有細致找,經過大家討論,終於希望引入liunx的inotify功能,監控某個liunx文件夾下的各種事件(create,delete,access等等). 
想了解inotify的朋友,請參考下面兩篇博文: 
1.http://www.ibm.com/developerworks/cn/linux/l-inotify.html使用 inotify 監控 Linux 文件系統事件 
2.http://www.ibm.com/developerworks/cn/linux/l-inotifynew/index.htmlinotify -- Linux 2.6 內核中的文件系統變化通知機制 

假設看完兩篇博文,你的想法是用C語言立即寫一個監控文件的程序(我當初也這么想的),先別忙,看看下面的文章,立即向您介紹一下inotify-tools這個工具包,眼下最新版是3.3版本號,這個工具包差點兒包括了文件夾和文件的監控點,也就是說,不用動手寫C代碼,已經有前人幫我寫好了,我們能夠直接通過bash腳本的調用完成這個功能. 

1、先查看linux的內核是否支持inotify,支持inotify的內核最小為2.6.13,輸入命令:uname –a。例如以下圖所看到的,內核為2.6.27,應該支持inotify.假設不支持,我建議你選擇一個高級別的linux內核.否則應該會有非常多麻煩. 

2、還能夠通過例如以下命令查看系統是否支持inotify:ll /proc/sys/fs/inotify 
假設有例如以下輸出,表示系統內核已經支持inotify: 
total 0 
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_queued_events 
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_instances 
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_watches 

3.inotify-tools的下載和安裝 
下載地址:[url]http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz?modtime=1199213676&big_mirror=0 
[/url] 
安裝過程:略. 

4.內部命令介紹 
系統下運行命令:man inotify、 man inotifywait、 man inotifywatch就可以得到對應的幫助信息,表示inotify成功安裝。 

man inotify: 
  捕獲文件系統的各種狀態事件 
 

Java代碼   收藏代碼
  1.  inotify events  
  2.   
  3.   Bit                Description  
  4. IN_ACCESS          File was accessed (read) (*)  
  5. IN_ATTRIB          Metadata changed (permissions, timestamps,  
  6.                    extended attributes, etc.) (*)  
  7. IN_CLOSE_WRITE     File opened for writing was closed (*)  
  8. IN_CLOSE_NOWRITE   File not opened for writing was closed (*)  
  9. IN_CREATE          File/directory created in watched directory (*)  
  10. IN_DELETE          File/directory deleted from watched directory (*)  
  11. IN_DELETE_SELF     Watched file/directory was itself deleted  
  12. IN_MODIFY          File was modified (*)  
  13. IN_MOVE_SELF       Watched file/directory was itself moved  
  14. IN_MOVED_FROM      File moved out of watched directory (*)  
  15. IN_MOVED_TO        File moved into watched directory (*)  
  16. IN_OPEN            File was opened (*)  
  17.    



man inotifywait: 
  等待並監控某個文件夾或文件的狀態改變,能夠適時的通過liunx腳本等待並監控文件改變的事件,能夠在事件發生時退出腳本,也能夠在事件發生時輸出一些信息. 

參數解釋: 

--fromfile <file>  僅僅監控文件夾下文件狀態的變化 
-m, --monitor      當事件發生后直接運行退出,-m 參數將不退出當前的shell腳本. 
-r, --recursive    遞歸監控當前文件夾下的全部文件和文件夾.(默認的文件和文件夾數最大是 8192個;假設不滿足能夠改動/proc/sys/fs/inotify/max_user_watches 
--exclude <pattern>  通過正則匹配文件名稱,大寫和小寫敏感. 
--excludei <pattern>  通過正則匹配文件名稱,大寫和小寫不敏感. 
-t <seconds>       事件發生時的秒數. 
  -e <event>        監聽那些事件的發生 
--timefmt option   指定輸出的時間格式 
--format <fmt>     輸出指定時間格式. 
     %w 監控事件發生時的文件名稱或文件路徑 
     %f 監控文件夾內部事件發生時文件名稱稱 
     %e 監控指定的事件發生 
     %T 輸出事件發生時的時間,--timefmt option指定格式 

inotifywatch: 
  使用linux的inotify特性監控某段時間內的文件狀態,並輸出摘要報表. 
例子:輸出beagle文件夾下60秒內的訪問和改動事件觸發報表 

Java代碼   收藏代碼
  1. % inotifywatch -v -e access -e modify -t 60 -r ~/.beagle  
  2.        Establishing watches...  
  3.        Setting up watch(es) on /home/rohan/.beagle  
  4.        OK, /home/rohan/.beagle is now being watched.  
  5.        Total of 302 watches.  
  6.        Finished establishing watches, now collecting statistics.  
  7.        Will listen for events for 60 seconds.  
  8.        total  access  modify  filename  
  9.        1436   1074    362     /home/rohan/.beagle/Indexes/FileSystemIndex/PrimaryIndex/  
  10.        1323   1053    270     /home/rohan/.beagle/Indexes/FileSystemIndex/SecondaryIndex/  
  11.        303    116     187     /home/rohan/.beagle/Indexes/KMailIndex/PrimaryIndex/  
  12.        261    74      187     /home/rohan/.beagle/TextCache/  
  13.        206    0       206     /home/rohan/.beagle/Log/  
  14.        42     0       42      /home/rohan/.beagle/Indexes/FileSystemIndex/Locks/  
  15.        18     6       12      /home/rohan/.beagle/Indexes/FileSystemIndex/  
  16.        12     0       12      /home/rohan/.beagle/Indexes/KMailIndex/Locks/  
  17.        3      0       3       /home/rohan/.beagle/TextCache/54/  
  18.        3      0       3       /home/rohan/.beagle/TextCache/bc/  
  19.        3      0       3       /home/rohan/.beagle/TextCache/20/  
  20.        3      0       3       /home/rohan/.beagle/TextCache/62/  
  21.        2      2       0       /home/rohan/.beagle/Indexes/KMailIndex/SecondaryIndex/  



編寫自己的監控腳本: 
需求:因為使用Nginx的反向代理,生成本地緩存的策略,所以須要監控某個文件夾的新增或刪除的變化,並將變化的文件名稱稱輸出到一個LOG中,帶后續文件有改動時,能夠通過該log定位文件地址,並刪除該文件,及時向前端反映文件變更后的變化. 
腳本; inodify_cache_list.sh 

Java代碼   收藏代碼
    1. #!/bin/sh    
    2. # A slightly complex but actually useful example    
    3. logfile="/opt/data/cache_list.txt"  
    4. temp_logfile="/opt/data/cache_tempfile.txt"   
    5.   
    6. /usr/local/bin/inotifywait -mrq  --format '%w%f' -e moved_to /opt/data/proxy_cache_dir/| while read  file;   
    7. do    
    8.         echo "/usr/bin/printf \"delete "`grep -a 'KEY:' ${file}| sed -e s/KEY://g;`"\\r\\n\" | nc 127.0.0.1 11211,rm -f "${file} |tee -a $logfile | tee -a $temp_logfile   
    9. done   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM