'File was accessed (read)', IN_MODIFY => 'File was modified', IN_ATTRIB => 'Metadata changed', IN_CLOSE_WRITE => 'File opened for writing was closed', IN_CLOSE_NOWRITE => 'File not opened for writing was closed', IN_OPEN => 'File was opened', IN_MOVED_TO => 'File moved into watched directory', IN_MOVED_FROM => 'File moved out of watched directory', IN_CREATE => 'File or directory created in watched directory', IN_DELETE => 'File or directory deleted in watched directory', IN_DELETE_SELF => 'Watched file or directory was deleted', IN_MOVE_SELF => 'Watch file or directory was moved', IN_CLOSE => 'Equals to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE', IN_MOVE => 'Equals to IN_MOVED_FROM | IN_MOVED_TO', IN_ALL_EVENTS => 'Bitmask of all the above constants', IN_UNMOUNT => 'File system containing watched object was unmounted', IN_Q_OVERFLOW => 'Event queue overflowed (wd is -1 for this event)', IN_IGNORED => 'Watch was removed (explicitly by inotify_rm_watch() or because file was removed or filesystem unmounted', IN_ISDIR => 'Subject of this event is a directory', IN_ONLYDIR => 'Only watch pathname if it is a directory', IN_DONT_FOLLOW => 'Do not dereference pathname if it is a symlink', IN_MASK_ADD => 'Add events to watch mask for this pathname if it already exists', IN_ONESHOT => 'Monitor pathname for one event, then remove from watch list.', 1073741840 => 'High-bit: File not opened for writing was closed', 1073741856 => 'High-bit: File was opened', 1073742080 => 'High-bit: File or directory created in watched directory', 1073742336 => 'High-bit: File or directory deleted in watched directory', ]; public $fds = []; public $paths = []; public $wds = []; public $timeout = 3; public $redis=null; public $db=1; public $key='redis_to_kafka'; public function __construct( $paths,$ip='127.0.0.1',$password='',$post=6379,$db='1',$key='redis_to_kafka'){ if (!empty($paths)) { foreach ($paths as $path) { if (file_exists($path)) { if (is_dir($path)) { $this->addDir($path); } else { $this->addFile($path); } } } } $this->redis = new Redis(); $this->db=$db; $this->key=$key; try{ $this->redis->pconnect($ip,$post,2.5); $this->redis->auth($password); //设置密码 $this->redis->select($this->db); $result = $this->redis->ping(); if($result=='+PONG') { DahuaUtil::rlog('redis连接成功'); } else { DahuaUtil::rlog("redis连接失败=>".$result.PHP_EOL); } //扫描未存起来的redis文件 DahuaUtil::rlog("扫描没有监听到的文件"); $this->scan_file(); DahuaUtil::rlog('已经调用了清理脚本'); }catch (Exception $e){ DahuaUtil::rlog("redis连接异常".$e->getMessage()); throw new Exception($e->getMessage()); } } public function __destruct( ){ if (!empty($this->fds)) { foreach ($this->fds as $fd) { fclose($fd); } } } public function addFile( $file ){ $file = realpath($file); $fd = inotify_init(); $fid = (int)$fd; //保存inotify资源 $this->fds[$fid] = $fd; //设置为非阻塞模式 stream_set_blocking($this->fds[$fid], 0); //保存文件路径 $this->paths[$fid] = $file; //保存监控描述�? $this->wds[$fid] =inotify_add_watch($this->fds[$fid], $file, self::MONITOR_EVENT); } public function addDir( $dir ){ $dir = realpath($dir); if ($dh = opendir($dir)) { //将目录加入监控中 $fd = inotify_init(); //一般文件的资源描述符是一个整形,可以用来当索�? $fid = (int)$fd; $this->fds[$fid] = $fd; stream_set_blocking($this->fds[$fid], 0); $this->paths[$fid] = $dir; $this->wds[$fid] = inotify_add_watch($this->fds[$fid], $dir, self::MONITOR_EVENT); //遍历目录下文 while (($file = readdir($dh)) !== false) { if ($file == '.' || $file == '..') { continue; } $file = $dir . DIRECTORY_SEPARATOR . $file; if (is_dir($file)) { $this->addDir($file); } } closedir($dh); } } public function remove( $fid ){ unset($this->paths[$fid]); fclose($this->fds[$fid]); unset($this->fds[$fid]); } public function run( ){ echo '我开始运行了'.PHP_EOL; while (true) { $reads = $this->fds; $write = []; $except = [];//异常事件 if (stream_select($reads, $write, $except, $this->timeout) > 0) { if (!empty($reads)) { foreach ($reads as $read) { //从可读流中读取数�? $events = inotify_read($read); //资源描述符,整形 $fid = (int)$read; //获取inotify实例的路�? $path = $this->paths[$fid]; foreach ($events as $event) { echo '监听读取'.PHP_EOL; $file = $path .'/' . $event['name']; if ($event['mask']==IN_CLOSE_WRITE) { echo 'create file->'.$file.PHP_EOL; //写入redis try { $this->set_redis($file); }catch(Exception $e) { DahuaUtil::rlog("redis数据库连接".$e->getMessage()); throw new Exception($e->getMessage()); } } //echo $event['name'], ' --- ', self::EVENT_MASK[$event['mask']], PHP_EOL; } } } if (!empty($except)) { DahuaUtil::rlog("监听异常".json_encode($except)); } } else { //echo '------------------', PHP_EOL; //echo '当前监控的路径列�?, PHP_EOL; //print_r($this->paths); //echo '------------------', PHP_EOL; } } } /** * function * 文件目录 * @param [type] $data * @return void */ public function set_redis($data) { $this->redis->ping(); if(strstr($data,'.zip')) { if(rename($data,$data.'.redis')) { $data=$data.'.redis'; $this->redis->lpush($this->key,$data); } } } //扫描目录下所有没有上传到redis文件 public function scan_file() { shell_exec("/home/wwwroot/nbfd_tp3/clear_file_redis.sh > /dev/null 2>&1 &"); // echo shell_exec("/home/wwwroot/nbfd_tp3/clear_file_redis.sh"); return; } }