AutoPingNode.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\NetworkDetection;
  4. use App\Models\Node;
  5. use App\Models\NodePing;
  6. use Illuminate\Console\Command;
  7. use Log;
  8. class AutoPingNode extends Command {
  9. protected $signature = 'autoPingNode';
  10. protected $description = '节点定时Ping测速';
  11. public function handle(): void {
  12. $jobStartTime = microtime(true);
  13. foreach(Node::whereIsRelay(0)->whereStatus(1)->get() as $node){
  14. $this->pingNode($node->id, $node->is_ddns? $node->server : $node->ip);
  15. }
  16. $jobEndTime = microtime(true);
  17. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  18. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  19. }
  20. // 节点Ping测速
  21. private function pingNode($nodeId, $ip): void {
  22. $result = NetworkDetection::ping($ip);
  23. if($result){
  24. $obj = new NodePing();
  25. $obj->node_id = $nodeId;
  26. $obj->ct = (int) $result['telecom']['time'];//电信
  27. $obj->cu = (int) $result['Unicom']['time'];// 联通
  28. $obj->cm = (int) $result['move']['time'];// 移动
  29. $obj->hk = (int) $result['HongKong']['time'];// 香港
  30. $obj->save();
  31. }else{
  32. Log::error("【".$ip."】Ping测速获取失败");
  33. }
  34. }
  35. }