inotify监控linux系统下的目录变化

最近,有个项目需要及时删除Nginx服务生成的缓存文件,由于不是很了解Nginx缓存生成的策略,在网上也没有仔细找,经过大家讨论,最终希望引入liunx的inotify功能,监控某个liunx目录下的各种事件(create,delete,access等等).
想了解inotify的朋友,请参考以下两篇博文:
1.[url]http://www.ibm.com/developerworks/cn/linux/l-inotify.html[/url]使用 inotify 监控 Linux 文件系统事件
2.[url]http://www.ibm.com/developerworks/cn/linux/l-inotifynew/index.html[/url]inotify -- 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的下载和安装
下载地址:[url]http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz?modtime=1199213676&big_mirror=0
[/url]
安装过程:略.

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

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

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秒内的访问和修改事件触发报表

% 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

#!/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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值