171 lines
5.3 KiB
PHP
171 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Paymenter\Extensions\Others\UptimeLab\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Paymenter\Extensions\Others\UptimeLab\Models\UptimeServer;
|
|
use Paymenter\Extensions\Others\UptimeLab\Services\UptimeMonitoringService;
|
|
|
|
class TestUptimeCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'uptime:test {--server= : Test specific server by ID}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Test uptime monitoring functionality';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('=== Uptime Lab Extension Test ===');
|
|
|
|
// Test database connection
|
|
$this->testDatabaseConnection();
|
|
|
|
// Test server models
|
|
$this->testServerModels();
|
|
|
|
// Test monitoring service
|
|
$this->testMonitoringService();
|
|
|
|
$this->info('=== Test completed ===');
|
|
}
|
|
|
|
/**
|
|
* Test database connection and tables
|
|
*/
|
|
private function testDatabaseConnection(): void
|
|
{
|
|
$this->info('Testing database connection...');
|
|
|
|
try {
|
|
$count = UptimeServer::count();
|
|
$this->info("✓ Database connected. Found {$count} servers.");
|
|
} catch (\Exception $e) {
|
|
$this->error('✗ Database connection failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test server models
|
|
*/
|
|
private function testServerModels(): void
|
|
{
|
|
$this->info('Testing server models...');
|
|
|
|
try {
|
|
$servers = UptimeServer::all();
|
|
|
|
if ($servers->count() > 0) {
|
|
$this->info('✓ Server models working correctly.');
|
|
$this->table(
|
|
['ID', 'Name', 'IP', 'Status', 'Uptime'],
|
|
$servers->map(function ($server) {
|
|
return [
|
|
$server->id,
|
|
$server->name,
|
|
$server->ip_address,
|
|
$server->status,
|
|
$server->uptime_percentage . '%'
|
|
];
|
|
})
|
|
);
|
|
} else {
|
|
$this->warn('⚠ No servers found in database.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->error('✗ Server models test failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test monitoring service
|
|
*/
|
|
private function testMonitoringService(): void
|
|
{
|
|
$this->info('Testing monitoring service...');
|
|
|
|
try {
|
|
$monitoringService = new UptimeMonitoringService();
|
|
|
|
if ($serverId = $this->option('server')) {
|
|
$server = UptimeServer::find($serverId);
|
|
if ($server) {
|
|
$this->testSpecificServer($server, $monitoringService);
|
|
} else {
|
|
$this->error("Server with ID {$serverId} not found.");
|
|
}
|
|
} else {
|
|
$this->testAllServers($monitoringService);
|
|
}
|
|
|
|
$this->info('✓ Monitoring service working correctly.');
|
|
} catch (\Exception $e) {
|
|
$this->error('✗ Monitoring service test failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test a specific server
|
|
*/
|
|
private function testSpecificServer(UptimeServer $server, UptimeMonitoringService $monitoringService): void
|
|
{
|
|
$this->info("Testing server: {$server->name} ({$server->ip_address})");
|
|
|
|
$checkResult = $monitoringService->checkServerStatus($server);
|
|
|
|
$this->table(
|
|
['Property', 'Value'],
|
|
[
|
|
['Status', $checkResult['online'] ? 'Online' : 'Offline'],
|
|
['Response Time', $checkResult['response_time'] ? $checkResult['response_time'] . 'ms' : 'N/A'],
|
|
['Check Time', $checkResult['check_time'] ? $checkResult['check_time'] . 'ms' : 'N/A'],
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test all servers
|
|
*/
|
|
private function testAllServers(UptimeMonitoringService $monitoringService): void
|
|
{
|
|
$this->info('Testing all active servers...');
|
|
|
|
$servers = UptimeServer::where('is_active', true)->get();
|
|
|
|
if ($servers->count() > 0) {
|
|
$this->info("Found {$servers->count()} active servers to test.");
|
|
|
|
$progressBar = $this->output->createProgressBar($servers->count());
|
|
$progressBar->start();
|
|
|
|
foreach ($servers as $server) {
|
|
try {
|
|
$checkResult = $monitoringService->checkServerStatus($server);
|
|
$monitoringService->updateServerStatus($server, $checkResult);
|
|
} catch (\Exception $e) {
|
|
$this->error("\nError checking server {$server->name}: " . $e->getMessage());
|
|
}
|
|
|
|
$progressBar->advance();
|
|
}
|
|
|
|
$progressBar->finish();
|
|
$this->newLine();
|
|
$this->info('✓ All servers tested successfully.');
|
|
} else {
|
|
$this->warn('⚠ No active servers found to test.');
|
|
}
|
|
}
|
|
}
|