第二十三節 inotify事件監控工具
標簽(空格分隔): Linux實戰教學筆記-陳思齊
---本教學筆記是本人學習和工作生涯中的摘記整理而成,此為初稿(尚有諸多不完善之處),為原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章原始出處,作者信息和本聲明。否則將追究法律責任。http://www.cnblogs.com/chensiqiqi/
第1章,NFS存儲服務器與backup備份服務器的搭建。
詳細細節知識與搭建請關注:
http://www.cnblogs.com/chensiqiqi/p/6514315.html Rsync數據同步工具
http://www.cnblogs.com/chensiqiqi/p/6530859.html 企業級NFS網絡文件共享服務
http://www.cnblogs.com/chensiqiqi/p/6531003.html【Rsync項目實戰】備份全網服務器數據
第2章:rsync + inotify 組合的起源
Rsync(remote sync)遠程同步工具,通過rsync可以實現對遠程服務器數據的增量備份同步,但rsync自身也有瓶頸,同步數據時,rsync采用核心算法對遠程服務器的目標文件進行比對,只進行差異同步。我們可以想象一下,如果服務器的文件數量達到了百萬甚至千萬量級,那么文件對比將是非常耗時的。而且發生變化的往往是其中很少的一部分,這是非常低效的方式。inotify的出現,可以緩解rsync不足之處,取長補短。
第3章 inotify簡介
- Inotify是一種強大的,細粒度的,異步的文件系統事件監控機制(軟件),linux內核從2.6.13起,加入了Inotify支持,通過Inotify可以監控文件系統中添加,刪除,修改,移動等各種事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools正是實施這樣監控的軟件。還有國人周洋在金山公司開發的sersync。
- Inotify實際是一種事件驅動機制,它為應用程序監控文件系統事件提供了實時響應事件的機制,而無須通過諸如cron等的輪詢機制來獲取事件。cron等機制不僅無法做到實時性,而且消耗大量系統資源。相比之下,inotify基于事件驅動,可以做到對事件處理的實時響應,也沒有輪詢造成的系統資源消耗,是非常自然的事件通知接口,也與自然世界的事件機制相符合。
- inotify 的實現有幾款軟件
1)inotify-tools,
2)sersync(金山周洋)
3)lsyncd
特別說明:
下面的inotify配置是建立在rsync服務基礎上的配置過程。
第4章 inotify 實施準備
大前提rsync daemon 服務配置成功,可以再rsync客戶端推送拉取數據,然后才能配置inotify服務。
第5章 開始安裝
默認yum源:
base + extras + updates
擴展的yum源:
epel
1.網易163源
2。阿里云epel源
在安裝inotify-tools前請先確認你的linux內核是否達到了2.6.13,并且在編譯時開啟CONFIG_INOTIFY選項,也可以通過以下命令檢測。
5.1 查看當前系統是否支持inotify
[root@backup ~]# uname -r
2.6.32-642.el6.x86_64
[root@backup ~]# ls -l /proc/sys/fs/inotify
總用量 0
-rw-r--r-- 1 root root 0 3月 11 05:01 max_queued_events
-rw-r--r-- 1 root root 0 3月 11 05:01 max_user_instances
-rw-r--r-- 1 root root 0 3月 11 05:01 max_user_watches
#顯示這三個文件證明支持
關鍵參數說明:
在/proc/sys/fs/inotify目錄下有三個文件,對inotify機制有一定的限制
max_user_watches:設置inotifywait或inotifywatch命令可以監視的文件數量(單進程)
max_user_instances:設置每個用戶可以運行的inotifywait或inotifywatch命令的進程數。
max_queued_events:設置inotify實例事件(event)隊列可容納的事件數量。
5.2 Yum安裝inotify-tools:
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install inotify-tools
rpm -qa inotify-tools
一共安裝了2個工具,即inotifywait和inotifywatch
inotifywait:在被監控的文件或目錄上等待特定文件系統事件(open,close,delete等)發生,執行后處于阻塞狀態,適合shell腳本中使用。
inotifywatch:收集被監視的文件系統使用度統計數據,指文件系統事件發生的次數統計。
5.3 inotifywait命令常用參數詳解
inotifywait --help
[root@backup ~]# inotifywait --help
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.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
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 <file>
Read files to watch from <file> or `-' for stdin.
-o|--outfile <file>
Print events to <file> 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 <fmt> Print using a specified printf-like format
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string.
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
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參數 | 含義說明 |
---|---|
-r --recursive | 遞歸查詢目錄 |
-q --quiet | 打印很少的信息,僅僅打印監控事件的信息 |
-m,--monitor | 始終保持事件監聽狀態 |
--exclude | 排除文件或目錄時,不區分大小寫。 |
--timefmt | 指定時間輸出的格式 |
--format | 打印使用指定的輸出類似格式字符串 |
-e,--event | 通過此參數可以指定需要監控的事件,如下一個列表所示 |
-e :--event的各種事件含義
Events | 含義 |
---|---|
access | 文件或目錄被讀取 |
modify | 文件或目錄內容被修改 |
attrib | 文件或目錄屬性被改變 |
close | 文件或目錄封閉,無論讀/寫模式 |
open | 文件或目錄被打開 |
moved_to | 文件或目錄被移動至另外一個目錄 |
move | 文件或目錄被移動到另一個目錄或從另一個目錄移動至當前目錄 |
create | 文件或目錄被創建在當前目錄 |
delete | 文件或目錄被刪除 |
umount | 文件系統被卸載 |
5.4 人工測試監控事件
開啟兩個窗口
5.4.1 測試create
在第一個窗口輸入如下內容:
[root@backup ~]# ls /backup
[root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e create /backup
在第二個窗口:輸入如下內容
[root@backup ~]# cd /backup
[root@backup backup]# touch chensiqi
此時回到第一個窗口出現如下內容:
17 03 11 07 19 /backup/chensiqi
#命令說明
inotifywait:ionotify的命令工具
-mrq:-q只輸入簡短信息 -r,遞歸監控整個目錄包括子目錄 -m進行不間斷持續監聽
--timefmt 指定輸出的時間格式
--format:指定輸出信息的格式
-e create:制定監控的時間類型,監控創建create事件。
5.4.2 測試delte
第一個窗口輸入如下信息:
[root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e delete /backup
第二個窗口輸入如下信息:
[root@backup backup]# rm -rf chensiqi
此時第一個窗口會出現如下信息:
17 03 11 07 29 /backup/chensiqi
#命令說明:
-e delete:指定監聽的事件類型。監聽刪除delete事件
5.4.3 測試close_write
第一個窗口輸入如下信息:
inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e close_write /backup
第二個窗口輸入如下信息:
[root@backup backup]# touch close_write.log
[root@backup backup]# echo 111 >> close_write.log
[root@backup backup]# rm -f close_write.log
此時第一個窗口會出現如下信息:
17 03 11 07 38 /backup/close_write.log
17 03 11 07 39 /backup/close_write.log
#命令說明:
-e close_write:指定監聽類型。監聽文件寫模式的關閉。
5.4.4 測試move_to
第一個窗口輸入如下信息:
[root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e moved_to /backup
第二個窗口輸入如下信息:
此時第一個窗口會出現如下信息:
[root@backup backup]# touch chensiqi
[root@backup backup]# mv chensiqi chen
[root@backup backup]# mkdir ddddd
[root@backup backup]# mv chen ddddd/
5.5 編寫inotify實時監控腳本
#!/bin/bash
backup_Server=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line
do
cd /data
rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
done
提示:
- 上邊那個腳本效率很低,效率低的原因在于只要目錄出現變化就都會導致我整個目錄下所有東西都被推送一遍。因此,我們可以做如下改動提高效率
#!/bin/bash
Path=/data
backup_Server=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line
do
if [ -f $line ];then
rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
else
cd $Path &&\
rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
fi
done
腳本可以加入開機啟動:
echo "/bin/sh /server/scripts/inotify.sh &" >> /etc/rc.local
提示:
一個& 代表從后臺開始運行該條命令。
5.6 關鍵參數調整
在/proc/sys/fs/inotify目錄下有三個文件,對inotify機制有一定的限制
max_user_watches:設置inotifywait或inotifywatch命令可以監視的文件數量(單進程)
max_user_instances:設置每個用戶可以運行的inotifywait或inotifywatch命令的進程數
max_queued_events:設置inotify實例事件(event)隊列可容納的事件數量。
實戰調整:
[root@nfs01 data]# cat /proc/sys/fs/inotify/max_
max_queued_events max_user_instances max_user_watches
[root@nfs01 data]# cat /proc/sys/fs/inotify/max_user_watches
8192
[root@nfs01 data]# echo "50000000" > /proc/sys/fs/inotify/max_user_watches
[root@nfs01 data]# cat /proc/sys/fs/inotify/max_user_watches
50000000
[root@nfs01 data]# cat /proc/sys/fs/inotify/max_queued_events
16384
[root@nfs01 data]# echo "326790" > /proc/sys/fs/inotify/max_queued_events
[root@nfs01 data]# cat /proc/sys/fs/inotify/max_queued_events
326790
[root@nfs01 data]# sysctl -p
5.7 Rsync+inotify實時數據同步并發簡單測試
10K-100K
每秒100個并發
[root@nfs01 data]# paste inotify_100_server.log
inotify_100_backup_server.log > inotify_100.txt
[root@nfs01 data]# cat inotify_100.txt
23:05 34227 23:05 34227
23:05 34387 23:05 34387
23:05 35027 23:05 35027
23:05 35587 23:05 35587
23:05 36473 23:05 36473
23:05 36707 23:05 36707
23:05 37587 23:05 37587
以下省略...
Inotify實時并發:
結論:經過測試,每秒200文件并發,數據同步幾乎無延遲(小于1秒)
5.8 inotify 優點:
1)監控文件系統事件變化,通過同步工具實現實時數據同步。
5.9 inotify 缺點
1)并發如果大于200個文件(10-100k),同步就會有延遲
2)我們前面寫的腳本,每次都是全部推送一次,但確實是增量的。也可以只同步變化的文件,不變化的不理。
3)監控到事件后,調用rsync同步是單進程的,而sersync為多進程同步。既然有了inotify-tools,為什么還要開發sersync?
5.10 serysync功能多:(inotify+rsync命令)
1)支持通過配置文件管理
2)真正的守護進程socket
3)可以對失敗文件定時重傳(定時任務功能)
4)第三方的HTTP接口(例如:更新cdn緩存)
5)默認多進程rsync同步
5.11 高并發數據實時同步方案小結:
1)inotify(sersync)+ rsync,是文件級別的。
2)drbd文件系統級別,文件系統級別,基于block塊同步,缺點:備節點數據不可用
3)第三方軟件的同步功能:mysql同步(主從復制),oracle,mongodb
4)程序雙寫,直接寫兩臺服務器。
5)利用產品業務邏輯解決(讀寫分離,備份讀不到,讀主)
說明:
用戶上傳的圖片或者附件單獨存在NFS主服務器上;
用戶讀取數據從兩臺NFS備份服務器上讀取;
NFS主和兩臺NFS備份通過inotify+rsync方式進行實時同步。
6)NFS集群(1,4,5方案整合)(雙寫主存儲,備存儲用inotify(sersync)+rsync
文章列表