NodeStatusDetection.php 4.7 KB

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