1. <?php
    2. /**
    3. * **************************************************************************************
    4. * Description : 自动化执行 命令行模式
    5. * monolog,Thriftlog,Phplog,Accesslog,php-fpm,nginx
    6. * yuyuecall,lunciyuyue,reqyuyue,vrayrender,houselog
    7. *
    8. * 导入到 ES
    9. * Filename : AlllogCommand.php
    10. * Create time : 2015-06-27
    11. * Last modified : 2015-06-30
    12. * update : bfire.lai
    13. * $getNum :第个文件读取行数 832行
    14. * **************************************************************************************
    15. */
    16. class AlllogCommand
    17. {
    18. /*@var _host*/
    19. protected $_host = '';
    20. /*@var _port*/
    21. protected $_port = '';
    22. /*@var _index*/
    23. protected $_index = '';
    24. /*@var _type*/
    25. protected $_type = '';
    26. /*@var _root*/
    27. protected $_root = '';
    28. /*@var _curl*/
    29. protected $_curl;
    30. /*@var _logdir */
    31. protected $_logdir = array();
    32. /*@var _logdir*/
    33. protected $_logflag = 0;
    34. /* 程序执行入口*/
    35. public function run()
    36. {
    37. //网站根目录
    38. if (!defined('ROOT')) {
    39. define('ROOT', dirname(__FILE__));
    40. //目前root 只显示到 D:\wamp\www\tboss\log2es
    41. }
    42. //导入 ES 与 log_Info 配置
    43. $all_Config = require_once(ROOT.'/Allconfig.php');
    44. //导入 cur 配置
    45. require_once(ROOT.'/Curl.php');
    46. $curl = new Curl();
    47. //库与表
    48. $index = $all_Config['params']['es_Conn']['_index'];
    49. $host = $all_Config['params']['es_Conn']['host'];
    50. $port = $all_Config['params']['es_Conn']['port'];
    51. $log_Info = $all_Config['params']['log_Info'];
    52. //init
    53. $this->setHost($host);
    54. $this->setPort($port);
    55. $this->setIndex($index);
    56. $this->setRoot(ROOT);
    57. $this->setCurl($curl);
    58. $this->setLogdir($all_Config['params']['log_Info']);
    59. $this->setLogflag($all_Config['params']['log_Flag']);
    60. $countLog = count($log_Info);
    61. //////////////////////生成文档 start //////////////////////
    62. //新增记录数组集
    63. $arr = array();
    64. $docCountArr = array();
    65. //////////////////////生成文档 end //////////////////////
    66. if(is_array($log_Info) && $countLog)
    67. {
    68. foreach ($log_Info as $key => $value)
    69. {
    70. switch($value['_logtype'])
    71. {
    72. case 1 :
    73. //app log monolog,Thriftlog,appphplog,accesslog
    74. $this->appJsonToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
    75. break;
    76. case 2 :
    77. //log2es
    78. $this->logJsonToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
    79. break;
    80. case 3 :
    81. //nginxlog
    82. $this->logOneLineToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
    83. break;
    84. case 4 :
    85. //phpfpm,yuyuecall,lunciyuyue,reqyuyue,vrayrender
    86. $this->logMoreLineToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
    87. break;
    88. case 5 :
    89. //
    90. break;
    91. default:
    92. # code...
    93. break;
    94. }
    95. }
    96. }
    97. //exit;
    98. }
    99. ///////////////////////////类属性方法 开始/////////////////////////////////
    100. /**
    101. * Sets specific curl values (updates and keeps default values)
    102. * @param string $curl Params
    103. * @return $this
    104. */
    105. public function setCurl($curl)
    106. {
    107. $this->_curl= $curl;
    108. return $this;
    109. }
    110. /**
    111. * Sets specific host values (updates and keeps default values)
    112. * @param string $host Params
    113. * @return $this
    114. */
    115. public function setHost($host)
    116. {
    117. $this->_host= $host;
    118. return $this;
    119. }
    120. /**
    121. * Sets specific port values (updates and keeps default values)
    122. * @param string $port Params
    123. * @return $this
    124. */
    125. public function setPort($port)
    126. {
    127. $this->_port= $port;
    128. return $this;
    129. }
    130. /**
    131. * Sets specific index values (updates and keeps default values)
    132. * @param string $index Params
    133. * @return $this
    134. */
    135. public function setIndex($index)
    136. {
    137. $this->_index= $index;
    138. return $this;
    139. }
    140. /**
    141. * Sets specific type values (updates and keeps default values)
    142. * @param string $type Params
    143. * @return $this
    144. */
    145. public function setType($type)
    146. {
    147. $this->_type = $type;
    148. return $this;
    149. }
    150. /**
    151. * Sets specific root values (updates and keeps default values)
    152. * @param string $root Params
    153. * @return $this
    154. */
    155. public function setRoot($root)
    156. {
    157. $this->_root = $root;
    158. return $this;
    159. }
    160. /**
    161. * Sets specific logdir values (updates and keeps default values)
    162. * @param string $logdir Params
    163. * @return $this
    164. */
    165. public function setLogdir($logdir)
    166. {
    167. $this->_logdir = $logdir;
    168. return $this;
    169. }
    170. /**
    171. * Sets specific logflag values (updates and keeps default values)
    172. * @param string $logflag Params
    173. * @return $this
    174. */
    175. public function setLogflag($logflag)
    176. {
    177. $this->_logflag = $logflag;
    178. return $this;
    179. }
    180. ///////////////////////////类属性方法 结束/////////////////////////////////
    181. /**
    182. * @功能:APP 类monolog,thriftlog,appphplog,accesslog 此四类JSON格式处理
    183. * @参数:
    184. * $fileName=> 文件名
    185. * $_pointname=> 指针名
    186. * $_type=> 业务
    187. * $_proj=> 服务
    188. * $_iid=> 节点
    189. * $_getNum=> 第次读取行数
    190. * $return => 返回结果集,
    191. */
    192. function appJsonToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
    193. {
    194. //读取方式1
    195. $logarr = $this->getLogArr($fileName,$_pointname,0,$_getNum);
    196. //print_r($logarr);
    197. //exit;
    198. //没有数据则直接返回
    199. $countlogarr = count($logarr);
    200. if($countlogarr < 2){
    201. return ;
    202. }
    203. //处理LOG数组
    204. foreach ($logarr as $key => $value)
    205. {
    206. //空值不作处理
    207. if(!$value){return;}
    208. //value str 转换 json 对象
    209. $jsonlogArr = array();
    210. $jsonlogArr = json_decode($value, true);
    211. if(is_array($jsonlogArr)){
    212. //处理第一条记录
    213. $logtime = date("Y-m-d H:i:s");
    214. $_id ='';
    215. $cl ='';
    216. $m ='';
    217. $tsn ='';
    218. $txt ='';
    219. $ip ='';
    220. $ag ='';
    221. $file ='';
    222. $ln =0;
    223. $uid ='';
    224. foreach ($jsonlogArr as $k => $v) {
    225. //日志文本中的时间
    226. if($k == 'time'){
    227. $logtime =$v;
    228. }
    229. if($k == 'REQUEST_TIME'){
    230. $logtime =$v['sec'];
    231. $logtime = date("Y-m-d H:i:s",$logtime);
    232. }
    233. //日志文本中的 IP
    234. if($k == 'REMOTE_ADDR'){
    235. $ip =$v;
    236. }
    237. //日志文本中的 ag
    238. if($k == 'HTTP_USER_AGENT'){
    239. $ag =$v;
    240. }
    241. //日志文本中的 monolog txt
    242. if($k == 'request'){
    243. foreach ($v as $k2 => $v2) {
    244. if($k2 == 'module') {
    245. $cl= $v2;
    246. }elseif($k2 == 'model'){
    247. $cl= $v2;
    248. }
    249. if($k2 == 'action') {$m= $v2;}
    250. }
    251. }
    252. //日志文本中的 thriftlog,appphplog,accesslog txt
    253. if($k == 'data'){
    254. foreach ($v as $k2 => $v2) {
    255. //thriftlog
    256. if($k2 == 'methodName') {$cl= $v2;}
    257. if($k2 == 'param') {
    258. foreach ($v2 as $k3 => $v3) {
    259. if($k3 == 'sort') {
    260. foreach ($v3 as $k4 => $v4) {
    261. if($v4['classname']) {
    262. $m= $v4['classname'];
    263. }
    264. }
    265. }
    266. }
    267. }
    268. //appphplog
    269. if($k2 == 'ERRFILE') {$file= $v2;}
    270. if($k2 == 'ERRLINE') {$ln = (int)$v2;}
    271. if($k2 == 'REQUEST') {
    272. foreach ($v2 as $k3 => $v3) {
    273. if($k3 == 'model') {$cl= $v3;}
    274. if($k3 == 'action') {$m= $v3;}
    275. if($k3 == 'uid') {$uid= $v3;}
    276. }
    277. }
    278. //accesslog
    279. if($k2 == 'req') {
    280. foreach ($v2 as $k3 => $v3) {
    281. if($k3 == 'model') {$cl= $v3;}
    282. if($k3 == 'action') {$m= $v3;}
    283. }
    284. }
    285. }
    286. }
    287. }
    288. $time = time();
    289. $_id = $cl.'-'.$m.'-'.$time.'-'.strtotime($logtime);
    290. $tsn = (int)strtotime($logtime)*1000;
    291. $txt = $value;
    292. $file = $file?$file:$fileName;
    293. $data = array(
    294. 'proj'=>$_proj,
    295. 'sid' =>'',
    296. 'file'=>$file,
    297. 'iid' =>$_iid,
    298. 'seq' =>0,
    299. 'ts' =>$logtime,
    300. 'tsn' =>$tsn,
    301. 'lv' =>'ERROR',
    302. 'lvn' =>40000,
    303. 'th' =>'',
    304. 'cl' =>$cl,
    305. 'm' =>$m,
    306. 'ln' =>$ln,
    307. 'bsid'=>'',
    308. 'esid'=>'',
    309. 'txt' =>$txt,
    310. 'ex' =>'',
    311. 'ag' =>$ag,
    312. 'ip' =>$ip,
    313. 'uid' =>$uid,
    314. );
    315. //数组转换JSON对象
    316. $jsonStr = json_encode($data);
    317. //数据导入
    318. $url = $_type.'/'.$_id.'/';
    319. //$url = $_type.'/_search';
    320. $this->curlToEs($url,$jsonStr,$_pointname);
    321. }
    322. }
    323. }
    324. /**
    325. * @功能:类 JAVA标准日志格式 处理
    326. * @参数:
    327. * $fileName=> 文件名
    328. * $_pointname=> 指针名
    329. * $_type=> 业务
    330. * $_proj=> 服务
    331. * $_iid=> 节点
    332. * $_getNum=> 第次读取行数
    333. * $return => 返回结果集,
    334. */
    335. function logJsonToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
    336. {
    337. //读取方式2
    338. $logarr = $this->getLogArr($fileName,$_pointname,0,$_getNum);
    339. //print_r($logarr);
    340. //exit;
    341. //没有数据则直接返回
    342. $countlogarr = count($logarr);
    343. if($countlogarr < 2){
    344. return ;
    345. }
    346. //处理LOG数组
    347. foreach ($logarr as $key => $value) {
    348. //空值不作处理
    349. if(!$value){return;}
    350. //value str 转换 json 对象
    351. $jsonlogArr = array();
    352. $jsonlogArr = json_decode($value, true);
    353. if(is_array($jsonlogArr)){
    354. //处理第一条记录
    355. $_id ='';//编号
    356. $proj =$_proj;//项目编号 服务
    357. $sid ='';//服务名字
    358. $file ='';//采集的日志文件名
    359. $iid =$_iid;//实例号
    360. $seq =0;//日志收集的序列号
    361. $ts = date("Y-m-d H:i:s");//时间戳
    362. $tsn ='';//时间戳的毫秒数字
    363. $lv ='';//日志级别:TRACE,DEBUG等
    364. $lvn ='';//日志级别的数字
    365. $th ='';//线程名
    366. $cl ='';//类名或文件名
    367. $m ='';//方法名
    368. $ln =0;//行号
    369. $bsid ='';//业务会话id
    370. $esid ='';//总线会话id
    371. $txt ='';//日志内容
    372. $ex ='';//异常Exception日志的堆栈信息
    373. $ag ='';//user agent
    374. $ip ='';//调用者ip
    375. $uid ='';//用户id
    376. foreach ($jsonlogArr as $k => $v) {
    377. if($k == 'proj'){ $proj =$v;}
    378. if($k == 'sid') { $sid =$v;}
    379. if($k == 'file'){ $file =$v;}
    380. if($k == 'iid'){ $iid =$v;}
    381. if($k == 'seq'){ $seq =$v;}
    382. if($k == 'ts') { $ts =$v;}
    383. if($k == 'tsn'){ $tsn =$v;}
    384. if($k == 'lv'){ $lv =$v;}
    385. if($k == 'lvn'){ $lvn =$v;}
    386. if($k == 'th'){ $th =$v;}
    387. if($k == 'cl'){ $cl =$v;}
    388. if($k == 'm'){ $m =$v;}
    389. if($k == 'ln'){ $ln =$v;}
    390. if($k == 'bsid'){ $bsid =$v;}
    391. if($k == 'esid'){ $esid =$v;}
    392. if($k == 'txt'){ $txt =$v;}
    393. if($k == 'ex'){ $ex =$v;}
    394. if($k == 'ag'){ $ag =$v;}
    395. if($k == 'ip'){ $ip =$v;}
    396. if($k == 'uid'){ $uid =$v;}
    397. }
    398. $time = time();
    399. $_id = $key.'-'.$time.'-'.$tsn;
    400. $file = $file?$file:$fileName;
    401. $tsn = (int)$tsn*1000;
    402. $data = array(
    403. 'proj'=>$proj,
    404. 'sid' =>$sid,
    405. 'file'=>$file,
    406. 'iid' =>$iid,
    407. 'seq' =>$seq,
    408. 'ts' =>$ts,
    409. 'tsn' =>$tsn,
    410. 'lv' =>$lv,
    411. 'lvn' =>$lvn,
    412. 'th' =>$th,
    413. 'cl' =>$cl,
    414. 'm' =>$m,
    415. 'ln' =>$ln,
    416. 'bsid'=>$bsid,
    417. 'esid'=>$esid,
    418. 'txt' =>$txt,
    419. 'ex' =>$ex,
    420. 'ag' =>$ag,
    421. 'ip' =>$ip,
    422. 'uid' =>$uid,
    423. );
    424. //print_r($data);
    425. // exit;
    426. //数组转换JSON对象
    427. $jsonStr = json_encode($data);
    428. //数据导入
    429. $url = $_type.'/'.$_id.'/';
    430. //$url = $_type.'/_search';
    431. $this->curlToEs($url,$jsonStr,$_pointname);
    432. }
    433. }
    434. }
    435. /**
    436. * @功能:类 单行 nginxlog 错误 日志格式 处理
    437. * @参数:
    438. * $fileName=> 文件名
    439. * $_pointname=> 指针名
    440. * $_type=> 业务
    441. * $_proj=> 服务
    442. * $_iid=> 节点
    443. * $_getNum=> 第次读取行数
    444. * $return => 返回结果集,
    445. */
    446. function logOneLineToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
    447. {
    448. //读取方式3
    449. $logarr = $this->getLogArr($fileName,$_pointname,0,$_getNum);
    450. //print_r($logarr);
    451. //exit;
    452. //没有数据则直接返回
    453. $countlogarr = count($logarr);
    454. if($countlogarr < 2){
    455. return ;
    456. }
    457. //处理第一条记录
    458. $_id ='';//编号
    459. $proj =$_proj;//项目编号 服务
    460. $sid ='';//服务名字
    461. $file ='';//采集的日志文件名
    462. $iid =$_iid;//实例号
    463. $seq =0;//日志收集的序列号
    464. $ts = date("Y-m-d H:i:s");//时间戳
    465. $tsn ='';//时间戳的毫秒数字
    466. $lv ='ERROR';//日志级别:TRACE,DEBUG等
    467. $lvn ='';//日志级别的数字
    468. $th ='';//线程名
    469. $cl ='';//类名或文件名
    470. $m ='';//方法名
    471. $ln =0;//行号
    472. $bsid ='';//业务会话id
    473. $esid ='';//总线会话id
    474. $txt ='';//日志内容
    475. $ex ='';//异常Exception日志的堆栈信息
    476. $ag ='';//user agent
    477. $ip ='';//调用者ip
    478. $uid ='';//用户id
    479. //处理日志记录时间与IP逻辑
    480. $findme1 = '[';
    481. $findme2 = ',';
    482. $findme3 = ']';
    483. $timestr ='';
    484. //处理LOG数组
    485. foreach ($logarr as $key => $value) {
    486. //空值不作处理
    487. if(!$value){return;}
    488. //获取时间日期位置
    489. $pos2 = $this->findStrindex($value,$findme1);
    490. if ($pos2 !== false) {
    491. $timestr = substr($value,0,$pos2);
    492. }
    493. //获取IP 开始与结束位置
    494. $pos1 = $this->findStrindex($value,$findme2,1);
    495. $pos3 = $this->findStrindex($value,$findme2,2);
    496. if ($pos1 !== false && $pos3 !== false) {
    497. $ip = substr($value,$pos1+1,$pos3-$pos1-1);
    498. }
    499. //获取lv 开始与结束位置
    500. $pos4 = $this->findStrindex($value,$findme1);
    501. $pos5 = $this->findStrindex($value,$findme3);
    502. if ($pos4 !== false && $pos5 !== false) {
    503. $lv = substr($value,$pos4+1,$pos5-$pos4-1);
    504. }
    505. //lv lvn 取值
    506. $level = $this->getLoglevel($lv);
    507. $lv = $level['lv'];
    508. $lvn = $level['lvn'];
    509. //处理
    510. $logtime = strtotime($timestr);
    511. $time = time();
    512. $_id = $key.'-'.$time.'-'.$logtime;
    513. $ts = date('Y/m/d H:i:s', $logtime);
    514. $tsn = (int)$logtime*1000;
    515. $txt = $value;
    516. $data = array(
    517. 'proj'=>$proj,
    518. 'sid' =>$sid,
    519. 'file'=>$file,
    520. 'iid' =>$iid,
    521. 'seq' =>$seq,
    522. 'ts' =>$ts,
    523. 'tsn' =>$tsn,
    524. 'lv' =>$lv,
    525. 'lvn' =>$lvn,
    526. 'th' =>$th,
    527. 'cl' =>$cl,
    528. 'm' =>$m,
    529. 'ln' =>$ln,
    530. 'bsid'=>$bsid,
    531. 'esid'=>$esid,
    532. 'txt' =>$txt,
    533. 'ex' =>$ex,
    534. 'ag' =>$ag,
    535. 'ip' =>$ip,
    536. 'uid' =>$uid,
    537. );
    538. //print_r($data);
    539. //exit;
    540. //数组转换JSON对象
    541. $jsonStr = json_encode($data);
    542. //数据导入
    543. $url = $_type.'/'.$_id.'/';
    544. //$url = $_type.'/_search';
    545. $this->curlToEs($url,$jsonStr,$_pointname);
    546. }
    547. }
    548. /**
    549. * @功能:类 多行 php标准日志格式 处理
    550. * @参数:
    551. * $fileName=> 文件名
    552. * $_pointname=> 指针名
    553. * $_type=> 业务
    554. * $_proj=> 服务
    555. * $_iid=> 节点
    556. * $_getNum=> 第次读取行数
    557. * $return => 返回结果集,
    558. */
    559. function logMoreLineToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
    560. {
    561. //读取方式4
    562. $logarr = $this->getLogArr($fileName,$_pointname,1,$_getNum);
    563. //print_r($logarr);
    564. //exit;
    565. //没有数据则直接返回
    566. $countlogarr = count($logarr);
    567. if($countlogarr < 2){
    568. return ;
    569. }
    570. //处理日志记录时间与IP逻辑
    571. $findme1 = '[';
    572. $findme2 = ',';
    573. $findme3 = ']';
    574. $timestr ='';
    575. //处理LOG数组
    576. //日志格式
    577. //[2015-06-29 09:20:09][INFO][IP: 192.168.3.39][pc] [flash-house] txt
    578. //$arr =array();
    579. foreach ($logarr as $key => $value) {
    580. //处理第一条记录
    581. $_id ='';//编号
    582. $proj =$_proj;//项目编号 服务
    583. $sid ='';//服务名字
    584. $file ='';//采集的日志文件名
    585. $iid =$_iid;//实例号
    586. $seq =0;//日志收集的序列号
    587. $ts = date("Y-m-d H:i:s");//时间戳
    588. $tsn ='';//时间戳的毫秒数字
    589. $lv ='ERROR';//日志级别:TRACE,DEBUG等
    590. $lvn =40000;//日志级别的数字
    591. $th ='';//线程名
    592. $cl ='';//类名或文件名
    593. $m ='';//方法名
    594. $ln =0;//行号
    595. $bsid ='';//业务会话id
    596. $esid ='';//总线会话id
    597. $txt ='';//日志内容
    598. $ex ='';//异常Exception日志的堆栈信息
    599. $ag ='';//user agent
    600. $ip ='';//调用者ip
    601. $uid ='';//用户id
    602. //空值不作处理
    603. if(!$value){return;}
    604. //获取时间日期结束位置
    605. $pos2 = $this->findStrindex($value,$findme3);
    606. if ($pos2 !== false) {
    607. $timestr = substr($value,0,$pos2);
    608. }
    609. //获取时间日期开始位置
    610. $pos1 = $this->findStrindex($timestr,$findme1);
    611. if ($pos1 !== false) {
    612. $timestr = substr($timestr,$pos1+1);
    613. }else{
    614. $value = '['.$value;
    615. }
    616. //获取lv 开始与结束位置
    617. $lvt = '';
    618. $pos5 = $this->findStrindex($value,$findme1,2);
    619. $pos6 = $this->findStrindex($value,$findme3,2);
    620. if ($pos5 !== false && $pos6 !== false) {
    621. $lvt = substr($value,$pos5+1,$pos6-$pos5-1);
    622. }
    623. $lvt = strtoupper($lvt);
    624. $loglevelArr = array('ERROR','WARN','INFO','DEBUG','TRACE');
    625. if (in_array($lvt,$loglevelArr, TRUE)){
    626. //lv lvn 取值
    627. $level = $this->getLoglevel($lvt);
    628. $lv = $level['lv'];
    629. $lvn = $level['lvn'];
    630. }
    631. //获取ip 开始与结束位置
    632. $ipt = '';
    633. $pos5 = $this->findStrindex($value,$findme1,3);
    634. $pos6 = $this->findStrindex($value,$findme3,3);
    635. if ($pos5 !== false && $pos6 !== false) {
    636. $ipt = substr($value,$pos5+1,$pos6-$pos5-1);
    637. }
    638. //判断某字符出现次数
    639. $ipcount = substr_count($ipt,'.');
    640. if($ipcount == 3){
    641. $ip = $ipt;
    642. }
    643. //获取proj 开始与结束位置
    644. $projt = '';
    645. $pos5 = $this->findStrindex($value,$findme1,4);
    646. $pos6 = $this->findStrindex($value,$findme3,4);
    647. if ($pos5 !== false && $pos6 !== false) {
    648. $projt = substr($value,$pos5+1,$pos6-$pos5-1);
    649. }
    650. $projlen = strlen($projt);
    651. if($projlen >0 && $projlen<20 && $_type !='yuyuecall' ){
    652. $proj = $projt;
    653. }
    654. //获取iid 开始与结束位置
    655. $iidt = '';
    656. $pos5 = $this->findStrindex($value,$findme1,5);
    657. $pos6 = $this->findStrindex($value,$findme3,5);
    658. if ($pos5 !== false && $pos6 !== false) {
    659. $iidt = substr($value,$pos5+1,$pos6-$pos5-1);
    660. }
    661. $iidlen = strlen($iidt);
    662. if($iidlen >0 && $iidlen<20 && $_type !='yuyuecall' ){
    663. $iid = $iidt;
    664. }
    665. //获取txt 开始与结束位置
    666. $txtt = '';
    667. $pos6 = $this->findStrindex($value,$findme3,5);
    668. if ($pos6 !== false) {
    669. $txtt = substr($value,$pos6+1);
    670. }
    671. //判断txt 是否是JSON 数据
    672. $txtarr = json_decode($txtt, true);
    673. $jsontxt = json_encode($txtarr);
    674. if($jsontxt !='null'){
    675. $txt = $txtt;
    676. }
    677. if(!$txt){
    678. $txt = $value;
    679. }
    680. //处理
    681. $logtime = strtotime($timestr);
    682. $time = time();
    683. $_id = $key.'-'.$time.'-'.$logtime;
    684. $ts = date('Y/m/d H:i:s', $logtime);
    685. $tsn = (int)$logtime*1000;
    686. $data = array(
    687. 'proj'=>$proj,
    688. 'sid' =>$sid,
    689. 'file'=>$file,
    690. 'iid' =>$iid,
    691. 'seq' =>$seq,
    692. 'ts' =>$ts,
    693. 'tsn' =>$tsn,
    694. 'lv' =>$lv,
    695. 'lvn' =>$lvn,
    696. 'th' =>$th,
    697. 'cl' =>$cl,
    698. 'm' =>$m,
    699. 'ln' =>$ln,
    700. 'bsid'=>$bsid,
    701. 'esid'=>$esid,
    702. 'txt' =>$txt,
    703. 'ex' =>$ex,
    704. 'ag' =>$ag,
    705. 'ip' =>$ip,
    706. 'uid' =>$uid,
    707. );
    708. //print_r($data);
    709. //exit;
    710. //数组转换JSON对象
    711. $jsonStr = json_encode($data);
    712. //数据导入
    713. $url = $_type.'/'.$_id.'/';
    714. //$url = $_type.'/_search';
    715. $this->curlToEs($url,$jsonStr,$_pointname);
    716. }
    717. //print_r($arr);
    718. //exit;
    719. }
    720. /**
    721. * @功能:通过CURL 方式把数据POST 到ES 系统中
    722. * @参数:
    723. * $url=> POST 地址
    724. * $jsonStr=> 数据的JSON串
    725. * $return => 返回结果集,
    726. */
    727. function curlToEs($url,$jsonStr,$intype='')
    728. {
    729. //$url = 'http://'.$this->_host.':'.$this->_port.'/'.$this->_index.'/'.$_type;
    730. //$url = $url.'/'.$_id
    731. //$url = $url.'/_search';
    732. //$output = $this->_curl->get($url);
    733. //URL 正式地址
    734. $url = 'http://'.$this->_host.':'.$this->_port.'/'.$this->_index.'/'.$url;
    735. $output = $this->_curl->put($url, $jsonStr);
    736. //$output = $this->_curl->get($url);
    737. //记录日志
    738. $param['intype'] = $intype;
    739. $param['txt'] = $output;
    740. $param['curlUrl'] = $url;
    741. $param['curlParam'] = $jsonStr;
    742. $args = $this->_curl->getErrNo();
    743. $this->rewritetolog($param, $args);
    744. }
    745. /**
    746. * @功能:获取日志数组
    747. * @参数:$fileName=>获取日志的路径,
    748. * $indexType=> 要获取的类型
    749. * $getNum=> 要获取的行数
    750. * $return => 返回结果集,
    751. */
    752. function del_dir()
    753. {
    754. $now = time();
    755. $time = strtotime('-1 day', $now);
    756. $yday = date('Ymd', $time);
    757. $root = $this->_root;
    758. $indexName = $root.'/log/logIndex'.$yday.'.txt';
    759. if (file_exists($indexName)) {
    760. $result=unlink ($indexName);
    761. }
    762. }
    763. /**
    764. * @功能:获取日志数组
    765. * @参数:$fileName=>获取日志的路径,
    766. * $indexType=> 要获取的类型
    767. * $moreLine=> 是否多行处理
    768. * $getNum=> 要获取的行数
    769. * $return => 返回结果集,
    770. */
    771. function getLogArr($fileName,$indexType,$moreLine=0,$getNum=100)
    772. {
    773. $root = $this->_root;
    774. $indexName = $root.'/log/logIndex'.date('Ymd').'.txt';
    775. $txtIndex = @file_get_contents($indexName);
    776. $arrIndex = explode("\n", $txtIndex);
    777. $IndexArr = array();
    778. if($arrIndex[0]){
    779. $allindexArr = json_decode($arrIndex[0], true);
    780. if(is_array($allindexArr)){
    781. $IndexArr = $allindexArr;
    782. }
    783. }else{
    784. $IndexArr[$indexType] = 0;
    785. $indexJson = json_encode($IndexArr);
    786. $this->writeFile($indexName,$indexJson,'w');
    787. }
    788. //del dir for yesteday
    789. //检查是否有昨天Index文件
    790. $this->del_dir();
    791. $logarr = array();
    792. $findex = 0;
    793. if (!file_exists($fileName)) {
    794. //记录日志
    795. $param['intype'] = $indexType;
    796. $param['txt'] = "this is no file in $fileName";
    797. $args = 404;
    798. $this->rewritetolog($param, $args);
    799. return $logarr;
    800. }
    801. $fp = @fopen($fileName, 'r');
    802. $i = 0;
    803. $content = '';
    804. fseek($fp, $findex);
    805. while(!feof($fp) && $i++< $getNum)
    806. //while(!feof($fp))
    807. {
    808. $content .= fgets($fp);
    809. $findex = ftell($fp);//记录findex以便下次读取
    810. }
    811. //日志多行处理方式
    812. if($moreLine){
    813. $logarr = explode("\n[", $content);
    814. $count = count($logarr);
    815. if($count>1){
    816. $first = $logarr[0];
    817. $end = end($logarr);
    818. $endarrName = $root.'/log/endarr.php';
    819. $txtIndex = @file_get_contents($endarrName);
    820. $arrIndex = explode("\n", $txtIndex);
    821. $endarr = array();
    822. if($arrIndex[0]){
    823. $allindexArr = json_decode($arrIndex[0], true);
    824. if(is_array($allindexArr)){
    825. $endarr = $allindexArr;
    826. }
    827. }
    828. //首字符
    829. $firstStr = substr( $first, 0, 1 );
    830. if($firstStr == '['){
    831. if(@$endarr[$indexType]){
    832. $logarr[$count-1] = $endarr[$indexType];
    833. }
    834. }else{
    835. $allendstr = $endarr[$indexType].$first;
    836. $logarr[0] = $allendstr;
    837. }
    838. //最后一行存入数组
    839. $endarr[$indexType] = $end;
    840. $indexJson = json_encode($endarr);
    841. $this->writeFile($endarrName,$indexJson,'w');
    842. }
    843. }else{
    844. //日志单行处理方式
    845. $logarr = explode("\n", $content);
    846. }
    847. //文件指针位置
    848. $IndexArr[$indexType] = $findex;
    849. $indexJson = json_encode($IndexArr);
    850. $this->writeFile($indexName,$indexJson,'w');
    851. return $logarr;
    852. }
    853. /**
    854. * 查找某字符在字符串中的位置
    855. * @param string $string Params
    856. * @param string $find Params
    857. * @param int $num Params
    858. * @return $pos
    859. */
    860. public function findStrindex($string, $find, $num=1)
    861. {
    862. $pos = -1;
    863. $n = 0;
    864. do{
    865. $pos = strpos($string, $find, $pos+1);
    866. $n++;
    867. if($n==$num){
    868. break;
    869. }
    870. }while($pos!==false);
    871. return $pos;
    872. }
    873. /*
    874. **
    875. * 写文件
    876. * @param string $file 文件路径
    877. * @param string $str 写入内容
    878. * @param char $mode 写入模式
    879. */
    880. function writeFile($file,$str,$mode='w'){
    881. $oldmask = @umask(0);
    882. $fp = @fopen($file,$mode);
    883. @flock($fp, 3);
    884. if(!$fp){
    885. Return false;
    886. } else {
    887. @fwrite($fp,$str);
    888. @fclose($fp);
    889. @umask($oldmask);
    890. Return true;
    891. }
    892. }
    893. /**
    894. * @功能:日志记录
    895. * @参数:
    896. @param $param Array()
    897. $param['intype'] => 要记录日志的类型,
    898. $param['txt'] => 返回结果集,
    899. * $args => 返回结果状态,0表示成功,非0表示失败
    900. * $logger => 是否要记录日志
    901. * @返回:若转换成功返回true,否则返回false或直接跳出
    902. * 显示行号 __LINE__
    903. */
    904. function rewritetolog($param=array(), $args='', $logger =true)
    905. {
    906. //$log['_id'] = '';//编号
    907. //$log['proj'] = '';//项目编号
    908. $log['sid'] = '';//服务名字
    909. $log['file'] = '';//采集的日志文件名
    910. $log['iid'] = '';//实例号
    911. $log['seq'] = 0;//日志收集的序列号
    912. //$log['ts'] = '';//时间戳
    913. //$log['tsn'] = '';//时间戳的毫秒数字
    914. $log['lv'] = 'ERROR';//日志级别:TRACE,DEBUG等
    915. $log['lvn'] = 40000;//日志级别的数字
    916. $log['th'] = '';//线程名
    917. $log['cl'] = '';//类名或文件名
    918. $log['m'] = '';//方法名
    919. $log['ln'] = 0;//行号
    920. $log['bsid'] = '';//业务会话id
    921. $log['esid'] = '';//总线会话id
    922. $log['txt'] = '';//日志内容
    923. $log['ex'] = '';//异常Exception日志的堆栈信息
    924. $log['ag'] = '';//user agent
    925. $log['ip'] = '';//调用者ip
    926. $log['uid'] = '';//用户id
    927. $time = time();
    928. $ts = date('Y-m-d H:i:s',$time);
    929. $tsn = (int)($time*1000);
    930. $log['proj'] = 'log2es';//项目编号
    931. $log['ts'] = $ts;//时间戳
    932. $log['tsn'] = $tsn;//时间戳的毫秒数字
    933. $logStr = '';
    934. if($logger){
    935. $status = 'INFO';
    936. $flag = $this->_logflag;
    937. $curl = $this->_curl;
    938. $t['time'] = (string)date("Y-m-d H:i:s");
    939. $t['name'] = $param['intype']?(string)$param['intype']:'';
    940. $t['txt'] = $param['txt']?$param['txt']:'';
    941. $t['curlUrl'] = @$param['curlUrl']?(string)$param['curlUrl']:'';
    942. $t['curlParam'] = @$param['curlParam']?$param['curlParam']:'';
    943. $t['curlInfo'] = $curl->getInfo(); //$curl->getInfo();
    944. $t['curlStatus'] = $curl->getStatus();//$curl->getStatus();
    945. $t['curlError'] = $curl->getError(); //$curl->getError();
    946. $t['curlErrNo'] = $curl->getErrNo(); //$curl->getErrNo();
    947. if($flag){
    948. if($args){
    949. $status = 'ERROR';
    950. }
    951. $t['status'] = (string)$status;
    952. //处理日志记录
    953. $level = $this->getLoglevel($status);//日志级别
    954. $log['lv'] = $level['lv'];//日志级别:TRACE,DEBUG等
    955. $log['lvn'] = $level['lvn'];//日志级别的数字
    956. $log['txt'] = $t;//日志内容
    957. //print_r($log);
    958. //exit;
    959. //$_SERVER
    960. $logStr = json_encode($log);
    961. $logStr = $logStr.'
    962. ';
    963. $root = $this->_root;
    964. $fileName = $root.'/log/';
    965. $fileName = $fileName.date('Y-m-d').".log";
    966. $this->writeFile($fileName,$logStr,'a');
    967. }else{
    968. if($args){
    969. $status = 'ERROR';
    970. //}
    971. $t['status'] = (string)$status;
    972. //处理日志记录
    973. $level = $this->getLoglevel($status);//日志级别
    974. $log['lv'] = $level['lv'];//日志级别:TRACE,DEBUG等
    975. $log['lvn'] = $level['lvn'];//日志级别的数字
    976. $log['txt'] = $t;//日志内容
    977. //$_SERVER
    978. $logStr = json_encode($log);
    979. $logStr = $logStr.'
    980. ';
    981. $root = $this->_root;
    982. $fileName = $root.'/log/';
    983. $fileName = $fileName.date('Y-m-d').".log";
    984. $this->writeFile($fileName,$logStr,'a');
    985. }
    986. }
    987. }
    988. }
    989. /*
    990. **
    991. * 日志级别
    992. * @param string $lv 日志级别字符串
    993. * @return int 返回数字
    994. */
    995. function getLoglevel($lv)
    996. {
    997. $lv = strtoupper($lv);
    998. $loglevel = array(
    999. 'ERROR'=>'40000',//40000
    1000. 'WARN'=>'30000',//30000
    1001. 'INFO'=>'20000',//20000
    1002. 'DEBUG'=>'10000',//10000
    1003. 'TRACE'=>'5000',//5000
    1004. );
    1005. if($loglevel[$lv]){
    1006. $level['lv'] = $lv;
    1007. $level['lvn']= $loglevel[$lv];
    1008. }else{
    1009. $level['lv'] = 'ERROR';
    1010. $level['lvn']= 40000;
    1011. }
    1012. return $level;
    1013. }
    1014. }
    1015. //执行程序脚本
    1016. $alllog = new AlllogCommand();
    1017. $alllog->run();
    1018. ?>