AutoPingNode.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\NetworkDetection;
  4. use App\Http\Models\SsNode;
  5. use App\Http\Models\SsNodePing;
  6. use Illuminate\Console\Command;
  7. use Log;
  8. class AutoPingNode extends Command
  9. {
  10. protected $signature = 'autoPingNode';
  11. protected $description = '节点定时Ping测速';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. $jobStartTime = microtime(TRUE);
  19. $nodeList = SsNode::query()->whereIsTransit(0)->whereStatus(1)->get();
  20. foreach($nodeList as $node){
  21. $this->pingNode($node->id, $node->is_ddns? $node->server : $node->ip);
  22. }
  23. $jobEndTime = microtime(TRUE);
  24. $jobUsedTime = round(($jobEndTime-$jobStartTime), 4);
  25. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  26. }
  27. // 节点Ping测速
  28. private function pingNode($nodeId, $ip)
  29. {
  30. $result = NetworkDetection::ping($ip);
  31. if($result){
  32. $obj = new SsNodePing();
  33. $obj->node_id = $nodeId;
  34. $obj->ct = intval($result['China Telecom']['time']);
  35. $obj->cu = intval($result['China Unicom']['time']);
  36. $obj->cm = intval($result['China Mobile']['time']);
  37. $obj->hk = intval($result['Hong Kong']['time']);
  38. $obj->save();
  39. }else{
  40. Log::info("【".$ip."】Ping测速获取失败");
  41. }
  42. }
  43. }