`

使用kfsmd监控文件系统的变化

阅读更多

http://www.linux.com/feature/124903

 

Applications can ask the Linux kernel to report changes to selected files and directories. I created the Kernel Filesystem Monitoring Daemon (kfsmd) to make monitoring filesystem changes simple.

There are packages available for both 32- and 64-bit Fedora 7 and 8 and Ubuntu 7.10 Gutsy, as well as 32-bit packages for openSUSE 10.3. You can also download a repo file, which can be used with Fedora 8 and yum. Placing the repo file into /etc/yum.repos.d allows you to install kfsmd and its dependencies with yum install kfsmd on a Fedora 8 machine. You can also compile directly from source if that is your preference.

Command-line clients for kfsmd come in two categories: monitoring and logging. The monitoring client produces output on the console whenever something happens to a filesystem you are watching. You can log to either a Berkeley DB4 file or a PostgreSQL database.

The following session shows a simple directory monitoring session using kfsmd. It creates and populates a temporary directory, then starts kfsmd-cat to watch /tmp/k for any filesystem changes. The main command-line parameter is the watch command, which takes the directory or file to watch as a single argument. While kfsmd-cat was running, I opened a second terminal and created the df5.txt file and then removed it. These actions were reported to the console by kfsmd.

$ mkdir /tmp/k $ cd /tmp/k $ date > df1.txt $ date > df2.txt $ kernel-filesystem-monitor-daemon-cat -v watch . setting up watch for:. setting up watches calling run event on wd:1 . filename:df5.txt CLOSE URL:./df5.txt event on wd:1 . filename:df5.txt DELETE_FILE URL:./df5.txt

If you specify a directory to monitor with a full filesystem path, then kfsmd also monitors existing and newly created subdirectories by default. You can use the ignorepfx argument to limit these recursive monitors by explicitly telling kfsmd not to monitor some subdirectories. In the next example, which uses ignorepfx, I created two subdirectories inside /tmp/k: junk1 and subdir1. Both of the directory create and delete events were reported by kfsmd, and because of the ignorepfx argument, kfsmd did not monitor the /tmp/k/junk1 subdirectory itself, so files I created in that directory were not monitored and reported by kfsmd. Note that as ignorepfx is the prefix of a path, using just "junk" means that the subdirectory junk1 is not monitored.

$ kernel-filesystem-monitor-daemon-cat -v \ watch /tmp/k ignorepfx /tmp/k/junk event on wd:1 /tmp/k filename:junk1 CREATE URL:/tmp/k/junk1 event on wd:1 /tmp/k filename:subdir1 CREATE URL:/tmp/k/subdir1 should adding monitor for:subdir1 event on wd:2 /tmp/k/subdir1 filename:subfileA.txt CREATE URL:/tmp/k/subdir1/subfileA.txt event on wd:2 /tmp/k/subdir1 filename:subfileA.txt CLOSE URL:/tmp/k/subdir1/subfileA.txt event on wd:2 /tmp/k/subdir1 filename:subfileA.txt DELETE URL:/tmp/k/subdir1/subfileA.txt event on wd:1 /tmp/k filename:subdir1 DELETE URL:/tmp/k/subdir1 event on wd:1 /tmp/k filename:junk1 DELETE URL:/tmp/k/junk1

You can see that filesystem changes reported by kfsmd have a regular style. The primary report has the prefix "EVENT_TYPE URL:" where the event type is what happened to the file and the URL: string is used as a direct prefix to the file path being reported. This structure makes it convenient to use the kfsmd-cat command and pipe the output into a script that will perform some action for you automatically when files change.

The following script uses Perl to print the paths of files that are deleted in any monitored directory. It uses the pwd command on the first line of the command to make the paths reported by kfsmd absolute. The kfsmd-cat command will produce output similar to that shown above, and Perl code massages the output into a particular format, or can execute a command whenever a deletion happens. The script ignores all lines that do not start with DELET. Lines which report file deletion then have the prefix string "anything...URL:" stripped off so only the file path is printed to the console.

$ kernel-filesystem-monitor-daemon-cat watch `pwd` \ | perl -ne '{ if( /^DELET/ ) { s/.*URL://g; print; } }' /tmp/k/df5.txt

A second invocation of kfsmd-cat, shown below, sends an email message whenever a file is deleted in the current directory.

$ kernel-filesystem-monitor-daemon-cat watch `pwd` \ | kfsmd-sendemail.pl

The Perl script kfsmd-sendemail.pl is shown below. The three lines which you might have to change to use this yourself are listed at the top of the script; the FromAddress and ToAddress should be modified to suit your local environment.

#!/usr/bin/perl -n $Mailer = "| /usr/sbin/sendmail -t"; $FromAddress = 'ben@localhost'; $ToAddress = 'ben@localhost'; if( /^DELETE_/ ) { s/.*URL://g; chomp; $url=$_; $now=`date`; open MAIL,"$Mailer"; print MAIL <<THE_EMAIL; From: $FromAddress To: $ToAddress Subject: KFSMD: A file was deleted The file: $url Was deleted at $now THE_EMAIL close MAIL; }

Logging with kfsmd

To log filesystem events into a PostgreSQL database, use the kfsmd-postgresql command. Before using this command you must set up the database with the postgresql-schema.sql script. This can be done using the first command shown below. Note that the the database setup command only needs to be run once. The kfsmd-postgresql daemon will run in the background by default. You can set up watches with it in the same manner as for the kfsmd-cat command, though you must specify the database host and database name.

$ cat postgresql-schema.sql | psql -h myPostgreSQLServer $ kernel-filesystem-monitor-daemon-postgresql \ -h myPostgreSQLServer \ -d kernel_filesystem_monitor_daemon_postgresql watch `pwd`

With the command above, filesystem changes are logged to the PostgreSQL database called kernel_filesystem_monitor_daemon_postgresql on the server myPostgreSQLServer. You can query the database using SQL as shown below.

$ psql -h myPostgreSQLServer # \c kernel_filesystem_monitor_daemon_postgresql # select * from dirs d, events e where d.pwd = e.pwd order by time; pwd | url | id | mask | pwd | time | name -----+--------+----+------+-----+----------------------------+---------- 1 | /tmp/k | 2 | 512 | 1 | 2008-01-03 13:26:39.913456 | df11.txt 1 | /tmp/k | 1 | 8 | 1 | 2008-01-03 13:26:39.913456 | df11.txt 1 | /tmp/k | 3 | 8 | 1 | 2008-01-03 13:26:41.917512 | df21.txt

There are many situations where kfsmd is the right tool for the job. For example, if you are editing a file and wish to automatically publish it to a remote server each time it is saved, you can use kfsmd-cat and when a CLOSED event is detected execute a little script to rsync the file to the server. If you have a long-running task and wish to know when it is completed, just monitor for a filesystem change that occurs at the end of the process, such as when a file download or a build completes.

The kfsmd-postgresql and kfsmd-stldb4 commands allow you to easily record filesystem changes in a database, which is great for auditing what happened and when.

分享到:
评论

相关推荐

    实现坐标转换程序(C#桌面窗体)

    需手动输入坐标,实现ECEF空间直角坐标系等四种坐标之间相互转换。 具体代码流程可见主页文章。 通过下拉框,自由选择输入输出坐标系后,在左侧文本框手动输入一组或多组坐标,可以实现ECEF空间直角坐标系,ECEF球面坐标系,ECEF椭球坐标系(大地坐标系),站心坐标系四个坐标系统之间的自由转换,共12种转换方式。然后将转换后的坐标结果输入到右边文本框。 适用于GNSS相关课程的编程作业,测绘、地信等专业实践。 ------------------------------------------------------------------------------------------------------------------------- 该代码可能存在部分不足与漏洞。实际运行时没有发生过错误。

    基于matlab实现直角坐标系下的牛顿拉夫逊潮流计算MATLAB程序.rar

    基于matlab实现直角坐标系下的牛顿拉夫逊潮流计算MATLAB程序.rar

    基于matlab实现麦克风阵列SRP-PHAT算法的二级空间快速声源定位,含模拟环境 .rar

    基于matlab实现麦克风阵列SRP-PHAT算法的二级空间快速声源定位,含模拟环境。.rar

    node-v10.16.0-linux-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    dephi+sqlserver2000题库与试卷生成系统.zip

    dephi+sqlserver2000题库与试卷生成系统.zip

    node-v10.24.0-darwin-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    ASP+ACCESS网页设计辅导系统(源代码+设计说明书).zip

    ASP+ACCESS网页设计辅导系统(源代码+设计说明书).zip

    中考实验视频.zip

    中考实验视频.zip

    基于VB+access实现的学生成绩管理系统(开题报告+答辩PPT+论文+系统).zip

    基于VB+access实现的学生成绩管理系统(开题报告+答辩PPT+论文+系统) 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。

    基于matlab实现模拟退火程序 f(x,y) = 5sin(xy) + x^2 + y^2的最小值

    基于matlab实现模拟退火程序。f(x,y) = 5sin(xy) + x^2 + y^2的最小值,对理解模拟退火算法是一个很好的程序示例.rar

    node-v5.4.1.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    ASP+access网上人才信息管理系统毕业设计(源代码+设计说明书).zip

    ASP+access网上人才信息管理系统毕业设计(源代码+设计说明书).zip

    node-v10.22.1-linux-ppc64le.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v12.16.3-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    CASS工艺参数设计计算表.xls

    污水处理计算书

    ASP+ACCESS文学网站建设设计(源代码+设计说明书+系统).zip

    ASP+ACCESS文学网站建设设计(源代码+设计说明书+系统).zip

    http代理服务器的实现(程序).zip

    http代理服务器的实现(程序).zip

    node-v6.14.4-x64.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    JSP教学管理系统设计(源代码+设计说明书).zip

    JSP教学管理系统设计(源代码+设计说明书).zip

    2024年木纹砖行业分析报告.pptx

    行业报告

Global site tag (gtag.js) - Google Analytics