NodeStatusDetection.php 4.7 KB

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