98 lines
2.6 KiB
PHP
98 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Jobs\CheckTcpPort;
|
|
use App\Models\TcpPort;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class MonitorTcpPorts extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'monitor:tcp-ports
|
|
{--tcp-port= : Specific TCP port ID to check}
|
|
{--site= : Specific site ID to check all ports for}
|
|
{--sync : Run synchronously instead of dispatching jobs}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Monitor all active TCP ports';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
$this->info('Starting TCP port monitoring...');
|
|
|
|
$query = TcpPort::with('site')->where('is_active', true);
|
|
|
|
if ($tcpPortId = $this->option('tcp-port')) {
|
|
$query->where('id', $tcpPortId);
|
|
}
|
|
|
|
if ($siteId = $this->option('site')) {
|
|
$query->where('site_id', $siteId);
|
|
}
|
|
|
|
$tcpPorts = $query->get();
|
|
|
|
if ($tcpPorts->isEmpty()) {
|
|
$this->warn('No active TCP ports found to monitor.');
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$this->info("Found {$tcpPorts->count()} TCP port(s) to check.");
|
|
|
|
$progressBar = $this->output->createProgressBar($tcpPorts->count());
|
|
$progressBar->start();
|
|
|
|
$dispatched = 0;
|
|
$failed = 0;
|
|
|
|
foreach ($tcpPorts as $tcpPort) {
|
|
try {
|
|
if ($this->option('sync')) {
|
|
// Run synchronously for testing/debugging
|
|
CheckTcpPort::dispatchSync($tcpPort);
|
|
} else {
|
|
// Dispatch to queue
|
|
CheckTcpPort::dispatch($tcpPort);
|
|
}
|
|
|
|
$dispatched++;
|
|
} catch (\Exception $e) {
|
|
$failed++;
|
|
Log::error('MonitorTcpPorts: Failed to dispatch job', [
|
|
'tcp_port_id' => $tcpPort->id,
|
|
'site_id' => $tcpPort->site_id,
|
|
'port' => $tcpPort->port,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
|
|
$progressBar->advance();
|
|
}
|
|
|
|
$progressBar->finish();
|
|
$this->newLine(2);
|
|
|
|
$this->info("Successfully dispatched {$dispatched} TCP port check job(s).");
|
|
|
|
if ($failed > 0) {
|
|
$this->error("Failed to dispatch {$failed} job(s).");
|
|
return self::FAILURE;
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|