您的位置:首页 > 运维架构 > Linux

使用inotify实现linux系统下的目录变化监控

2013-09-13 12:22 731 查看
近期,有个项目须要及时删除Nginx服务生成的缓存文件,因为不是非常了解Nginx缓存生成的策略,在网上也沒有细致找,经过大家讨论,终于希望引入liunx的inotify功能,监控某个liunx文件夹下的各种事件(create,delete,access等等).
想了解inotify的朋友,请参考下面两篇博文:
1.http://www.ibm.com/developerworks/cn/linux/l-inotify.html使用 inotify 监控 Linux 文件系统事件
2.http://www.ibm.com/developerworks/cn/linux/l-inotifynew/index.htmlinotify -- Linux 2.6 内核中的文件系统变化通知机制

假设看完两篇博文,你的想法是用C语言立即写一个监控文件的程序(我当初也这么想的),先别忙,看看下面的文章,立即向您介绍一下inotify-tools这个工具包,眼下最新版是3.3版本号,这个工具包差点儿包括了文件夹和文件的监控点,也就是说,不用动手写C代码,已经有前人帮我写好了,我们能够直接通过bash脚本的调用完成这个功能.

1、先查看linux的内核是否支持inotify,支持inotify的内核最小为2.6.13,输入命令:uname –a。例如以下图所看到的,内核为2.6.27,应该支持inotify.假设不支持,我建议你选择一个高级别的linux内核.否则应该会有非常多麻烦.

2、还能够通过例如以下命令查看系统是否支持inotify:ll /proc/sys/fs/inotify
假设有例如以下输出,表示系统内核已经支持inotify:
total 0
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_queued_events
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_instances
-rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_watches

3.inotify-tools的下载和安装
下载地址:http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz?modtime=1199213676&big_mirror=0

安装过程:略.

4.内部命令介绍
系统下运行命令:man inotify、 man inotifywait、 man inotifywatch就可以得到对应的帮助信息,表示inotify成功安装。

man inotify:
捕获文件系统的各种状态事件

Java代码


inotify events

Bit Description

IN_ACCESS File was accessed (read) (*)

IN_ATTRIB Metadata changed (permissions, timestamps,

extended attributes, etc.) (*)

IN_CLOSE_WRITE File opened for writing was closed (*)

IN_CLOSE_NOWRITE File not opened for writing was closed (*)

IN_CREATE File/directory created in watched directory (*)

IN_DELETE File/directory deleted from watched directory (*)

IN_DELETE_SELF Watched file/directory was itself deleted

IN_MODIFY File was modified (*)

IN_MOVE_SELF Watched file/directory was itself moved

IN_MOVED_FROM File moved out of watched directory (*)

IN_MOVED_TO File moved into watched directory (*)

IN_OPEN File was opened (*)

man inotifywait:
等待并监控某个文件夹或文件的状态改变,能够适时的通过liunx脚本等待并监控文件改变的事件,能够在事件发生时退出脚本,也能够在事件发生时输出一些信息.

参数解释:

--fromfile <file> 仅仅监控文件夹下文件状态的变化
-m, --monitor 当事件发生后直接运行退出,-m 参数将不退出当前的shell脚本.
-r, --recursive 递归监控当前文件夹下的全部文件和文件夹.(默认的文件和文件夹数最大是 8192个;假设不满足能够改动/proc/sys/fs/inotify/max_user_watches
--exclude <pattern> 通过正则匹配文件名称,大写和小写敏感.
--excludei <pattern> 通过正则匹配文件名称,大写和小写不敏感.
-t <seconds> 事件发生时的秒数.
-e <event> 监听那些事件的发生
--timefmt option 指定输出的时间格式
--format <fmt> 输出指定时间格式.
%w 监控事件发生时的文件名称或文件路径
%f 监控文件夹内部事件发生时文件名称称
%e 监控指定的事件发生
%T 输出事件发生时的时间,--timefmt option指定格式

inotifywatch:
使用linux的inotify特性监控某段时间内的文件状态,并输出摘要报表.
例子:输出beagle文件夹下60秒内的訪问和改动事件触发报表

Java代码


% inotifywatch -v -e access -e modify -t 60 -r ~/.beagle

Establishing watches...

Setting up watch(es) on /home/rohan/.beagle

OK, /home/rohan/.beagle is now being watched.

Total of 302 watches.

Finished establishing watches, now collecting statistics.

Will listen for events for 60 seconds.

total access modify filename

1436 1074 362 /home/rohan/.beagle/Indexes/FileSystemIndex/PrimaryIndex/

1323 1053 270 /home/rohan/.beagle/Indexes/FileSystemIndex/SecondaryIndex/

303 116 187 /home/rohan/.beagle/Indexes/KMailIndex/PrimaryIndex/

261 74 187 /home/rohan/.beagle/TextCache/

206 0 206 /home/rohan/.beagle/Log/

42 0 42 /home/rohan/.beagle/Indexes/FileSystemIndex/Locks/

18 6 12 /home/rohan/.beagle/Indexes/FileSystemIndex/

12 0 12 /home/rohan/.beagle/Indexes/KMailIndex/Locks/

3 0 3 /home/rohan/.beagle/TextCache/54/

3 0 3 /home/rohan/.beagle/TextCache/bc/

3 0 3 /home/rohan/.beagle/TextCache/20/

3 0 3 /home/rohan/.beagle/TextCache/62/

2 2 0 /home/rohan/.beagle/Indexes/KMailIndex/SecondaryIndex/

编写自己的监控脚本:
需求:因为使用Nginx的反向代理,生成本地缓存的策略,所以须要监控某个文件夹的新增或删除的变化,并将变化的文件名称称输出到一个LOG中,带后续文件有改动时,能够通过该log定位文件地址,并删除该文件,及时向前端反映文件变更后的变化.
脚本; inodify_cache_list.sh

Java代码


#!/bin/sh

# A slightly complex but actually useful example

logfile="/opt/data/cache_list.txt"

temp_logfile="/opt/data/cache_tempfile.txt"

/usr/local/bin/inotifywait -mrq --format '%w%f' -e moved_to /opt/data/proxy_cache_dir/| while read file;

do

echo "/usr/bin/printf \"delete "`grep -a 'KEY:' ${file}| sed -e s/KEY://g;`"\\r\\n\" | nc 127.0.0.1 11211,rm -f "${file} |tee -a $logfile | tee -a $temp_logfile

done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: