StatisticWorker.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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, 'time'=>xxx]
  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. public function dealInput($recv_str)
  52. {
  53. return 0;
  54. }
  55. /**
  56. * 业务处理
  57. * @see Man\Core.SocketWorker::dealProcess()
  58. */
  59. public function dealProcess($recv_str)
  60. {
  61. // 解码
  62. $unpack_data = StatisticProtocol::decode($recv_str);
  63. $module = $unpack_data['module'];
  64. $interface = $unpack_data['interface'];
  65. $cost_time = $unpack_data['cost_time'];
  66. $success = $unpack_data['success'];
  67. $time = $unpack_data['time'];
  68. $code = $unpack_data['code'];
  69. $msg = str_replace("\n", "<br>", $unpack_data['msg']);
  70. $ip = $this->getRemoteIp();
  71. // 模块接口统计
  72. $this->collectStatistics($module, $interface, $cost_time, $success, $ip, $code, $msg);
  73. // 全局统计
  74. $this->collectStatistics('WorkerMan', 'Statistics', $cost_time, $success, $ip, $code, $msg);
  75. // 失败记录日志
  76. if(!$success)
  77. {
  78. $this->logBuffer .= date('Y-m-d H:i:s',$time)."\t$ip\t$module::$interface\tcode:$code\tmsg:$msg\n";
  79. if(strlen($this->logBuffer) >= self::MAX_LOG_BUFFER_SZIE)
  80. {
  81. $this->writeLogToDisk();
  82. }
  83. }
  84. }
  85. /**
  86. * 收集统计数据
  87. * @param string $module
  88. * @param string $interface
  89. * @param float $cost_time
  90. * @param int $success
  91. * @param string $ip
  92. * @param int $code
  93. * @param string $msg
  94. * @return void
  95. */
  96. protected function collectStatistics($module, $interface , $cost_time, $success, $ip, $code, $msg)
  97. {
  98. // 统计相关信息
  99. if(!isset($this->statisticData[$ip]))
  100. {
  101. $this->statisticData[$ip] = array();
  102. }
  103. if(!isset($this->statisticData[$ip][$module]))
  104. {
  105. $this->statisticData[$ip][$module] = array();
  106. }
  107. if(!isset($this->statisticData[$ip][$module][$interface]))
  108. {
  109. $this->statisticData[$ip][$module][$interface] = array('code'=>array(), 'suc_cost_time'=>0, 'fail_cost_time'=>0, 'suc_count'=>0, 'fail_count'=>0, 'time'=>$this->stLastWriteTime);
  110. }
  111. if(!isset($this->statisticData[$ip][$module][$interface]['code'][$code]))
  112. {
  113. $this->statisticData[$ip][$module][$interface]['code'][$code] = 0;
  114. }
  115. $this->statisticData[$ip][$module][$interface]['code'][$code]++;
  116. if($success)
  117. {
  118. $this->statisticData[$ip][$module][$interface]['suc_cost_time'] += $cost_time;
  119. $this->statisticData[$ip][$module][$interface]['suc_count'] ++;
  120. }
  121. else
  122. {
  123. $this->statisticData[$ip][$module][$interface]['fail_cost_time'] += $cost_time;
  124. $this->statisticData[$ip][$module][$interface]['fail_count'] ++;
  125. }
  126. }
  127. /**
  128. * 将统计数据写入磁盘
  129. * @return void
  130. */
  131. public function writeStatisticsToDisk()
  132. {
  133. $time = time();
  134. // 循环将每个ip的统计数据写入磁盘
  135. foreach($this->statisticData as $ip => $mod_if_data)
  136. {
  137. foreach($mod_if_data as $module=>$items)
  138. {
  139. // 文件夹不存在则创建一个
  140. $file_dir = WORKERMAN_LOG_DIR . $this->statisticDir.$module;
  141. if(!is_dir($file_dir))
  142. {
  143. umask(0);
  144. mkdir($file_dir, 0777, true);
  145. }
  146. // 依次写入磁盘
  147. foreach($items as $interface=>$data)
  148. {
  149. 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);
  150. }
  151. }
  152. }
  153. // 清空统计
  154. $this->statisticData = array();
  155. }
  156. /**
  157. * 将日志数据写入磁盘
  158. * @return void
  159. */
  160. public function writeLogToDisk()
  161. {
  162. // 没有统计数据则返回
  163. if(empty($this->logBuffer))
  164. {
  165. return;
  166. }
  167. // 写入磁盘
  168. file_put_contents(WORKERMAN_LOG_DIR . $this->logDir . date('Y-m-d'), $this->logBuffer, FILE_APPEND | LOCK_EX);
  169. $this->logBuffer = '';
  170. }
  171. /**
  172. * 初始化
  173. * 统计目录检查
  174. * 初始化任务
  175. * @see Man\Core.SocketWorker::onStart()
  176. */
  177. protected function onStart()
  178. {
  179. // 初始化目录
  180. umask(0);
  181. $statistic_dir = WORKERMAN_LOG_DIR . $this->statisticDir;
  182. if(!is_dir($statistic_dir))
  183. {
  184. mkdir($statistic_dir, 0777, true);
  185. }
  186. $log_dir = WORKERMAN_LOG_DIR . $this->logDir;
  187. if(!is_dir($log_dir))
  188. {
  189. mkdir($log_dir, 0777, true);
  190. }
  191. // 初始化任务
  192. \Man\Core\Lib\Task::init($this->event);
  193. // 定时保存统计数据
  194. \Man\Core\Lib\Task::add(self::WRITE_PERIOD_LENGTH, array($this, 'writeStatisticsToDisk'));
  195. \Man\Core\Lib\Task::add(self::WRITE_PERIOD_LENGTH, array($this, 'writeLogToDisk'));
  196. // 定时清理不用的统计数据
  197. \Man\Core\Lib\Task::add(self::CLEAR_PERIOD_LENGTH, array($this, 'clearDisk'), array(WORKERMAN_LOG_DIR . $this->statisticDir, self::EXPIRED_TIME));
  198. \Man\Core\Lib\Task::add(self::CLEAR_PERIOD_LENGTH, array($this, 'clearDisk'), array(WORKERMAN_LOG_DIR . $this->logDir, self::EXPIRED_TIME));
  199. }
  200. /**
  201. * 进程停止时需要将数据写入磁盘
  202. * @see Man\Core.SocketWorker::onStop()
  203. */
  204. protected function onStop()
  205. {
  206. $this->writeLogToDisk();
  207. $this->writeStatisticsToDisk();
  208. }
  209. /**
  210. * 清除磁盘数据
  211. * @param string $file
  212. * @param int $exp_time
  213. */
  214. protected function clearDisk($file = null, $exp_time = 86400)
  215. {
  216. $time_now = time();
  217. if(is_file($file))
  218. {
  219. $mtime = filemtime($file);
  220. if(!$mtime)
  221. {
  222. $this->notice("filemtime $file fail");
  223. return;
  224. }
  225. if($time_now - $mtime > $exp_time)
  226. {
  227. unlink($file);
  228. }
  229. return;
  230. }
  231. foreach (glob($file."/*") as $file_name) {
  232. if(is_dir($file_name))
  233. {
  234. $this->clearDisk($file_name, $exp_time);
  235. continue;
  236. }
  237. $mtime = filemtime($file);
  238. if(!$mtime)
  239. {
  240. $this->notice("filemtime $file fail");
  241. return;
  242. }
  243. if($time_now - $mtime > $exp_time)
  244. {
  245. unlink($file_name);
  246. }
  247. }
  248. }
  249. }