Linux 下监控某个文件、目录读写次数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/signmem/article/details/91413486

目的

在 Linux 下监控某个文件或者某个目录下所有文件的读写

软件下载

inotify-tools 下载

编译方法

如果缺少编译包,自行利用 yum 安装编译软件, 如 gcc, make 等 rpm

参考下面编译安装步骤

tar xf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13/
./configure
make
make install

命令作用

inotifywatch

可以独立监控某个文件的读写访问次数
可以指定时间(秒)内的访问次数
可以针对文件,目录事件变化进行指定监控

指定事件

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

命令例子

指定某个目录在 60 秒内发生的读写变化

[root@mycomputer commitlogs]# inotifywatch -v -e access -e modify -t 60 -r /apps/dat/m3db/commitlogs/*
Establishing watches...
Setting up watch(es) on /apps/dat/m3db/commitlogs/commitlog-0-22023.db
OK, /apps/dat/m3db/commitlogs/commitlog-0-22023.db is now being watched.
Setting up watch(es) on /apps/dat/m3db/commitlogs/commitlog-0-22024.db
OK, /apps/dat/m3db/commitlogs/commitlog-0-22024.db is now being watched.
Setting up watch(es) on /apps/dat/m3db/commitlogs/commitlog-0-22025.db
OK, /apps/dat/m3db/commitlogs/commitlog-0-22025.db is now being watched.
Total of 3 watches.
Finished establishing watches, now collecting statistics.
Will listen for events for 60 seconds.
total  modify  filename
426    426     /apps/dat/m3db/commitlogs/commitlog-0-22024.db

上面例子说明当前目录只有一个文件在 60 秒内发生了 426 次的文件修改操作

展开阅读全文

没有更多推荐了,返回首页