NodeStatusDetection.php 5.3 KB

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