CheckServer.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\ServerService;
  4. use App\Services\TelegramService;
  5. use App\Utils\CacheKey;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Cache;
  8. class CheckServer extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'check:server';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '节点检查任务';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $this->checkOffline();
  39. }
  40. private function checkOffline()
  41. {
  42. $serverService = new ServerService();
  43. $servers = $serverService->getAllServers();
  44. foreach ($servers as $server) {
  45. if ($server['parent_id']) continue;
  46. if ($server['last_check_at'] && (time() - $server['last_check_at']) > 1800) {
  47. $telegramService = new TelegramService();
  48. $message = sprintf(
  49. "节点掉线通知\r\n----\r\n节点名称:%s\r\n节点地址:%s\r\n",
  50. $server['name'],
  51. $server['host']
  52. );
  53. $telegramService->sendMessageWithAdmin($message);
  54. Cache::forget(CacheKey::get(sprintf("SERVER_%s_LAST_CHECK_AT", strtoupper($server['type'])), $server->id));
  55. }
  56. }
  57. }
  58. }