diff --git a/zabbix-autoregister/deploy_zabbix_agent_linux_arm.sh b/zabbix-autoregister/deploy_zabbix_agent_linux_arm.sh index 79c86c8..0acbaa4 100644 --- a/zabbix-autoregister/deploy_zabbix_agent_linux_arm.sh +++ b/zabbix-autoregister/deploy_zabbix_agent_linux_arm.sh @@ -99,11 +99,31 @@ install_agent_package_manager() { fi # The arm64 Zabbix repos only support Debian 12+ (bookworm, trixie). - # For older/EOL releases like bullseye, skip directly to binary install. + # For older/EOL releases like bullseye, try Debian's own repos first, + # then fall back to extracting from the bookworm .deb. case "${codename}" in buster|bullseye|stretch|jessie) log "Zabbix arm64 apt repo not available for EOL release '${codename}'." - log "Falling back to binary tarball..." + log "Attempting install from Debian's own repositories..." + + # Disable defunct backports repos first + local backports_list + backports_list=$(grep -rl 'backports' /etc/apt/sources.list /etc/apt/sources.list.d/ 2>/dev/null || true) + if [ -n "${backports_list}" ]; then + log "Disabling defunct backports repositories..." + for f in ${backports_list}; do + sed -i '/backports/s/^[^#]/#&/' "${f}" + done + fi + + # Try installing from existing repos (Debian's own packages) + apt-get update 2>&1 | grep -v "^W:" || true + if apt-get install -y zabbix-agent2 2>/dev/null; then + log "Installed from Debian repositories." + return 0 + fi + + log "Not available in Debian repos. Falling back to binary extraction..." install_agent_binary return 0 ;; @@ -156,17 +176,16 @@ install_agent_package_manager() { } install_agent_binary() { - log "Installing Zabbix Agent 2 from direct .deb package (bookworm arm64)..." + log "Installing Zabbix Agent 2 from direct .deb package..." # Zabbix does not publish static binary tarballs for ARM architectures. - # Instead, we fetch the .deb package directly from the debian-arm64 bookworm pool. - # Agent 2 is a Go binary with minimal libc dependencies, so installing a bookworm - # .deb on bullseye works reliably. + # Strategy: download the .deb and extract the binary without running post-install + # scripts (which try to start the service before we've configured it). local deb_path="/tmp/zabbix_agent2.deb" local pool_base="https://repo.zabbix.com/zabbix/7.0/debian-arm64/pool/main/z/zabbix" - # Try multiple URL patterns for the .deb package + # Try bookworm (Debian 12) arm64 .deb local deb_urls=( "${pool_base}/zabbix-agent2_${ZABBIX_VERSION}-1+debian12_arm64.deb" "${pool_base}/zabbix-agent2_${ZABBIX_VERSION}-1%2Bdebian12_arm64.deb" @@ -190,15 +209,14 @@ install_agent_binary() { rm -f "${deb_path}" done - # Fallback: try the CDN static tarball path (may work for some versions) if [ "${downloaded}" = false ]; then + # Fallback: try CDN static tarball (may work for some versions/arches) local tarball_url="https://cdn.zabbix.com/zabbix/binaries/stable/7.0/${ZABBIX_VERSION}/zabbix_agent2-${ZABBIX_VERSION}-${TARBALL_ARCH}-static.tar.gz" local tarball_path="/tmp/zabbix_agent2.tar.gz" log "Trying CDN tarball: ${tarball_url}" if wget -q "${tarball_url}" -O "${tarball_path}" 2>/dev/null || \ curl -sfL "${tarball_url}" -o "${tarball_path}" 2>/dev/null; then - # Verify it's actually a gzip file if file "${tarball_path}" 2>/dev/null | grep -q gzip; then install_from_tarball "${tarball_path}" return 0 @@ -207,42 +225,99 @@ install_agent_binary() { rm -f "${tarball_path}" log "ERROR: Failed to download Zabbix Agent 2 for ${ARCH_LABEL}." - log " No static binary or .deb package found." log " Options:" log " 1. Upgrade to Debian 12 (Bookworm) and re-run this script" log " 2. Manually download from https://www.zabbix.com/download" - log " 3. Install from Debian's own repos: apt install zabbix-agent2" exit 1 fi - # Install the .deb package - log "Installing .deb package..." - mkdir -p /etc/zabbix - mkdir -p /var/log/zabbix - mkdir -p /var/run/zabbix + # Extract the .deb contents manually instead of dpkg -i. + # This avoids post-install scripts that try to start the service before config is ready, + # and avoids glibc version issues by testing the binary directly. + log "Extracting .deb package contents..." + local extract_dir="/tmp/zabbix_agent2_extract" + rm -rf "${extract_dir}" + mkdir -p "${extract_dir}" + dpkg-deb -x "${deb_path}" "${extract_dir}" + + # Check if the binary actually runs on this system (glibc compatibility) + local agent_bin="${extract_dir}/usr/sbin/zabbix_agent2" + if [ ! -f "${agent_bin}" ]; then + agent_bin=$(find "${extract_dir}" -name "zabbix_agent2" -type f 2>/dev/null | head -1) + fi + + if [ -z "${agent_bin}" ] || [ ! -f "${agent_bin}" ]; then + log "ERROR: Could not find zabbix_agent2 binary in package." + rm -rf "${extract_dir}" "${deb_path}" + exit 1 + fi + + # Test if the binary can actually execute on this system + chmod +x "${agent_bin}" + if ! "${agent_bin}" --version >/dev/null 2>&1; then + log "WARNING: Bookworm binary incompatible with this system (likely glibc mismatch)." + log "Attempting full dpkg install with dependency resolution..." + rm -rf "${extract_dir}" + + # Stop any existing broken service + systemctl stop zabbix-agent2 2>/dev/null || true + systemctl disable zabbix-agent2 2>/dev/null || true + + # Try full install — may work if deps can be satisfied + dpkg -i --force-depends "${deb_path}" 2>/dev/null || true + + # Check if binary landed and works + if [ -f /usr/sbin/zabbix_agent2 ] && /usr/sbin/zabbix_agent2 --version >/dev/null 2>&1; then + log "Package installation succeeded despite warnings." + rm -f "${deb_path}" + return 0 + fi + + # Clean up the broken install + dpkg --remove --force-remove-reinstreq zabbix-agent2 2>/dev/null || true + + log "ERROR: Zabbix Agent 2 binary from Debian 12 is not compatible with this system." + log " Your system runs glibc $(ldd --version 2>&1 | head -1 | grep -oP '\\d+\\.\\d+$' || echo 'unknown') but the package requires glibc 2.36+." + log " Options:" + log " 1. Upgrade to Debian 12 (Bookworm): https://www.debian.org/releases/bookworm/" + log " 2. Use Zabbix Agent (v1) instead: apt install zabbix-agent" + log " 3. Build from source: https://www.zabbix.com/documentation/current/en/manual/installation/install" + rm -f "${deb_path}" + exit 1 + fi + + # Binary works! Install files to their proper locations. + log "Binary compatibility verified. Installing files..." # Create zabbix user if it doesn't exist if ! id -u zabbix &>/dev/null; then useradd -r -s /sbin/nologin -d /var/lib/zabbix -M zabbix fi - # Install with --force-depends to handle minor dependency version mismatches - # between bookworm and bullseye - if ! dpkg -i --force-depends "${deb_path}" 2>&1; then - log "dpkg install had issues, attempting to fix dependencies..." - apt-get install -f -y 2>/dev/null || true + # Copy binary + cp "${agent_bin}" /usr/sbin/zabbix_agent2 + chmod +x /usr/sbin/zabbix_agent2 + + # Copy config files and directories + mkdir -p /etc/zabbix + if [ -d "${extract_dir}/etc/zabbix" ]; then + cp -rn "${extract_dir}/etc/zabbix/"* /etc/zabbix/ 2>/dev/null || true fi - rm -f "${deb_path}" + # Set up directories + mkdir -p /var/log/zabbix /var/run/zabbix /var/lib/zabbix + chown -R zabbix:zabbix /var/log/zabbix /var/run/zabbix /var/lib/zabbix - # Verify the binary is in place - if command -v zabbix_agent2 >/dev/null 2>&1 || [ -f /usr/sbin/zabbix_agent2 ]; then - log "Package installation complete." - chown -R zabbix:zabbix /var/log/zabbix /var/run/zabbix 2>/dev/null || true + # Install systemd service from package or create our own + if [ -f "${extract_dir}/lib/systemd/system/zabbix-agent2.service" ]; then + cp "${extract_dir}/lib/systemd/system/zabbix-agent2.service" /lib/systemd/system/ + systemctl daemon-reload else - log "ERROR: zabbix_agent2 binary not found after package install." - exit 1 + create_systemd_service fi + + rm -rf "${extract_dir}" "${deb_path}" + log "Installation complete." } install_from_tarball() {