<服务>Inotify

时间:Nov. 29, 2016 分类:

目录:

Inotify

rsync的核心算法是对目标文件进行对比,只进行差异同步,对于大数据量对比是非常耗时的,并且低效,Inotify的作用是获取client端目录文件的变化,然后执行rsync命令同步数据。 Inotify从kernel内核2.6.13起对Inotify的支持。

Inotify的组件

  • Inotify 效果大概为300个文件/s
  • sersync 效果大概为150个文件/s
  • lsyncd

    Inotify使用

检查内核版本

[root@sersync why]# uname -r
2.6.32-431.el6.x86_64

Inotify配置

[root@nfs data1]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 11月 27 05:02 max_queued_events
-rw-r--r-- 1 root root 0 11月 27 05:02 max_user_instances
-rw-r--r-- 1 root root 0 11月 27 05:02 max_user_watches
  • max_queued_events监控队列的最大值,默认为16384。监控的执行的命令放到队列里,如果出现堵塞可以调大该值。
  • max_user_instances用户实例
  • max_user_watches用户watch

Inotify

下载Inotify http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 使用wget下不下来。

[root@nfs ~]# tar xf inotify-tools-3.14.tar.gz 
[root@nfs ~]# cd inotify-tools-3.14
[root@nfs inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[root@nfs inotify-tools-3.14]# make && make install
[root@nfs inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools

Inotify目录

[root@nfs inotify-tools-3.14]# cd !$
cd /usr/local/inotify-tools
[root@nfs inotify-tools]# ll
总用量 16
drwxr-xr-x 2 root root 4096 11月 27 05:55 bin
drwxr-xr-x 3 root root 4096 11月 27 05:55 include
drwxr-xr-x 2 root root 4096 11月 27 05:55 lib
drwxr-xr-x 4 root root 4096 11月 27 05:55 share

Inotify参数

[root@nfs inotify-tools]# ./bin/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     #文件系统被卸载

Inotify手动测试

启动监听create事件

启动监听

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --timefmt '%d/%m/%y/ %H:%M:%S' --format '%T %w%f' -e create /data1/

重启一个tty终端

[root@nfs data1]# touch 1
[root@nfs data1]# touch 2
[root@nfs data1]# touch 3
[root@nfs data1]# mkdir 4
[root@nfs data1]# mkdir 5

原tty终端显示

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --timefmt '%d/%m/%y/ %H:%M:%S' --format '%T %w%f' -e create /data1/
27/11/16/ 09:05:34 /data1/1
27/11/16/ 09:05:36 /data1/2
27/11/16/ 09:05:38 /data1/3
27/11/16/ 09:05:42 /data1/4
27/11/16/ 09:05:44 /data1/5

gif展示

多种监控事件

启动监听

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --timefmt '%d/%m/%y/ %H:%M:%S' --format '%T %w%f' -e create,delete /data1/

新tty终端删除

[root@nfs data1]# rm -f {1,2,3}

原tty终端显示

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --timefmt '%d/%m/%y/ %H:%M:%S' --format '%T %w%f' -e create,delete /data1/
27/11/16/ 09:06:35 /data1/1
27/11/16/ 09:06:35 /data1/2
27/11/16/ 09:06:35 /data1/3

监控事件重复

启动监听

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --timefmt '%d/%m/%y/ %H:%M:%S' --format '%T %w%f' -e create,delete,close_write /data1/

新tty终端创建

[root@nfs data1]# touch 1

原tty终端显示

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --timefmt '%d/%m/%y/ %H:%M:%S' --format '%T %w%f' -e create,delete,close_write /data1/
27/11/16/ 09:10:23 /data1/1
27/11/16/ 09:10:23 /data1/1

create和close_write同时监听到,所以显示两次。

新tty终端写入

[root@nfs data1]# echo 1 > 1

原tty终端显示

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --timefmt '%d/%m/%y/ %H:%M:%S' --format '%T %w%f' -e create,delete,close_write /data1/
27/11/16/ 09:10:23 /data1/1
27/11/16/ 09:10:23 /data1/1
27/11/16/ 09:11:00 /data1/1

不显示时间

[root@nfs inotify-tools]# ./bin/inotifywait -mrq --format '%w%f' -e create,delete,close_write /data1/
/data1/2
/data1/2

脚本推送

#!/bin/sh
cmd=/usr/local/inotify-tools/bin/inotifywait
$cmd -mrq --format '%w%f' -e create,delete,close_write /data1 | \
while read line
do
    rsync -az --delete /data1/  rsync://rsync_backup@192.168.0.184/why --password-file=/etc/rsync.password
done

参数调优

默认值

[root@sersync inotify]# cat max_queued_events 16384 [root@sersync inotify]# cat max_user_instances 128 [root@sersync inotify]# cat max_user_watches 8192

调优值

  • max_queued_events = 327679
  • max_user_watches = 5000000

    性能极限

一般在50~500k大小的文件进行同步时,可以达到200~300的并发。

脚本优化

#!/bin/sh
cmd=/usr/local/inotify-tools/bin/inotifywait
$cmd -mrq --format '%w%f' -e create,close_write /data1 | \
while read line
do
    rsync -az $line  rsync://rsync_backup@192.168.0.184/why --password-file=/etc/rsync.password
done


$cmd -mrq --format '%w%f' -e delete /data1 | \
while 
do
    rsync -az --delete /data1  rsync://rsync_backup@192.168.0.184/why --password-file=/etc/rsync.password
done

因为在推送的过程中,一旦文件数量特大,每次都要做推送整个目录的比对,效率太低,只推送状态和改变的即可。 或者添加判断。

#!/bin/sh
cmd=/usr/local/inotify-tools/bin/inotifywait
$cmd -mrq --format '%w%f' -e create,close_write /data1 | \
while read line
do
    [ ! -e "$line" ] && continue
    rsync -az --delete $line rsync://rsync_backup@192.168.0.184/why --password-file=/etc/rsync.password
done