您的位置:首页 > 大数据 > 人工智能

inotifywait监控系列之一:对文件和目录访问进行记录 推荐

2013-03-03 15:51 429 查看
linux下有一个命令inotifywait,可以对文件及目录的访问进行记录,默认的系统中没有这个命令,需要安装,在内核版本高于2.6.13
inotifywait
是一个可以实时监控文件变动的工具,它利用linux内核中的inotify机制实现监控功能,在内核高于2.6.13的版本中,据说不需要软件的安装包,OK,下面就让我们开始inotify的体验之旅吧:
[root@World77 ~]# uname  -a
Linux World77 2.6.32-042stab068.8 #1 SMP Fri Dec 7 17:06:14 MSK 2012 i686 i686 i386 GNU/Linux


我的内核版本比较高,可惜的是不能用yum来安装,心里老郁闷了,可能是我的yum源的问题,不管了,那就下载源码去安装,反正一样。
http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
下载之后解压缩然后 :configuration ,make make install 一切都很正常。
怀着激动的心情,敲下了inotifywait

1: [root@World77 ~]# inotifywait –m /root
2: inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot                                                                              open shared object file: No such file or directory

居然报错,那里出问题了。。。。
我找找貌似库文件有问题,记得那里看到过这个问题,但是看到这类问题很纠结,后来不知道从那位前辈的博客上看到了这种问题的万能解决办法,下面来看看我是如何解决这种问题的,哈哈,老鸟勿喷啊。。。

1: [root@World77 ~]# find / -name libinotifytools.so.0
2: /usr/local/lib/libinotifytools.so.0
3: /root/inotify-tools-3.14/libinotifytools/src/.libs/libinotifytools.so.0
4: [root@World77 ~]# cp /root/inotify-tools-3.14/libinotifytools/src/.libs/libinoti                                                                             fytools.so.0 /usr/lib
5: [root@World77 ~]# cp /root/inotify-tools-3.14/libinotifytools/src/.libs/libinoti                                                                             fytools.so.0 /usr/local/lib/
6: cp: overwrite `/usr/local/lib/libinotifytools.so.0'? y


在源码文件夹中找到库文件,覆盖就可以了,哈哈,果然是万能的方法啊,但是这个vps是我练习所用,所谓的万能的法子,有坑能对您不适应,尤其是生产机器中,千万小心,弱出事情,本人概不负责。。。好了,下面让我们来看看实际效果吧

1: [root@World77 ~]# inotifywait -rm /root
2: Setting up watches.  Beware: since -r was given, this may take a while!
3: Watches established.
4: /root/ OPEN .bash_profile
5: /root/ ACCESS .bash_profile
6: /root/ CLOSE_NOWRITE,CLOSE .bash_profile
7: /root/ OPEN .bashrc
8: /root/ ACCESS .bashrc
9: /root/ CLOSE_NOWRITE,CLOSE .bashrc
10: /root/ OPEN .bash_history
11: /root/ ACCESS .bash_history
12: /root/ CLOSE_NOWRITE,CLOSE .bash_history
13: /root/ OPEN .bash_history
14: /root/ ACCESS .bash_history
15: /root/ CLOSE_NOWRITE,CLOSE .bash_history
16: /root/ OPEN,ISDIR
17: /root/ CLOSE_NOWRITE,CLOSE,ISDIR
18: /root/ OPEN,ISDIR
19: /root/ CLOSE_NOWRITE,CLOSE,ISDIR
20: /root/ OPEN,ISDIR meinv
21: /root/meinv/ OPEN,ISDIR
22: /root/ CLOSE_NOWRITE,CLOSE,ISDIR meinv
23: /root/meinv/ CLOSE_NOWRITE,CLOSE,ISDIR
24: /root/ OPEN safe.sh
25: /root/ ACCESS safe.sh
26: /root/ MODIFY BlackIP.txt
27: /root/ OPEN BlackIP.txt
28: /root/ MODIFY BlackIP.txt
29: /root/ CLOSE_WRITE,CLOSE BlackIP.txt
30: /root/ ACCESS safe.sh
31: /root/ OPEN BlackIP.txt


上述效果是我打开另外一个putty重新登录服务器的时候,在一个终端内显示的监控效果,哈哈,果然不错,顿时开始性奋了,哈哈。。。
inotifywait 命令中的 -m 选项表示 monitor ,即开启监视。-r 选项表示递归监视,比如上面监视整个 /root
目录,如果在其中的子目录下修改文件也是能被观察到的。
还可以用 -e 选项指定要监控的“事件”(events),比如:

1: inotifywait -rme modify,attrib,move,close_write,create,delete,delete_self > /root/test.txt


如果不加参数-e的话,默认就是监控所有的事件,在运维服务器的时候,这个工具可以帮助你监控服务器上重要的配置文件和重要的目录,哈哈,怎么样,强大吧,如果感兴趣的话,还不赶快去实验一下,往后会介绍更高级的应用,如何把监控记录到数据库是下一个系列,敬请期待
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