第二十三節 inotify事件監控工具
標籤(空格分隔): Linux實戰教學筆記-陳思齊css
原文:http://www.cnblogs.com/chensiqiqi/p/6542268.htmlhtml
第1章,NFS存儲服務器與backup備份服務器的搭建。
詳細細節知識與搭建請關注:mysql
http://www.cnblogs.com/chensiqiqi/p/6514315.html Rsync數據同步工具linux
http://www.cnblogs.com/chensiqiqi/p/6530859.html 企業級NFS網絡文件共享服務算法
http://www.cnblogs.com/chensiqiqi/p/6531003.html【Rsync項目實戰】備份全網服務器數據sql
第2章:rsync + inotify 組合的起源
Rsync(remote sync)遠程同步工具,經過rsync能夠實現對遠程服務器數據的增量備份同步,但rsync自身也有瓶頸,同步數據時,rsync採用核心算法對遠程服務器的目標文件進行比對,只進行差別同步。咱們能夠想象一下,若是服務器的文件數量達到了百萬甚至千萬量級,那麼文件對比將是很是耗時的。並且發生變化的每每是其中不多的一部分,這是很是低效的方式。inotify的出現,能夠緩解rsync不足之處,取長補短。mongodb
第3章 inotify簡介
- Inotify是一種強大的,細粒度的,異步的文件系統事件監控機制(軟件),linux內核從2.6.13起,加入了Inotify支持,經過Inotify能夠監控文件系統中添加,刪除,修改,移動等各類事件,利用這個內核接口,第三方軟件就能夠監控文件系統下文件的各類變化狀況,而inotify-tools正是實施這樣監控的軟件。還有國人周洋在金山公司開發的sersync。
- Inotify實際是一種事件驅動機制,它爲應用程序監控文件系統事件提供了實時響應事件的機制,而無須經過諸如cron等的輪詢機制來獲取事件。cron等機制不只沒法作到實時性,並且消耗大量系統資源。相比之下,inotify基於事件驅動,能夠作到對事件處理的實時響應,也沒有輪詢形成的系統資源消耗,是很是天然的事件通知接口,也與天然世界的事件機制相符合。
- inotify 的實現有幾款軟件
1)inotify-tools,
2)sersync(金山周洋)
3)lsyncd
特別說明:shell
下面的inotify配置是創建在rsync服務基礎上的配置過程。express
第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