VpnStatusCommand.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Storage;
  7. class VpnStatusCommand extends Command
  8. {
  9. protected $signature = 'vpn:status {--refresh : Refresh status from system} {--sync : Sync database with system status}';
  10. protected $description = 'Controlla e sincronizza lo stato della connessione VPN';
  11. public function handle()
  12. {
  13. $this->info('=== VPN STATUS REPORT ===');
  14. $this->newLine();
  15. // 1. Stato dal database
  16. $this->checkDatabaseStatus();
  17. // 2. Stato dal sistema (se richiesto)
  18. if ($this->option('refresh') || $this->option('sync')) {
  19. $this->checkSystemStatus();
  20. }
  21. // 3. Sincronizzazione (se richiesta)
  22. if ($this->option('sync')) {
  23. $this->syncStatus();
  24. }
  25. // 4. Verifica file e configurazioni
  26. $this->checkFiles();
  27. // 5. Mostra log recenti
  28. $this->showRecentLogs();
  29. $this->newLine();
  30. $this->info('=== END REPORT ===');
  31. }
  32. private function checkDatabaseStatus()
  33. {
  34. $this->line('<comment>1. Database Status:</comment>');
  35. try {
  36. $status = DB::table('vpn_status')->first();
  37. if ($status) {
  38. $statusColor = $status->status === 'connected' ? 'green' : 'red';
  39. $this->line(" Status: <fg={$statusColor}>{$status->status}</>");
  40. $this->line(" Last Update: {$status->last_update}");
  41. $this->line(" Record ID: {$status->id}");
  42. } else {
  43. $this->error(' No status record found in database');
  44. }
  45. } catch (\Exception $e) {
  46. $this->error(' Database error: ' . $e->getMessage());
  47. }
  48. $this->newLine();
  49. }
  50. private function syncStatus()
  51. {
  52. $this->line('<comment>3. Synchronizing Database with System:</comment>');
  53. try {
  54. // Ottieni stato dal sistema
  55. $output = [];
  56. exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output);
  57. $statusText = implode(' ', $output);
  58. $systemStatus = 'unknown';
  59. if (strpos($statusText, 'state: Connected') !== false) {
  60. $systemStatus = 'connected';
  61. } elseif (strpos($statusText, 'Disconnected') !== false) {
  62. $systemStatus = 'disconnected';
  63. }
  64. // Ottieni stato dal database
  65. $dbRecord = DB::table('vpn_status')->first();
  66. $dbStatus = $dbRecord ? $dbRecord->status : 'no_record';
  67. $this->line(" System Status: <fg=yellow>{$systemStatus}</>");
  68. $this->line(" Database Status: <fg=yellow>{$dbStatus}</>");
  69. if ($systemStatus !== 'unknown' && $systemStatus !== $dbStatus) {
  70. // Aggiorna il database
  71. DB::table('vpn_status')->updateOrInsert(
  72. ['id' => 1],
  73. [
  74. 'status' => $systemStatus,
  75. 'last_update' => now(),
  76. 'updated_at' => now(),
  77. ]
  78. );
  79. $this->line(" ✅ <fg=green>Database updated to: {$systemStatus}</>");
  80. Log::info('VPN status synced via command', [
  81. 'old_status' => $dbStatus,
  82. 'new_status' => $systemStatus
  83. ]);
  84. } else {
  85. $this->line(' ℹ️ No sync needed - statuses match');
  86. }
  87. } catch (\Exception $e) {
  88. $this->error(" ❌ Sync failed: " . $e->getMessage());
  89. }
  90. }
  91. private function checkSystemStatus()
  92. {
  93. $this->line('<comment>2. System Status (Live Check):</comment>');
  94. if (file_exists('/opt/cisco/anyconnect/bin/vpn')) {
  95. $this->line(' ✅ Cisco AnyConnect: Installed');
  96. $output = [];
  97. $returnVar = 0;
  98. exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output, $returnVar);
  99. $this->line(' System Status Output:');
  100. foreach ($output as $line) {
  101. $this->line(' ' . $line);
  102. }
  103. // Analizza l'output
  104. $statusText = implode(' ', $output);
  105. if (strpos($statusText, 'Connected') !== false) {
  106. $this->line(' ✅ <fg=green>System Status: CONNECTED</>', 'green');
  107. } elseif (strpos($statusText, 'Disconnected') !== false) {
  108. $this->line(' ❌ <fg=red>System Status: DISCONNECTED</>');
  109. } else {
  110. $this->line(' ⚠️ <fg=yellow>System Status: UNKNOWN</>');
  111. }
  112. } else {
  113. $this->error(' ❌ Cisco AnyConnect: Not Installed');
  114. }
  115. $this->newLine();
  116. }
  117. private function checkFiles()
  118. {
  119. $this->line('<comment>3. Configuration Files:</comment>');
  120. $files = [
  121. 'scripts/vpn-connect.sh' => 'Connection Script',
  122. 'scripts/vpn-disconnect.sh' => 'Disconnection Script',
  123. 'scripts/vpn-config.conf' => 'Configuration File',
  124. ];
  125. foreach ($files as $file => $description) {
  126. if (Storage::exists($file)) {
  127. $this->line(" ✅ {$description}: Present");
  128. // Mostra i permessi per gli script
  129. if (str_ends_with($file, '.sh')) {
  130. $fullPath = storage_path($file);
  131. $perms = substr(sprintf('%o', fileperms($fullPath)), -4);
  132. $this->line(" Permissions: {$perms}");
  133. }
  134. } else {
  135. $this->error(" ❌ {$description}: Missing");
  136. }
  137. }
  138. // Controlla directory logs
  139. if (Storage::exists('logs')) {
  140. $this->line(' ✅ Logs Directory: Present');
  141. } else {
  142. $this->error(' ❌ Logs Directory: Missing');
  143. }
  144. $this->newLine();
  145. }
  146. private function showRecentLogs()
  147. {
  148. $this->line('<comment>4. Recent Log Entries:</comment>');
  149. $logFile = storage_path('app/logs/vpn-connection.log');
  150. if (file_exists($logFile)) {
  151. $this->line(" Log file: {$logFile}");
  152. // Leggi le ultime 10 righe
  153. $lines = file($logFile);
  154. $recentLines = array_slice($lines, -10);
  155. $this->line(' Last 10 entries:');
  156. foreach ($recentLines as $line) {
  157. $this->line(' ' . trim($line));
  158. }
  159. } else {
  160. $this->error(' Log file not found');
  161. }
  162. $this->newLine();
  163. }
  164. }