StatisticWorker.php 21 KB

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