inotify事件监控工具实践inotify事件监控工具

-私人课件,不公开,不出版,禁止传播
想做好运维工作,人先要学会勤快;
居安而思危,勤记而补拙,方可不断提高;
别人资料自己你用着再如何爽那
也是别人的;自己总结的东西是你自己特有的一种思想与理念的展现;
精髓不是手术来的,精髓是记出来的;
请同学们在学习的过程中养成好的学习习惯;
勤于实践,抛弃教案,勤于动手,整理文档。

特别说明:
下面的inotify配置是建立在rsync服务基础上的配置过程。

第4章inotify实施准备

大型rsync daemon服务配置成功,可以再rsync客户端推送拉取数据,然后才能配置inotify服务。

第5章开始安装

默认yum源:
基础+附加功能+更新
扩展的yum源:
epel
1.网易163源
2。阿里云epel源
在安装inotify-tools前请先确认你的Linux内核是否达到了2.6.13,并且在编译时开启CONFIG_INOTIFY选项,也可以通过以下命令检测。

5.1查看当前系统是否支持inotify

  1. [root@backup ~]# uname -r
  2. 2.6.32-642.el6.x86_64
  3. [root@backup ~]# ls -l /proc/sys/fs/inotify
  4. 总用量 0
  5. -rw-r—r— 1 root root 0 3月 11 05:01 max_queued_events
  6. -rw-r—r— 1 root root 0 3月 11 05:01 max_user_instances
  7. -rw-r—r— 1 root root 0 3月 11 05:01 max_user_watches
  8. 显示这三个文件证明支持

关键参数说明:

  1. 在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制
  2. max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
  3. max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch命令的进程数。
  4. max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量。

    5.2 Yum安装inotify-tools:

  5. wget -O /etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-6.repo

  6. 2020-12-3日centos6停止更新,启动备用源

  7. wget http://files.tttidc.com/centos6/epel-6.repo

  8. yum -y install inotify-tools
  9. rpm -qa inotify-tools

一共安装了2个工具,即inotifywait和inotifywatch
inotifywait:在被监视的文件或目录上等待特定文件系统事件(打开,关闭,删除等)发生,执行后在特定状态下,适合shell
脚本中使用。inotifywatch:收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。

5.3 inotifywait命令常用参数详解

