From e1fafa5c09e20e79ebc0167e58a23b64bde9b94d Mon Sep 17 00:00:00 2001 From: Enrico Ludwig Date: Sun, 4 Feb 2024 15:04:01 +0100 Subject: [PATCH] Added more error handling and cleanup --- agent2_autoinstall.sh | 296 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 290 insertions(+), 6 deletions(-) diff --git a/agent2_autoinstall.sh b/agent2_autoinstall.sh index 2cf3632..6cdf9b6 100755 --- a/agent2_autoinstall.sh +++ b/agent2_autoinstall.sh @@ -64,39 +64,172 @@ if [[ "$OS" == *"CentOS"* ]]; then if [ -x "$(command -v chkconfig)" ]; then _info "chkconfig - OK"; else _error "chkconfig - MISSING" ; exit 1 ; fi _info "Downloading and installing Zabbix repositories ..." + rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/6/x86_64/zabbix-release-6.4-1.el6.noarch.rpm &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to download or install Zabbix repository package" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + yum clean all &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _warn "Command 'yum clean all' failed" + _warn "See logfile at $LOG_FILE for further information" + fi + _info "Installing Zabbix Agent 2 ..." + yum install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to install Zabbix 2 Agent" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release-6.4-1.el6.noarch.rpm + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + elif [[ "$VERSION" == "7"* ]]; then if [ -x "$(command -v systemctl)" ]; then _info "systemctl - OK"; else _error "systemctl - MISSING" ; exit 1 ; fi _info "Downloading and installing Zabbix repositories ..." + rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/7/x86_64/zabbix-release-6.4-1.el7.noarch.rpm &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to download or install Zabbix repository package" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + yum clean all &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _warn "Command 'yum clean all' failed" + _warn "See logfile at $LOG_FILE for further information" + fi + _info "Installing Zabbix Agent 2 ..." + yum install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to install Zabbix 2 Agent" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release-6.4-1.el7.noarch.rpm + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + elif [[ "$VERSION" == "8"* ]]; then if [ -x "$(command -v systemctl)" ]; then _info "systemctl - OK"; else _error "systemctl - MISSING" ; exit 1 ; fi _info "Downloading and installing Zabbix repositories ..." + rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/8/x86_64/zabbix-release-6.4-1.el8.noarch.rpm &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to download or install Zabbix repository package" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + dnf clean all &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _warn "Command 'dnf clean all' failed" + _warn "See logfile at $LOG_FILE for further information" + fi + _info "Installing Zabbix Agent 2 ..." + dnf install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to install Zabbix 2 Agent" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release-6.4-1.el8.noarch.rpm + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + elif [[ "$VERSION" == "9"* ]]; then if [ -x "$(command -v systemctl)" ]; then _info "systemctl - OK"; else _error "systemctl - MISSING" ; exit 1 ; fi - _info "Downloading and installing Zabbix repositories ..." + _info "Disable Zabbix packages provided by EPEL ..." + sed -i '/\[epel\]/a excludepkgs=Zabbix*' /etc/yum.repos.d/epel.repo &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _warn "Failed to disable EPEL source!" + _warn "See logfile at $LOG_FILE for further information" + fi + + _info "Downloading and installing Zabbix repositories ..." + rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/zabbix-release-6.4-1.el9.noarch.rpm &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to download or install Zabbix repository package" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + dnf clean all &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _warn "Command 'dnf clean all' failed" + _warn "See logfile at $LOG_FILE for further information" + fi + _info "Installing Zabbix Agent 2 ..." + dnf install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to install Zabbix 2 Agent" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release-6.4-1.el9.noarch.rpm + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + else _error "Your CentOS version is not supported." exit 1 fi + elif [[ "$OS" == *"Debian"* ]]; then if [ -x "$(command -v wget)" ]; then _info "wget - OK"; else _error "wget - MISSING" ; exit 1 ; fi if [ -x "$(command -v dpkg)" ]; then _info "dpkg - OK"; else _error "dpkg - MISSING" ; exit 1 ; fi @@ -105,33 +238,189 @@ elif [[ "$OS" == *"Debian"* ]]; then if [[ "$VERSION" == "9"* ]]; then _info "Downloading Zabbix repositories package ..." + wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian9_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "File download failed!" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian9_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Repository package installation failed!" + _error "See logfile at $LOG_FILE for further information" + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian9_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian9_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + elif [[ "$VERSION" == "10"* ]]; then _info "Downloading Zabbix repositories package ..." + wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian10_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "File download failed!" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian10_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Repository package installation failed!" + _error "See logfile at $LOG_FILE for further information" + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian10_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian10_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + elif [[ "$VERSION" == "11"* ]]; then _info "Downloading Zabbix repositories package ..." + wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian11_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "File download failed!" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian11_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Repository package installation failed!" + _error "See logfile at $LOG_FILE for further information" + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian11_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian11_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + elif [[ "$VERSION" == "12"* ]]; then _info "Downloading Zabbix repositories package ..." + wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian12_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "File download failed!" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian12_all.deb &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Repository package installation failed!" + _error "See logfile at $LOG_FILE for further information" + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian12_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + + exit 1 + fi + + _info "Removing repository installation package" + + rm -f zabbix-release_6.4-1+debian12_all.deb + + if [ $? -ne 0 ]; then + _warn "Failed to remove downloaded temporary files" + _warn "See logfile at $LOG_FILE for further information" + fi + else _error "Your Debian version is not supported." exit 1 fi _info "Running 'apt update' ..." + apt update &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Command 'apt update' failed!" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + _info "Installing Zabbix Agent 2 ..." + apt install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" + + if [ $? -ne 0 ]; then + _error "Failed to install Zabbix 2 Agent" + _error "See logfile at $LOG_FILE for further information" + exit 1 + fi + else _error "Your operating system is not supported." exit 1 @@ -194,11 +483,6 @@ else exit 1 fi -# Cleanup -_info "Cleaning up temporary files ..." -# TODO -_info "Cleanup done!" - # Display the installation result _info _info "Installation of Zabbix Agent 2 successful!"