echo -e 'server:
    # The following options enables statistics queries
    statistics-interval: 0
    statistics-cumulative: no
    extended-statistics: yes

remote-control:
    # Enable remote control with unbound-control(8) here.
    # set up the keys and certificates with unbound-control-setup.
    control-enable: yes
   
    # what interfaces are listened to for remote control.
    # give 0.0.0.0 and ::0 to listen to all interfaces.
    control-interface: 127.0.0.1
   
    # port number for remote control operations.
    control-port: 8953
   
    # unbound server key file.
    server-key-file: "/etc/unbound/unbound_server.key"
   
    # unbound server certificate file.
    server-cert-file: "/etc/unbound/unbound_server.pem"
   
    # unbound-control key file.
    control-key-file: "/etc/unbound/unbound_control.key"
   
    # unbound-control certificate file.
    control-cert-file: "/etc/unbound/unbound_control.pem"

' > /etc/unbound/unbound.conf.d/statistics-enable.conf

systemctl restart unbound

echo -e "Defaults:zabbix !requiretty
zabbix ALL = NOPASSWD: /usr/sbin/unbound-control
" > /etc/sudoers.d/zabbix-agent
chmod 0440 /etc/sudoers.d/zabbix-agent

# Defina os possíveis caminhos em um vetor
possible_paths=(
    "/etc/zabbix/zabbix_agentd.d/"
    "/etc/zabbix/zabbix_agentd.conf.d/"
)

# Possíveis caminhos
for path in "${possible_paths[@]}"; do
    # Verifica se o diretório existe
    if [ -d "$path" ]; then
        # Escreve o arquivo userparameter_bind.conf no diretório encontrado
        echo -e "UserParameter=unbound.type[*],echo -n 0; sudo /usr/sbin/unbound-control stats_noreset | grep num.query.type.\$1= | cut -d= -f2
UserParameter=unbound.mem[*],sudo /usr/sbin/unbound-control stats_noreset | grep mem.\$1= | cut -d= -f2
UserParameter=unbound.flag[*],sudo /usr/sbin/unbound-control stats_noreset | grep num.query.\$1= | cut -d= -f2
UserParameter=unbound.total[*],sudo /usr/sbin/unbound-control stats_noreset | grep total.num.\$1= | cut -d= -f2
UserParameter=unbound.rcode[*],sudo /usr/sbin/unbound-control stats_noreset | grep num.answer.rcode.\$1= | cut -d= -f2
UserParameter=unbound.class[*],sudo /usr/sbin/unbound-control stats_noreset | grep num.query.class.\$1= | cut -d= -f2
UserParameter=unbound.time.up[*],sudo /usr/sbin/unbound-control stats_noreset | grep time.up | cut -d= -f2
UserParameter=unbound.histogram[*],sudo /usr/sbin/unbound-control stats_noreset | grep histogram.\$1= | cut -d= -f2
UserParameter=unbound.histogram.total[*],sudo /usr/sbin/unbound-control stats_noreset | grep histogram.\$1= | cut -d= -f2
" > "${path}/userparameter_unbound.conf"
        echo "Arquivo userparameter_unbound.conf criado em ${path}"
        break  # Sai do loop após encontrar o primeiro diretório existente
    fi
done

# Se nenhum diretório válido foi encontrado, exibe uma mensagem de erro
if [ ! -f "${path}/userparameter_unbound.conf" ]; then
    echo "Nenhum diretório válido encontrado para criar o arquivo userparameter_unbound.conf."
fi

systemctl restart zabbix-agent
