第二十三节 inotify事件监控工具
标签(空格分隔): Linux实战教学笔记-陈思齐
原文:
第1章,NFS存储服务器与backup备份服务器的搭建。详细细节知识与搭建请关注:
第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:收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。
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