Maison  >  Article  >  développement back-end  >  linux文件文件夹递归监控

linux文件文件夹递归监控

高洛峰
高洛峰original
2016-11-02 10:47:101234parcourir

linux文件文件夹递归监控

开发了一个规范的php composer包,使用的时候直接require即可。

实现

php版本的实现沿用了shell版本的思想,通过管道命令传递shell命令的结果,然后对结果做各种处理,达到我们监控文件的目的。

为了完成这个功能,首先要实现一个php版本的管道命令,这里我对resource popen ( string $command , string $mode )做了封装,可以通过很友好的处理命令的返回值。

完成了上面的模块,接下来就是具体的实现了,实现这里有多重友好的方式,添加多路径递归监控、包含正则匹配、排除正则匹配。

pipe的开发

<?php/** * php对对popen的封装,通过回调的方式模拟管道命令 * */namespace Aizuyan\Pipe;class Pipe{    /**     * 要通过管道执行的命令     */
    protected $command = "";
    /**     * 回调函数,将管道数据传递给该函数     */
    protected $callback = null;
    /**     * 数据之间的分隔符     */
    protected $delimiter = "\n";
    /**     * 设置命令     *     * @param cmd string 要运行的命令     */
    public function setCmd($cmd)
    {        $this->command = $cmd;
        return $this;
    }    /**     * 设置回调函数,处理管道输出的命令     */
    public function setCallback(callable $cb)
    {        $this->callback = $cb;
        return $this;
    }    /**     * 设置数据片段之间的分隔符     */
    public function setDelimiter($delimiter)
    {        $this->delimiter = $delimiter;
        return $this;
    }    /**     * 开始运行     */
    public function run()
    {        $fp = popen($this->command, "r");
        if (false === $fp) {            throw new \RuntimeException("popen execute command failed!");
        }        $item = "";
        while (!feof($fp)) {            $char = fgetc($fp);
            if ($this->delimiter == $char) {                call_user_func($this->callback, $item);
                $item = "";
            } else {                $item .= $char;
            }
        }        pclose($fp);
    }
}

下面是测试程序

include "vendor/autoload.php";
$test = new \Ritoyan\Pipe\Pipe();
$test->setCmd("tail -f /root/t.txt")->setCallback(function($item){
       echo "获取内容:" . $item . "\n";
})->setDelimiter("\n")->run();

对tail -f /root/t.txt这个shell命令的返回结果进行实时处理,setCallback(callable func)设置了回调函数,func的参数是shell命令的标准输出,setDelimiter($delimiter)设置了传入回调函数参数的分隔符,这样就可以很容易、很任性的将输出传递给回调函数了。

inotify开发

这部分代码就不提出来了,主要就是依赖上面的Pipe然后对inotifywait -mrq --format '%w,%e,%f'命令的输出做处理,正向逆向过滤,下面是对Inotify的调用

<?php
require "vendor/autoload.php";
$obj = new Aizuyan\Inotify\Inotify();
$obj->addExclude([
    "/swp$/",
    "/swpx$/",
    "/~$/",
    "/\d$/",
    "/swx$/"
])->setCallback(function ($item){
    echo $item["event"] . " 文件 " . $item["file"] . "\n";
})->addPaths("/datas/git/")->start();

这样运行之后,到我们修改/datas/git目录下的文件的时候会输出下面的内容,可以很方便的对修改文件做定制化的处理

CREATE 文件 /datas/git/inotify/README.md
MODIFY 文件 /datas/git/inotify/README.md
MOVED_TO 文件 /datas/git/aizuyan/pinyin-1/README.md
DELETE 文件 /datas/git/aizuyan/pinyin-1/LICENSE
......

inotify-tools安装

整个功能依赖于一个linux软件 —— inotify-tools
centos安装yum install inotify-tools,或者通过源码直接安装(文档)尝试在OS中安装,发现失败了~

如何使用

我已经将他发布到了composer仓库中,可以轻松安装:
composer require aizuyan/inotify,之后就可以像上面的例子一样使用了

另外这是开发的两个组件的github地址:Aizuyan\Pipe\Pipe , Aizuyan\Inotify\Inotify


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:linux下yum安装PHP APCArticle suivant:Linux C代码实现cgi shell