如果想在Linux上监控文件系统的变化,如访问属性、读写属性、权限属性、删除创建、移动等操作。可以考虑使用inotify-tools 工具,inotify-tools 是一个C库和一组命令行的工作提供Linux下inotify的简单接口。下面对inotify-tools讲解。
inotify-tools
inotify是一个API,需要通过开发应用程序进行调用,对于大多数用户来讲这有着许多不便,inotify-tools的出现弥补了这一不足。inotify-tools是一套组件,它包括一个C库和几个命令行工具,这些命令行工具可用于通过命令行或脚本对某文件系统的事件进行监控。
inotifywait命令 可以用来收集有关文件访问信息,Linux发行版一般没有包括这个命令,需要安装inotify-tools,这个命令还需要将inotify支持编译入Linux内核,好在大多数Linux发行版都在内核中启用了inotify。
inotifywatch命令 用于收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。
开始之前需要检测系统内核是否支持inotify,使用命令如下uname -a
用uname -a命令检查Linux内核,如果低于2.6.13,就需要重新编译内核加入inotify的支持。使用ll /proc/sys/fs/inotify命令,是否有以下三条信息输出,如果没有表示不支持。
如果想要在Linux发行版上,查看是否安装inotify-tools,使用命令如下。
rpm -qa inotify-tools
如果没有安装,可以使用yum install inotify-tools -y
yum -y install inotify-tools
看到到上面的信息,说明安装成功。安装成功后notify-tools提供的两个命令行工具:
inotifywait:通过inotify API等待被监控文件上的相应事件并返回监控结果,默认情况下,正常的结果返回至标准输出,诊断类的信息则返回至标准错误输出。它可以在监控到对应监控对象上指定的事件后退出,也可以进行持续性的监控。
inotifywatch:通过inotify API收集被监控文件或目录的相关事件并输出统计信息。
inotifywait使用介绍
inotifywait 仅执行阻塞,等待 inotify 事件,你可以使用它来监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等),并且可以结合 shell 脚本,更好的使用 inotifywait。来看看inotifywait如何使用:
inotifywait [-hcmrq] [-e ] [-t ] [–format ] [–timefmt ] [ … ]
inotifywait -h
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ … ]
Options:
-h|–help Show this help text.
@ Exclude the specified file from being watched.
–exclude
Exclude all events on files matching the
extended regular expression .
–excludei
Like --exclude but case insensitive.
-m|–monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|–daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|–recursive Watch directories recursively.
–fromfile
Read files to watch from or `-’ for stdin.
-o|–outfile
Print events to rather than stdout.
-s|–syslog Send errors to syslog rather than stderr.
-q|–quiet Print less (only print events).
-qq Print nothing (not even events).
–format Print using a specified printf-like format
string; read the man page for more details.
–timefmt strftime-compatible format string for use with
%T in --format string.
-c|–csv Print events in CSV format.
-t|–timeout
When listening for a single event, time out after
waiting for an event for seconds.
If is 0, inotifywait will never time out.
-e|–event [ -e|–event … ]
Listen for specific event(s). If omitted, all events are
listened for.
Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error 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
writeable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/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 from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted
实例
inotifywait 实时监控目录
在命令行上运行inotifywait,等待“/home/minger/share/tencent/rand/”目录中的任何文件被访问。运行inotifywait后,如果改变/home/minger/share/tencent/rand/
目录下的文件大小,则会弹出:
实时对 /data/web 目录进行监控
实现对 /data/web 目录进行监控,监控文件删除,修改,创建和权限相关事件,并且要求将监控信息写入/var/log/web_watch.log。要求日志条目要清晰明了,能突显文件路径、事件名和时间。
#!/bin/bash
inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f %e' --event delete,modify,create,attrib /data/web | while read date time file event
do
case $event in
MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
echo $event'-'$file'-'$date'-'$time >> /var/log/web_watch.log
;;
MOVED_FROM|MOVED_FROM,ISDIR|DELETE|DELETE,ISDIR)
echo $event'-'$file'-'$date'-'$time /var/log/web_watch.log
;;
esac
done
运行输出:
从上面的信息来看,如果在/data/web目录上删除,修改,创建文件等操作,则会打印信息到/var/log/web_watch.log中。
总结
inotify-tools是为linux下inotify文件监控工具提供的一套C的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。 inotify-tools是用C编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。更多的使用方式,请查看 man手册。
欢迎关注公众号【程序猿编码】,添加本人微信号(17865354792),回复:领取学习资料。或者回复:进入技术交流群。网盘资料有如下: