InotifyMonitor.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. echo "redis连接成功";
  60. DahuaUtil::rlog('redis连接成功');
  61. }
  62. else
  63. {
  64. DahuaUtil::rlog("redis连接失败=>".$result.PHP_EOL);
  65. echo "redis连接失败=>".$result.PHP_EOL;
  66. }
  67. //扫描未存起来的redis文件
  68. DahuaUtil::rlog("扫描没有监听到的文件");
  69. $this->scan_file($paths[0]);
  70. }catch (Exception $e){
  71. DahuaUtil::rlog("redis连接异常".$e->getMessage());
  72. }
  73. }
  74. public function __destruct( ){
  75. if (!empty($this->fds)) {
  76. foreach ($this->fds as $fd) {
  77. fclose($fd);
  78. }
  79. }
  80. }
  81. public function addFile( $file ){
  82. $file = realpath($file);
  83. $fd = inotify_init();
  84. $fid = (int)$fd;
  85. //保存inotify资源
  86. $this->fds[$fid] = $fd;
  87. //设置为非阻塞模式
  88. stream_set_blocking($this->fds[$fid], 0);
  89. //保存文件路径
  90. $this->paths[$fid] = $file;
  91. //保存监控描述�?
  92. $this->wds[$fid] =inotify_add_watch($this->fds[$fid], $file, self::MONITOR_EVENT);
  93. }
  94. public function addDir( $dir ){
  95. $dir = realpath($dir);
  96. if ($dh = opendir($dir)) {
  97. //将目录加入监控中
  98. $fd = inotify_init();
  99. //一般文件的资源描述符是一个整形,可以用来当索�?
  100. $fid = (int)$fd;
  101. $this->fds[$fid] = $fd;
  102. stream_set_blocking($this->fds[$fid], 0);
  103. $this->paths[$fid] = $dir;
  104. $this->wds[$fid] = inotify_add_watch($this->fds[$fid], $dir, self::MONITOR_EVENT);
  105. //遍历目录下文
  106. while (($file = readdir($dh)) !== false) {
  107. if ($file == '.' || $file == '..') {
  108. continue;
  109. }
  110. $file = $dir . DIRECTORY_SEPARATOR . $file;
  111. if (is_dir($file)) {
  112. $this->addDir($file);
  113. }
  114. }
  115. closedir($dh);
  116. }
  117. }
  118. public function remove( $fid ){
  119. unset($this->paths[$fid]);
  120. fclose($this->fds[$fid]);
  121. unset($this->fds[$fid]);
  122. }
  123. public function run( ){
  124. echo '我开始运行了'.PHP_EOL;
  125. while (true) {
  126. $reads = $this->fds;
  127. $write = [];
  128. $except = [];//异常事件
  129. if (stream_select($reads, $write, $except, $this->timeout) > 0) {
  130. if (!empty($reads)) {
  131. foreach ($reads as $read) {
  132. //从可读流中读取数�?
  133. $events = inotify_read($read);
  134. //资源描述符,整形
  135. $fid = (int)$read;
  136. //获取inotify实例的路�?
  137. $path = $this->paths[$fid];
  138. foreach ($events as $event) {
  139. echo '监听读取'.PHP_EOL;
  140. $file = $path .'/' . $event['name'];
  141. if ($event['mask']==IN_CLOSE_WRITE) {
  142. echo 'create file->'.$file.PHP_EOL;
  143. //写入kafka
  144. $this->set_redis($file);
  145. //$this->postData($data);
  146. }
  147. //echo $event['name'], ' --- ', self::EVENT_MASK[$event['mask']], PHP_EOL;
  148. }
  149. }
  150. }
  151. if (!empty($except)) {
  152. //debug_log('push_vidieo_images_log',json_encode($except));//记录下异常事�?
  153. }
  154. } else {
  155. //echo '------------------', PHP_EOL;
  156. //echo '当前监控的路径列�?, PHP_EOL;
  157. //print_r($this->paths);
  158. //echo '------------------', PHP_EOL;
  159. }
  160. }
  161. }
  162. /**
  163. * function
  164. * 文件目录
  165. * @param [type] $data
  166. * @return void
  167. */
  168. public function set_redis($data)
  169. {
  170. if(!strstr($data,'md5'))
  171. {
  172. if(rename($data,$data.'.redis'))
  173. {
  174. $data=$data.'.redis';
  175. }
  176. $this->redis->lpush('redis_to_kafka',$data);
  177. }
  178. }
  179. //扫描目录下所有没有上传到redis文件
  180. public function scan_file()
  181. {
  182. echo shell_exec("php /home/wwwroot/nbfd_tp3/index.php listening_file_create/scan_no_listening");
  183. return;
  184. }
  185. }