| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- class VpnStatusCommand extends Command
- {
- protected $signature = 'vpn:status {--refresh : Refresh status from system} {--sync : Sync database with system status}';
- protected $description = 'Controlla e sincronizza lo stato della connessione VPN';
- public function handle()
- {
- $this->info('=== VPN STATUS REPORT ===');
- $this->newLine();
- // 1. Stato dal database
- $this->checkDatabaseStatus();
- // 2. Stato dal sistema (se richiesto)
- if ($this->option('refresh') || $this->option('sync')) {
- $this->checkSystemStatus();
- }
- // 3. Sincronizzazione (se richiesta)
- if ($this->option('sync')) {
- $this->syncStatus();
- }
- // 4. Verifica file e configurazioni
- $this->checkFiles();
- // 5. Mostra log recenti
- $this->showRecentLogs();
- $this->newLine();
- $this->info('=== END REPORT ===');
- }
- private function checkDatabaseStatus()
- {
- $this->line('<comment>1. Database Status:</comment>');
- try {
- $status = DB::table('vpn_status')->first();
- if ($status) {
- $statusColor = $status->status === 'connected' ? 'green' : 'red';
- $this->line(" Status: <fg={$statusColor}>{$status->status}</>");
- $this->line(" Last Update: {$status->last_update}");
- $this->line(" Record ID: {$status->id}");
- } else {
- $this->error(' No status record found in database');
- }
- } catch (\Exception $e) {
- $this->error(' Database error: ' . $e->getMessage());
- }
- $this->newLine();
- }
- private function syncStatus()
- {
- $this->line('<comment>3. Synchronizing Database with System:</comment>');
- try {
- // Ottieni stato dal sistema
- $output = [];
- exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output);
- $statusText = implode(' ', $output);
- $systemStatus = 'unknown';
- if (strpos($statusText, 'state: Connected') !== false) {
- $systemStatus = 'connected';
- } elseif (strpos($statusText, 'Disconnected') !== false) {
- $systemStatus = 'disconnected';
- }
- // Ottieni stato dal database
- $dbRecord = DB::table('vpn_status')->first();
- $dbStatus = $dbRecord ? $dbRecord->status : 'no_record';
- $this->line(" System Status: <fg=yellow>{$systemStatus}</>");
- $this->line(" Database Status: <fg=yellow>{$dbStatus}</>");
- if ($systemStatus !== 'unknown' && $systemStatus !== $dbStatus) {
- // Aggiorna il database
- DB::table('vpn_status')->updateOrInsert(
- ['id' => 1],
- [
- 'status' => $systemStatus,
- 'last_update' => now(),
- 'updated_at' => now(),
- ]
- );
- $this->line(" ✅ <fg=green>Database updated to: {$systemStatus}</>");
- Log::info('VPN status synced via command', [
- 'old_status' => $dbStatus,
- 'new_status' => $systemStatus
- ]);
- } else {
- $this->line(' ℹ️ No sync needed - statuses match');
- }
- } catch (\Exception $e) {
- $this->error(" ❌ Sync failed: " . $e->getMessage());
- }
- }
- private function checkSystemStatus()
- {
- $this->line('<comment>2. System Status (Live Check):</comment>');
- if (file_exists('/opt/cisco/anyconnect/bin/vpn')) {
- $this->line(' ✅ Cisco AnyConnect: Installed');
- $output = [];
- $returnVar = 0;
- exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output, $returnVar);
- $this->line(' System Status Output:');
- foreach ($output as $line) {
- $this->line(' ' . $line);
- }
- // Analizza l'output
- $statusText = implode(' ', $output);
- if (strpos($statusText, 'Connected') !== false) {
- $this->line(' ✅ <fg=green>System Status: CONNECTED</>', 'green');
- } elseif (strpos($statusText, 'Disconnected') !== false) {
- $this->line(' ❌ <fg=red>System Status: DISCONNECTED</>');
- } else {
- $this->line(' ⚠️ <fg=yellow>System Status: UNKNOWN</>');
- }
- } else {
- $this->error(' ❌ Cisco AnyConnect: Not Installed');
- }
- $this->newLine();
- }
- private function checkFiles()
- {
- $this->line('<comment>3. Configuration Files:</comment>');
- $files = [
- 'scripts/vpn-connect.sh' => 'Connection Script',
- 'scripts/vpn-disconnect.sh' => 'Disconnection Script',
- 'scripts/vpn-config.conf' => 'Configuration File',
- ];
- foreach ($files as $file => $description) {
- if (Storage::exists($file)) {
- $this->line(" ✅ {$description}: Present");
- // Mostra i permessi per gli script
- if (str_ends_with($file, '.sh')) {
- $fullPath = storage_path($file);
- $perms = substr(sprintf('%o', fileperms($fullPath)), -4);
- $this->line(" Permissions: {$perms}");
- }
- } else {
- $this->error(" ❌ {$description}: Missing");
- }
- }
- // Controlla directory logs
- if (Storage::exists('logs')) {
- $this->line(' ✅ Logs Directory: Present');
- } else {
- $this->error(' ❌ Logs Directory: Missing');
- }
- $this->newLine();
- }
- private function showRecentLogs()
- {
- $this->line('<comment>4. Recent Log Entries:</comment>');
- $logFile = storage_path('app/logs/vpn-connection.log');
- if (file_exists($logFile)) {
- $this->line(" Log file: {$logFile}");
- // Leggi le ultime 10 righe
- $lines = file($logFile);
- $recentLines = array_slice($lines, -10);
- $this->line(' Last 10 entries:');
- foreach ($recentLines as $line) {
- $this->line(' ' . trim($line));
- }
- } else {
- $this->error(' Log file not found');
- }
- $this->newLine();
- }
- }
|