NodeStatusDetection.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\NetworkDetection;
  4. use App\Models\Node;
  5. use App\Models\NodeHeartbeat;
  6. use App\Models\User;
  7. use App\Notifications\NodeBlocked;
  8. use App\Notifications\NodeOffline;
  9. use Cache;
  10. use Illuminate\Console\Command;
  11. use Log;
  12. use Notification;
  13. class NodeStatusDetection extends Command
  14. {
  15. protected $signature = 'nodeStatusDetection';
  16. protected $description = '节点状态检测';
  17. public function handle(): void
  18. {
  19. $jobStartTime = microtime(true);
  20. // 检测节点心跳是否异常
  21. if (sysConfig('node_offline_notification')) {
  22. $this->checkNodeStatus();
  23. }
  24. // 监测节点网络状态
  25. if (sysConfig('node_blocked_notification')) {
  26. if (! Cache::has('LastCheckTime')) {
  27. $this->checkNodeNetwork();
  28. } elseif (Cache::get('LastCheckTime') <= time()) {
  29. $this->checkNodeNetwork();
  30. } else {
  31. Log::info('下次节点阻断检测时间:'.date('Y-m-d H:i:s', Cache::get('LastCheckTime')));
  32. }
  33. }
  34. //$this->checkNodeWeihu();
  35. $jobEndTime = microtime(true);
  36. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  37. Log::info("---【{$this->description}】完成---,耗时 {$jobUsedTime} 秒");
  38. }
  39. private function checkNodeWeihu(){
  40. $query = Node::with(['onlineLogs', 'dailyDataFlows']);
  41. $nodeList = $query->where("country_code","=","hk");
  42. foreach ($nodeList as $node) {
  43. // 在线人数
  44. $online_log = $node->onlineLogs()->where('log_time', '>=', strtotime('-5 minutes'))->latest('log_time')->first();
  45. $node->online_users = $online_log->online_user ?? 0;
  46. Log::info("---【{$this->description}】节点名称{$node->name}---,在线人数 {$node->online_users} ----");
  47. // // 已产生流量
  48. // $node->transfer = flowAutoShow($node->dailyDataFlows()->sum('total'));
  49. //
  50. // // 负载(10分钟以内)
  51. // $node_info = $node->heartbeats()->recently()->first();
  52. // $node->isOnline = empty($node_info) || empty($node_info->load) ? 0 : 1;
  53. // $node->load = $node->isOnline ? $node_info->load : '离线';
  54. // $node->uptime = empty($node_info) ? 0 : seconds2time($node_info->uptime);
  55. }
  56. }
  57. private function checkNodeStatus(): void
  58. {
  59. $offlineCheckTimes = sysConfig('offline_check_times');
  60. $onlineNode = NodeHeartbeat::recently()->distinct()->pluck('node_id')->toArray();
  61. foreach (Node::whereIsRelay(0)->whereStatus(1)->whereNotIn('id', $onlineNode)->get() as $node) {
  62. // 10分钟内无节点负载信息则认为是后端炸了
  63. if ($offlineCheckTimes) {
  64. // 已通知次数
  65. $cacheKey = 'offline_check_times'.$node->id;
  66. $times = 1;
  67. if (Cache::has($cacheKey)) {
  68. $times = Cache::get($cacheKey);
  69. } else {
  70. Cache::put($cacheKey, 1, Day); // 键将保留24小时
  71. }
  72. if ($times > $offlineCheckTimes) {
  73. continue;
  74. }
  75. Cache::increment($cacheKey);
  76. }
  77. $data[] = [
  78. 'name' => $node->name,
  79. 'ip' => $node->ip,
  80. ];
  81. }
  82. if (isset($data)) {
  83. Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
  84. return $query->role('Super Admin');
  85. })->get(), new NodeOffline($data));
  86. }
  87. }
  88. private function checkNodeNetwork(): void
  89. {
  90. $detectionCheckTimes = sysConfig('detection_check_times');
  91. $sendText = false;
  92. $message = "| 线路 | 协议 | 状态 |\r\n| ------ | ------ | ------ |\r\n";
  93. $additionalMessage = '';
  94. foreach (Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get() as $node) {
  95. $info = false;
  96. if ($node->detection_type === 0) {
  97. continue;
  98. }
  99. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  100. if ($node->is_ddns) {
  101. $ip = gethostbyname($node->server);
  102. if (strcmp($ip, $node->server) !== 0) {
  103. $node->ip = $ip;
  104. } else {
  105. Log::warning('【节点阻断检测】检测'.$node->server.'时,IP获取失败'.$ip.' | '.$node->server);
  106. }
  107. }
  108. if ($node->detection_type !== 1) {
  109. $icmpCheck = (new NetworkDetection)->networkCheck($node->ip, true);
  110. if ($icmpCheck !== false && $icmpCheck !== '通讯正常') {
  111. $message .= '| '.$node->name.' | ICMP | '.$icmpCheck." |\r\n";
  112. $sendText = true;
  113. $info = true;
  114. }
  115. }
  116. if ($node->detection_type !== 2) {
  117. $tcpCheck = (new NetworkDetection)->networkCheck($node->ip, false, $node->single ? $node->port : 22);
  118. if ($tcpCheck !== false && $tcpCheck !== '通讯正常') {
  119. $message .= '| '.$node->name.' | TCP | '.$tcpCheck." |\r\n";
  120. $sendText = true;
  121. $info = true;
  122. }
  123. }
  124. sleep(5);
  125. // 节点检测次数
  126. if ($info && $detectionCheckTimes) {
  127. // 已通知次数
  128. $cacheKey = 'detection_check_times'.$node->id;
  129. if (Cache::has($cacheKey)) {
  130. $times = Cache::get($cacheKey);
  131. } else {
  132. // 键将保留12小时,多10分钟防意外
  133. Cache::put($cacheKey, 1, 43800);
  134. $times = 1;
  135. }
  136. if ($times < $detectionCheckTimes) {
  137. Cache::increment($cacheKey);
  138. } else {
  139. Cache::forget($cacheKey);
  140. $node->update(['status' => 0]);
  141. $additionalMessage .= "\r\n节点【{$node->name}】自动进入维护状态\r\n";
  142. }
  143. }
  144. }
  145. //只有在出现阻断线路时,才会发出警报
  146. if ($sendText) {
  147. Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
  148. return $query->role('Super Admin');
  149. })->get(), new NodeBlocked($message.$additionalMessage));
  150. Log::info("阻断日志: \r\n".$message.$additionalMessage);
  151. }
  152. // 随机生成下次检测时间
  153. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700);
  154. }
  155. }