NodeStatusDetection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. $jobTime = 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. $jobTime = round(microtime(true) - $jobTime, 4);
  31. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  32. }
  33. private function checkNodeStatus()
  34. {
  35. $offlineCheckTimes = sysConfig('offline_check_times');
  36. $onlineNode = NodeHeartbeat::recently()->distinct()->pluck('node_id')->toArray();
  37. foreach (Node::whereRelayNodeId(null)->whereStatus(1)->whereNotIn('id', $onlineNode)->get() as $node) {
  38. // 近期无节点负载信息则认为是后端炸了
  39. if ($offlineCheckTimes) {
  40. // 已通知次数
  41. $cacheKey = 'offline_check_times'.$node->id;
  42. $times = 1;
  43. if (Cache::has($cacheKey)) {
  44. $times = Cache::get($cacheKey);
  45. } else {
  46. Cache::put($cacheKey, 1, Day); // 键将保留24小时
  47. }
  48. if ($times > $offlineCheckTimes) {
  49. continue;
  50. }
  51. Cache::increment($cacheKey);
  52. }
  53. $data[] = [
  54. 'name' => $node->name,
  55. 'host' => $node->host,
  56. ];
  57. }
  58. if (isset($data)) {
  59. Notification::send(User::find(1), new NodeOffline($data));
  60. }
  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. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  68. foreach ($node->ips() as $ip) {
  69. if ($node->detection_type !== 1) {
  70. $icmpCheck = (new NetworkDetection)->networkCheck($ip, true, $node->port ?? 22);
  71. if ($icmpCheck !== false && $icmpCheck !== 1) {
  72. $data[$node_id][$ip]['icmp'] = config('common.network_status')[$icmpCheck];
  73. }
  74. }
  75. if ($node->detection_type !== 2) {
  76. $tcpCheck = (new NetworkDetection)->networkCheck($ip, false, $node->port ?? 22);
  77. if ($tcpCheck !== false && $tcpCheck !== 1) {
  78. $data[$node_id][$ip]['tcp'] = config('common.network_status')[$tcpCheck];
  79. }
  80. }
  81. }
  82. // 节点检测次数
  83. if (isset($data[$node_id]) && $detectionCheckTimes) {
  84. // 已通知次数
  85. $cacheKey = 'detection_check_times'.$node_id;
  86. if (Cache::has($cacheKey)) {
  87. $times = Cache::get($cacheKey);
  88. } else {
  89. // 键将保留12小时,多10分钟防意外
  90. Cache::put($cacheKey, 1, 43800);
  91. $times = 1;
  92. }
  93. if ($times < $detectionCheckTimes) {
  94. Cache::increment($cacheKey);
  95. } else {
  96. Cache::forget($cacheKey);
  97. $node->update(['status' => 0]);
  98. $data[$node_id]['message'] = '自动进入维护状态';
  99. }
  100. }
  101. if (isset($data[$node_id])) {
  102. $data[$node_id]['name'] = $node->name;
  103. }
  104. sleep(5);
  105. }
  106. if (isset($data)) { //只有在出现阻断线路时,才会发出警报
  107. Notification::send(User::find(1), new NodeBlocked($data));
  108. Log::notice("节点状态日志: \r\n".var_export($data, true));
  109. }
  110. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700); // 随机生成下次检测时间
  111. }
  112. }