StatisticProvider.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. /**
  3. *
  4. * @author walkor <walkor@workerman.net>
  5. */
  6. class StatisticProvider extends Man\Core\SocketWorker
  7. {
  8. /**
  9. * 最大日志buffer,大于这个值就写磁盘
  10. * @var integer
  11. */
  12. const MAX_LOG_BUFFER_SZIE = 1024000;
  13. /**
  14. * 多长时间写一次数据到磁盘
  15. * @var integer
  16. */
  17. const WRITE_PERIOD_LENGTH = 60;
  18. /**
  19. * 多长时间清理一次老的磁盘数据
  20. * @var integer
  21. */
  22. const CLEAR_PERIOD_LENGTH = 86400;
  23. /**
  24. * 数据多长时间过期
  25. * @var integer
  26. */
  27. const EXPIRED_TIME = 1296000;
  28. /**
  29. * 统计数据
  30. * ip=>modid=>interface=>['code'=>[xx=>count,xx=>count],'suc_cost_time'=>xx,'fail_cost_time'=>xx, 'suc_count'=>xx, 'fail_count'=>xx]
  31. * @var array
  32. */
  33. protected $statisticData = array();
  34. /**
  35. * 日志的buffer
  36. * @var string
  37. */
  38. protected $logBuffer = '';
  39. /**
  40. * 放统计数据的目录(相对于workerman/logs/)
  41. * @var string
  42. */
  43. protected $statisticDir = 'statistic/statistic/';
  44. /**
  45. * 存放统计日志的目录(相对于workerman/logs/)
  46. * @var string
  47. */
  48. protected $logDir = 'statistic/log/';
  49. /**
  50. * 用于接收广播的udp socket
  51. * @var resource
  52. */
  53. protected $broadcastSocket = null;
  54. public function onStart()
  55. {
  56. $listen = \Man\Core\Lib\Config::get($this->workerName . '.listen');
  57. $udp_address = str_replace('tcp', 'udp', $listen);
  58. $this->broadcastSocket = stream_socket_server($udp_address, $error_no, $error_msg, STREAM_SERVER_BIND);
  59. $this->event->add($this->broadcastSocket, \Man\Core\Events\BaseEvent::EV_READ, array($this, 'dealBroadcastUdp'));
  60. }
  61. /**
  62. * 接收Udp数据
  63. * 如果数据超过一个udp包长,需要业务自己解析包体,判断数据是否全部到达
  64. * @param resource $socket
  65. * @param $null_one $flag
  66. * @param $null_two $base
  67. * @return void
  68. */
  69. public function dealBroadcastUdp($socket, $null_one = null, $null_two = null)
  70. {
  71. $data = stream_socket_recvfrom($socket , self::MAX_UDP_PACKEG_SIZE, 0, $address);
  72. // 可能是惊群效应
  73. if(false === $data || empty($address))
  74. {
  75. return false;
  76. }
  77. // 解析包体
  78. $data = json_decode(trim($data), true);
  79. if(empty($data))
  80. {
  81. return false;
  82. }
  83. // 无法解析的包
  84. if(empty($data['cmd']) || $data['cmd'] != 'REPORT_IP' )
  85. {
  86. return false;
  87. }
  88. // 回应
  89. return stream_socket_sendto($this->broadcastSocket, json_encode(array('result'=>'ok')), 0, $address);
  90. }
  91. /**
  92. * udp 默认全部接收完毕
  93. * @see Man\Core.SocketWorker::dealInput()
  94. */
  95. public function dealInput($recv_buffer)
  96. {
  97. return 0;
  98. }
  99. /**
  100. * 处理请求统计
  101. * @param string $recv_buffer
  102. */
  103. public function dealProcess($recv_buffer)
  104. {
  105. $req_data = json_decode(trim($recv_buffer), true);
  106. $module = $req_data['module'];
  107. $interface = $req_data['interface'];
  108. $cmd = $req_data['cmd'];
  109. $start_time = isset($req_data['start_time']) ? $req_data['start_time'] : '';
  110. $end_time = isset($req_data['end_time']) ? $req_data['end_time'] : '';
  111. $date = isset($req_data['date']) ? $req_data['date'] : '';
  112. $code = isset($req_data['code']) ? $req_data['code'] : '';
  113. $msg = isset($req_data['msg']) ? $req_data['msg'] : '';
  114. $offset = isset($req_data['offset']) ? $req_data['offset'] : '';
  115. $count = isset($req_data['count']) ? $req_data['count'] : 10;
  116. switch($cmd)
  117. {
  118. case 'get_statistic':
  119. $buffer = json_encode(array('modules'=>$this->getModules($module), 'statistic' => $this->getStatistic($date, $module, $interface)))."\n";
  120. $this->sendToClient($buffer);
  121. break;
  122. case 'get_log':
  123. $buffer = json_encode($this->getStasticLog($module, $interface , $start_time , $end_time, $code, $msg, $offset, $count))."\n";
  124. $this->sendToClient($buffer);
  125. break;
  126. default :
  127. $this->sendToClient('pack err');
  128. }
  129. }
  130. /**
  131. * 获取模块
  132. * @return array
  133. */
  134. public function getModules($current_module = '')
  135. {
  136. $st_dir = WORKERMAN_LOG_DIR . $this->statisticDir;
  137. $modules_name_array = array();
  138. foreach(glob($st_dir."/*", GLOB_ONLYDIR) as $module_file)
  139. {
  140. $tmp = explode("/", $module_file);
  141. $module = end($tmp);
  142. $modules_name_array[$module] = array();
  143. if($current_module == $module)
  144. {
  145. $st_dir = $st_dir.$current_module.'/';
  146. $all_interface = array();
  147. foreach(glob($st_dir."*") as $file)
  148. {
  149. if(is_dir($file))
  150. {
  151. continue;
  152. }
  153. list($interface, $date) = explode(".", basename($file));
  154. $all_interface[$interface] = $interface;
  155. }
  156. $modules_name_array[$module] = $all_interface;
  157. }
  158. }
  159. return $modules_name_array;
  160. }
  161. /**
  162. * 获得统计数据
  163. * @param string $module
  164. * @param string $interface
  165. * @param int $date
  166. * @return bool/string
  167. */
  168. protected function getStatistic($date, $module, $interface)
  169. {
  170. if(empty($module) || empty($interface))
  171. {
  172. return '';
  173. }
  174. // log文件
  175. $log_file = WORKERMAN_LOG_DIR . $this->statisticDir."{$module}/{$interface}.{$date}";
  176. $handle = @fopen($log_file, 'r');
  177. if(!$handle)
  178. {
  179. return '';
  180. }
  181. // 预处理统计数据,每5分钟一行
  182. // [time=>[ip=>['suc_count'=>xx, 'suc_cost_time'=>xx, 'fail_count'=>xx, 'fail_cost_time'=>xx, 'code_map'=>[code=>count, ..], ..], ..]
  183. $statistics_data = array();
  184. while(!feof($handle))
  185. {
  186. $line = fgets($handle, 4096);
  187. if($line)
  188. {
  189. $explode = explode("\t", $line);
  190. if(count($explode) < 7)
  191. {
  192. continue;
  193. }
  194. list($ip, $time, $suc_count, $suc_cost_time, $fail_count, $fail_cost_time, $code_map) = $explode;
  195. $time = ceil($time/300)*300;
  196. if(!isset($statistics_data[$time]))
  197. {
  198. $statistics_data[$time] = array();
  199. }
  200. if(!isset($statistics_data[$time][$ip]))
  201. {
  202. $statistics_data[$time][$ip] = array(
  203. 'suc_count' =>0,
  204. 'suc_cost_time' =>0,
  205. 'fail_count' =>0,
  206. 'fail_cost_time' =>0,
  207. 'code_map' =>array(),
  208. );
  209. }
  210. $statistics_data[$time][$ip]['suc_count'] += $suc_count;
  211. $statistics_data[$time][$ip]['suc_cost_time'] += round($suc_cost_time, 5);
  212. $statistics_data[$time][$ip]['fail_count'] += $fail_count;
  213. $statistics_data[$time][$ip]['fail_cost_time'] += round($fail_cost_time, 5);
  214. $code_map = json_decode(trim($code_map), true);
  215. if($code_map && is_array($code_map))
  216. {
  217. foreach($code_map as $code=>$count)
  218. {
  219. if(!isset($statistics_data[$time][$ip]['code_map'][$code]))
  220. {
  221. $statistics_data[$time][$ip]['code_map'][$code] = 0;
  222. }
  223. $statistics_data[$time][$ip]['code_map'][$code] +=$count;
  224. }
  225. }
  226. } // end if
  227. } // end while
  228. fclose($handle);
  229. ksort($statistics_data);
  230. // 整理数据
  231. $statistics_str = '';
  232. foreach($statistics_data as $time => $items)
  233. {
  234. foreach($items as $ip => $item)
  235. {
  236. $statistics_str .= "$ip\t$time\t{$item['suc_count']}\t{$item['suc_cost_time']}\t{$item['fail_count']}\t{$item['fail_cost_time']}\t".json_encode($item['code_map'])."\n";
  237. }
  238. }
  239. return $statistics_str;
  240. }
  241. /**
  242. * 获取指定日志
  243. *
  244. */
  245. protected function getStasticLog($module, $interface , $start_time = '', $end_time = '', $code = '', $msg = '', $offset='', $count=100)
  246. {
  247. // log文件
  248. $log_file = WORKERMAN_LOG_DIR . $this->logDir. (empty($start_time) ? date('Y-m-d') : date('Y-m-d', $start_time));
  249. if(!is_readable($log_file))
  250. {
  251. return array('offset'=>0, 'data'=>'');
  252. }
  253. // 读文件
  254. $h = fopen($log_file, 'r');
  255. // 如果有时间,则进行二分查找,加速查询
  256. if($start_time && $offset == 0 && ($file_size = filesize($log_file)) > 1024000)
  257. {
  258. $offset = $this->binarySearch(0, $file_size, $start_time-1, $h);
  259. $offset = $offset < 100000 ? 0 : $offset - 100000;
  260. }
  261. // 正则表达式
  262. $pattern = "/^([\d: \-]+)\t\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\t";
  263. if($module && $module != 'WorkerMan')
  264. {
  265. $pattern .= $module."::";
  266. }
  267. else
  268. {
  269. $pattern .= ".*::";
  270. }
  271. if($interface && $module != 'WorkerMan')
  272. {
  273. $pattern .= $interface."\t";
  274. }
  275. else
  276. {
  277. $pattern .= ".*\t";
  278. }
  279. if($code !== '')
  280. {
  281. $pattern .= "code:$code\t";
  282. }
  283. else
  284. {
  285. $pattern .= "code:\d+\t";
  286. }
  287. if($msg)
  288. {
  289. $pattern .= "msg:$msg";
  290. }
  291. $pattern .= '/';
  292. // 指定偏移位置
  293. if($offset > 0)
  294. {
  295. fseek($h, (int)$offset-1);
  296. }
  297. // 查找符合条件的数据
  298. $now_count = 0;
  299. $log_buffer = '';
  300. while(1)
  301. {
  302. if(feof($h))
  303. {
  304. break;
  305. }
  306. // 读1行
  307. $line = fgets($h);
  308. if(preg_match($pattern, $line, $match))
  309. {
  310. // 判断时间是否符合要求
  311. $time = strtotime($match[1]);
  312. if($start_time)
  313. {
  314. if($time<$start_time)
  315. {
  316. continue;
  317. }
  318. }
  319. if($end_time)
  320. {
  321. if($time>$end_time)
  322. {
  323. break;
  324. }
  325. }
  326. // 收集符合条件的log
  327. $log_buffer .= $line;
  328. if(++$now_count >= $count)
  329. {
  330. break;
  331. }
  332. }
  333. }
  334. // 记录偏移位置
  335. $offset = ftell($h);
  336. return array('offset'=>$offset, 'data'=>$log_buffer);
  337. }
  338. /**
  339. * 日志二分查找法
  340. * @param int $start_point
  341. * @param int $end_point
  342. * @param int $time
  343. * @param fd $fd
  344. * @return int
  345. */
  346. protected function binarySearch($start_point, $end_point, $time, $fd)
  347. {
  348. if($end_point - $start_point < 65535)
  349. {
  350. return $start_point;
  351. }
  352. // 计算中点
  353. $mid_point = (int)(($end_point+$start_point)/2);
  354. // 定位文件指针在中点
  355. fseek($fd, $mid_point - 1);
  356. // 读第一行
  357. $line = fgets($fd);
  358. if(feof($fd) || false === $line)
  359. {
  360. return $start_point;
  361. }
  362. // 第一行可能数据不全,再读一行
  363. $line = fgets($fd);
  364. if(feof($fd) || false === $line || trim($line) == '')
  365. {
  366. return $start_point;
  367. }
  368. // 判断是否越界
  369. $current_point = ftell($fd);
  370. if($current_point>=$end_point)
  371. {
  372. return $start_point;
  373. }
  374. // 获得时间
  375. $tmp = explode("\t", $line);
  376. $tmp_time = strtotime($tmp[0]);
  377. // 判断时间,返回指针位置
  378. if($tmp_time > $time)
  379. {
  380. return $this->binarySearch($start_point, $current_point, $time, $fd);
  381. }
  382. elseif($tmp_time < $time)
  383. {
  384. return $this->binarySearch($current_point, $end_point, $time, $fd);
  385. }
  386. else
  387. {
  388. return $current_point;
  389. }
  390. }
  391. }
  392. /**
  393. *
  394. * struct statisticPortocol
  395. * {
  396. * unsigned char module_name_len;
  397. * unsigned char interface_name_len;
  398. * float cost_time;
  399. * unsigned char success;
  400. * int code;
  401. * unsigned short msg_len;
  402. * unsigned int time;
  403. * char[module_name_len] module_name;
  404. * char[interface_name_len] interface_name;
  405. * char[msg_len] msg;
  406. * }
  407. *
  408. * @author workerman.net
  409. */
  410. class StatisticProtocol
  411. {
  412. /**
  413. * 包头长度
  414. * @var integer
  415. */
  416. const PACKAGE_FIXED_LENGTH = 17;
  417. /**
  418. * udp 包最大长度
  419. * @var integer
  420. */
  421. const MAX_UDP_PACKGE_SIZE = 65507;
  422. /**
  423. * char类型能保存的最大数值
  424. * @var integer
  425. */
  426. const MAX_CHAR_VALUE = 255;
  427. /**
  428. * usigned short 能保存的最大数值
  429. * @var integer
  430. */
  431. const MAX_UNSIGNED_SHORT_VALUE = 65535;
  432. /**
  433. * 编码
  434. * @param string $module
  435. * @param string $interface
  436. * @param float $cost_time
  437. * @param int $success
  438. * @param int $code
  439. * @param string $msg
  440. * @return string
  441. */
  442. public static function encode($module, $interface , $cost_time, $success, $code = 0,$msg = '')
  443. {
  444. // 防止模块名过长
  445. if(strlen($module) > self::MAX_CHAR_VALUE)
  446. {
  447. $module = substr($module, 0, self::MAX_CHAR_VALUE);
  448. }
  449. // 防止接口名过长
  450. if(strlen($interface) > self::MAX_CHAR_VALUE)
  451. {
  452. $interface = substr($interface, 0, self::MAX_CHAR_VALUE);
  453. }
  454. // 防止msg过长
  455. $module_name_length = strlen($module);
  456. $interface_name_length = strlen($interface);
  457. $avalible_size = self::MAX_UDP_PACKGE_SIZE - self::PACKAGE_FIXED_LENGTH - $module_name_length - $interface_name_length;
  458. if(strlen($msg) > $avalible_size)
  459. {
  460. $msg = substr($msg, 0, $avalible_size);
  461. }
  462. // 打包
  463. return pack('CCfCNnN', $module_name_length, $interface_name_length, $cost_time, $success ? 1 : 0, $code, strlen($msg), time()).$module.$interface.$msg;
  464. }
  465. /**
  466. * 解包
  467. * @param string $bin_data
  468. * @return array
  469. */
  470. public static function decode($bin_data)
  471. {
  472. // 解包
  473. $data = unpack("Cmodule_name_len/Cinterface_name_len/fcost_time/Csuccess/Ncode/nmsg_len/Ntime", $bin_data);
  474. $module = substr($bin_data, self::PACKAGE_FIXED_LENGTH, $data['module_name_len']);
  475. $interface = substr($bin_data, self::PACKAGE_FIXED_LENGTH + $data['module_name_len'], $data['interface_name_len']);
  476. $msg = substr($bin_data, self::PACKAGE_FIXED_LENGTH + $data['module_name_len'] + $data['interface_name_len']);
  477. return array(
  478. 'module' => $module,
  479. 'interface' => $interface,
  480. 'cost_time' => $data['cost_time'],
  481. 'success' => $data['success'],
  482. 'time' => $data['time'],
  483. 'code' => $data['code'],
  484. 'msg' => $msg,
  485. );
  486. }
  487. }