NodeStatusDetection.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $jobEndTime = microtime(true);
  35. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  36. Log::info("---【{$this->description}】完成---,耗时 {$jobUsedTime} 秒");
  37. }
  38. private function checkNodeStatus(): void
  39. {
  40. $offlineCheckTimes = sysConfig('offline_check_times');
  41. $onlineNode = NodeHeartbeat::recently()->distinct()->pluck('node_id')->toArray();
  42. foreach (Node::whereIsRelay(0)->whereStatus(1)->whereNotIn('id', $onlineNode)->get() as $node) {
  43. // 10分钟内无节点负载信息则认为是后端炸了
  44. if ($offlineCheckTimes) {
  45. // 已通知次数
  46. $cacheKey = 'offline_check_times'.$node->id;
  47. $times = 1;
  48. if (Cache::has($cacheKey)) {
  49. $times = Cache::get($cacheKey);
  50. } else {
  51. Cache::put($cacheKey, 1, Day); // 键将保留24小时
  52. }
  53. if ($times > $offlineCheckTimes) {
  54. continue;
  55. }
  56. Cache::increment($cacheKey);
  57. }
  58. $data[] = [
  59. 'name' => $node->name,
  60. 'ip' => $node->ip,
  61. ];
  62. }
  63. if (isset($data)) {
  64. Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
  65. return $query->role('Super Admin');
  66. })->get(), new NodeOffline($data));
  67. }
  68. }
  69. private function checkNodeNetwork(): void
  70. {
  71. $detectionCheckTimes = sysConfig('detection_check_times');
  72. $sendText = false;
  73. $message = "| 线路 | 协议 | 状态 |\r\n| ------ | ------ | ------ |\r\n";
  74. $additionalMessage = '';
  75. foreach (Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get() as $node) {
  76. $info = false;
  77. if ($node->detection_type === 0) {
  78. continue;
  79. }
  80. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  81. if ($node->is_ddns) {
  82. $ip = gethostbyname($node->server);
  83. if (strcmp($ip, $node->server) !== 0) {
  84. $node->ip = $ip;
  85. } else {
  86. Log::warning('【节点阻断检测】检测'.$node->server.'时,IP获取失败'.$ip.' | '.$node->server);
  87. }
  88. }
  89. if ($node->detection_type !== 1) {
  90. $icmpCheck = (new NetworkDetection)->networkCheck($node->ip, true);
  91. if ($icmpCheck !== false && $icmpCheck !== '通讯正常') {
  92. $message .= '| '.$node->name.' | ICMP | '.$icmpCheck." |\r\n";
  93. $sendText = true;
  94. $info = true;
  95. }
  96. }
  97. if ($node->detection_type !== 2) {
  98. $tcpCheck = (new NetworkDetection)->networkCheck($node->ip, false, $node->single ? $node->port : 22);
  99. if ($tcpCheck !== false && $tcpCheck !== '通讯正常') {
  100. $message .= '| '.$node->name.' | TCP | '.$tcpCheck." |\r\n";
  101. $sendText = true;
  102. $info = true;
  103. }
  104. }
  105. sleep(5);
  106. // 节点检测次数
  107. if ($info && $detectionCheckTimes) {
  108. // 已通知次数
  109. $cacheKey = 'detection_check_times'.$node->id;
  110. if (Cache::has($cacheKey)) {
  111. $times = Cache::get($cacheKey);
  112. } else {
  113. // 键将保留12小时,多10分钟防意外
  114. Cache::put($cacheKey, 1, 43800);
  115. $times = 1;
  116. }
  117. if ($times < $detectionCheckTimes) {
  118. Cache::increment($cacheKey);
  119. } else {
  120. Cache::forget($cacheKey);
  121. $node->update(['status' => 0]);
  122. $additionalMessage .= "\r\n节点【{$node->name}】自动进入维护状态\r\n";
  123. }
  124. }
  125. }
  126. //只有在出现阻断线路时,才会发出警报
  127. if ($sendText) {
  128. Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
  129. return $query->role('Super Admin');
  130. })->get(), new NodeBlocked($message.$additionalMessage));
  131. Log::info("阻断日志: \r\n".$message.$additionalMessage);
  132. }
  133. // 随机生成下次检测时间
  134. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700);
  135. }
  136. }