AutoCheckNodeStatus.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use Illuminate\Console\Command;
  5. use App\Components\ServerChan;
  6. use App\Http\Models\EmailLog;
  7. use App\Http\Models\SsNode;
  8. use App\Http\Models\SsNodeInfo;
  9. use App\Mail\nodeCrashWarning;
  10. use Cache;
  11. use Mail;
  12. use Log;
  13. class AutoCheckNodeStatus extends Command
  14. {
  15. protected $signature = 'autoCheckNodeStatus';
  16. protected $description = '自动检测节点状态';
  17. protected static $systemConfig;
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. self::$systemConfig = Helpers::systemConfig();
  22. }
  23. public function handle()
  24. {
  25. $jobStartTime = microtime(true);
  26. // 监测节点状态
  27. if (self::$systemConfig['is_tcp_check']) {
  28. $this->checkNodes();
  29. }
  30. $jobEndTime = microtime(true);
  31. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  32. Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
  33. }
  34. // 监测节点状态
  35. private function checkNodes()
  36. {
  37. $title = "节点异常警告";
  38. $nodeList = SsNode::query()->where('status', 1)->where('is_tcp_check', 1)->get();
  39. foreach ($nodeList as $node) {
  40. $tcpCheck = $this->tcpCheck($node->ip);
  41. if (false !== $tcpCheck) {
  42. switch ($tcpCheck) {
  43. case 1:
  44. $text = '服务器宕机';
  45. break;
  46. case 2:
  47. $text = '海外不通';
  48. break;
  49. case 3:
  50. $text = 'TCP阻断';
  51. break;
  52. case 0:
  53. default:
  54. $text = '正常';
  55. }
  56. // 异常才发通知消息
  57. if ($tcpCheck) {
  58. if (self::$systemConfig['tcp_check_warning_times']) {
  59. // 已通知次数
  60. $cacheKey = 'tcp_check_warning_times_' . $node->id;
  61. if (Cache::has($cacheKey)) {
  62. $times = Cache::get($cacheKey);
  63. } else {
  64. Cache::put($cacheKey, 1, 725); // 因为每小时检测一次,最多设置提醒12次,12*60=720分钟缓存时效,多5分钟防止异常
  65. $times = 1;
  66. }
  67. if ($times < self::$systemConfig['tcp_check_warning_times']) {
  68. Cache::increment('tcp_check_warning_times_' . $node->id);
  69. $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**", $node->name, $node->server);
  70. } elseif ($times >= self::$systemConfig['tcp_check_warning_times']) {
  71. Cache::forget('tcp_check_warning_times_' . $node->id);
  72. SsNode::query()->where('id', $node->id)->update(['status' => 0]);
  73. $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**,节点自动进入维护状态", $node->name, $node->server);
  74. }
  75. } else {
  76. $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**", $node->name, $node->server);
  77. }
  78. }
  79. Log::info("【TCP阻断检测】" . $node->name . ' - ' . $node->ip . ' - ' . $text);
  80. }
  81. // 10分钟内无节点负载信息且TCP检测认为不是宕机则认为是SSR(R)后端炸了
  82. $nodeTTL = SsNodeInfo::query()->where('node_id', $node->id)->where('log_time', '>=', strtotime("-10 minutes"))->orderBy('id', 'desc')->first();
  83. if ($tcpCheck !== 1 && !$nodeTTL) {
  84. $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**异常:**心跳异常**", $node->name, $node->server);
  85. }
  86. // 天若有情天亦老,我为长者续一秒
  87. sleep(1);
  88. }
  89. }
  90. /**
  91. * 用ipcheck.need.sh进行TCP阻断检测
  92. *
  93. * @param string $ip 被检测的IP
  94. *
  95. * @return bool|int
  96. */
  97. private function tcpCheck($ip)
  98. {
  99. $url = 'https://ipcheck.need.sh/api_v2.php?ip=' . $ip;
  100. $ret = $this->curlRequest($url);
  101. $ret = json_decode($ret);
  102. if (!$ret || $ret->result != 'success') {
  103. Log::warning("【TCP阻断检测】ipcheck.need.sh的TCP阻断检测接口挂了");
  104. return false;
  105. }
  106. if (!$ret->data->inside_gfw->tcp->alive && !$ret->data->outside_gfw->tcp->alive) {
  107. return 1; // 服务器宕机或者检测接口挂了
  108. } elseif ($ret->data->inside_gfw->tcp->alive && !$ret->data->outside_gfw->tcp->alive) {
  109. return 2; // 国外访问异常
  110. } elseif (!$ret->data->inside_gfw->tcp->alive && $ret->data->outside_gfw->tcp->alive) {
  111. return 3; // 被墙
  112. } else {
  113. return 0; // 正常
  114. }
  115. }
  116. /**
  117. * 通知管理员
  118. *
  119. * @param string $title 消息标题
  120. * @param string $content 消息内容
  121. * @param string $nodeName 节点名称
  122. * @param string $nodeServer 节点域名
  123. */
  124. private function notifyMaster($title, $content, $nodeName, $nodeServer)
  125. {
  126. $this->notifyMasterByEmail($title, $content, $nodeName, $nodeServer);
  127. $this->notifyMasterByServerchan($title, $content);
  128. }
  129. /**
  130. * 发邮件通知管理员
  131. *
  132. * @param string $title 消息标题
  133. * @param string $content 消息内容
  134. * @param string $nodeName 节点名称
  135. * @param string $nodeServer 节点域名
  136. */
  137. private function notifyMasterByEmail($title, $content, $nodeName, $nodeServer)
  138. {
  139. if (self::$systemConfig['is_node_crash_warning'] && self::$systemConfig['crash_warning_email']) {
  140. try {
  141. Mail::to(self::$systemConfig['crash_warning_email'])->send(new nodeCrashWarning(self::$systemConfig['website_name'], $nodeName, $nodeServer));
  142. $this->addEmailLog(1, $title, $content);
  143. } catch (\Exception $e) {
  144. $this->addEmailLog(1, $title, $content, 0, $e->getMessage());
  145. }
  146. }
  147. }
  148. /**
  149. * 通过ServerChan发微信消息提醒管理员
  150. *
  151. * @param string $title 消息标题
  152. * @param string $content 消息内容
  153. */
  154. private function notifyMasterByServerchan($title, $content)
  155. {
  156. if (self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
  157. $serverChan = new ServerChan();
  158. $serverChan->send($title, $content);
  159. }
  160. }
  161. /**
  162. * 添加邮件发送日志
  163. *
  164. * @param int $userId 接收者用户ID
  165. * @param string $title 标题
  166. * @param string $content 内容
  167. * @param int $status 投递状态
  168. * @param string $error 投递失败时记录的异常信息
  169. */
  170. private function addEmailLog($userId, $title, $content, $status = 1, $error = '')
  171. {
  172. $emailLogObj = new EmailLog();
  173. $emailLogObj->user_id = $userId;
  174. $emailLogObj->title = $title;
  175. $emailLogObj->content = $content;
  176. $emailLogObj->status = $status;
  177. $emailLogObj->error = $error;
  178. $emailLogObj->created_at = date('Y-m-d H:i:s');
  179. $emailLogObj->save();
  180. }
  181. /**
  182. * 发起一个CURL请求
  183. *
  184. * @param string $url 请求地址
  185. * @param array $data POST数据,留空则为GET
  186. *
  187. * @return mixed
  188. */
  189. private function curlRequest($url, $data = [])
  190. {
  191. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  192. $ch = curl_init();
  193. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  194. curl_setopt($ch, CURLOPT_TIMEOUT, 500);
  195. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  196. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  197. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  198. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  199. curl_setopt($ch, CURLOPT_URL, $url);
  200. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  201. 'Accept: application/json', // 请求报头
  202. 'Content-Type: application/json', // 实体报头
  203. 'Content-Length: ' . strlen($data)
  204. ]);
  205. // 如果data有数据,则用POST请求
  206. if ($data) {
  207. curl_setopt($ch, CURLOPT_POST, 1);
  208. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  209. }
  210. $result = curl_exec($ch);
  211. curl_close($ch);
  212. return $result;
  213. }
  214. }