InotifyMonitor.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. require 'DahuaUtil.php';
  3. class InotifyMonitor {
  4. const MONITOR_EVENT = IN_CLOSE_WRITE;
  5. const EVENT_MASK = [
  6. IN_ACCESS => 'File was accessed (read)',
  7. IN_MODIFY => 'File was modified',
  8. IN_ATTRIB => 'Metadata changed',
  9. IN_CLOSE_WRITE => 'File opened for writing was closed',
  10. IN_CLOSE_NOWRITE => 'File not opened for writing was closed',
  11. IN_OPEN => 'File was opened',
  12. IN_MOVED_TO => 'File moved into watched directory',
  13. IN_MOVED_FROM => 'File moved out of watched directory',
  14. IN_CREATE => 'File or directory created in watched directory',
  15. IN_DELETE => 'File or directory deleted in watched directory',
  16. IN_DELETE_SELF => 'Watched file or directory was deleted',
  17. IN_MOVE_SELF => 'Watch file or directory was moved',
  18. IN_CLOSE => 'Equals to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE',
  19. IN_MOVE => 'Equals to IN_MOVED_FROM | IN_MOVED_TO',
  20. IN_ALL_EVENTS => 'Bitmask of all the above constants',
  21. IN_UNMOUNT => 'File system containing watched object was unmounted',
  22. IN_Q_OVERFLOW => 'Event queue overflowed (wd is -1 for this event)',
  23. IN_IGNORED => 'Watch was removed (explicitly by inotify_rm_watch() or because file was removed or filesystem unmounted',
  24. IN_ISDIR => 'Subject of this event is a directory',
  25. IN_ONLYDIR => 'Only watch pathname if it is a directory',
  26. IN_DONT_FOLLOW => 'Do not dereference pathname if it is a symlink',
  27. IN_MASK_ADD => 'Add events to watch mask for this pathname if it already exists',
  28. IN_ONESHOT => 'Monitor pathname for one event, then remove from watch list.',
  29. 1073741840 => 'High-bit: File not opened for writing was closed',
  30. 1073741856 => 'High-bit: File was opened',
  31. 1073742080 => 'High-bit: File or directory created in watched directory',
  32. 1073742336 => 'High-bit: File or directory deleted in watched directory',
  33. ];
  34. public $fds = [];
  35. public $paths = [];
  36. public $wds = [];
  37. public $timeout = 3;
  38. public $redis=null;
  39. public function __construct( $paths,$ip='127.0.0.1',$password='',$post=6379){
  40. if (!empty($paths)) {
  41. foreach ($paths as $path) {
  42. if (file_exists($path)) {
  43. if (is_dir($path)) {
  44. $this->addDir($path);
  45. } else {
  46. $this->addFile($path);
  47. }
  48. }
  49. }
  50. }
  51. $this->redis = new Redis();
  52. try{
  53. $this->redis->pconnect($ip,$post,2.5);
  54. $this->redis->auth($password); //设置密码
  55. $this->redis->select(1);
  56. $result = $this->redis->ping();
  57. if($result=='+PONG')
  58. {
  59. DahuaUtil::rlog('redis连接成功');
  60. }
  61. else
  62. {
  63. DahuaUtil::rlog("redis连接失败=>".$result.PHP_EOL);
  64. }
  65. //扫描未存起来的redis文件
  66. DahuaUtil::rlog("扫描没有监听到的文件");
  67. $this->scan_file();
  68. DahuaUtil::rlog('已经调用了清理脚本');
  69. }catch (Exception $e){
  70. DahuaUtil::rlog("redis连接异常".$e->getMessage());
  71. }
  72. }
  73. public function __destruct( ){
  74. if (!empty($this->fds)) {
  75. foreach ($this->fds as $fd) {
  76. fclose($fd);
  77. }
  78. }
  79. }
  80. public function addFile( $file ){
  81. $file = realpath($file);
  82. $fd = inotify_init();
  83. $fid = (int)$fd;
  84. //保存inotify资源
  85. $this->fds[$fid] = $fd;
  86. //设置为非阻塞模式
  87. stream_set_blocking($this->fds[$fid], 0);
  88. //保存文件路径
  89. $this->paths[$fid] = $file;
  90. //保存监控描述�?
  91. $this->wds[$fid] =inotify_add_watch($this->fds[$fid], $file, self::MONITOR_EVENT);
  92. }
  93. public function addDir( $dir ){
  94. $dir = realpath($dir);
  95. if ($dh = opendir($dir)) {
  96. //将目录加入监控中
  97. $fd = inotify_init();
  98. //一般文件的资源描述符是一个整形,可以用来当索�?
  99. $fid = (int)$fd;
  100. $this->fds[$fid] = $fd;
  101. stream_set_blocking($this->fds[$fid], 0);
  102. $this->paths[$fid] = $dir;
  103. $this->wds[$fid] = inotify_add_watch($this->fds[$fid], $dir, self::MONITOR_EVENT);
  104. //遍历目录下文
  105. while (($file = readdir($dh)) !== false) {
  106. if ($file == '.' || $file == '..') {
  107. continue;
  108. }
  109. $file = $dir . DIRECTORY_SEPARATOR . $file;
  110. if (is_dir($file)) {
  111. $this->addDir($file);
  112. }
  113. }
  114. closedir($dh);
  115. }
  116. }
  117. public function remove( $fid ){
  118. unset($this->paths[$fid]);
  119. fclose($this->fds[$fid]);
  120. unset($this->fds[$fid]);
  121. }
  122. public function run( ){
  123. echo '我开始运行了'.PHP_EOL;
  124. while (true) {
  125. $reads = $this->fds;
  126. $write = [];
  127. $except = [];//异常事件
  128. if (stream_select($reads, $write, $except, $this->timeout) > 0) {
  129. if (!empty($reads)) {
  130. foreach ($reads as $read) {
  131. //从可读流中读取数�?
  132. $events = inotify_read($read);
  133. //资源描述符,整形
  134. $fid = (int)$read;
  135. //获取inotify实例的路�?
  136. $path = $this->paths[$fid];
  137. foreach ($events as $event) {
  138. echo '监听读取'.PHP_EOL;
  139. $file = $path .'/' . $event['name'];
  140. if ($event['mask']==IN_CLOSE_WRITE) {
  141. echo 'create file->'.$file.PHP_EOL;
  142. //写入redis
  143. $this->set_redis($file);
  144. }
  145. //echo $event['name'], ' --- ', self::EVENT_MASK[$event['mask']], PHP_EOL;
  146. }
  147. }
  148. }
  149. if (!empty($except)) {
  150. DahuaUtil::rlog("监听异常".json_encode($except));
  151. }
  152. } else {
  153. //echo '------------------', PHP_EOL;
  154. //echo '当前监控的路径列�?, PHP_EOL;
  155. //print_r($this->paths);
  156. //echo '------------------', PHP_EOL;
  157. }
  158. }
  159. }
  160. /**
  161. * function
  162. * 文件目录
  163. * @param [type] $data
  164. * @return void
  165. */
  166. public function set_redis($data)
  167. {
  168. if(!strstr($data,'md5'))
  169. {
  170. if(rename($data,$data.'.redis'))
  171. {
  172. $data=$data.'.redis';
  173. }
  174. $this->redis->lpush('redis_to_kafka',$data);
  175. }
  176. }
  177. //扫描目录下所有没有上传到redis文件
  178. public function scan_file()
  179. {
  180. shell_exec("/home/wwwroot/nbfd_tp3/clear_file_redis.sh > /dev/null 2>&1 &");
  181. //echo shell_exec("/home/wwwroot/nbfd_tp3/clear_file_redis.sh");
  182. return;
  183. }
  184. }