Apilog.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace addons\apilog;
  3. use app\common\library\Menu;
  4. use think\Addons;
  5. use think\addons\Service;
  6. use think\Request;
  7. use app\common\library\Auth;
  8. use addons\apilog\model\Apilog as ModelApilog;
  9. use think\Cache;
  10. use app\common\library\Email;
  11. /**
  12. * 插件
  13. */
  14. class Apilog extends Addons
  15. {
  16. /**
  17. * 插件安装方法
  18. * @return bool
  19. */
  20. public function install()
  21. {
  22. $menu = [
  23. [
  24. 'name' => 'apilog',
  25. 'title' => 'API访问监测分析',
  26. 'icon' => 'fa fa-pie-chart',
  27. 'ismenu' => 1,
  28. 'sublist' => [
  29. [
  30. "name" => "apilog/data",
  31. "title" => "基础数据",
  32. "ismenu" => 1,
  33. "icon" => "fa fa-dashboard",
  34. 'sublist' => [
  35. ['name' => 'apilog/data/index', 'title' => '查看'],
  36. ]
  37. ],
  38. [
  39. "name" => "apilog/trend",
  40. "title" => "趋势数据",
  41. "ismenu" => 1,
  42. "icon" => "fa fa-area-chart",
  43. 'sublist' => [
  44. ['name' => 'apilog/trend/index', 'title' => '查看'],
  45. ]
  46. ],
  47. [
  48. "name" => "apilog/index",
  49. "title" => "请求列表",
  50. "ismenu" => 1,
  51. "icon" => "fa fa-list",
  52. 'sublist' => [
  53. ['name' => 'apilog/index/index', 'title' => '查看'],
  54. ['name' => 'apilog/index/del', 'title' => '删除'],
  55. ['name' => 'apilog/index/detail', 'title' => '详情'],
  56. ['name' => 'apilog/index/banip', 'title' => '禁用IP'],
  57. ['name' => 'apilog/index/clear', 'title' => '清空数据'],
  58. ]
  59. ],
  60. ]
  61. ]
  62. ];
  63. Menu::create($menu);
  64. Service::refresh();
  65. return true;
  66. }
  67. /**
  68. * 插件卸载方法
  69. * @return bool
  70. */
  71. public function uninstall()
  72. {
  73. Menu::delete("apilog");
  74. Service::refresh();
  75. return true;
  76. }
  77. /**
  78. * 插件启用方法
  79. * @return bool
  80. */
  81. public function enable()
  82. {
  83. Menu::enable('apilog');
  84. return true;
  85. }
  86. /**
  87. * 插件禁用方法
  88. * @return bool
  89. */
  90. public function disable()
  91. {
  92. Menu::disable("apilog");
  93. return true;
  94. }
  95. public function responseSend(&$params)
  96. {
  97. try {
  98. if (Request::instance()->module() == "api") {
  99. $log['time'] = (microtime(true) - Request::instance()->time(true)) * 1000;
  100. $auth = Auth::instance();
  101. $user_id = $auth->isLogin() ? $auth->id : 0;
  102. $username = $auth->isLogin() ? $auth->username : __('Unknown');
  103. $log['url'] = substr(Request::instance()->baseUrl(), 0, 200);
  104. $log['method'] = Request::instance()->method();
  105. $log['param'] = json_encode(Request::instance()->param());
  106. $log['ip'] = Request::instance()->ip();
  107. $log['ua'] = substr(Request::instance()->header('user-agent'), 0, 200);
  108. $log['controller'] = Request::instance()->controller();
  109. $log['action'] = Request::instance()->action();
  110. $log['code'] = $params->getCode();
  111. $log['user_id'] = $user_id;
  112. $log['username'] = $username;
  113. $log['response'] = $params->getContent();
  114. (new ModelApilog)->save($log);
  115. $config = get_addon_config('apilog');
  116. //状态码记录
  117. if ($config['error']['open'] == 1) {
  118. $count_code = Cache::get('countcode', null);
  119. if (is_null($count_code)) {
  120. Cache::set('countcode', 0, $config['error']['pl']);
  121. $tagkey = Cache::get('tag_' . md5('code'));
  122. $keys = $tagkey ? array_filter(explode(',', $tagkey)) : [];
  123. foreach ($keys as $k => $v) {
  124. Cache::rm($v);
  125. }
  126. Cache::rm($tagkey);
  127. }
  128. $count_code = Cache::inc('countcode');
  129. $k_code = 'code:' . $params->getCode();
  130. $yj_code = Cache::get($k_code, null);
  131. if (is_null($yj_code)) {
  132. Cache::set($k_code, 0, 0);
  133. Cache::tag('code', $k_code);
  134. }
  135. Cache::inc($k_code);
  136. $codes = array_filter(explode(',', $config['error']['sj']));
  137. $now = 0;
  138. foreach ($codes as $k => $v) {
  139. $now += Cache::get('code:' . $v, 0);
  140. }
  141. if ($now / $count_code >= $config['error']['zb'] / 100) {
  142. // echo '触发错误预警' . $now / $count_code;
  143. $this->emailnotify($config['base']['email'], '请求错误监控', '当前api请求错误率已达到【' . round($now / $count_code * 100, 2) . '%】,请及时关注!');
  144. }
  145. }
  146. //超时记录数
  147. if ($config['time']['open'] == 1) {
  148. $count_time = Cache::get('counttime', null);
  149. if (is_null($count_time)) {
  150. Cache::set('counttime', 0, $config['time']['pl']);
  151. Cache::rm('time');
  152. }
  153. $tot_time = Cache::inc('counttime');
  154. if ($log['time'] > $config['time']['sj']) {
  155. $yj_time = Cache::get('time', null);
  156. if (is_null($yj_time)) {
  157. Cache::set('time', 0, 0);
  158. }
  159. $now_time = Cache::inc('time');
  160. if ($now_time / $tot_time >= $config['time']['zb'] / 100) {
  161. // echo '触发超时预警' . $now_time / $tot_time;
  162. $this->emailnotify($config['base']['email'], '响应超时监控', '当前api响应超时请求占比已达到【' . round($now_time / $tot_time * 100, 2) . '%】,请及时关注!');
  163. }
  164. }
  165. }
  166. }
  167. } catch (\Exception $e) {
  168. //宁可不记录也不能影响api的正常访问
  169. }
  170. }
  171. public function moduleInit(&$params)
  172. {
  173. try {
  174. if (Request::instance()->module() == "api") {
  175. $ip = 'banip:' . Request::instance()->ip();
  176. $cacheIp = Cache::get($ip);
  177. if ($cacheIp !== false) {
  178. $this->respone(500, '抱歉,您的IP已被禁止访问');
  179. }
  180. $config = get_addon_config('apilog');
  181. //总请求数
  182. if ($config['count']['open'] == 1) {
  183. $yj_count = Cache::get('count', null);
  184. if (is_null($yj_count)) {
  185. Cache::set('count', 0, $config['count']['pl']);
  186. }
  187. Cache::inc('count');
  188. //预警
  189. if ($yj_count + 1 >= $config['count']['max']) {
  190. Cache::rm('count');
  191. // $this->respone(500, '触发请求量预警');
  192. $this->emailnotify($config['base']['email'], '请求量监控', '当前最大请求数量已达到【' . ++$yj_count . '次】,请及时关注!');
  193. }
  194. }
  195. //IP访问请求数
  196. if ($config['ip']['open'] == 1) {
  197. $count_ip = Cache::get('countip', null);
  198. if (is_null($count_ip)) {
  199. Cache::set('countip', 0, $config['ip']['pl']);
  200. $tagkey = Cache::get('tag_' . md5('ip'));
  201. $keys = $tagkey ? array_filter(explode(',', $tagkey)) : [];
  202. foreach ($keys as $k => $v) {
  203. Cache::rm($v);
  204. }
  205. Cache::rm($tagkey);
  206. }
  207. $count_ip = Cache::inc('countip');
  208. $k_ip = 'ip:' . Request::instance()->ip();
  209. $yj_ip = Cache::get($k_ip, null);
  210. if (is_null($yj_ip)) {
  211. Cache::set($k_ip, 0, 0);
  212. Cache::tag('ip', $k_ip);
  213. }
  214. $this_ip = Cache::inc($k_ip);
  215. //白名单
  216. $white = array_filter(explode(',', $config['ip']['white']));
  217. //预警
  218. if (!in_array(Request::instance()->ip(), $white) && $this_ip / $count_ip >= $config['ip']['zb'] / 100) {
  219. //$this->respone(500, '触发IP预警');
  220. $this->emailnotify($config['base']['email'], 'IP异常监控', 'IP【' . Request::instance()->ip()
  221. . '】的访问请求占比已达到【' . round($this_ip / $count_ip * 100, 2) . '%】,请及时关注!');
  222. }
  223. }
  224. }
  225. } catch (\Exception $e) {
  226. //宁可不记录也不能影响api的正常访问
  227. }
  228. }
  229. protected function respone($code, $msg)
  230. {
  231. $result = [
  232. 'code' => $code,
  233. 'msg' => $msg,
  234. 'time' => Request::instance()->server('REQUEST_TIME'),
  235. 'data' => null,
  236. ];
  237. $type = Request::instance()->param(config('var_jsonp_handler')) ? 'jsonp' : 'json';
  238. $response = \think\Response::create($result, $type, 500);
  239. throw new \think\exception\HttpResponseException($response);
  240. }
  241. /**
  242. * 发送邮件预警
  243. * 同类型预警半小时最多发一次
  244. */
  245. protected function emailnotify($receiver, $subject, $content)
  246. {
  247. $cache = Cache::get('notify:' . $subject, null);
  248. if (is_null($cache)) {
  249. $email = new Email;
  250. $result = $email
  251. ->to($receiver)
  252. ->subject('【API预警】' . $subject)
  253. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . $content . '</div>')
  254. ->send();
  255. if ($result) {
  256. Cache::set('notify:' . $subject, 1, 1800);
  257. }
  258. }
  259. }
  260. }