inotifywait-帮助

  1. [root@backup ~]# inotifywait —help
  2. inotifywait 3.14
  3. Wait for a particular event on a file or set of files.
  4. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ … ]
  5. Options:
  6. -h|—help Show this help text.
  7. @ Exclude the specified file from being watched.
  8. —exclude
  9. Exclude all events on files matching the
  10. extended regular expression .
  11. —excludei
  12. Like —exclude but case insensitive.
  13. -m|—monitor Keep listening for events forever. Without
  14. this option, inotifywait will exit after one
  15. event is received.
  16. -d|—daemon Same as —monitor, except run in the background
  17. logging events to a file specified by —outfile.
  18. Implies —syslog.
  19. -r|—recursive Watch directories recursively.
  20. —fromfile
  21. Read files to watch from or `-‘ for stdin.
  22. -o|—outfile
  23. Print events to rather than stdout.
  24. -s|—syslog Send errors to syslog rather than stderr.
  25. -q|—quiet Print less (only print events).
  26. -qq Print nothing (not even events).
  27. —format Print using a specified printf-like format
  28. string; read the man page for more details.
  29. —timefmt strftime-compatible format string for use with
  30. %T in —format string.
  31. -c|—csv Print events in CSV format.
  32. -t|—timeout
  33. When listening for a single event, time out after
  34. waiting for an event for seconds.
  35. If is 0, inotifywait will never time out.
  36. -e|—event [ -e|—event … ]
  37. Listen for specific event(s). If omitted, all events are
  38. listened for.
  39. Exit status:
  40. 0 - An event you asked to watch for was received.
  41. 1 - An event you did not ask to watch for was received
  42. (usually delete_self or unmount), or some error occurred.
  43. 2 - The —timeout option was given and no events occurred
  44. in the specified interval of time.
  45. Events:
  46. access file or directory contents were read
  47. modify file or directory contents were written
  48. attrib file or directory attributes changed
  49. close_write file or directory closed, after being opened in
  50. writeable mode
  51. close_nowrite file or directory closed, after being opened in
  52. read-only mode
  53. close file or directory closed, regardless of read/write mode
  54. open file or directory opened
  55. moved_to file or directory moved to watched directory
  56. moved_from file or directory moved from watched directory
  57. move file or directory moved to or from watched directory
  58. create file or directory created within watched directory
  59. delete file or directory deleted within watched directory
  60. delete_self file or directory was deleted
  61. unmount file system containing file or directory unmounted

下面用列表详细解释一下各个参数的含义

inotifywait参数 含义说明
-r-递归 递归查询目录
-q-安静 打印很少的信息,仅打印监控事件的信息
-m,-显示器 始终保持事件监听状态
- 排除 排除文件或目录时,不区分大小写。
—timefmt 指定时间输出的格式
- 格式 打印使用指定的输出类似格式字符串
-e,-event 通过此参数可以指定需要监控的事件,如下一个列表所示

-e:-event的各种事件含义

大事记 意味着
使用权 文件或目录被读取
调整 文件或目录内容被修改
属性 文件或目录属性被改变
关闭 文件或目录封闭,无论读/写模式
打开 文件或目录被打开
搬去 文件或目录被移动至另一个一个目录
移动 文件或目录被移动到另一个目录或从另一个目录移动到当前目录
创建 文件或目录被创建在当前目录
删除 文件或目录被删除
上山 文件系统被卸载

5.4人工测试监控事件

开启两个窗口

5.4.1测试创建

  1. 在第一个窗口输入如下内容:
  2. [root@backup ~]# ls /backup
  3. [root@backup ~]# inotifywait -mrq —timefmt ‘%y %m %d %H %M’ —format ‘%T %w%f’ -e create /backup
  4. 在第二个窗口:输入如下内容
  5. [root@backup ~]# cd /backup
  6. [root@backup backup]# touch chensiqi
  7. 此时回到第一个窗口出现如下内容:
  8. 17 03 11 07 19 /backup/chensiqi
  9. 命令说明

  10. inotifywait:ionotify的命令工具
  11. -mrq:-q只输入简短信息 -r,递归监控整个目录包括子目录 -m进行不间断持续监听
  12. —timefmt 指定输出的时间格式
  13. —format:指定输出信息的格式
  14. -e create:制定监控的时间类型,监控创建create事件。

    5.4.2测试delte

  15. 第一个窗口输入如下信息:

  16. [root@backup ~]# inotifywait -mrq —timefmt ‘%y %m %d %H %M’ —format ‘%T %w%f’ -e delete /backup
  17. 第二个窗口输入如下信息:
  18. [root@backup backup]# rm -rf chensiqi
  19. 此时第一个窗口会出现如下信息:
  20. 17 03 11 07 29 /backup/chensiqi
  21. 命令说明:

  22. -e delete:指定监听的事件类型。监听删除delete事件

    5.4.3测试close_write

  23. 第一个窗口输入如下信息:

  24. inotifywait -mrq —timefmt ‘%y %m %d %H %M’ —format ‘%T %w%f’ -e close_write /backup
  25. 第二个窗口输入如下信息:
  26. [root@backup backup]# touch close_write.log
  27. [root@backup backup]# echo 111 >> close_write.log
  28. [root@backup backup]# rm -f close_write.log
  29. 此时第一个窗口会出现如下信息:
  30. 17 03 11 07 38 /backup/close_write.log
  31. 17 03 11 07 39 /backup/close_write.log
  32. 命令说明:

  33. -e close_write:指定监听类型。监听文件写模式的关闭。

    5.4.4测试move_to

  34. 第一个窗口输入如下信息:

  35. [root@backup ~]# inotifywait -mrq —timefmt ‘%y %m %d %H %M’ —format ‘%T %w%f’ -e moved_to /backup
  36. 第二个窗口输入如下信息:
  37. 此时第一个窗口会出现如下信息:
  38. [root@backup backup]# touch chensiqi
  39. [root@backup backup]# mv chensiqi chen
  40. [root@backup backup]# mkdir ddddd
  41. [root@backup backup]# mv chen ddddd/

    5.5编写inotify实时监控脚本

  42. !/bin/bash

  43. backup_Server=172.16.1.41
  44. /usr/bin/inotifywait -mrq —format ‘%w%f’ -e create,close_write,delete /data | while read line
  45. do
  46. cd /data
  47. rsync -az ./ —delete rsync_backup@$backup_Server::nfsbackup —password-file=/etc/rsync.password
  48. done

提示:

  • 上边那个脚本效率很低,效率低的原因在于只要目录出现变化就都会导致我整个目录下所有东西都被推一遍。因此,我们可以做如下提升效率
  1. !/bin/bash

  2. Path=/data
  3. backup_Server=172.16.1.41
  4. /usr/bin/inotifywait -mrq —format ‘%w%f’ -e create,close_write,delete /data | while read line
  5. do
  6. if [ -f $line ];then
  7. rsync -az $line —delete rsync_backup@$backup_Server::nfsbackup —password-file=/etc/rsync.password
  8. else
  9. cd $Path &&\
  10. rsync -az ./ —delete rsync_backup@$backup_Server::nfsbackup —password-file=/etc/rsync.password
  11. fi
  12. 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实例事件(事件)可以可容纳的事件数量。
实战调整:

  1. [root@nfs01 data]# cat /proc/sys/fs/inotify/max_
  2. max_queued_events max_user_instances max_user_watches
  3. [root@nfs01 data]# cat /proc/sys/fs/inotify/max_user_watches
  4. 8192
  5. [root@nfs01 data]# echo “50000000” > /proc/sys/fs/inotify/max_user_watches
  6. [root@nfs01 data]# cat /proc/sys/fs/inotify/max_user_watches
  7. 50000000
  8. [root@nfs01 data]# cat /proc/sys/fs/inotify/max_queued_events
  9. 16384
  10. [root@nfs01 data]# echo “326790” > /proc/sys/fs/inotify/max_queued_events
  11. [root@nfs01 data]# cat /proc/sys/fs/inotify/max_queued_events
  12. 326790
  13. [root@nfs01 data]# sysctl -p

    5.7 Rsync + inotify实时数据同步并发简单测试

    10K-100K
    每秒100个并发

  14. [root@nfs01 data]# paste inotify_100_server.log

  15. inotify_100_backup_server.log > inotify_100.txt
  16. [root@nfs01 data]# cat inotify_100.txt
  17. 23:05 34227 23:05 34227
  18. 23:05 34387 23:05 34387
  19. 23:05 35027 23:05 35027
  20. 23:05 35587 23:05 35587
  21. 23:05 36473 23:05 36473
  22. 23:05 36707 23:05 36707
  23. 23:05 37587 23:05 37587
  24. 以下省略…

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)真正的守护进程套接字
3)可以对失败文件定时重传(定时任务功能)
4)第三方的HTTP接口(例如:cdn缓存更新)
5)多个多进程rsync同步

5.11高并发数据实时同步方案小结:

1)inotify(sersync)+ rsync,是文件
等级的。2)drbd文件系统等级,文件系统等级,基于块块同步,缺点:准备数据不可用
3)第三方软件的同步功能:mysql同步(主从复制),oracle,mongodb
4)程序双写,直接写两台服务器
。5)利用产品业务逻辑解决(读取分离,备份读不到,读主)
说明:
用户上传的图片或附件单独存在NFS主服务器上;
用户读取数据从两台NFS备份服务器上读取;
NFS主和两台NFS备份通过inotify + rsync方式进行实时同步。
6)NFS占用(1,4,5方案整合)(双写主存储,备存储用inotify(sersync)+ rsync
+