|
@@ -0,0 +1,394 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class NbListeningFileCreateAction extends Action {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public function redis_to_kafka( ){
|
|
|
|
+ include('NbInotifyMonitor.php');
|
|
|
|
+ //文件夹,ip,密码,端口
|
|
|
|
+ if(!C('WATCH_REDIS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_REDIS'=>array(
|
|
|
|
+ 'ip'=>'192.168.1.105',
|
|
|
|
+ 'password'=>'',
|
|
|
|
+ 'port'=>'6379',
|
|
|
|
+ 'db'=>1,
|
|
|
|
+ 'key'=>'redis_to_kafka',
|
|
|
|
+ );";
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ if(!C('WATCH_FTP_ADDRESS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_FTP_ADDRESS'=>'/home/renlian'";
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ $path = C('WATCH_FTP_ADDRESS');
|
|
|
|
+ $redis = C('WATCH_REDIS');
|
|
|
|
+ $test = new InotifyMonitor([$path],$redis['ip'],$redis['password'],$redis['6379'],$redis['db'],$redis['key']);
|
|
|
|
+ $test->run();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public function redis_to_kafka( ){
|
|
|
|
+ include('DahuaUtil.php');
|
|
|
|
+ ini_set('memory_limit', '1024M');
|
|
|
|
+ if (!extension_loaded('rdkafka')){
|
|
|
|
+ DahuaUtil::rlog('pushToKafka fail,extension of rdkafka has not installed!!'.PHP_EOL);
|
|
|
|
+ DahuaUtil::rlog('请重新安装kafka扩展');
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if (!extension_loaded('redis')){
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog('redis fail,extension of rdkafka has not installed!!'.PHP_EOL);
|
|
|
|
+ DahuaUtil::rlog('请重新安装redis扩展');
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(!C('WATCH_REDIS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_REDIS'=>array(
|
|
|
|
+ 'ip'=>'192.168.1.105',
|
|
|
|
+ 'password'=>'',
|
|
|
|
+ 'port'=>'6379',
|
|
|
|
+ 'db'=>1,
|
|
|
|
+ 'key'=>'redis_to_kafka',
|
|
|
|
+ );";
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(!C('WATCH_FTP_ADDRESS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_FTP_ADDRESS'=>'/home/renlian'";
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ if(!C('WATCH_KAFKA'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_KAFKA 该常量不存在');
|
|
|
|
+ $str="'WATCH_KAFKA'=>array(
|
|
|
|
+ 'address'=>'192.168.1.105:9092',
|
|
|
|
+ 'topic'=>'ningbo_rfid_kafka_topic',
|
|
|
|
+ );";
|
|
|
|
+ DahuaUtil::rlog('WATCH_KAFKA 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $path=C('WATCH_FTP_ADDRESS');
|
|
|
|
+ //kafka配置
|
|
|
|
+ $topic =C('WATCH_KAFKA')['topic'];
|
|
|
|
+ $topic1 =C('WATCH_KAFKA')['topic1'];
|
|
|
|
+ $kafka_address=C('WATCH_KAFKA')['address'];
|
|
|
|
+ //redis配置
|
|
|
|
+ $redis_ip=C('WATCH_REDIS')['ip'];
|
|
|
|
+ $redis_password=C('WATCH_REDIS')['password'];
|
|
|
|
+ $redis_port=C('WATCH_REDIS')['port'];
|
|
|
|
+ $redis_key=C('WATCH_REDIS')['key'];
|
|
|
|
+ $redis_db=C('WATCH_REDIS')['db'];
|
|
|
|
+ DahuaUtil::rlog('从redis中获取被扫描的文件存入kafka');
|
|
|
|
+ DahuaUtil::rlog('开始连接kafka');
|
|
|
|
+ $conf = new Rdkafka\Conf();
|
|
|
|
+ //$conf->set('batch.num.messages', 2);
|
|
|
|
+ $conf->set('metadata.broker.list',$kafka_address);
|
|
|
|
+ $conf->setErrorCb(function($producer, $msg) {
|
|
|
|
+ DahuaUtil::rlog(rd_kafka_err2str($err), $errstr);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ $conf->setDrMsgCb(function($producer, $msg) {
|
|
|
|
+ if($msg->err) {
|
|
|
|
+ DahuaUtil::rlog('Message delivery failed:' . $msg->errstr());
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ $rk = new RdKafka\Producer($conf);
|
|
|
|
+ $topic = $rk->newTopic($topic);
|
|
|
|
+ $topic1 = $rk->newTopic($topic1);
|
|
|
|
+ DahuaUtil::rlog("kafka连接成功");
|
|
|
|
+
|
|
|
|
+ try{
|
|
|
|
+
|
|
|
|
+ $redis = new redis();
|
|
|
|
+ DahuaUtil::rlog('redis连接中....');
|
|
|
|
+ $redis->connect($redis_ip,$redis_port);
|
|
|
|
+ $redis->auth($redis_password);
|
|
|
|
+ $redis->select($redis_db);
|
|
|
|
+ $result = $redis->ping();
|
|
|
|
+ if($result=='+PONG')
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('redis连接成功');
|
|
|
|
+ }else
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('redis连接失败');
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }catch(Exception $e)
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('redis连接异常',$e->getMessage());
|
|
|
|
+ throw new Exception($e->getMessage());
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $i=0;
|
|
|
|
+ $f=0;
|
|
|
|
+ DahuaUtil::rlog('开始扫描文件');
|
|
|
|
+ while(true)
|
|
|
|
+ {
|
|
|
|
+ $data=null;
|
|
|
|
+ try{
|
|
|
|
+ $res = $redis->rPop($redis_key);
|
|
|
|
+
|
|
|
|
+ }catch(Exception $e)
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('redis取出数据异常'.$e->getMessage());
|
|
|
|
+ throw new Exception($e->getMessage());
|
|
|
|
+ }
|
|
|
|
+ if(!$res)
|
|
|
|
+ {
|
|
|
|
+ sleep(1);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if(!file_exists($res))
|
|
|
|
+ {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog('扫描文件路径为:'.$res);
|
|
|
|
+ $zip = new ZipArchive();
|
|
|
|
+ DahuaUtil::rlog('开始解压文件:'.$res);
|
|
|
|
+ $new_res=str_replace(".zip.redis","redis.zip",$res);
|
|
|
|
+ //将名字中的.redis取消掉
|
|
|
|
+ rename($res,$new_res);
|
|
|
|
+ $res=$new_res;
|
|
|
|
+ if ($zip->open($res)===true){
|
|
|
|
+ $zip->extractTo($path);
|
|
|
|
+ $zip->close();
|
|
|
|
+ //删除压缩包
|
|
|
|
+ DahuaUtil::rlog('解压文件成功');
|
|
|
|
+ unlink($res);
|
|
|
|
+ DahuaUtil::rlog('删除压缩包');
|
|
|
|
+ $res=str_replace("redis.zip",".dat",$res);
|
|
|
|
+ DahuaUtil::rlog('压缩包中的文件为'.$res);
|
|
|
|
+
|
|
|
|
+ }else
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('解压文件失败,进行跳过');
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if(!file_exists($res))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('解压中的文件不存在');
|
|
|
|
+ unlink($res);
|
|
|
|
+
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $f++;
|
|
|
|
+ $start_time = microtime(true);
|
|
|
|
+ $fuc=function($res)
|
|
|
|
+ {
|
|
|
|
+ try{
|
|
|
|
+ $handle = fopen($res, 'rb');
|
|
|
|
+
|
|
|
|
+ }catch(Exception $e)
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('打开文件失败'.$e->getMessage());
|
|
|
|
+ throw new Exception($e->getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ while (feof($handle)===false) {
|
|
|
|
+ # code...
|
|
|
|
+ yield fgets($handle);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ fclose($handle);
|
|
|
|
+ };
|
|
|
|
+ DahuaUtil::rlog('读取文件成功');
|
|
|
|
+ $t=0;
|
|
|
|
+ //如果文件名称带有sta 代表是基站 否则是代表是轨迹
|
|
|
|
+ if(strstr(pathinfo($res)["filename"],"station")){
|
|
|
|
+ $topic=$topic1;
|
|
|
|
+ }
|
|
|
|
+ foreach($fuc($res) as $value)
|
|
|
|
+ {
|
|
|
|
+ if(!empty($value))
|
|
|
|
+ {
|
|
|
|
+ try{
|
|
|
|
+ $topic->produce(RD_KAFKA_PARTITION_UA, 0,$value);
|
|
|
|
+ $rk->poll(0);
|
|
|
|
+ $t++;
|
|
|
|
+ $i++;
|
|
|
|
+ while ($rk->getOutQLen() >10000) {
|
|
|
|
+ $rk->poll(10);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }catch(Exception $e)
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('kafka存取异常'."路径是".$res);
|
|
|
|
+ DahuaUtil::rlog('kafka存取异常'.$e->getMessage());
|
|
|
|
+
|
|
|
|
+ throw new Exception($e->getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog('kafaka文件成功:'.$res);
|
|
|
|
+ $end_time = microtime(true);
|
|
|
|
+ unlink($res);
|
|
|
|
+ DahuaUtil::rlog('完成扫描文件:'.$res);
|
|
|
|
+ DahuaUtil::rlog('删除文件:'.$res);
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog('一个文件处理完成时间:'.($end_time-$start_time).'s');
|
|
|
|
+ DahuaUtil::rlog('上传的文件有'.$t.'条');
|
|
|
|
+ DahuaUtil::rlog('总共有了'.$i.'条');
|
|
|
|
+ DahuaUtil::rlog('总共有的文件数据'.$f.'条');
|
|
|
|
+ echo '______________________________'.PHP_EOL;
|
|
|
|
+ echo '______________________________'.PHP_EOL;
|
|
|
|
+ echo '______________________________'.PHP_EOL;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public function scan_no_listening( ){
|
|
|
|
+ include('DahuaUtil.php');
|
|
|
|
+ if(!C('WATCH_REDIS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_REDIS'=>array(
|
|
|
|
+ 'ip'=>'192.168.1.105',
|
|
|
|
+ 'password'=>'',
|
|
|
|
+ 'port'=>'6379',
|
|
|
|
+ 'db'=>1,
|
|
|
|
+ 'key'=>'redis_to_kafka',
|
|
|
|
+ );";
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ if(!C('WATCH_FTP_ADDRESS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_FTP_ADDRESS'=>'/home/renlian'";
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $dir = C('WATCH_FTP_ADDRESS');
|
|
|
|
+ $redis=C('WATCH_REDIS');
|
|
|
|
+ $ip=$redis['ip'];
|
|
|
|
+ $post=$redis['port'];
|
|
|
|
+ $password=$redis['password'];
|
|
|
|
+ $key=$redis['key'];
|
|
|
|
+ $db=$redis['db'];
|
|
|
|
+
|
|
|
|
+ if(is_file($dir))
|
|
|
|
+ {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ $redis = new Redis();
|
|
|
|
+ DahuaUtil::rlog('连接redis'. $data);
|
|
|
|
+ try{
|
|
|
|
+ $redis->connect($ip,$post,2.5);
|
|
|
|
+ $redis->auth($password); //设置密码
|
|
|
|
+ $redis->select($db);
|
|
|
|
+ $result = $redis->ping();
|
|
|
|
+ if($result=='+PONG')
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog('redis连接成功');
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog("redis连接失败=>".$result);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }catch (Exception $e){
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog("redis连接异常".$e->getMessage());
|
|
|
|
+ throw new Exception($e->getMessage());
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog('开始扫描文件夹 '. $dir);
|
|
|
|
+ $files = scandir($dir);
|
|
|
|
+ foreach($files as $k=>$filename) {//务必使用!==,防止目录下出现类似文件名“0”等情况
|
|
|
|
+ if ($filename != "." && $filename != ".." &&!strstr($filename,'.zip.redis')&&!strstr($filename,'.tump') &&!strstr($filename,'redis.zip')&&strstr($filename,'.zip') &&is_file($dir.'/'.$filename)) {
|
|
|
|
+ $data = $dir.'/'.$filename;
|
|
|
|
+
|
|
|
|
+ DahuaUtil::rlog('将文件添加到redis中'. $data);
|
|
|
|
+ if(rename($data,$data.'.redis'))
|
|
|
|
+ {
|
|
|
|
+ try{
|
|
|
|
+ $redis->lpush($key,$data.'.redis');
|
|
|
|
+ }catch(Exception $e)
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog("redis连接异常".$e->getMessage());
|
|
|
|
+ throw new Exception($e->getMessage());
|
|
|
|
+
|
|
|
|
+ //重新连接redis
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ closedir($dir);
|
|
|
|
+ $redis->close();
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public function start_listening( ){
|
|
|
|
+ include('NbInotifyMonitor.php');
|
|
|
|
+ //文件夹,ip,密码,端口
|
|
|
|
+ if(!C('WATCH_REDIS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_REDIS'=>array(
|
|
|
|
+ 'ip'=>'192.168.1.105',
|
|
|
|
+ 'password'=>'',
|
|
|
|
+ 'port'=>'6379',
|
|
|
|
+ 'db'=>1,
|
|
|
|
+ 'key'=>'redis_to_kafka',
|
|
|
|
+ );";
|
|
|
|
+ DahuaUtil::rlog('WATCH_REDIS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ if(!C('WATCH_FTP_ADDRESS'))
|
|
|
|
+ {
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 该常量不存在');
|
|
|
|
+ $str= "'WATCH_FTP_ADDRESS'=>'/home/renlian'";
|
|
|
|
+ DahuaUtil::rlog('WATCH_FTP_ADDRESS 具体格式:'.$str);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ $path = C('WATCH_FTP_ADDRESS');
|
|
|
|
+ $redis = C('WATCH_REDIS');
|
|
|
|
+ $test = new InotifyMonitor([$path],$redis['ip'],$redis['password'],$redis['6379'],$redis['db'],$redis['key']);
|
|
|
|
+ $test->run();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|