AutoPingNode.php 1.4 KB

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