StatisticWorker.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <?php
  2. require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
  3. /**
  4. *
  5. * @author walkor <worker-man@qq.com>
  6. */
  7. class StatisticWorker extends Man\Core\SocketWorker
  8. {
  9. /**
  10. * 最大日志buffer,大于这个值就写磁盘
  11. * @var integer
  12. */
  13. const MAX_LOG_BUFFER_SZIE = 1024000;
  14. /**
  15. * 多长时间写一次数据到磁盘
  16. * @var integer
  17. */
  18. const WRITE_PERIOD_LENGTH = 60;
  19. /**
  20. * 多长时间清理一次老的磁盘数据
  21. * @var integer
  22. */
  23. const CLEAR_PERIOD_LENGTH = 86400;
  24. /**
  25. * 数据多长时间过期
  26. * @var integer
  27. */
  28. const EXPIRED_TIME = 1296000;
  29. /**
  30. * 统计数据
  31. * ip=>modid=>interface=>['code'=>[xx=>count,xx=>count],'suc_cost_time'=>xx,'fail_cost_time'=>xx, 'suc_count'=>xx, 'fail_count'=>xx]
  32. * @var array
  33. */
  34. protected $statisticData = array();
  35. /**
  36. * 日志的buffer
  37. * @var string
  38. */
  39. protected $logBuffer = '';
  40. /**
  41. * 放统计数据的目录(相对于workerman/logs/)
  42. * @var string
  43. */
  44. protected $statisticDir = 'statistic/statistic/';
  45. /**
  46. * 存放统计日志的目录(相对于workerman/logs/)
  47. * @var string
  48. */
  49. protected $logDir = 'statistic/log/';
  50. /**
  51. * 提供统计查询的socket
  52. * @var resource
  53. */
  54. protected $providerSocket = null;
  55. /**
  56. * udp 默认全部接收完毕
  57. * @see Man\Core.SocketWorker::dealInput()
  58. */
  59. public function dealInput($recv_str)
  60. {
  61. return 0;
  62. }
  63. /**
  64. * 业务处理
  65. * @see Man\Core.SocketWorker::dealProcess()
  66. */
  67. public function dealProcess($recv_str)
  68. {
  69. // 如果是JSON协议,则是请求统计数据
  70. if($recv_str[0] === '{')
  71. {
  72. return $this->dealProvider($recv_str);
  73. }
  74. // 解码
  75. $unpack_data = StatisticProtocol::decode($recv_str);
  76. $module = $unpack_data['module'];
  77. $interface = $unpack_data['interface'];
  78. $cost_time = $unpack_data['cost_time'];
  79. $success = $unpack_data['success'];
  80. $time = $unpack_data['time'];
  81. $code = $unpack_data['code'];
  82. $msg = str_replace("\n", "<br>", $unpack_data['msg']);
  83. $ip = $this->getRemoteIp();
  84. // 模块接口统计
  85. $this->collectStatistics($module, $interface, $cost_time, $success, $ip, $code, $msg);
  86. // 全局统计
  87. $this->collectStatistics('WorkerMan', 'Statistics', $cost_time, $success, $ip, $code, $msg);
  88. // 失败记录日志
  89. if(!$success)
  90. {
  91. $this->logBuffer .= date('Y-m-d H:i:s',$time)."\t$ip\t$module::$interface\tcode:$code\tmsg:$msg\n";
  92. if(strlen($this->logBuffer) >= self::MAX_LOG_BUFFER_SZIE)
  93. {
  94. $this->writeLogToDisk();
  95. }
  96. }
  97. }
  98. /**
  99. * 收集统计数据
  100. * @param string $module
  101. * @param string $interface
  102. * @param float $cost_time
  103. * @param int $success
  104. * @param string $ip
  105. * @param int $code
  106. * @param string $msg
  107. * @return void
  108. */
  109. protected function collectStatistics($module, $interface , $cost_time, $success, $ip, $code, $msg)
  110. {
  111. // 统计相关信息
  112. if(!isset($this->statisticData[$ip]))
  113. {
  114. $this->statisticData[$ip] = array();
  115. }
  116. if(!isset($this->statisticData[$ip][$module]))
  117. {
  118. $this->statisticData[$ip][$module] = array();
  119. }
  120. if(!isset($this->statisticData[$ip][$module][$interface]))
  121. {
  122. $this->statisticData[$ip][$module][$interface] = array('code'=>array(), 'suc_cost_time'=>0, 'fail_cost_time'=>0, 'suc_count'=>0, 'fail_count'=>0);
  123. }
  124. if(!isset($this->statisticData[$ip][$module][$interface]['code'][$code]))
  125. {
  126. $this->statisticData[$ip][$module][$interface]['code'][$code] = 0;
  127. }
  128. $this->statisticData[$ip][$module][$interface]['code'][$code]++;
  129. if($success)
  130. {
  131. $this->statisticData[$ip][$module][$interface]['suc_cost_time'] += $cost_time;
  132. $this->statisticData[$ip][$module][$interface]['suc_count'] ++;
  133. }
  134. else
  135. {
  136. $this->statisticData[$ip][$module][$interface]['fail_cost_time'] += $cost_time;
  137. $this->statisticData[$ip][$module][$interface]['fail_count'] ++;
  138. }
  139. }
  140. /**
  141. * 将统计数据写入磁盘
  142. * @return void
  143. */
  144. public function writeStatisticsToDisk()
  145. {
  146. $time = time();
  147. // 循环将每个ip的统计数据写入磁盘
  148. foreach($this->statisticData as $ip => $mod_if_data)
  149. {
  150. foreach($mod_if_data as $module=>$items)
  151. {
  152. // 文件夹不存在则创建一个
  153. $file_dir = WORKERMAN_LOG_DIR . $this->statisticDir.$module;
  154. if(!is_dir($file_dir))
  155. {
  156. umask(0);
  157. mkdir($file_dir, 0777, true);
  158. }
  159. // 依次写入磁盘
  160. foreach($items as $interface=>$data)
  161. {
  162. file_put_contents($file_dir. "/{$interface}|".date('Y-m-d'), "$ip\t$time\t{$data['suc_count']}\t{$data['suc_cost_time']}\t{$data['fail_count']}\t{$data['fail_cost_time']}\t".json_encode($data['code'])."\n", FILE_APPEND | LOCK_EX);
  163. }
  164. }
  165. }
  166. // 清空统计
  167. $this->statisticData = array();
  168. }
  169. /**
  170. * 将日志数据写入磁盘
  171. * @return void
  172. */
  173. public function writeLogToDisk()
  174. {
  175. // 没有统计数据则返回
  176. if(empty($this->logBuffer))
  177. {
  178. return;
  179. }
  180. // 写入磁盘
  181. file_put_contents(WORKERMAN_LOG_DIR . $this->logDir . date('Y-m-d'), $this->logBuffer, FILE_APPEND | LOCK_EX);
  182. $this->logBuffer = '';
  183. }
  184. /**
  185. * 初始化
  186. * 统计目录检查
  187. * 初始化任务
  188. * @see Man\Core.SocketWorker::onStart()
  189. */
  190. protected function onStart()
  191. {
  192. // 初始化目录
  193. umask(0);
  194. $statistic_dir = WORKERMAN_LOG_DIR . $this->statisticDir;
  195. if(!is_dir($statistic_dir))
  196. {
  197. mkdir($statistic_dir, 0777, true);
  198. }
  199. $log_dir = WORKERMAN_LOG_DIR . $this->logDir;
  200. if(!is_dir($log_dir))
  201. {
  202. mkdir($log_dir, 0777, true);
  203. }
  204. // 初始化任务
  205. \Man\Core\Lib\Task::init($this->event);
  206. // 定时保存统计数据
  207. \Man\Core\Lib\Task::add(self::WRITE_PERIOD_LENGTH, array($this, 'writeStatisticsToDisk'));
  208. \Man\Core\Lib\Task::add(self::WRITE_PERIOD_LENGTH, array($this, 'writeLogToDisk'));
  209. // 定时清理不用的统计数据
  210. \Man\Core\Lib\Task::add(self::CLEAR_PERIOD_LENGTH, array($this, 'clearDisk'), array(WORKERMAN_LOG_DIR . $this->statisticDir, self::EXPIRED_TIME));
  211. \Man\Core\Lib\Task::add(self::CLEAR_PERIOD_LENGTH, array($this, 'clearDisk'), array(WORKERMAN_LOG_DIR . $this->logDir, self::EXPIRED_TIME));
  212. // 创建一个tcp监听,用来提供统计查询服务
  213. $this->providerSocket = stream_socket_server(\Man\Core\Lib\Config::get($this->workerName.'.provider_listen'));
  214. if($this->providerSocket)
  215. {
  216. $ret = $this->event->add($this->providerSocket, \Man\Core\Events\BaseEvent::EV_READ, array($this, 'accept'));
  217. }
  218. }
  219. /**
  220. * 进程停止时需要将数据写入磁盘
  221. * @see Man\Core.SocketWorker::onStop()
  222. */
  223. protected function onStop()
  224. {
  225. $this->writeLogToDisk();
  226. $this->writeStatisticsToDisk();
  227. }
  228. /**
  229. * 清除磁盘数据
  230. * @param string $file
  231. * @param int $exp_time
  232. */
  233. protected function clearDisk($file = null, $exp_time = 86400)
  234. {
  235. $time_now = time();
  236. if(is_file($file))
  237. {
  238. $mtime = filemtime($file);
  239. if(!$mtime)
  240. {
  241. $this->notice("filemtime $file fail");
  242. return;
  243. }
  244. if($time_now - $mtime > $exp_time)
  245. {
  246. unlink($file);
  247. }
  248. return;
  249. }
  250. foreach (glob($file."/*") as $file_name) {
  251. if(is_dir($file_name))
  252. {
  253. $this->clearDisk($file_name, $exp_time);
  254. continue;
  255. }
  256. $mtime = filemtime($file);
  257. if(!$mtime)
  258. {
  259. $this->notice("filemtime $file fail");
  260. return;
  261. }
  262. if($time_now - $mtime > $exp_time)
  263. {
  264. unlink($file_name);
  265. }
  266. }
  267. }
  268. /**
  269. * 处理请求统计
  270. * @param string $recv_str
  271. */
  272. protected function dealProvider($recv_str)
  273. {
  274. $req_data = json_decode(trim($recv_str), true);
  275. $module = $req_data['module'];
  276. $interface = $req_data['interface'];
  277. $cmd = $req_data['cmd'];
  278. $start_time = isset($req_data['start_time']) ? $req_data['start_time'] : '';
  279. $end_time = isset($req_data['end_time']) ? $req_data['end_time'] : '';
  280. $date = isset($req_data['date']) ? $req_data['date'] : '';
  281. $code = isset($req_data['code']) ? $req_data['code'] : '';
  282. $msg = isset($req_data['msg']) ? $req_data['msg'] : '';
  283. $pointer = isset($req_data['pointer']) ? $req_data['pointer'] : '';
  284. $count = isset($req_data['count']) ? $req_data['count'] : 10;
  285. switch($cmd)
  286. {
  287. case 'get_statistic':
  288. $buffer = json_encode(array('modules'=>$this->getModules($module), 'statistic' => $this->getStatistic($date, $module, $interface)))."\n";
  289. return $this->sendToClient($buffer);
  290. case 'get_log':
  291. $buffer = json_encode($this->getStasticLog($module, $interface , $start_time , $end_time, $code = '', $msg = '', $pointer='', $count=10))."\n";
  292. return $this->sendToClient($buffer);
  293. }
  294. }
  295. /**
  296. * 获取模块
  297. * @return array
  298. */
  299. public function getModules($current_module = '')
  300. {
  301. $st_dir = WORKERMAN_ROOT_DIR . $this->statisticDir;
  302. $modules_name_array = array();
  303. foreach(glob($st_dir."/*", GLOB_ONLYDIR) as $module_file)
  304. {
  305. $tmp = explode("/", $module_file);
  306. $module = end($tmp);
  307. $modules_name_array[$module] = array();
  308. if($current_module == $module)
  309. {
  310. $st_dir = $st_dir.$current_module.'/';
  311. $all_interface = array();
  312. foreach(glob($st_dir."*") as $file)
  313. {
  314. if(is_dir($file))
  315. {
  316. continue;
  317. }
  318. list($interface, $date) = explode("|", basename($file));
  319. $all_interface[$interface] = $interface;
  320. }
  321. $modules_name_array[$module] = $all_interface;
  322. }
  323. }
  324. return $modules_name_array;
  325. }
  326. /**
  327. * 获得统计数据
  328. * @param string $module
  329. * @param string $interface
  330. * @param int $date
  331. * @return bool/string
  332. */
  333. protected function getStatistic($date, $module, $interface)
  334. {
  335. if(empty($module) || empty($interface))
  336. {
  337. return '';
  338. }
  339. // log文件
  340. $log_file = $this->statisticDir."{$module}/{$interface}|{$date}";
  341. return @file_get_contents($log_file);
  342. }
  343. /**
  344. * 批量请求
  345. * @param array $request_buffer_array ['ip:port'=>req_buf, 'ip:port'=>req_buf, ...]
  346. * @return array
  347. */
  348. public function multiRequest($request_buffer_array)
  349. {
  350. $client_array = $sock_to_ip = $ip_list = array();
  351. foreach($request_buffer_array as $address => $buffer)
  352. {
  353. $client = stream_socket_client($address, $errno, $errmsg, 1);
  354. if(!$client)
  355. {
  356. $this->notice("connect $address fail");
  357. continue;
  358. }
  359. $client_array[$address] = $client;
  360. stream_set_timeout($client_array[$address], 0, 100000);
  361. fwrite($client_array[$address], $buffer);
  362. stream_set_blocking($client_array[$address], 0);
  363. $sock_to_address[(int)$client] = $address;
  364. }
  365. $read = $client_array;
  366. $write = $except = $read_buffer = array();
  367. $time_start = microtime(true);
  368. // 超时设置
  369. $timeout = 1;
  370. // 轮询处理数据
  371. while(count($read) > 0)
  372. {
  373. if(stream_select($read, $write, $except, $timeout))
  374. {
  375. foreach($read as $socket)
  376. {
  377. $address = $sock_to_address[(int)$socket];
  378. $buf = fread($socket, 8192);
  379. if(!$buf)
  380. {
  381. if(feof($socket))
  382. {
  383. unset($client_array[$address]);
  384. }
  385. continue;
  386. }
  387. if(!isset($read_buffer[$address]))
  388. {
  389. $read_buffer[$address] = $buf;
  390. }
  391. else
  392. {
  393. $read_buffer[$address] .= $buf;
  394. }
  395. // 数据接收完毕
  396. if("\n" === $read_buffer[$address][strlen($read_buffer[$address])-1])
  397. {
  398. unset($client_array[$address]);
  399. }
  400. }
  401. }
  402. // 超时了
  403. if(microtime(true) - $time_start > $timeout)
  404. {
  405. break;
  406. }
  407. $read = $client_array;
  408. }
  409. ksort($read_buffer);
  410. return $read_buffer;
  411. }
  412. /**
  413. * 获取指定日志
  414. *
  415. */
  416. protected function getStasticLog($module, $interface , $start_time = '', $end_time = '', $code = '', $msg = '', $pointer='', $count=100)
  417. {
  418. // log文件
  419. $log_file = WORKERMAN_ROOT_DIR . $this->logDir. (empty($start_time) ? date('Y-m-d') : date('Y-m-d', $start_time));
  420. if(!is_readable($log_file))
  421. {
  422. return array('pointer'=>0, 'data'=>$log_file . 'not exists or not readable');
  423. }
  424. // 读文件
  425. $h = fopen($log_file, 'r');
  426. // 如果有时间,则进行二分查找,加速查询
  427. if($start_time && $pointer === '' && ($file_size = filesize($log_file) > 50000))
  428. {
  429. $pointer = $this->binarySearch(0, $file_size, $start_time-1, $h);
  430. $pointer = $pointer < 1000 ? 0 : $pointer - 1000;
  431. }
  432. // 正则表达式
  433. $pattern = "/^([\d: \-]+)\t";
  434. if($module && $module != 'WorkerMan')
  435. {
  436. $pattern .= $module."::";
  437. }
  438. else
  439. {
  440. $pattern .= ".*::";
  441. }
  442. if($interface && $module != 'PHPServer')
  443. {
  444. $pattern .= $interface."\t";
  445. }
  446. else
  447. {
  448. $pattern .= ".*\t";
  449. }
  450. if($code !== '')
  451. {
  452. $pattern .= "code:$code\t";
  453. }
  454. else
  455. {
  456. $pattern .= "code:\d+\t";
  457. }
  458. if($msg)
  459. {
  460. $pattern .= "msg:$msg";
  461. }
  462. $pattern .= '/';
  463. // 指定偏移位置
  464. if($pointer >= 0)
  465. {
  466. fseek($h, (int)$pointer);
  467. }
  468. // 查找符合条件的数据
  469. $now_count = 0;
  470. $log_buffer = '';
  471. while(1)
  472. {
  473. if(feof($h))
  474. {
  475. break;
  476. }
  477. // 读1行
  478. $line = fgets($h);
  479. if(preg_match($pattern, $line, $match))
  480. {
  481. // 判断时间是否符合要求
  482. $time = strtotime($match[1]);
  483. if($start_time)
  484. {
  485. if($time<$start_time)
  486. {
  487. continue;
  488. }
  489. }
  490. if($end_time)
  491. {
  492. if($time>$end_time)
  493. {
  494. break;
  495. }
  496. }
  497. // 收集符合条件的log
  498. $log_buffer .= $line;
  499. if(++$now_count >= $count)
  500. {
  501. break;
  502. }
  503. }
  504. }
  505. // 记录偏移位置
  506. $pointer = ftell($h);
  507. return array('pointer'=>$pointer, 'data'=>$log_buffer);
  508. }
  509. /**
  510. * 日志二分查找法
  511. * @param int $start_point
  512. * @param int $end_point
  513. * @param int $time
  514. * @param fd $fd
  515. * @return int
  516. */
  517. protected function binarySearch($start_point, $end_point, $time, $fd)
  518. {
  519. // 计算中点
  520. $mid_point = (int)(($end_point+$start_point)/2);
  521. // 定位文件指针在中点
  522. fseek($fd, $mid_point);
  523. // 读第一行
  524. $line = fgets($fd);
  525. if(feof($fd) || false === $line)
  526. {
  527. return ftell($fd);
  528. }
  529. // 第一行可能数据不全,再读一行
  530. $line = fgets($fd);
  531. if(feof($fd) || false === $line || trim($line) == '')
  532. {
  533. return ftell($fd);
  534. }
  535. // 判断是否越界
  536. $current_point = ftell($fd);
  537. if($current_point>=$end_point)
  538. {
  539. return $end_point;
  540. }
  541. // 获得时间
  542. $tmp = explode("\t", $line);
  543. $tmp_time = strtotime($tmp[0]);
  544. // 判断时间,返回指针位置
  545. if($tmp_time > $time)
  546. {
  547. return $this->binarySearch($start_point, $current_point, $time, $fd);
  548. }
  549. elseif($tmp_time < $time)
  550. {
  551. return $this->binarySearch($current_point, $end_point, $time, $fd);
  552. }
  553. else
  554. {
  555. return $current_point;
  556. }
  557. }
  558. }
  559. /**
  560. *
  561. * struct statisticPortocol
  562. * {
  563. * unsigned char module_name_len;
  564. * unsigned char interface_name_len;
  565. * float cost_time;
  566. * unsigned char success;
  567. * int code;
  568. * unsigned short msg_len;
  569. * unsigned int time;
  570. * char[module_name_len] module_name;
  571. * char[interface_name_len] interface_name;
  572. * char[msg_len] msg;
  573. * }
  574. *
  575. * @author workerman.net
  576. */
  577. class StatisticProtocol
  578. {
  579. /**
  580. * 包头长度
  581. * @var integer
  582. */
  583. const PACKEGE_FIXED_LENGTH = 17;
  584. /**
  585. * udp 包最大长度
  586. * @var integer
  587. */
  588. const MAX_UDP_PACKGE_SIZE = 65507;
  589. /**
  590. * char类型能保存的最大数值
  591. * @var integer
  592. */
  593. const MAX_CHAR_VALUE = 255;
  594. /**
  595. * usigned short 能保存的最大数值
  596. * @var integer
  597. */
  598. const MAX_UNSIGNED_SHORT_VALUE = 65535;
  599. /**
  600. * 编码
  601. * @param string $module
  602. * @param string $interface
  603. * @param float $cost_time
  604. * @param int $success
  605. * @param int $code
  606. * @param string $msg
  607. * @return string
  608. */
  609. public static function encode($module, $interface , $cost_time, $success, $code = 0,$msg = '')
  610. {
  611. // 防止模块名过长
  612. if(strlen($module) > self::MAX_CHAR_VALUE)
  613. {
  614. $module = substr($module, 0, self::MAX_CHAR_VALUE);
  615. }
  616. // 防止接口名过长
  617. if(strlen($interface) > self::MAX_CHAR_VALUE)
  618. {
  619. $interface = substr($interface, 0, self::MAX_CHAR_VALUE);
  620. }
  621. // 防止msg过长
  622. $module_name_length = strlen($module);
  623. $interface_name_length = strlen($interface);
  624. $avalible_size = self::MAX_UDP_PACKGE_SIZE - self::PACKEGE_FIXED_LENGTH - $module_name_length - $interface_name_length;
  625. if(strlen($msg) > $avalible_size)
  626. {
  627. $msg = substr($msg, 0, $avalible_size);
  628. }
  629. // 打包
  630. return pack('CCfCNnN', $module_name_length, $interface_name_length, $cost_time, $success ? 1 : 0, $code, strlen($msg), time()).$module.$interface.$msg;
  631. }
  632. /**
  633. * 解包
  634. * @param string $bin_data
  635. * @return array
  636. */
  637. public static function decode($bin_data)
  638. {
  639. // 解包
  640. $data = unpack("Cmodule_name_len/Cinterface_name_len/fcost_time/Csuccess/Ncode/nmsg_len/Ntime", $bin_data);
  641. $module = substr($bin_data, self::PACKEGE_FIXED_LENGTH, $data['module_name_len']);
  642. $interface = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'], $data['interface_name_len']);
  643. $msg = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'] + $data['interface_name_len']);
  644. return array(
  645. 'module' => $module,
  646. 'interface' => $interface,
  647. 'cost_time' => $data['cost_time'],
  648. 'success' => $data['success'],
  649. 'time' => $data['time'],
  650. 'code' => $data['code'],
  651. 'msg' => $msg,
  652. );
  653. }
  654. }