inotify的安装与使用
第1章 inotify原理及介绍1.1inotify简介
inotify is an inode-based filesystem notification technology. Itprovides possibility to simply monitor various events on files in filesystems.It is a very much powerful replacement of (obsolete) dnotify. inotify brings acomfortable way how to manage files used in your applications.(inotify是基于inode的文件系统通知技术。 它提供了简单地监视文件系统中文件的各种事件的可能性。这是一个非常强大的替代(过时的)通知。 inotify带来一个舒适的方式如何管理您的应用程序中使用的文件。)
Inotify是一种强大的、细粒度的、异步的文件系统监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施这样监控的软件。
1.2应用场景
检测文件和目录中的变化(例如配置文件,邮件目录)
保护关键文件及其最终的自动恢复
文件使用统计和类似的目的
自动上传处理
监控包装系统之外的安装
自动更新备份和/或版本控制
反映搜索数据库的变化
第2章 inotify的安装与配置2.1原理图
2.2服务规划
2.2.1服务器规划表
rsync+inotify客户端 | 1台 | 监控文件或目录的变化后,通过rsync传输 |
说明:总共需要1台服务器完成本次项目 |
2.2.2主机IP规划表
服务器说明 | IP地址 | 主机名称规则 |
inotify服务器 | 172.16.1.8/24 | web01 |
2.2.3主机名解析
172.16.1.8 web01
2.3安装环境
2.3.1操作系统版本
[root@server ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@server ~]# uname -r 2.6.32-431.el6.x86_64 [root@server ~]# uname -m x86_64
2.3.2inotify软件版本
[root@nfs01 local]# rpm-qa inotify-tools inotify-tools-3.14-1.el6.x86_64
2.4软件安装
Inotify可以通过yum命令安装,或者通过源码编译的方式安装。在安装inotify-tools前先确认linux内核是否达到2.6.13,并且在编译时开启CONFIG_INOTIFY选项。
2.4.1查看当前系统是否支持inotify
[root@nfs backup]# uname -r 2.6.32-431.el6.x86_64 [root@nfs backup]# ls -l/proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Oct 27 22:08max_queued_events -rw-r--r-- 1 root root 0 Oct 27 22:08max_user_instances -rw-r--r-- 1 root root 0 Oct 27 22:08max_user_watche
#<==显示这三项则证明支持
2.4.2查看是否已安装inotify
[root@nfs backup]# rpm -qa inotify-tools [root@nfs backup]# #<==这里如果没有任何返回值就表明没有安装
2.4.3编译源码方式安装
1)下载inotify源码包,地址如下:
http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@web01 ~]# wgethttp://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --2017-11-05 18:40:47-- http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz Resolving github.com... 192.30.255.112,192.30.255.113 Connecting to github.com|192.30.255.112|:80...connected. HTTP request sent, awaiting response... 301 MovedPermanently #<==这里可能很多次都下载不了,可以通过windows浏览器下载,然后在上传到服务器上
2)解压、编译和安装
[root@nfs src]# tar -zxvfinotify-tools-3.14.tar.gz [root@nfs src]# 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
3)创建软连接
[root@nfs local]# cd /usr/local/ && ln -sinotify-tools-3.14 inotify-tools [root@nfs local]# ll total 44 drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 2 root root 4096 Sep 23 2011 include lrwxrwxrwx 1 root root 18 Oct 27 22:25 inotify-tools -> inotify-tools-3.14 drwxr-xr-x 6 root root 4096 Oct 27 22:23 inotify-tools-3.14 drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64 drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin drwxr-xr-x. 5 root root 4096 Oct 22 23:49 share drwxr-xr-x. 3 root root 4096 Oct 27 22:22 src
4)成功编译后会创建四个目录
[root@nfs local]# cd /usr/local/inotify-tools/&& ls -l total 16 drwxr-xr-x 2 root root 4096 Oct 27 22:23 bin #<==inotify执行命令(二进制) drwxr-xr-x 3 root root 4096 Oct 27 22:23 include#<==inotify程序所需头文件 drwxr-xr-x 2 root root 4096 Oct 27 22:23 lib #<==动态链接库文件 drwxr-xr-x 4 root root 4096 Oct 27 22:23share #<==帮助文档
2.4.4yum命令安装
1)添加epel源,因为inotify的yum源不在系统默认的yum源里面
wget -O/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@nfs01 backup]# wget -O/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo --2017-10-26 23:01:50-- http://mirrors.aliyun.com/repo/epel-6.repo Resolving mirrors.aliyun.com... 61.191.187.200,61.191.187.190, 61.191.187.211, ... Connecting tomirrors.aliyun.com|61.191.187.200|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1083 (1.1K) [application/octet-stream] Saving to: “/etc/yum.repos.d/epel.repo” 100%[=============================================================================================>]1,083 --.-K/s in 0s 2017-10-26 23:01:51 (389 MB/s) -“/etc/yum.repos.d/epel.repo” saved [1083/1083]
2)使用yum命令安装软件
[root@nfs01 backup]# yum-y install inotify-tools Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base:mirrors.sohu.com * epel:mirrors.aliyun.com * extras:mirrors.sohu.com * updates:ftp.nsc.ru epel | 4.3 kB 00:00 #<==中间自动安装过程省略 Running Transaction Installing : inotify-tools-3.14-1.el6.x86_64 1/1 Verifying :inotify-tools-3.14-1.el6.x86_64 1/1 Installed: inotify-tools.x86_64 0:3.14-1.el6 Complete!
3)检测是否安装完毕
[root@nfs01 backup]# rpm-qa inotify-tools inotify-tools-3.14-1.el6.x86_64 #<==有结果说明安装正确第3章 inotify的使用
3.1inotifywait监控参数说明
可以通过以下命令查看inotifywait相关参数
[root@web01 bin]# /usr/local/inotify-tools/bin/inotifywait–help #<==编译安装查看
inotifywait 3.14 Wait for a particular event on a file or set offiles. Usage:inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ] Options: -h|--help Showthis help text. @<file> Excludethe specified file from being watched. --exclude<pattern> Excludeall events on files matching the extendedregular expression <pattern>. --excludei<pattern> Like--exclude but case insensitive. #<==排除时不区分大小写 -m|--monitor Keeplistening for events forever. Without thisoption, inotifywait will exit after one eventis received. #<==始终保持监听状态 -d|--daemon Sameas --monitor, except run in the background loggingevents to a file specified by --outfile. Implies--syslog. -r|--recursive Watch directories recursively. #<==递归查询目录 --fromfile<file> Readfiles to watch from <file> or `-' for stdin. -o|--outfile<file> Printevents to <file> rather than stdout. -s|--syslog Senderrors to syslog rather than stderr. -q|--quiet Printless (only print events).#<==只打印监控的信息 -qq Printnothing (not even events). --format <fmt> Printusing a specified printf-like format string;read the man page for more details. #<==打印使用指定输出类似的字符串 --timefmt <fmt> strftime-compatibleformat string for use with %Tin --format string. #<==指定时间的输出格式 -c|--csv Printevents in CSV format. -t|--timeout<seconds> Whenlistening for a single event, time out after waitingfor an event for <seconds> seconds. If<seconds> is 0, inotifywait will never time out. -e|--event<event1> [ -e|--event <event2> ... ] Listenfor specific event(s). If omitted, allevents are listenedfor. #<==指定需要监控的事件 Exit status: 0 - Anevent you asked to watch for was received. 1 - Anevent you did not ask to watch for was received (usually delete_self or unmount), or someerror 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 writeablemode close_nowrite file or directory closed, after being openedin read-onlymode close file or directory closed, regardless ofread/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 fromwatched directory create file or directory created within watcheddirectory delete file or directory deleted within watcheddirectory delete_self file or directory was deleted unmount file system containing file or directoryunmounted
3.2测试实时监控
1)开启两个窗口,其中一个输入如下命令,进入监控状态
[root@web01 ~]# /usr/local/inotify-tools/bin/inotifywait -mrq--timefmt '%d%m%y %H:%M' --format '%T%w%f' -e create,delete /backup/
参数说明:
/usr/local/inotify-tools/bin/inotifywait --监控的命令
-mrq --指定监控的参数,m(保持监听状态),r(递归监控),q(只打印监控信息)
--timefmt '%d%m%y %H:%M' --指定时间的输出格式
--format '%T %w%f' --指定输出字符串的格式
-e create,delete --指定需要监控的事件为(create,delete)
/backup/ --指定需要监控的目录
注意:注意--format参数里面的%T,是指输出前面的时间格式。如果不加,就不显示时间了。
2)另外一个窗口进入监控目录,在目录下进行文件操作
[root@web01 backup]# touch 1 [root@web01 backup]# touch 2 [root@web01 backup]# rm -f 1 [root@web01 backup]# rm -f 2
在监控端的显示如下:
[root@web01 ~]#/usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format'%T %w%f' -e create,delete /backup/ 011117 13:26 /backup/1 011117 13:26 /backup/2 011117 13:26 /backup/1 011117 13:26 /backup/2
3)简化输出
有事输出时可以选择不打印时间,只打印又变动的文件
[root@nfs01~]# inotifywait -mrq --format '%w%f' -e create,delete /backup/ /backup/1
/backup/2
/backup/1
/backup/2
3.3inotify的调优
3.3.1调优参数
监控事件相关参数的默认大小:
[root@web01 inotify]# cat max_queued_events
16384
[root@web01 inotify]# cat max_user_instances
128
[root@web01 inotify]# cat max_user_watches
8192
参数说明:
在目录/proc/sys/fs/inotify下有三个文件,对inotify机制有一定的影响。
[root@web01 inotify]# ls -l
total 0
-rw-r--r-- 1 root root 0 Oct 31 17:01 max_queued_events
-rw-r--r-- 1 root root 0 Oct 31 17:01 max_user_instances
-rw-r--r-- 1 root root 0 Oct 31 17:01 max_user_watches
max_queued_events:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)。
max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch命令的进程数。
max_user_watches:设置inotify实例事件(event)队列可容纳的事件数量。
3.3.2修改参数
修改文件中的数字参数如下:
[root@web01 inotify]# echo "50000000"> /proc/sys/fs/inotify/max_user_watches [root@web01 inotify]# echo "50000000"> /proc/sys/fs/inotify/max_queued_events
这两个参数重启服务器之后,配置会被还原,所以需要将这两行写入到开机启动文件当中:
[root@web01 inotify]# tail -2 /etc/rc.local tail: inotify cannot be used, reverting topolling echo "50000000" >/proc/sys/fs/inotify/max_user_watches echo "50000000" >/proc/sys/fs/inotify/max_queued_events第4章 小结
inotify的优点:
1)监控文件系统事件变化,通过同步工具实现实时数据同步。
inotify的缺点:
1)并发如果大于200个文件(10-100K),同步就会有延迟。
2)推送是全部推送一次,但确实是增量的
3)监控到事件之后,调用rsync同步是单进程的(加&并发),sersync多进程同步。