InotifyMonitor.php 8.6 KB

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