StatisticWorker.php 21 KB

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