You've already forked Atomcms-edit
101 lines
3.4 KiB
PHP
Executable File
101 lines
3.4 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\AlertService;
|
|
use App\Services\RconService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class EmulatorMonitorCommand extends Command
|
|
{
|
|
#[\Override]
|
|
protected $signature = 'monitor:emulator {--notify-online : Stuur een melding wanneer emulator online komt}';
|
|
|
|
#[\Override]
|
|
protected $description = 'Monitor de emulator status en stuur alerts bij problemen';
|
|
|
|
private const string CACHE_KEY_EMULATOR_STATUS = 'emulator_monitor_status';
|
|
|
|
private const string CACHE_KEY_OFFLINE_SINCE = 'emulator_offline_since';
|
|
|
|
public function handle(AlertService $alertService): int
|
|
{
|
|
$this->info('Emulator status controleren...');
|
|
|
|
$rconService = new RconService;
|
|
$isConnected = $rconService->isConnected();
|
|
|
|
Cache::get(self::CACHE_KEY_EMULATOR_STATUS);
|
|
$wasOffline = Cache::has(self::CACHE_KEY_OFFLINE_SINCE);
|
|
|
|
if ($isConnected) {
|
|
$this->handleOnlineStatus($alertService, $wasOffline);
|
|
} else {
|
|
$this->handleOfflineStatus($alertService);
|
|
}
|
|
|
|
Cache::put(self::CACHE_KEY_EMULATOR_STATUS, [
|
|
'connected' => $isConnected,
|
|
'checked_at' => now()->toIso8601String(),
|
|
]);
|
|
|
|
$this->info('Emulator status: ' . ($isConnected ? 'ONLINE' : 'OFFLINE'));
|
|
|
|
return $isConnected ? Command::SUCCESS : Command::FAILURE;
|
|
}
|
|
|
|
private function handleOnlineStatus(AlertService $alertService, bool $wasOffline): void
|
|
{
|
|
if ($wasOffline && $this->option('notify-online')) {
|
|
$offlineSince = Cache::get(self::CACHE_KEY_OFFLINE_SINCE);
|
|
|
|
if ($offlineSince) {
|
|
$duration = now()->diffInMinutes(Carbon::parse($offlineSince));
|
|
$this->warn("Emulator was offline voor {$duration} minuten. Nu weer online!");
|
|
}
|
|
|
|
$alertService->sendEmulatorOnline();
|
|
}
|
|
|
|
Cache::forget(self::CACHE_KEY_OFFLINE_SINCE);
|
|
$this->line('Emulator is online en reageert.');
|
|
}
|
|
|
|
private function handleOfflineStatus(AlertService $alertService): void
|
|
{
|
|
if (! Cache::has(self::CACHE_KEY_OFFLINE_SINCE)) {
|
|
Cache::put(self::CACHE_KEY_OFFLINE_SINCE, now()->toIso8601String());
|
|
$this->warn('Emulator is OFFLINE! Eerste detectie - alert wordt verzonden.');
|
|
$alertService->sendEmulatorOffline('RCON verbinding mislukt bij monitoring check');
|
|
|
|
return;
|
|
}
|
|
|
|
$offlineSince = Cache::get(self::CACHE_KEY_OFFLINE_SINCE);
|
|
$offlineDuration = now()->diffInMinutes(Carbon::parse($offlineSince));
|
|
|
|
$escalationMinutes = $this->getEscalationMinutes($offlineDuration);
|
|
|
|
if ($escalationMinutes > 0 && $offlineDuration % $escalationMinutes === 0) {
|
|
$this->warn("Emulator is al {$offlineDuration} minuten offline. Escalating alert.");
|
|
$alertService->sendEmulatorOffline("Emulator offline voor {$offlineDuration} minuten");
|
|
}
|
|
|
|
$this->error("Emulator is OFFLINE! Offline sinds: {$offlineSince} ({$offlineDuration} minuten geleden)");
|
|
}
|
|
|
|
private function getEscalationMinutes(float|int $offlineMinutes): int
|
|
{
|
|
return match (true) {
|
|
$offlineMinutes >= 60 => 30,
|
|
$offlineMinutes >= 30 => 15,
|
|
$offlineMinutes >= 15 => 5,
|
|
default => 0,
|
|
};
|
|
}
|
|
}
|