NodeStatusDetection.php 4.5 KB

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