1
0

Added Zabbix Agent 2 Autoinstaller (WIP)

This commit is contained in:
Enrico Ludwig 2024-02-03 18:09:49 +01:00
parent ce350d816d
commit 17ad8ba870

149
agent2_autoinstall.sh Executable file
View File

@ -0,0 +1,149 @@
#!/bin/bash
# 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)
VERSION=$(awk -F= '/^VERSION_ID/{print $2}' /etc/os-release)
# 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"
# Check if running as root
if [[ $EUID -ne 0 ]]; then
_error "This script must be run as root."
exit 1
fi
exit 0
# 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"
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"
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"
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"
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"
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"
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"
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"
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"
else
_error "Your operating system is not supported."
exit 1
fi
# 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"
# 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/# 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
# 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
# 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"
elif [[ $VERSION == "7"* ]]; then
systemctl restart zabbix-agent2 | tee -a "$LOG_FILE"
systemctl enable zabbix-agent2 | tee -a "$LOG_FILE"
elif [[ $VERSION == "8"* ]]; then
systemctl restart zabbix-agent2 | tee -a "$LOG_FILE"
systemctl enable zabbix-agent2 | tee -a "$LOG_FILE"
elif [[ $VERSION == "9"* ]]; then
systemctl restart zabbix-agent2 | tee -a "$LOG_FILE"
systemctl enable zabbix-agent2 | tee -a "$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"
else
_error "Your operating system is not supported."
exit 1
fi
# Display the installation result
_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"