find($this->moduleVmwareId); if (!$vm) { Log::error('[ChangeIpVMJob] ModuleVmware not found', ['module_vmware_id' => $this->moduleVmwareId]); if ($this->newIpPoolId) { $ipReservation->releaseByPoolId($this->newIpPoolId); } return; } $raw = $action->execute( $vm, $this->ipAddress, $this->gateway, $this->osType, $this->dns, $this->guestUser, $this->guestPassword, ); $failed = $raw === 'Login_Again' || $raw === 'vCenter_Not_Found' || str_starts_with((string) $raw, 'Error:') || str_contains((string) $raw, '[STDERR]'); if ($failed) { if ($this->newIpPoolId) { $ipReservation->releaseByPoolId($this->newIpPoolId); } } else { $vm->update(['ip' => $this->ipAddress, 'dns' => $this->dns]); if ($this->oldIpPoolId) { $ipReservation->releaseByPoolId($this->oldIpPoolId); } } Log::channel('Custom')->info('[ChangeIpVMJob] raw output', [ 'module_vmware_id' => $this->moduleVmwareId, 'ip_address' => $this->ipAddress, 'gateway' => $this->gateway, 'os_type' => $this->osType, 'vm_name' => $vm->name, 'vmware_id' => $vm->vmware_id, 'raw' => $raw, ]); $telegram = new Telegram(); if ($raw === 'Login_Again' || $raw === 'vCenter_Not_Found') { $errMsg = $raw === 'vCenter_Not_Found' ? 'Không tìm thấy vCenter cho VM này.' : 'Không thể đăng nhập vào vCenter.'; $telegram->SendErrorMessage("❌ Đổi IP thất bại [{$this->osType}]\n" . "🖥 VM: {$vm->name} ({$vm->vmware_id})\n" . "📍 IP mới: {$this->ipAddress}\n" . "🔗 GW: {$this->gateway}\n" . "🚫 Lỗi: {$errMsg}"); return; } if ($failed) { $telegram->SendErrorMessage("❌ Đổi IP thất bại [{$this->osType}]\n" . "🖥 VM: {$vm->name} ({$vm->vmware_id})\n" . "📍 IP mới: {$this->ipAddress}\n" . "🔗 GW: {$this->gateway}\n" . "📋 Output:\n" . mb_substr((string) $raw, 0, 800)); return; } // Empty raw = script chạy không có output → coi là thành công (PowerShell script không echo) // Nhưng vẫn gửi cảnh báo để theo dõi if (trim((string) $raw) === '') { $telegram->SendMessage("⚠️ Đổi IP [{$this->osType}] - Script chạy nhưng không có output\n" . "🖥 VM: {$vm->name} ({$vm->vmware_id})\n" . "📍 IP mới: {$this->ipAddress}\n" . "🔗 GW: {$this->gateway}\n" . "ℹ️ Kiểm tra lại VM để xác nhận IP đã thay đổi chưa."); } } public function failed(\Throwable $e): void { Log::error('[ChangeIpVMJob] job failed with exception', [ 'module_vmware_id' => $this->moduleVmwareId, 'ip_address' => $this->ipAddress, 'error' => $e->getMessage(), ]); if ($this->newIpPoolId) { app(VmIpReservationService::class)->releaseByPoolId($this->newIpPoolId); } } }