NodeBlockedDetection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Components\NetworkDetection;
  5. use App\Components\PushNotification;
  6. use App\Mail\nodeCrashWarning;
  7. use App\Models\Node;
  8. use Cache;
  9. use Illuminate\Console\Command;
  10. use Log;
  11. use Mail;
  12. class NodeBlockedDetection extends Command {
  13. protected $signature = 'nodeBlockedDetection';
  14. protected $description = '节点阻断检测';
  15. public function handle(): void {
  16. $jobStartTime = microtime(true);
  17. if(sysConfig('nodes_detection')){
  18. if(!Cache::has('LastCheckTime')){
  19. $this->checkNodes();
  20. }elseif(Cache::get('LastCheckTime') <= time()){
  21. $this->checkNodes();
  22. }else{
  23. Log::info('下次节点阻断检测时间:'.date('Y-m-d H:i:s', Cache::get('LastCheckTime')));
  24. }
  25. }
  26. $jobEndTime = microtime(true);
  27. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  28. Log::info("---【{$this->description}】完成---,耗时 {$jobUsedTime} 秒");
  29. }
  30. // 监测节点状态
  31. private function checkNodes(): void {
  32. $detectionCheckTimes = sysConfig('detection_check_times');
  33. $sendText = false;
  34. $message = "| 线路 | 协议 | 状态 |\r\n| ------ | ------ | ------ |\r\n";
  35. $additionalMessage = '';
  36. foreach(Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get() as $node){
  37. $info = false;
  38. if($node->detection_type == 0){
  39. continue;
  40. }
  41. // 使用DDNS的node先通过gethostbyname获取ipv4地址
  42. if($node->is_ddns){
  43. $ip = gethostbyname($node->server);
  44. if(strcmp($ip, $node->server) != 0){
  45. $node->ip = $ip;
  46. }else{
  47. Log::warning("【节点阻断检测】检测".$node->server."时,IP获取失败".$ip." | ".$node->server);
  48. $this->notifyMaster("{$node->name}动态IP获取失败", "节点 {$node->name} : IP获取失败 ");
  49. }
  50. }
  51. if($node->detection_type != 1){
  52. $icmpCheck = NetworkDetection::networkCheck($node->ip, true);
  53. if($icmpCheck != false && $icmpCheck !== "通讯正常"){
  54. $message .= "| ".$node->name." | ICMP | ".$icmpCheck." |\r\n";
  55. $sendText = true;
  56. $info = true;
  57. }
  58. }
  59. if($node->detection_type != 2){
  60. $tcpCheck = NetworkDetection::networkCheck($node->ip, false, $node->single? $node->port : null);
  61. if($tcpCheck != false && $tcpCheck !== "通讯正常"){
  62. $message .= "| ".$node->name." | TCP | ".$tcpCheck." |\r\n";
  63. $sendText = true;
  64. $info = true;
  65. }
  66. }
  67. // 节点检测次数
  68. if($info && $detectionCheckTimes){
  69. // 已通知次数
  70. $cacheKey = 'detection_check_times'.$node->id;
  71. if(Cache::has($cacheKey)){
  72. $times = Cache::get($cacheKey);
  73. }else{
  74. // 键将保留12小时,多10分钟防意外
  75. Cache::put($cacheKey, 1, 43800);
  76. $times = 1;
  77. }
  78. if($times < $detectionCheckTimes){
  79. Cache::increment($cacheKey);
  80. }else{
  81. Cache::forget($cacheKey);
  82. Node::find($node->id)->update(['status' => 0]);
  83. $additionalMessage .= "\r\n节点【{$node->name}】自动进入维护状态\r\n";
  84. }
  85. }
  86. }
  87. //只有在出现阻断线路时,才会发出警报
  88. if($sendText){
  89. $this->notifyMaster("节点阻断警告", "阻断日志: \r\n\r\n".$message.$additionalMessage);
  90. Log::info("阻断日志: \r\n".$message.$additionalMessage);
  91. }
  92. // 随机生成下次检测时间
  93. Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700);
  94. }
  95. /**
  96. * 通知管理员
  97. *
  98. * @param string $title 消息标题
  99. * @param string $content 消息内容
  100. *
  101. */
  102. private function notifyMaster($title, $content): void {
  103. $result = PushNotification::send($title, $content);
  104. if(!$result && sysConfig('webmaster_email')){
  105. $logId = Helpers::addNotificationLog($title, $content, 1, sysConfig('webmaster_email'));
  106. Mail::to(sysConfig('webmaster_email'))->send(new nodeCrashWarning($logId));
  107. }
  108. }
  109. }