#!/bin/bash
# Script para instalar Zabbix Agent 6 LTS no Ubuntu 24.04
# Autor: Lucas Gabriel

# Solicita HostName ao usuário
read -rp "Digite o HostName para este agente Zabbix: " HOSTNAME

echo "Atualizando pacotes..."
sudo apt update -y

echo "Instalando repositório Zabbix para Ubuntu 24.04..."
wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_6.0+ubuntu24.04_all.deb
sudo dpkg -i zabbix-release_latest_6.0+ubuntu24.04_all.deb
sudo apt update -y

echo "Instalando Zabbix Agent..."
sudo apt install -y zabbix-agent

echo "Configurando Zabbix Agent..."
sudo sed -i "s/^ServerActive=.*/#ServerActive=/" /etc/zabbix/zabbix_agentd.conf
sudo sed -i "s/^Server=.*/Server=monitor.ldata.com.br/" /etc/zabbix/zabbix_agentd.conf
sudo sed -i "s/^Hostname=.*/Hostname=$HOSTNAME/" /etc/zabbix/zabbix_agentd.conf
sudo sed -i "s/^# Hostname=.*/Hostname=$HOSTNAME/" /etc/zabbix/zabbix_agentd.conf

echo "Reiniciando e habilitando o serviço..."
sudo systemctl restart zabbix-agent
sudo systemctl enable zabbix-agent

echo "Configurando firewall UFW..."
sudo ufw allow from 45.160.103.143 to any port 10050 proto tcp comment 'zabbix-ldata'

echo "==== INSTALAÇÃO FINALIZADA ===="
echo "Servidor configurado: monitor.ldata.com.br"
echo "HostName definido: $HOSTNAME"
echo "Firewall liberado para: 45.160.103.143 -> porta 10050/tcp"
