AutoPingNode.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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(
  17. $node->id,
  18. $node->is_ddns ? $node->server : $node->ip
  19. );
  20. }
  21. $jobEndTime = microtime(true);
  22. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  23. Log::info(
  24. '---【' . $this->description . '】完成---,耗时' . $jobUsedTime . '秒'
  25. );
  26. }
  27. // 节点Ping测速
  28. private function pingNode($nodeId, $ip): void
  29. {
  30. $result = NetworkDetection::ping($ip);
  31. if ($result) {
  32. $obj = new NodePing();
  33. $obj->node_id = $nodeId;
  34. $obj->ct = (int)$result['telecom']['time'];//电信
  35. $obj->cu = (int)$result['Unicom']['time'];// 联通
  36. $obj->cm = (int)$result['move']['time'];// 移动
  37. $obj->hk = (int)$result['HongKong']['time'];// 香港
  38. $obj->save();
  39. } else {
  40. Log::error("【" . $ip . "】Ping测速获取失败");
  41. }
  42. }
  43. }