#!/bin/bash # 1. Silenciar avisos e preparar ambiente export LC_ALL=C ZABBIX_SERVERS="192.168.120.231,192.168.120.75,192.168.120.76" VM_HOSTNAME=$(hostname) CONFIG_FILE="/etc/zabbix/zabbix_agent2.conf" echo "Detectando sistema operacional..." if [ -f /etc/debian_version ]; then # --- Lógica para Debian / Ubuntu --- echo "Sistema detectado: Debian/Ubuntu" wget -q https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_latest+debian12_all.deb dpkg -i zabbix-release_latest+debian12_all.deb > /dev/null 2>&1 apt-get update -y > /dev/null 2>&1 apt-get install zabbix-agent2 -y > /dev/null 2>&1 elif [ -f /etc/redhat-release ]; then # --- Lógica para RHEL / CentOS / Rocky / AlmaLinux --- echo "Sistema detectado: RHEL/CentOS" rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/zabbix-release-6.4-1.el9.noarch.rpm > /dev/null 2>&1 dnf clean all > /dev/null 2>&1 dnf install zabbix-agent2 -y > /dev/null 2>&1 else echo "Erro: Sistema operacional não suportado por este script automático." exit 1 fi # 2. Configuração Comum do arquivo zabbix_agent2.conf if [ -f "$CONFIG_FILE" ]; then echo "Configurando agente para o host: $VM_HOSTNAME" # Backup da config original cp $CONFIG_FILE "$CONFIG_FILE.bak" # Ajusta os parâmetros sed -i "s|^Server=127.0.0.1|Server=$ZABBIX_SERVERS|" $CONFIG_FILE sed -i "s|^ServerActive=127.0.0.1|ServerActive=$ZABBIX_SERVERS|" $CONFIG_FILE sed -i "s|^Hostname=Zabbix server|Hostname=$VM_HOSTNAME|" $CONFIG_FILE # 3. Reiniciar e Habilitar o serviço systemctl restart zabbix-agent2 systemctl enable zabbix-agent2 echo "Sucesso: Zabbix Agent 2 configurado e ativo." else echo "Erro: Arquivo de configuração não encontrado em $CONFIG_FILE" exit 1 fi