NodeStatusDetection.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Node;
  4. use App\Models\User;
  5. use App\Notifications\NodeBlocked;
  6. use App\Notifications\NodeOffline;
  7. use App\Utils\NetworkDetection;
  8. use Cache;
  9. use Illuminate\Console\Command;
  10. use Log;
  11. use Notification;
  12. class NodeStatusDetection extends Command
  13. {
  14. protected $signature = 'node:detection';
  15. protected $description = '检测节点状态,包括节点心跳和网络状态';
  16. public function handle(): void
  17. {
  18. $jobTime = microtime(true);
  19. if (sysConfig('node_offline_notification')) {// 通知节点心跳异常
  20. $this->checkNodeStatus();
  21. }
  22. if (sysConfig('node_blocked_notification')) {// 通知节点网络状态异常
  23. $lastCheckTime = Cache::get('LastCheckTime');
  24. if (! $lastCheckTime || $lastCheckTime <= time()) {
  25. $this->checkNodeNetwork();
  26. } else {
  27. Log::info(trans('notification.next_check_time', ['time' => date('Y-m-d H:i:s', $lastCheckTime)]));
  28. }
  29. }
  30. $jobTime = round(microtime(true) - $jobTime, 4);
  31. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  32. }
  33. private function checkNodeStatus(): void
  34. {
  35. $offlineCheckTimes = sysConfig('offline_check_times');
  36. $data = [];
  37. foreach (Node::whereRelayNodeId(null)->whereStatus(1)->whereDoesntHave('latestHeartbeat')->get() as $node) {
  38. // 近期无节点负载信息则认为是后端炸了
  39. if ($offlineCheckTimes > 0) {
  40. $times = $this->updateCache('offline_check_times'.$node->id, 24);
  41. if ($times > $offlineCheckTimes) {
  42. continue;
  43. }
  44. }
  45. $data[] = [
  46. 'name' => $node->name,
  47. 'host' => $node->host,
  48. ];
  49. }
  50. if (! empty($data)) {
  51. Notification::send(User::find(1), new NodeOffline($data));
  52. }
  53. }
  54. private function updateCache(string $key, int $durationInHour): int
  55. {
  56. if (! Cache::has($key)) {
  57. Cache::put($key, 1, now()->addHours($durationInHour));
  58. return 1;
  59. }
  60. return Cache::increment($key);
  61. }
  62. private function checkNodeNetwork(): void
  63. {
  64. $detectionCheckTimes = sysConfig('detection_check_times');
  65. foreach (Node::whereStatus(1)->where('detection_type', '<>', 0)->get() as $node) {
  66. $node_id = $node->id;
  67. $data = [];
  68. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  69. foreach ($node->ips() as $ip) {
  70. if ($node->detection_type) {
  71. $status = NetworkDetection::networkStatus($ip, $node->port ?? 22);
  72. if ($node->detection_type !== 1 && $status['icmp'] !== 1) {
  73. $data[$node_id][$ip]['icmp'] = trans("admin.network_status.{$status['icmp']}");
  74. }
  75. if ($node->detection_type !== 2 && $status['tcp'] !== 1) {
  76. $data[$node_id][$ip]['tcp'] = trans("admin.network_status.{$status['tcp']}");
  77. }
  78. sleep(2);
  79. }
  80. }
  81. // 节点检测次数
  82. if (! empty($data[$node_id]) && $detectionCheckTimes) {
  83. // 已通知次数
  84. $cacheKey = 'detection_check_times'.$node_id;
  85. $times = $this->updateCache($cacheKey, 12);
  86. if ($times > $detectionCheckTimes) {
  87. Cache::forget($cacheKey);
  88. $node->update(['status' => 0]);
  89. $data[$node_id]['message'] = trans('notification.into_maintenance');
  90. }
  91. }
  92. if (! empty($data[$node_id])) {
  93. $data[$node_id]['name'] = $node->name;
  94. }
  95. }
  96. if (! empty($data)) { // 只有在出现阻断线路时,才会发出警报
  97. Notification::send(User::find(1), new NodeBlocked($data));
  98. Log::notice(trans('notification.node_block').": \r\n".var_export($data, true));
  99. }
  100. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700); // 随机生成下次检测时间
  101. $this->reliveNode();
  102. }
  103. private function reliveNode(): void
  104. {
  105. foreach (Node::whereRelayNodeId(null)->whereStatus(0)->where('detection_type', '<>', 0)->whereHas('latestHeartbeat')->get() as $node) {
  106. $ips = $node->ips();
  107. $reachableIPs = 0;
  108. foreach ($ips as $ip) {
  109. if ($node->detection_type) {
  110. $status = NetworkDetection::networkStatus($ip, $node->port ?? 22);
  111. if (($node->detection_type === 1 && $status['tcp'] === 1) || ($node->detection_type === 2 && $status['icmp'] === 1) || ($status['tcp'] === 1 && $status['icmp'] === 1)) {
  112. $reachableIPs++;
  113. }
  114. sleep(1);
  115. }
  116. }
  117. if ($reachableIPs === count($ips)) {
  118. $node->update(['status' => 1]);
  119. }
  120. }
  121. }
  122. }