From da1f64c4c257c0009acd40cf7aadfdce26c459fd Mon Sep 17 00:00:00 2001 From: Enrico Ludwig Date: Sat, 3 Feb 2024 21:49:02 +0100 Subject: [PATCH] Some more logging, now hiding original command ouputs --- agent2_autoinstall.sh | 93 ++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/agent2_autoinstall.sh b/agent2_autoinstall.sh index fc57300..fa0d36a 100755 --- a/agent2_autoinstall.sh +++ b/agent2_autoinstall.sh @@ -46,51 +46,71 @@ fi # Download and install the Zabbix agent if [[ "$OS" == *"CentOS"* ]]; then if [[ "$VERSION" == "6"* ]]; then - rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/6/x86_64/zabbix-release-6.4-1.el6.noarch.rpm | tee -a "$LOG_FILE" - yum clean all | tee -a "$LOG_FILE" - yum install zabbix-agent2 zabbix-agent2-plugin-* -y | tee -a "$LOG_FILE" + _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" + yum clean all &>> "$LOG_FILE" + _info "Installing Zabbix Agent 2 ..." + yum install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" elif [[ "$VERSION" == "7"* ]]; then - rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/7/x86_64/zabbix-release-6.4-1.el7.noarch.rpm | tee -a "$LOG_FILE" - yum clean all | tee -a "$LOG_FILE" - yum install zabbix-agent2 zabbix-agent2-plugin-* -y | tee -a "$LOG_FILE" + _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" + yum clean all &>> "$LOG_FILE" + _info "Installing Zabbix Agent 2 ..." + yum install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" elif [[ "$VERSION" == "8"* ]]; then - rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/8/x86_64/zabbix-release-6.4-1.el8.noarch.rpm | tee -a "$LOG_FILE" - dnf clean all | tee -a "$LOG_FILE" - dnf install zabbix-agent2 zabbix-agent2-plugin-* -y | tee -a "$LOG_FILE" + _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" + dnf clean all &>> "$LOG_FILE" + _info "Installing Zabbix Agent 2 ..." + dnf install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" elif [[ "$VERSION" == "9"* ]]; then - sed -i '/\[epel\]/a excludepkgs=Zabbix*' /etc/yum.repos.d/epel.repo | tee -a "$LOG_FILE" - rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/zabbix-release-6.4-1.el9.noarch.rpm | tee -a "$LOG_FILE" - dnf clean all | tee -a "$LOG_FILE" - dnf install zabbix-agent2 zabbix-agent2-plugin-* -y | tee -a "$LOG_FILE" + _info "Downloading and installing Zabbix repositories ..." + sed -i '/\[epel\]/a excludepkgs=Zabbix*' /etc/yum.repos.d/epel.repo &>> "$LOG_FILE" + rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/zabbix-release-6.4-1.el9.noarch.rpm &>> "$LOG_FILE" + dnf clean all &>> "$LOG_FILE" + _info "Installing Zabbix Agent 2 ..." + dnf install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" else _error "Your CentOS version is not supported." exit 1 fi elif [[ "$OS" == *"Debian"* ]]; then if [[ "$VERSION" == "9"* ]]; then - wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian9_all.deb | tee -a "$LOG_FILE" - dpkg -i zabbix-release_6.4-1+debian9_all.deb | tee -a "$LOG_FILE" + _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" + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian9_all.deb &>> "$LOG_FILE" elif [[ "$VERSION" == "10"* ]]; then - wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian10_all.deb | tee -a "$LOG_FILE" - dpkg -i zabbix-release_6.4-1+debian10_all.deb | tee -a "$LOG_FILE" + _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" + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian10_all.deb &>> "$LOG_FILE" elif [[ "$VERSION" == "11"* ]]; then - wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian11_all.deb | tee -a "$LOG_FILE" - dpkg -i zabbix-release_6.4-1+debian11_all.deb | tee -a "$LOG_FILE" + _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" + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian11_all.deb &>> "$LOG_FILE" elif [[ "$VERSION" == "12"* ]]; then - wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian12_all.deb | tee -a "$LOG_FILE" - dpkg -i zabbix-release_6.4-1+debian12_all.deb | tee -a "$LOG_FILE" + _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" + _info "Installing Zabbix repositories package ..." + dpkg -i zabbix-release_6.4-1+debian12_all.deb &>> "$LOG_FILE" else _error "Your Debian version is not supported." exit 1 fi - apt update | tee -a "$LOG_FILE" - apt install zabbix-agent2 zabbix-agent2-plugin-* -y | tee -a "$LOG_FILE" + _info "Running 'apt update' ..." + apt update &>> "$LOG_FILE" + _info "Installing Zabbix Agent 2 ..." + apt install zabbix-agent2 zabbix-agent2-plugin-* -y &>> "$LOG_FILE" 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:]') @@ -98,6 +118,8 @@ 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 @@ -105,6 +127,9 @@ sed -i "s/# ListenIP=0.0.0.0/ListenIP=$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/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 "" )} }' @@ -113,27 +138,29 @@ UserParameter=ct.cpu.load[*],cut -d" " -f1-3 /proc/loadavg | awk -F'[, ]+' '{avg 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 | tee -a "$LOG_FILE" - chkconfig --level 35 zabbix-agent on | tee -a "$LOG_FILE" + service zabbix-agent restart &>> "$LOG_FILE" + chkconfig --level 35 zabbix-agent on &>> "$LOG_FILE" elif [[ $VERSION == "7"* ]]; then - systemctl restart zabbix-agent2 | tee -a "$LOG_FILE" - systemctl enable zabbix-agent2 | tee -a "$LOG_FILE" + systemctl restart zabbix-agent2 &>> "$LOG_FILE" + systemctl enable zabbix-agent2 &>> "$LOG_FILE" elif [[ $VERSION == "8"* ]]; then - systemctl restart zabbix-agent2 | tee -a "$LOG_FILE" - systemctl enable zabbix-agent2 | tee -a "$LOG_FILE" + systemctl restart zabbix-agent2 &>> "$LOG_FILE" + systemctl enable zabbix-agent2 &>> "$LOG_FILE" elif [[ $VERSION == "9"* ]]; then - systemctl restart zabbix-agent2 | tee -a "$LOG_FILE" - systemctl enable zabbix-agent2 | tee -a "$LOG_FILE" + 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 | tee -a "$LOG_FILE" - systemctl enable zabbix-agent2 | tee -a "$LOG_FILE" + systemctl restart zabbix-agent2 &>> "$LOG_FILE" + systemctl enable zabbix-agent2 &>> "$LOG_FILE" else _error "Your operating system is not supported." exit 1