inotifywait監控多個目錄
inotifywait監控多個目錄
一.需求:
1.監控特定的服務配置檔案和目錄變更情況。
2.監控自定義檔案和目錄變更情況。
3.可以手動殺掉程序。
4.把所有變更資訊弄到日誌裡。
二.Inotify介紹
Inotify 是一個核心用於通知使用者空間程式檔案系統變化的機制,是基於inode級別的檔案系統監控技術,是一種強大的、細粒度的、非同步的機制,它滿足各種各樣的檔案監控需要,不僅限於安全和效能,核心要求2.6.13以上。
2.1 Inotify監控事項
Events 含義
access 檔案或目錄被讀取
modify 檔案或目錄內容被修改
attrib 檔案或目錄屬性被改變
close 檔案或目錄封閉,無論讀/寫模式
open 檔案或目錄被開啟
moved_to 檔案或目錄被移動至另外一個目錄
moved_from 檔案或目錄被移動到另一個目錄或從另一個目錄移動至當前目錄
create 檔案或目錄被建立在當前目錄
delete 檔案或目錄被刪除
unmount 檔案系統被解除安裝
2.2 Inotify引數說明
引數 說明
-m 持續監聽
-r 使用遞迴形式監視目錄
-q 減少冗餘資訊,只打印出需要的資訊
-e 指定要監視的事件,多個時間使用逗號隔開
--timefmt 時間格式
--format 監聽到的檔案變化的資訊
ymd分別表示年月日,H表示小時,M表示分鐘
--format 說明:
引數 說明
%w 表示發生事件的目錄
%f 表示發生事件的檔案
%e 表示發生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定義的時間格式
三.指令碼如下:
#!/bin/bash #監控檔案和資料夾變更 A=y B=`grep INOTIFY_USER /boot/config-$(uname -r)` C=`echo ${B:0-1}` system=`getconf LONG_BIT` #定義安裝inotify function Install_inotify() { if [ "$A" = "$C" ] then echo -e "\033[31mstart install INOTIFY-3.14...... \033[0m" cd /opt tar zxvf inotify-tools-3.14.tar.gz &> /dev/null cd /opt/inotify-tools-3.14 ./configure && make && make install &> /dev/null echo -e "\033[31mINOTIFY-3.14 install successful! \033[0m" if [ "$system" = 64 ] then echo -e "\033[31minstall 64-bit system patch......\033[0m" ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0 else echo -e "\033[31minstall 32-bit system patch......\033[0m" ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0 fi exit else echo -e "\033[31mSystem not supported INOTIFY...... \033[0m" exit fi } #定義tomcat監控目錄 function Inotify_tomcat () { #查詢tomcat配置檔案 echo -e "\033[31m正在搜尋tomcat配置檔案目錄,正在列印.....\033[0m" echo "`find / -name "server.xml"`" read -p "Please insert tomcat configuration file": File echo -e "\033[31mtomcat configuration file is $File \033[0m" read -p "Please insert tomcat Application web directory": Directory echo -e "\033[31mtomcat Application web directory is $Directory \033[0m" touch /opt/Inotify_tomcat.log echo -n "" > /opt/Inotify_tomcat.log /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib,,moved_from,moved_to,unmount $File $Directory >> /opt/Inotify_tomcat.log & echo -e "\033[31mlook /opt/Inotify_tomcat.log information \033[0m" exit } #自定義目錄和檔案監控 function Inotify_custom () { echo -e "\033[31m編輯自定義檔案和目錄監控配置檔案/opt/mon.txt: \033[0m" touch /opt/Inotify_custom.log echo -n "" > /opt/Inotify_custom.log for line in $(cat /opt/mon.txt) do echo $line /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify,delete,create,attrib,,moved_from,moved_to,unmount $line >> /opt/Inotify_custom.log & done exit } #定義關閉監控程序 function Inotify_kill () { echo -e "\033[31mMonitoring is turned off \033[0m" pid=`ps -ef | grep inotifywait | grep -v grep | awk '{print $2}'` if [ -n "$pid" ] then echo -e "\033[31mkill inotifywait pid : \033[0m"$pid kill -9 $pid echo -e "\033[31minotifywait turned off successful \033[0m" exit else echo -e "\033[31mMonitoring does not exist \033[0m" exit fi } #PS3一般為選單提示資訊# PS3="Please Select Menu": #select為選單選擇命令,格式為select $var in ..command.. do .... done select i in "Install_inotify" "Inotify_tomcat" "Inotify_custom" "Inotify_kill" do #case 方式,一般用於多種條件下的判斷 case $i in Install_inotify ) Install_inotify ;; Inotify_tomcat ) Inotify_tomcat ;; Inotify_custom ) Inotify_custom ;; Inotify_kill ) Inotify_kill ;; *) echo echo "Please Insert $0: Install_inotify(1)|Inotify_tomcat(2) |Inotify_custom(3) |Inotify_kill(4)" break ;; esac done
四.使用說明:
一、功能介紹
1.檔案變更刪除,建立,移動,修改,解除安裝,許可權改動實時監控
2.資料夾變更刪除,建立,移動,修改,解除安裝,許可權改動實時監控
二、使用選項說明
1.安裝服務
2.tomcat監控 #配置檔案和web應用目錄需要提供
如:
找到tomcat配置檔案並輸入進去 Please insert tomcat configurationfile: /opt/apache-tomcat-7.0.67/conf/server.xml 找到tomcat-web應用目錄並輸入進去 Please insert tomcat Application web directory: /opt/apache-tomcat-7.0.67/webapps/vaiops 相應的監控日誌在: /opt/Inotify_tomcat.log
3.自定義監控 #需要手動編輯/opt/mon.txt檔案,把需要監控的目錄和檔案填入。
如:
vim /opt/mon.txt
/opt/mon.txt
/etc
/opt
4.殺死監控程序
如:
Monitoring is turned off
kill inotifywait pid : 4538 4539 4540
inotifywait turned off successful
三、指令碼執行引數說明
例子:
執行sh intify.sh
1) Install_inotify
2) Inotify_tomcat
3) Inotify_custom
4) Inotify_kill
###為了帶孩子,最近都沒時間發文章了,感覺天天覺都不夠睡,今天有點空閒發一篇指令碼記錄下。