linux:Inotify事件监控工具
inotify事件监控工具原文地址:https://www.cnblogs.com/chensiqiqi/p/6542268.html 第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 组合的起源
第3章 inotify简介
特别说明: 下面的inotify配置是建立在rsync服务基础上的配置过程。 第4章 inotify 实施准备大前提rsync daemon 服务配置成功,可以再rsync客户端推送拉取数据,然后才能配置inotify服务。 第5章 开始安装默认yum源: 扩展的yum源: 在安装inotify-tools前请先确认你的linux内核是否达到了2.6.13,并且在编译时开启CONFIG_INOTIFY选项,也可以通过以下命令检测。 5.1 查看当前系统是否支持inotify[[email?protected]backup ~]# uname -r 2.6.32-642.el6.x86_64 [[email?protected]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
5.3 inotifywait命令常用参数详解inotifywait --help [[email?protected] ~]# 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
下面用列表详细解释一下各个参数的含义
-e :--event的各种事件含义
5.4 人工测试监控事件开启两个窗口 5.4.1 测试create在第一个窗口输入如下内容:
[[email?protected]backup ~]# ls /backup [[email?protected]backup ~]# inotifywait -mrq --timefmt ‘%y %m %d %H %M‘ --format ‘%T %w%f‘ -e create /backup 在第二个窗口:输入如下内容 [[email?protected]backup ~]# cd /backup [[email?protected]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第一个窗口输入如下信息:
[[email?protected] ~]# inotifywait -mrq --timefmt ‘%y %m %d %H %M‘ --format ‘%T %w%f‘ -e delete /backup 第二个窗口输入如下信息: [[email?protected] 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 第二个窗口输入如下信息: [[email?protected] backup]# touch close_write.log [[email?protected] backup]# echo 111 >> close_write.log [[email?protected] 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第一个窗口输入如下信息:
[[email?protected] ~]# inotifywait -mrq --timefmt ‘%y %m %d %H %M‘ --format ‘%T %w%f‘ -e moved_to /backup 第二个窗口输入如下信息: 此时第一个窗口会出现如下信息: [[email?protected] backup]# touch chensiqi [[email?protected] backup]# mv chensiqi chen [[email?protected] backup]# mkdir ddddd [[email?protected] 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 [email?protected]$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,delete /data | while read line do if [ -f $line ];then rsync -az $line --delete [email?protected]$backup_Server::nfsbackup --password-file=/etc/rsync.password else cd $Path && rsync -az ./ --delete [email?protected]$backup_Server::nfsbackup --password-file=/etc/rsync.password fi done
脚本可以加入开机启动: 5.6 关键参数调整
实战调整: [[email?protected] data]# cat /proc/sys/fs/inotify/max_ max_queued_events max_user_instances max_user_watches [[email?protected] data]# cat /proc/sys/fs/inotify/max_user_watches 8192 [[email?protected] data]# echo "50000000" > /proc/sys/fs/inotify/max_user_watches [[email?protected] data]# cat /proc/sys/fs/inotify/max_user_watches 50000000 [[email?protected] data]# cat /proc/sys/fs/inotify/max_queued_events 16384 [[email?protected] data]# echo "326790" > /proc/sys/fs/inotify/max_queued_events [[email?protected] data]# cat /proc/sys/fs/inotify/max_queued_events 326790 [[email?protected] data]# sysctl -p
5.7 Rsync+inotify实时数据同步并发简单测试10K-100K 每秒100个并发 [[email?protected] data]# paste inotify_100_server.log inotify_100_backup_server.log > inotify_100.txt [[email?protected] 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),同步就会有延迟 5.10 serysync功能多:(inotify+rsync命令)1)支持通过配置文件管理 5.11 高并发数据实时同步方案小结:1)inotify(sersync)+ rsync,是文件级别的。 说明: 用户上传的图片或者附件单独存在NFS主服务器上; 用户读取数据从两台NFS备份服务器上读取; NFS主和两台NFS备份通过inotify+rsync方式进行实时同步。 6)NFS集群(1,4,5方案整合)(双写主存储,备存储用inotify(sersync)+rsync (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |