From c0995d9c4c32152babad4d0fa1c8393ddb5e7b37 Mon Sep 17 00:00:00 2001 From: Maksym Buz Date: Fri, 26 Sep 2025 10:50:21 +0200 Subject: [PATCH] change: logic in upgrade handling --- agent-backup-tool/agent_tool_linux.py | 63 ++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/agent-backup-tool/agent_tool_linux.py b/agent-backup-tool/agent_tool_linux.py index 67d31ef..c46e83f 100755 --- a/agent-backup-tool/agent_tool_linux.py +++ b/agent-backup-tool/agent_tool_linux.py @@ -227,17 +227,60 @@ class ZabbixAgentTool: def _upgrade_zabbix_package(self): """Upgrade Zabbix agent package based on distribution family""" - commands = { - 'debian': ["sudo apt update", "sudo apt upgrade -y zabbix-agent*"], - 'rhel': ["sudo yum update -y zabbix-agent* || sudo dnf update -y zabbix-agent*"] - } - - if self.distro_family not in commands: - raise Exception(f"Unsupported distribution family: {self.distro_family}") - logger.info(f"Upgrading Zabbix agent on {self.distro_family}-based system") - for cmd in commands[self.distro_family]: - self._run_command(cmd) + + if self.distro_family == 'debian': + # Update package lists + self._run_command("sudo apt update") + + # Check what zabbix packages are installed first + result = self._run_command("dpkg -l | grep -E 'zabbix-agent'", check=False, log_output=True) + if result.returncode != 0: + logger.warning("No Zabbix agent packages found installed") + return + + # Get list of installed zabbix agent packages + installed_packages = [] + if result.stdout: + for line in result.stdout.strip().split('\n'): + if 'zabbix-agent' in line: + # Extract package name from dpkg output + parts = line.split() + if len(parts) >= 2: + package_name = parts[1] + installed_packages.append(package_name) + + if not installed_packages: + logger.warning("No Zabbix agent packages found to upgrade") + return + + # Upgrade each installed package individually + for package in installed_packages: + try: + logger.info(f"Upgrading {package}") + # Use DEBIAN_FRONTEND=noninteractive to avoid prompts + self._run_command(f"sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y {package}") + except Exception as e: + logger.warning(f"Could not upgrade {package}: {e}") + + elif self.distro_family == 'rhel': + # For RHEL-based systems + try: + # Try yum first + result = self._run_command("yum list installed | grep zabbix-agent", check=False) + if result.returncode == 0: + self._run_command("sudo yum update -y zabbix-agent*") + else: + # Try dnf + result = self._run_command("dnf list installed | grep zabbix-agent", check=False) + if result.returncode == 0: + self._run_command("sudo dnf update -y zabbix-agent*") + else: + logger.warning("No Zabbix agent packages found to upgrade") + except Exception as e: + logger.warning(f"Could not upgrade packages: {e}") + else: + raise Exception(f"Unsupported distribution family: {self.distro_family}") def _merge_custom_settings(self, new_config_file, custom_settings, backup_path): """Merge custom settings into new configuration file"""