NodeStatusDetection.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 = 'nodeStatusDetection';
  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. $cacheKey = 'offline_check_times'.$node->id;
  43. if (! Cache::has($cacheKey)) { // 已通知次数
  44. Cache::put($cacheKey, 1, now()->addDay()); // 键将保留24小时
  45. } else {
  46. $times = Cache::increment($cacheKey);
  47. if ($times > $offlineCheckTimes) {
  48. continue;
  49. }
  50. }
  51. }
  52. $data[] = [
  53. 'name' => $node->name,
  54. 'host' => $node->host,
  55. ];
  56. }
  57. if (! empty($data)) {
  58. Notification::send(User::find(1), new NodeOffline($data));
  59. }
  60. }
  61. private function checkNodeNetwork(): void
  62. {
  63. $detectionCheckTimes = sysConfig('detection_check_times');
  64. foreach (Node::whereStatus(1)->where('detection_type', '<>', 0)->get() as $node) {
  65. $node_id = $node->id;
  66. $data = [];
  67. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  68. foreach ($node->ips() as $ip) {
  69. if ($node->detection_type) {
  70. $status = (new NetworkDetection)->networkStatus($ip, $node->port ?? 22);
  71. if ($node->detection_type !== 1 && $status['icmp'] !== 1) {
  72. $data[$node_id][$ip]['icmp'] = config('common.network_status')[$status['icmp']];
  73. }
  74. if ($node->detection_type !== 2 && $status['tcp'] !== 1) {
  75. $data[$node_id][$ip]['tcp'] = config('common.network_status')[$status['tcp']];
  76. }
  77. sleep(2);
  78. }
  79. }
  80. // 节点检测次数
  81. if (! empty($data[$node_id]) && $detectionCheckTimes) {
  82. // 已通知次数
  83. $cacheKey = 'detection_check_times'.$node_id;
  84. if (! Cache::has($cacheKey)) { // 已通知次数
  85. Cache::put($cacheKey, 1, now()->addHours(12)); // 键将保留12小时
  86. $times = 1;
  87. } else {
  88. $times = Cache::increment($cacheKey);
  89. }
  90. if ($times > $detectionCheckTimes) {
  91. Cache::forget($cacheKey);
  92. $node->update(['status' => 0]);
  93. $data[$node_id]['message'] = '自动进入维护状态';
  94. }
  95. }
  96. if (! empty($data[$node_id])) {
  97. $data[$node_id]['name'] = $node->name;
  98. }
  99. }
  100. if (! empty($data)) { //只有在出现阻断线路时,才会发出警报
  101. Notification::send(User::find(1), new NodeBlocked($data));
  102. Log::notice("节点状态日志: \r\n".var_export($data, true));
  103. }
  104. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700); // 随机生成下次检测时间
  105. $this->reliveNode();
  106. }
  107. private function reliveNode(): void
  108. {
  109. $onlineNode = NodeHeartbeat::recently()->distinct()->pluck('node_id');
  110. foreach (Node::whereRelayNodeId(null)->whereStatus(0)->whereIn('id', $onlineNode)->where('detection_type', '<>', 0)->get() as $node) {
  111. $ips = $node->ips();
  112. $reachableIPs = 0;
  113. foreach ($ips as $ip) {
  114. if ($node->detection_type) {
  115. $status = (new NetworkDetection)->networkStatus($ip, $node->port ?? 22);
  116. if (($node->detection_type === 1 && $status['tcp'] === 1) || ($node->detection_type === 2 && $status['icmp'] === 1) || ($status['tcp'] === 1 && $status['icmp'] === 1)) {
  117. $reachableIPs++;
  118. }
  119. sleep(1);
  120. }
  121. }
  122. if ($reachableIPs === count($ips)) {
  123. $node->update(['status' => 1]);
  124. }
  125. }
  126. }
  127. }