AutoPingNode.php 1.2 KB

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