欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

inotify监听的配置 文件读写监视工具监视软件 有大用


  • inotify 不好用吗? 非得自己造轮子.

  • We_are_all_one封装了一下linux inotify接口而已
    2018-1-21 13:42回复
  • DFeamTag真不够好用,所以后来内核又引入了fanotify,但fanotify能监控的事件又没inotify多

原文:http://blog.csdn.net/god_wot/article/details/50448814

Linux内核从2.6.13开始,引入了inotify机制。通过intofity机制,能够对文件系统的变化进行监控,如对文件进行创建、删除、修改等操作,可以及时通知应用程序进行相关事件的处理。这种响应处理机制,避免了频繁的文件轮询任务,提高了任务的处理效率。

一、检查系统内核版本

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ ~]# uname -a    

  2. Linux iZ25w1kdi5zZ 2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20:51 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux    


二、检查系统是否支持inotify

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ ~]# ls -lsart /proc/sys/fs/inotify    

  2. total 0    

  3. 0 dr-xr-xr-x 0 root root 0 Sep 19 09:38 ..    

  4. 0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_watches    

  5. 0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_instances    

  6. 0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_queued_events    

  7. 0 dr-xr-xr-x 0 root root 0 Jan  1 13:51 .    

如果出现上面结果说明系统支持inotify。

三、下载安装

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ src]#wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz    

  2.     

  3. [root@iZ25w1kdi5zZ src]# tar -zvxf inotify-tools-3.14.tar.gz    

  4. [root@iZ25w1kdi5zZ src]# cd inotify-tools-3.14    

  5.     

  6. [root@iZ25w1kdi5zZ inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify    

  7. [root@iZ25w1kdi5zZ inotify-tools-3.14]# make    

  8. [root@iZ25w1kdi5zZ inotify-tools-3.14]# make install    

四、查看inotify默认参数
[plain] view plain copy
  1. [root@iZ25w1kdi5zZ bin]# sysctl -a | grep max_queued_events    

  2. fs.inotify.max_queued_events = 16384    

  3.     

  4. [root@iZ25w1kdi5zZ bin]# sysctl -a | grep max_user_watches    

  5. fs.inotify.max_user_watches = 8192    

  6. fs.epoll.max_user_watches = 798863    

  7.     

  8. [root@iZ25w1kdi5zZ bin]# sysctl -a | grep max_user_instances    

  9. fs.inotify.max_user_instances = 128 <span> </span>  

五、修改inotify参数

1、命令修改

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ bin]# sysctl -w fs.inotify.max_user_instances=130    

  2. fs.inotify.max_user_instances = 130    

2、文件修改
[plain] view plain copy
  1. [root@iZ25w1kdi5zZ]# vi /etc/sysctl.conf    

  2. #添加如下代码    

  3. fs.inotify.max_user_instances=130    


3、参数说明 

max_user_instances:每个用户创建inotify实例最大值

max_queued_events:inotify队列最大长度,如果值太小,会出现错误,导致监控文件不准确

max_user_watches:要知道同步的文件包含的目录数,可以用:[root@iZ25w1kdi5zZhome]# find /home/rain -type d|wc -l 统计,必须保证参数值大于统计结果(/home/rain为同步文件目录)。

六、创建实时监控脚本

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ ~]# mkdir -p /opt/scripts    

  2.     

  3. [root@iZ25w1kdi5zZ ~]# cd /opt/scripts    

  4.     

  5. [root@iZ25w1kdi5zZ scripts]# vi inotify_start.sh    

  6. /usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete \    

  7. --fromfile '/opt/scripts/ffile' \    

  8. --timefmt '%y-%m-%d %H:%M' --format '%T %f %e' \    

  9. --outfile '/tmp/rsync.log'    

[plain] view plain copy
  1. inotifywait常用参数:    

  2. --timefmt 时间格式    

  3. %y年 %m月 %d日 %H小时 %M分钟    

  4. --format 输出格式    

  5. %T时间 %w路径 %f文件名 %e状态    

  6. -m 始终保持监听状态,默认触发事件即退出。    

  7. -r 递归查询目录    

  8. -q 打印出监控事件    

  9. -e 定义监控的事件,可用参数:    

  10. open 打开文件    

  11. access 访问文件    

  12. modify 修改文件    

  13. delete 删除文件    

  14. create 新建文件    

  15. attrb  属性变更    

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ scripts]# vi ffile    

  2. /home/rain/    

  3. @/home/rain/cache    


[root@iZ25w1kdi5zZ scripts]# chmod a+x ./inotify_start.sh启动:

[root@iZ25w1kdi5zZ scripts]#  ./inotify_start.sh

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ ~]# cd /home/rain    

  2. [root@iZ25w1kdi5zZ rain]# mkdir yy  <span>  

  3. </span>  

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ rain]# cat /tmp/rsync.log    

  2. 16-01-02 16:21 yy CREATE,ISDIR   

七、附录

1、inotifywait

使用方法和参数说明:

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ bin]# ./inotifywait -h    

  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.         @<file>         Exclude the specified file from being watched.    

  8.         --exclude <pattern>    

  9.                         Exclude all events on files matching the    

  10.                         extended regular expression <pattern>.    

  11.         --excludei <pattern>    

  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 <file>    

  21.                         Read files to watch from <file> or `-' for stdin.    

  22.         -o|--outfile <file>    

  23.                         Print events to <file> 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 <fmt>  Print using a specified printf-like format    

  28.                         string; read the man page for more details.    

  29.         --timefmt <fmt> strftime-compatible format string for use with    

  30.                         %T in --format string.    

  31.         -c|--csv        Print events in CSV format.    

  32.         -t|--timeout <seconds>    

  33.                         When listening for a single event, time out after    

  34.                         waiting for an event for <seconds> seconds.    

  35.                         If <seconds> is 0, inotifywait will never time out.    

  36.         -e|--event <event1> [ -e|--event <event2> ... ]    

  37.                 Listen for specific event(s).  If omitted, all events are     

  38.                 listened for.    

  39.     

  40. Exit status:    

  41.         0  -  An event you asked to watch for was received.    

  42.         1  -  An event you did not ask to watch for was received    

  43.               (usually delete_self or unmount), or some error occurred.    

  44.         2  -  The --timeout option was given and no events occurred    

  45.               in the specified interval of time.    

  46.     

  47. Events:    

  48.         access          file or directory contents were read    

  49.         modify          file or directory contents were written    

  50.         attrib          file or directory attributes changed    

  51.         close_write     file or directory closed, after being opened in    

  52.                         writeable mode    

  53.         close_nowrite   file or directory closed, after being opened in    

  54.                         read-only mode    

  55.         close           file or directory closed, regardless of read/write mode    

  56.         open            file or directory opened    

  57.         moved_to        file or directory moved to watched directory    

  58.         moved_from      file or directory moved from watched directory    

  59.         move            file or directory moved to or from watched directory    

  60.         create          file or directory created within watched directory    

  61.         delete          file or directory deleted within watched directory    

  62.         delete_self     file or directory was deleted    

  63.         unmount         file system containing file or directory unmounted    



2、inotifywatch

使用方法和参数说明:

[plain] view plain copy
  1. [root@iZ25w1kdi5zZ bin]# ./inotifywatch -h    

  2. inotifywatch 3.14    

  3. Gather filesystem usage statistics using inotify.    

  4. Usage: inotifywatch [ options ] file1 [ file2 ] [ ... ]    

  5. Options:    

  6.         -h|--help       Show this help text.    

  7.         -v|--verbose    Be verbose.    

  8.         @<file>         Exclude the specified file from being watched.    

  9.         --fromfile <file>    

  10.                 Read files to watch from <file> or `-' for stdin.    

  11.         --exclude <pattern>    

  12.                 Exclude all events on files matching the extended regular    

  13.                 expression <pattern>.    

  14.         --excludei <pattern>    

  15.                 Like --exclude but case insensitive.    

  16.         -z|--zero    

  17.                 In the final table of results, output rows and columns even    

  18.                 if they consist only of zeros (the default is to not output    

  19.                 these rows and columns).    

  20.         -r|--recursive  Watch directories recursively.    

  21.         -t|--timeout <seconds>    

  22.                 Listen only for specified amount of time in seconds; if    

  23.                 omitted or 0, inotifywatch will execute until receiving an    

  24.                 interrupt signal.    

  25.         -e|--event <event1> [ -e|--event <event2> ... ]    

  26.                 Listen for specific event(s).  If omitted, all events are     

  27.                 listened for.    

  28.         -a|--ascending <event>    

  29.                 Sort ascending by a particular event, or `total'.    

  30.         -d|--descending <event>    

  31.                 Sort descending by a particular event, or `total'.    

  32.     

  33. Exit status:    

  34.         0  -  Exited normally.    

  35.         1  -  Some error occurred.    

  36.     

  37. Events:    

  38.         access          file or directory contents were read    

  39.         modify          file or directory contents were written    

  40.         attrib          file or directory attributes changed    

  41.         close_write     file or directory closed, after being opened in    

  42.                         writeable mode    

  43.         close_nowrite   file or directory closed, after being opened in    

  44.                         read-only mode    

  45.         close           file or directory closed, regardless of read/write mode    

  46.         open            file or directory opened    

  47.         moved_to        file or directory moved to watched directory    

  48.         moved_from      file or directory moved from watched directory    

  49.         move            file or directory moved to or from watched directory    

  50.         create          file or directory created within watched directory    

  51.         delete          file or directory deleted within watched directory    

  52.         delete_self     file or directory was deleted    

  53.         unmount         file system containing file or directory unmounted    


文章标签: linux

来自  https://blog.csdn.net/tongtong0704/article/details/70232812

普通分类: