123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace App\Console\Commands;
- use App\Components\NetworkDetection;
- use App\Models\Node;
- use App\Models\NodeHeartbeat;
- use App\Models\User;
- use App\Notifications\NodeBlocked;
- use App\Notifications\NodeOffline;
- use Cache;
- use Illuminate\Console\Command;
- use Log;
- use Notification;
- class NodeStatusDetection extends Command
- {
- protected $signature = 'nodeStatusDetection';
- protected $description = '节点状态检测';
- public function handle(): void
- {
- $jobStartTime = microtime(true);
- // 检测节点心跳是否异常
- if (sysConfig('node_offline_notification')) {
- $this->checkNodeStatus();
- }
- // 监测节点网络状态
- if (sysConfig('node_blocked_notification')) {
- if (! Cache::has('LastCheckTime')) {
- $this->checkNodeNetwork();
- } elseif (Cache::get('LastCheckTime') <= time()) {
- $this->checkNodeNetwork();
- } else {
- Log::info('下次节点阻断检测时间:'.date('Y-m-d H:i:s', Cache::get('LastCheckTime')));
- }
- }
- //$this->checkNodeWeihu();
- $jobEndTime = microtime(true);
- $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
- Log::info("---【{$this->description}】完成---,耗时 {$jobUsedTime} 秒");
- }
- private function checkNodeWeihu(){
- $query = Node::with(['onlineLogs', 'dailyDataFlows']);
- $nodeList = $query->where("country_code","=","hk");
- foreach ($nodeList as $node) {
- // 在线人数
- $online_log = $node->onlineLogs()->where('log_time', '>=', strtotime('-5 minutes'))->latest('log_time')->first();
- $node->online_users = $online_log->online_user ?? 0;
- Log::info("---【{$this->description}】节点名称{$node->name}---,在线人数 {$node->online_users} ----");
- // // 已产生流量
- // $node->transfer = flowAutoShow($node->dailyDataFlows()->sum('total'));
- //
- // // 负载(10分钟以内)
- // $node_info = $node->heartbeats()->recently()->first();
- // $node->isOnline = empty($node_info) || empty($node_info->load) ? 0 : 1;
- // $node->load = $node->isOnline ? $node_info->load : '离线';
- // $node->uptime = empty($node_info) ? 0 : seconds2time($node_info->uptime);
- }
- }
- private function checkNodeStatus(): void
- {
- $offlineCheckTimes = sysConfig('offline_check_times');
- $onlineNode = NodeHeartbeat::recently()->distinct()->pluck('node_id')->toArray();
- foreach (Node::whereIsRelay(0)->whereStatus(1)->whereNotIn('id', $onlineNode)->get() as $node) {
- // 10分钟内无节点负载信息则认为是后端炸了
- if ($offlineCheckTimes) {
- // 已通知次数
- $cacheKey = 'offline_check_times'.$node->id;
- $times = 1;
- if (Cache::has($cacheKey)) {
- $times = Cache::get($cacheKey);
- } else {
- Cache::put($cacheKey, 1, Day); // 键将保留24小时
- }
- if ($times > $offlineCheckTimes) {
- continue;
- }
- Cache::increment($cacheKey);
- }
- $data[] = [
- 'name' => $node->name,
- 'ip' => $node->ip,
- ];
- }
- if (isset($data)) {
- Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
- return $query->role('Super Admin');
- })->get(), new NodeOffline($data));
- }
- }
- private function checkNodeNetwork(): void
- {
- $detectionCheckTimes = sysConfig('detection_check_times');
- $sendText = false;
- $message = "| 线路 | 协议 | 状态 |\r\n| ------ | ------ | ------ |\r\n";
- $additionalMessage = '';
- foreach (Node::whereIsRelay(0)->whereStatus(1)->where('detection_type', '>', 0)->get() as $node) {
- $info = false;
- if ($node->detection_type === 0) {
- continue;
- }
- // 使用DDNS的node先通过gethostbyname获取ipv4地址
- if ($node->is_ddns) {
- $ip = gethostbyname($node->server);
- if (strcmp($ip, $node->server) !== 0) {
- $node->ip = $ip;
- } else {
- Log::warning('【节点阻断检测】检测'.$node->server.'时,IP获取失败'.$ip.' | '.$node->server);
- }
- }
- if ($node->detection_type !== 1) {
- $icmpCheck = (new NetworkDetection)->networkCheck($node->ip, true);
- if ($icmpCheck !== false && $icmpCheck !== '通讯正常') {
- $message .= '| '.$node->name.' | ICMP | '.$icmpCheck." |\r\n";
- $sendText = true;
- $info = true;
- }
- }
- if ($node->detection_type !== 2) {
- $tcpCheck = (new NetworkDetection)->networkCheck($node->ip, false, $node->single ? $node->port : 22);
- if ($tcpCheck !== false && $tcpCheck !== '通讯正常') {
- $message .= '| '.$node->name.' | TCP | '.$tcpCheck." |\r\n";
- $sendText = true;
- $info = true;
- }
- }
- sleep(5);
- // 节点检测次数
- if ($info && $detectionCheckTimes) {
- // 已通知次数
- $cacheKey = 'detection_check_times'.$node->id;
- if (Cache::has($cacheKey)) {
- $times = Cache::get($cacheKey);
- } else {
- // 键将保留12小时,多10分钟防意外
- Cache::put($cacheKey, 1, 43800);
- $times = 1;
- }
- if ($times < $detectionCheckTimes) {
- Cache::increment($cacheKey);
- } else {
- Cache::forget($cacheKey);
- $node->update(['status' => 0]);
- $additionalMessage .= "\r\n节点【{$node->name}】自动进入维护状态\r\n";
- }
- }
- }
- //只有在出现阻断线路时,才会发出警报
- if ($sendText) {
- Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
- return $query->role('Super Admin');
- })->get(), new NodeBlocked($message.$additionalMessage));
- Log::info("阻断日志: \r\n".$message.$additionalMessage);
- }
- // 随机生成下次检测时间
- Cache::put('LastCheckTime', time() + random_int(3000, Hour), 3700);
- }
- }
|