NodeStatusDetection.php 5.5 KB

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