AutoPingNode.php 1.3 KB

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