NodeStatusDetection.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. 'host' => $node->host,
  57. ];
  58. }
  59. if (isset($data)) {
  60. Notification::send(User::find(1), new NodeOffline($data));
  61. }
  62. }
  63. private function checkNodeNetwork(): void
  64. {
  65. $detectionCheckTimes = sysConfig('detection_check_times');
  66. foreach (Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get() as $node) {
  67. if ($node->detection_type === 0) {
  68. continue;
  69. }
  70. $node_id = (int) $node->id;
  71. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  72. if ($node->is_ddns) {
  73. $ip = gethostbyname($node->server);
  74. if (strcmp($ip, $node->server) !== 0) {
  75. $node->ip = $ip;
  76. } else {
  77. Log::warning('【节点阻断检测】检测'.$node->server.'时,IP获取失败'.$ip.' | '.$node->server);
  78. }
  79. }
  80. if ($node->detection_type !== 1) {
  81. $icmpCheck = (new NetworkDetection)->networkCheck($node->ip, true);
  82. if ($icmpCheck !== false && $icmpCheck !== '通讯正常') {
  83. $data[$node_id]['icmp'] = $icmpCheck;
  84. }
  85. }
  86. if ($node->detection_type !== 2) {
  87. $tcpCheck = (new NetworkDetection)->networkCheck($node->ip, false, $node->single ? $node->port : 22);
  88. if ($tcpCheck !== false && $tcpCheck !== '通讯正常') {
  89. $data[$node_id]['tcp'] = $tcpCheck;
  90. }
  91. }
  92. // 节点检测次数
  93. if (isset($data[$node_id]) && $detectionCheckTimes) {
  94. // 已通知次数
  95. $cacheKey = 'detection_check_times'.$node_id;
  96. if (Cache::has($cacheKey)) {
  97. $times = Cache::get($cacheKey);
  98. } else {
  99. // 键将保留12小时,多10分钟防意外
  100. Cache::put($cacheKey, 1, 43800);
  101. $times = 1;
  102. }
  103. if ($times < $detectionCheckTimes) {
  104. Cache::increment($cacheKey);
  105. } else {
  106. Cache::forget($cacheKey);
  107. $node->update(['status' => 0]);
  108. $data[$node_id]['message'] = '自动进入维护状态';
  109. }
  110. }
  111. if (isset($data[$node_id])) {
  112. $data[$node_id]['name'] = $node->name;
  113. }
  114. sleep(5);
  115. }
  116. if (isset($data)) { //只有在出现阻断线路时,才会发出警报
  117. Notification::send(User::find(1), new NodeBlocked($data));
  118. Log::info("节点状态日志: \r\n".var_export($data, true));
  119. }
  120. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700); // 随机生成下次检测时间
  121. }
  122. }