106 lines
2.9 KiB
PHP
106 lines
2.9 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use App\Jobs\CheckDnsRecord;
|
||
|
|
use App\Models\DnsRecord;
|
||
|
|
use App\Models\Site;
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
|
||
|
|
class MonitorDns extends Command
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The name and signature of the console command.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $signature = 'monitor:dns
|
||
|
|
{--dns-record= : Specific DNS record ID to monitor}
|
||
|
|
{--site= : Monitor all DNS records for a specific site ID}
|
||
|
|
{--sync : Run checks synchronously instead of queuing}';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The console command description.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $description = 'Monitor DNS records for all sites';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the console command.
|
||
|
|
*/
|
||
|
|
public function handle(): int
|
||
|
|
{
|
||
|
|
$this->info('Starting DNS monitoring...');
|
||
|
|
|
||
|
|
$query = DnsRecord::with('site')->where('is_active', true);
|
||
|
|
|
||
|
|
// Filter by specific DNS record
|
||
|
|
if ($dnsRecordId = $this->option('dns-record')) {
|
||
|
|
$query->where('id', $dnsRecordId);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Filter by site
|
||
|
|
if ($siteId = $this->option('site')) {
|
||
|
|
$query->where('site_id', $siteId);
|
||
|
|
}
|
||
|
|
|
||
|
|
$dnsRecords = $query->get();
|
||
|
|
|
||
|
|
if ($dnsRecords->isEmpty()) {
|
||
|
|
$this->warn('No active DNS records found.');
|
||
|
|
return self::SUCCESS;
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->info("Found {$dnsRecords->count()} active DNS record(s).");
|
||
|
|
|
||
|
|
$bar = $this->output->createProgressBar($dnsRecords->count());
|
||
|
|
$bar->start();
|
||
|
|
|
||
|
|
$dispatched = 0;
|
||
|
|
$errors = 0;
|
||
|
|
|
||
|
|
foreach ($dnsRecords as $dnsRecord) {
|
||
|
|
try {
|
||
|
|
if ($this->option('sync')) {
|
||
|
|
// Run synchronously
|
||
|
|
CheckDnsRecord::dispatchSync($dnsRecord);
|
||
|
|
$this->newLine();
|
||
|
|
$this->line(" ✓ Checked: {$dnsRecord->site->name} - {$dnsRecord->record_type} {$dnsRecord->domain}");
|
||
|
|
} else {
|
||
|
|
// Queue the job
|
||
|
|
CheckDnsRecord::dispatch($dnsRecord);
|
||
|
|
}
|
||
|
|
|
||
|
|
$dispatched++;
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$errors++;
|
||
|
|
$this->newLine();
|
||
|
|
$this->error(" ✗ Error checking {$dnsRecord->domain}: {$e->getMessage()}");
|
||
|
|
}
|
||
|
|
|
||
|
|
$bar->advance();
|
||
|
|
}
|
||
|
|
|
||
|
|
$bar->finish();
|
||
|
|
$this->newLine(2);
|
||
|
|
|
||
|
|
// Summary
|
||
|
|
$this->info("DNS monitoring completed!");
|
||
|
|
$this->table(
|
||
|
|
['Metric', 'Count'],
|
||
|
|
[
|
||
|
|
['Total DNS Records', $dnsRecords->count()],
|
||
|
|
['Successfully Dispatched', $dispatched],
|
||
|
|
['Errors', $errors],
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
if (!$this->option('sync')) {
|
||
|
|
$this->comment('Jobs have been queued. Run the queue worker to process them.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return self::SUCCESS;
|
||
|
|
}
|
||
|
|
}
|