StatisticWorker.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. {
  252. $this->clearDisk($file_name, $exp_time);
  253. }
  254. }
  255. /**
  256. * 处理请求统计
  257. * @param string $recv_str
  258. */
  259. protected function dealProvider($recv_str)
  260. {
  261. $req_data = json_decode(trim($recv_str), true);
  262. $module = $req_data['module'];
  263. $interface = $req_data['interface'];
  264. $cmd = $req_data['cmd'];
  265. $start_time = isset($req_data['start_time']) ? $req_data['start_time'] : '';
  266. $end_time = isset($req_data['end_time']) ? $req_data['end_time'] : '';
  267. $date = isset($req_data['date']) ? $req_data['date'] : '';
  268. $code = isset($req_data['code']) ? $req_data['code'] : '';
  269. $msg = isset($req_data['msg']) ? $req_data['msg'] : '';
  270. $offset = isset($req_data['offset']) ? $req_data['offset'] : '';
  271. $count = isset($req_data['count']) ? $req_data['count'] : 10;
  272. switch($cmd)
  273. {
  274. case 'get_statistic':
  275. $buffer = json_encode(array('modules'=>$this->getModules($module), 'statistic' => $this->getStatistic($date, $module, $interface)))."\n";
  276. return $this->sendToClient($buffer);
  277. case 'get_log':
  278. $buffer = json_encode($this->getStasticLog($module, $interface , $start_time , $end_time, $code = '', $msg = '', $offset='', $count=10))."\n";
  279. return $this->sendToClient($buffer);
  280. }
  281. }
  282. /**
  283. * 获取模块
  284. * @return array
  285. */
  286. public function getModules($current_module = '')
  287. {
  288. $st_dir = WORKERMAN_ROOT_DIR . $this->statisticDir;
  289. $modules_name_array = array();
  290. foreach(glob($st_dir."/*", GLOB_ONLYDIR) as $module_file)
  291. {
  292. $tmp = explode("/", $module_file);
  293. $module = end($tmp);
  294. $modules_name_array[$module] = array();
  295. if($current_module == $module)
  296. {
  297. $st_dir = $st_dir.$current_module.'/';
  298. $all_interface = array();
  299. foreach(glob($st_dir."*") as $file)
  300. {
  301. if(is_dir($file))
  302. {
  303. continue;
  304. }
  305. list($interface, $date) = explode("|", basename($file));
  306. $all_interface[$interface] = $interface;
  307. }
  308. $modules_name_array[$module] = $all_interface;
  309. }
  310. }
  311. return $modules_name_array;
  312. }
  313. /**
  314. * 获得统计数据
  315. * @param string $module
  316. * @param string $interface
  317. * @param int $date
  318. * @return bool/string
  319. */
  320. protected function getStatistic($date, $module, $interface)
  321. {
  322. if(empty($module) || empty($interface))
  323. {
  324. return '';
  325. }
  326. // log文件
  327. $log_file = $this->statisticDir."{$module}/{$interface}|{$date}";
  328. return @file_get_contents($log_file);
  329. }
  330. /**
  331. * 批量请求
  332. * @param array $request_buffer_array ['ip:port'=>req_buf, 'ip:port'=>req_buf, ...]
  333. * @return array
  334. */
  335. public function multiRequest($request_buffer_array)
  336. {
  337. $client_array = $sock_to_ip = $ip_list = array();
  338. foreach($request_buffer_array as $address => $buffer)
  339. {
  340. $client = stream_socket_client($address, $errno, $errmsg, 1);
  341. if(!$client)
  342. {
  343. $this->notice("connect $address fail");
  344. continue;
  345. }
  346. $client_array[$address] = $client;
  347. stream_set_timeout($client_array[$address], 0, 100000);
  348. fwrite($client_array[$address], $buffer);
  349. stream_set_blocking($client_array[$address], 0);
  350. $sock_to_address[(int)$client] = $address;
  351. }
  352. $read = $client_array;
  353. $write = $except = $read_buffer = array();
  354. $time_start = microtime(true);
  355. // 超时设置
  356. $timeout = 1;
  357. // 轮询处理数据
  358. while(count($read) > 0)
  359. {
  360. if(stream_select($read, $write, $except, $timeout))
  361. {
  362. foreach($read as $socket)
  363. {
  364. $address = $sock_to_address[(int)$socket];
  365. $buf = fread($socket, 8192);
  366. if(!$buf)
  367. {
  368. if(feof($socket))
  369. {
  370. unset($client_array[$address]);
  371. }
  372. continue;
  373. }
  374. if(!isset($read_buffer[$address]))
  375. {
  376. $read_buffer[$address] = $buf;
  377. }
  378. else
  379. {
  380. $read_buffer[$address] .= $buf;
  381. }
  382. // 数据接收完毕
  383. if("\n" === $read_buffer[$address][strlen($read_buffer[$address])-1])
  384. {
  385. unset($client_array[$address]);
  386. }
  387. }
  388. }
  389. // 超时了
  390. if(microtime(true) - $time_start > $timeout)
  391. {
  392. break;
  393. }
  394. $read = $client_array;
  395. }
  396. ksort($read_buffer);
  397. return $read_buffer;
  398. }
  399. /**
  400. * 获取指定日志
  401. *
  402. */
  403. protected function getStasticLog($module, $interface , $start_time = '', $end_time = '', $code = '', $msg = '', $offset='', $count=100)
  404. {
  405. // log文件
  406. $log_file = WORKERMAN_ROOT_DIR . $this->logDir. (empty($start_time) ? date('Y-m-d') : date('Y-m-d', $start_time));
  407. if(!is_readable($log_file))
  408. {
  409. return array('offset'=>0, 'data'=>$log_file . 'not exists or not readable');
  410. }
  411. // 读文件
  412. $h = fopen($log_file, 'r');
  413. // 如果有时间,则进行二分查找,加速查询
  414. if($start_time && $offset === '' && ($file_size = filesize($log_file) > 50000))
  415. {
  416. $offset = $this->binarySearch(0, $file_size, $start_time-1, $h);
  417. $offset = $offset < 1000 ? 0 : $offset - 1000;
  418. }
  419. // 正则表达式
  420. $pattern = "/^([\d: \-]+)\t";
  421. if($module && $module != 'WorkerMan')
  422. {
  423. $pattern .= $module."::";
  424. }
  425. else
  426. {
  427. $pattern .= ".*::";
  428. }
  429. if($interface && $module != 'WorkerMan')
  430. {
  431. $pattern .= $interface."\t";
  432. }
  433. else
  434. {
  435. $pattern .= ".*\t";
  436. }
  437. if($code !== '')
  438. {
  439. $pattern .= "code:$code\t";
  440. }
  441. else
  442. {
  443. $pattern .= "code:\d+\t";
  444. }
  445. if($msg)
  446. {
  447. $pattern .= "msg:$msg";
  448. }
  449. $pattern .= '/';
  450. // 指定偏移位置
  451. if($offset >= 0)
  452. {
  453. fseek($h, (int)$offset);
  454. }
  455. // 查找符合条件的数据
  456. $now_count = 0;
  457. $log_buffer = '';
  458. while(1)
  459. {
  460. if(feof($h))
  461. {
  462. break;
  463. }
  464. // 读1行
  465. $line = fgets($h);
  466. if(preg_match($pattern, $line, $match))
  467. {
  468. // 判断时间是否符合要求
  469. $time = strtotime($match[1]);
  470. if($start_time)
  471. {
  472. if($time<$start_time)
  473. {
  474. continue;
  475. }
  476. }
  477. if($end_time)
  478. {
  479. if($time>$end_time)
  480. {
  481. break;
  482. }
  483. }
  484. // 收集符合条件的log
  485. $log_buffer .= $line;
  486. if(++$now_count >= $count)
  487. {
  488. break;
  489. }
  490. }
  491. }
  492. // 记录偏移位置
  493. $offset = ftell($h);
  494. return array('offset'=>$offset, 'data'=>$log_buffer);
  495. }
  496. /**
  497. * 日志二分查找法
  498. * @param int $start_point
  499. * @param int $end_point
  500. * @param int $time
  501. * @param fd $fd
  502. * @return int
  503. */
  504. protected function binarySearch($start_point, $end_point, $time, $fd)
  505. {
  506. // 计算中点
  507. $mid_point = (int)(($end_point+$start_point)/2);
  508. // 定位文件指针在中点
  509. fseek($fd, $mid_point);
  510. // 读第一行
  511. $line = fgets($fd);
  512. if(feof($fd) || false === $line)
  513. {
  514. return ftell($fd);
  515. }
  516. // 第一行可能数据不全,再读一行
  517. $line = fgets($fd);
  518. if(feof($fd) || false === $line || trim($line) == '')
  519. {
  520. return ftell($fd);
  521. }
  522. // 判断是否越界
  523. $current_point = ftell($fd);
  524. if($current_point>=$end_point)
  525. {
  526. return $end_point;
  527. }
  528. // 获得时间
  529. $tmp = explode("\t", $line);
  530. $tmp_time = strtotime($tmp[0]);
  531. // 判断时间,返回指针位置
  532. if($tmp_time > $time)
  533. {
  534. return $this->binarySearch($start_point, $current_point, $time, $fd);
  535. }
  536. elseif($tmp_time < $time)
  537. {
  538. return $this->binarySearch($current_point, $end_point, $time, $fd);
  539. }
  540. else
  541. {
  542. return $current_point;
  543. }
  544. }
  545. }
  546. /**
  547. *
  548. * struct statisticPortocol
  549. * {
  550. * unsigned char module_name_len;
  551. * unsigned char interface_name_len;
  552. * float cost_time;
  553. * unsigned char success;
  554. * int code;
  555. * unsigned short msg_len;
  556. * unsigned int time;
  557. * char[module_name_len] module_name;
  558. * char[interface_name_len] interface_name;
  559. * char[msg_len] msg;
  560. * }
  561. *
  562. * @author workerman.net
  563. */
  564. class StatisticProtocol
  565. {
  566. /**
  567. * 包头长度
  568. * @var integer
  569. */
  570. const PACKEGE_FIXED_LENGTH = 17;
  571. /**
  572. * udp 包最大长度
  573. * @var integer
  574. */
  575. const MAX_UDP_PACKGE_SIZE = 65507;
  576. /**
  577. * char类型能保存的最大数值
  578. * @var integer
  579. */
  580. const MAX_CHAR_VALUE = 255;
  581. /**
  582. * usigned short 能保存的最大数值
  583. * @var integer
  584. */
  585. const MAX_UNSIGNED_SHORT_VALUE = 65535;
  586. /**
  587. * 编码
  588. * @param string $module
  589. * @param string $interface
  590. * @param float $cost_time
  591. * @param int $success
  592. * @param int $code
  593. * @param string $msg
  594. * @return string
  595. */
  596. public static function encode($module, $interface , $cost_time, $success, $code = 0,$msg = '')
  597. {
  598. // 防止模块名过长
  599. if(strlen($module) > self::MAX_CHAR_VALUE)
  600. {
  601. $module = substr($module, 0, self::MAX_CHAR_VALUE);
  602. }
  603. // 防止接口名过长
  604. if(strlen($interface) > self::MAX_CHAR_VALUE)
  605. {
  606. $interface = substr($interface, 0, self::MAX_CHAR_VALUE);
  607. }
  608. // 防止msg过长
  609. $module_name_length = strlen($module);
  610. $interface_name_length = strlen($interface);
  611. $avalible_size = self::MAX_UDP_PACKGE_SIZE - self::PACKEGE_FIXED_LENGTH - $module_name_length - $interface_name_length;
  612. if(strlen($msg) > $avalible_size)
  613. {
  614. $msg = substr($msg, 0, $avalible_size);
  615. }
  616. // 打包
  617. return pack('CCfCNnN', $module_name_length, $interface_name_length, $cost_time, $success ? 1 : 0, $code, strlen($msg), time()).$module.$interface.$msg;
  618. }
  619. /**
  620. * 解包
  621. * @param string $bin_data
  622. * @return array
  623. */
  624. public static function decode($bin_data)
  625. {
  626. // 解包
  627. $data = unpack("Cmodule_name_len/Cinterface_name_len/fcost_time/Csuccess/Ncode/nmsg_len/Ntime", $bin_data);
  628. $module = substr($bin_data, self::PACKEGE_FIXED_LENGTH, $data['module_name_len']);
  629. $interface = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'], $data['interface_name_len']);
  630. $msg = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'] + $data['interface_name_len']);
  631. return array(
  632. 'module' => $module,
  633. 'interface' => $interface,
  634. 'cost_time' => $data['cost_time'],
  635. 'success' => $data['success'],
  636. 'time' => $data['time'],
  637. 'code' => $data['code'],
  638. 'msg' => $msg,
  639. );
  640. }
  641. }