#!/bin/bash # TODO: # - More error messages based on exit status value of command (like a failed download) # - Allow parameters and interactive run to be able to set the server ip, allow / disallow creation of additional configurations (like for Proxmox Containers) # Define the Zabbix server IP ZABBIX_SERVER_IP="192.168.8.41" LOG_FILE="za2ai.log" # Detect the operating system and version OS=$(awk -F= '/^NAME/{print $2}' /etc/os-release | tr -d '"') VERSION=$(awk -F= '/^VERSION_ID/{print $2}' /etc/os-release | tr -d '"') # Log functions _info() { local message=$1 echo -e "\e[32m[INF] $message\e[0m" 2>&1 | tee -a "$LOG_FILE" } _warn() { local message=$1 echo -e "\e[33m[WRN] $message\e[0m" | tee -a "$LOG_FILE" } _error() { local message=$1 echo -e "\033[1m\e[31m[ERR] $message\e[0m" | tee -a "$LOG_FILE" } SCRIPT_VERSION="1.0" _info "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓" _info "┃ ┃" _info "┃ Zabbix Agent 2 Auto-Installer by Zion Networks UG ┃" _info "┃ ┃" _info "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛" _info "" _info "Script Version: $SCRIPT_VERSION" _info "Detected OS: $OS $VERSION" _info "Logging to file: $LOG_FILE" # Check if running as root if [[ $EUID -ne 0 ]]; then _error "This script must be run as root." exit 1 fi _info "Checking dependencies ..." if [ -x "$(command -v sed)" ]; then _info "sed - OK"; else _error "sed - MISSING" ; exit 1 ; fi if [ -x "$(command -v hostname)" ]; then _info "hostname - OK"; else _error "hostname - MISSING" ; exit 1 ; fi if [ -x "$(command -v ip)" ]; then _info "ip - OK"; else _error "ip - MISSING" ; exit 1 ; fi # Download and install the Zabbix agent if [[ "$OS" == *"CentOS"* ]]; then if [ -x "$(command -v rpm)" ]; then _info "rpm - OK"; else _error "rpm - MISSING" ; exit 1 ; fi if [ -x "$(command -v yum)" ]; then _info "yum - OK"; else _error "yum - MISSING" ; exit 1 ; fi if [[ "$VERSION" == "6"* ]]; then if [ -x "$(command -v service)" ]; then _info "service - OK"; else _error "service - MISSING" ; exit 1 ; fi 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 "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 if [ -x "$(command -v apt)" ]; then _info "apt - OK"; else _error "apt - MISSING" ; exit 1 ; fi if [ -x "$(command -v systemctl)" ]; then _info "systemctl - OK"; else _error "systemctl - MISSING" ; exit 1 ; fi 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 fi _info "Installation done!" # Get the IP address and hostname IP=$(ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -f1 -d'/') HOST=$(hostname | tr '[:lower:]' '[:upper:]') _info "Detected IP: $IP" _info "Detected Hostname: $HOST" _info "Updating Zabbix Agent 2 configuration ..." # Update the Zabbix agent configuration sed -i "s/# SourceIP=/SourceIP=$IP/g" /etc/zabbix/zabbix_agent2.conf sed -i "s/Server=127.0.0.1/Server=$ZABBIX_SERVER_IP/g" /etc/zabbix/zabbix_agent2.conf sed -i "s/ServerActive=127.0.0.1/ServerActive=$ZABBIX_SERVER_IP/g" /etc/zabbix/zabbix_agent2.conf sed -i "s/# ListenIP=0.0.0.0/ListenIP=0.0.0.0/g" /etc/zabbix/zabbix_agent2.conf sed -i "s/Hostname=Zabbix server/Hostname=$HOST/g" /etc/zabbix/zabbix_agent2.conf _info "Updating done!" _info "Writing additional configuration file for Proxmox Container support" # Create the zabbix_container.conf file cat << EOF > /etc/zabbix/zabbix_agent2.d/zabbix_container.conf UserParameter=ct.memory.size[*],free -b | awk 'NR==2 {total=\$2; used=(\$3+\$5); pused=((\$3+\$5)*100/\$2); free=\$4; pfree=(\$4*100/\$2); shared=\$5; buffers=\$6; cached=\$7; available=\$8; pavailable=(\$8*100/\$2); if("\$1" == "") {printf("%.0f", total )} else {printf("%.0f", \$1 "" )} }' UserParameter=ct.swap.size[*],free -b | awk 'NR==3 {total=\$2; used=\$3; free=\$4; pfree=(\$4*100/\$2); pused=(\$3*100/\$2); if("\$1" == "") {printf("%.0f", free )} else {printf("%.0f", \$1 "" )} }' UserParameter=ct.cpu.load[*],cut -d" " -f1-3 /proc/loadavg | awk -F'[, ]+' '{avg1=\$(NF-2); avg5=\$(NF-1); avg15=\$(NF)}{print \$2/'$(nproc)'}' UserParameter=ct.uptime,cut -d"." -f1 /proc/uptime EOF _info "Restarting and enabling service ..." # Restart and enable service if [[ $OS == *"CentOS"* ]]; then if [[ $VERSION == "6"* ]]; then service zabbix-agent restart &>> "$LOG_FILE" chkconfig --level 35 zabbix-agent on &>> "$LOG_FILE" elif [[ $VERSION == "7"* ]]; then systemctl restart zabbix-agent2 &>> "$LOG_FILE" systemctl enable zabbix-agent2 &>> "$LOG_FILE" elif [[ $VERSION == "8"* ]]; then systemctl restart zabbix-agent2 &>> "$LOG_FILE" systemctl enable zabbix-agent2 &>> "$LOG_FILE" elif [[ $VERSION == "9"* ]]; then systemctl restart zabbix-agent2 &>> "$LOG_FILE" systemctl enable zabbix-agent2 &>> "$LOG_FILE" else _error "Your CentOS version is not supported." exit 1 fi elif [[ $OS == *"Debian"* ]]; then systemctl restart zabbix-agent2 &>> "$LOG_FILE" systemctl enable zabbix-agent2 &>> "$LOG_FILE" else _error "Your operating system is not supported." exit 1 fi # Display the installation result _info _info "Installation of Zabbix Agent 2 successful!" _info "IP: $IP" _info "Hostname: $HOST" _info _info "Remember to create a new host in your Zabbix Dashboard at https://mon.zion-networks.de/zabbix.php?action=host.edit"