NodeBlockedDetection.php 3.9 KB

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