# Zabbix

Instalação em camadas

# Zabbix: Instalando o banco de dados

Caso você tenha acabado de instalar o debian minimalista, faça os ajustes para ter as ferramentas de diagnóstico, edição e troubleshooting.

[Script pós instalação](https://wiki.pm.ro.gov.br/books/servidores-linux/page/script-pos-instalacao "Script pós instalação")

## Instalação do banco de dados postgresql

```bash
vim instalaDBpostgresql.sh
```

```bash
#!/bin/bash

# ────────────────────────────────────────────────────────────────
# Script de instalação do postgreesql para o Zabbix 7 no Debian 13
# ────────────────────────────────────────────────────────────────

SERVER_ADDRESS='192.168.100.121'
ZABBIX_DB_PASS="MinhaSenhaForte" # Troque por uma senha forte real
LOCAL_NET='192.168.100.0/24'

echo "[1/8] INSTALANDO O REPOSITORIO OFICIAL DA ZABBIX SIA..."
wget https://repo.zabbix.com/zabbix/7.0/debian/pool/main/z/zabbix-release/zabbix-release_latest_7.0+debian13_all.deb
dpkg -i zabbix-release_latest_7.0+debian13_all.deb


echo "[2/8] ATUALIZANDO O SISTEMA..."
apt update && apt upgrade -y


echo "[3/8] INSTALANDO O BANCO DE DADOS POSTGRESQL..."
apt install -y postgresql-17


echo "[4/8] INSTALANDO OS PACOTES PARA O SERVIDOR DE BANCO DE DADOS..."
apt install -y zabbix-sql-scripts zabbix-agent2


echo "[5/8] INICIALIZANDO O SERVIDOR DE BANCO DE DADOS..."
systemctl enable --now postgresql@17-main.service


echo "[6/8] Criando banco de dados para Zabbix..."
sudo -u postgres psql <<EOF
CREATE USER zabbix WITH PASSWORD '$ZABBIX_DB_PASS';
CREATE DATABASE zabbix OWNER zabbix;
EOF


echo "[7/8] FAZENDO UPLOAD DAS TABELAS PARA O BANCO DO ZABBIX..."
zcat /usr/share/zabbix/sql-scripts/postgresql/server.sql.gz | sudo -u zabbix psql zabbix


echo "[8/8] CONFIGURANDO O POSTGRESQL PARA RECEBER O ZABBIX SERVER..."
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /etc/postgresql/17/main/postgresql.conf

echo "[8/8] Liberando toda a minha rede local..."
echo "" >> /etc/postgresql/17/main/pg_hba.conf
echo "# Permissions for access zabbix on lan" >> /etc/postgresql/17/main/pg_hba.conf
echo "host    all          all          $LOCAL_NET        md5" >> /etc/postgresql/17/main/pg_hba.conf


echo "[4/4] Configurando Zabbix Agent2..."
sed -i "s/Server=127.0.0.1/Server=$SERVER_ADDRESS/g" /etc/zabbix/zabbix_agent2.conf
sed -i "s/ServerActive=127.0.0.1/ServerActive=$SERVER_ADDRESS/g" /etc/zabbix/zabbix_agent2.conf
sed -i "s/Hostname=Zabbix server/Hostname=ZBXDB/g" /etc/zabbix/zabbix_agent2.conf
sed -i 's/^# DenyKey=system.run\[\*\]/AllowKey=system.run[*]/' /etc/zabbix/zabbix_agent2.conf


echo "🔁 Ajustando e Reiniciando serviços..."
systemctl restart postgresql@17-main zabbix-agent2


echo "✅ Instalação concluída."
```

Agora dê as pemissões e execute o script

```bash
chmod +x instalaDBpostgresql.sh
./instalaDBpostgresql.sh
```

## Conferindo os logs do agent2

```bash
 tail -f /var/log/zabbix/zabbix_agent2.log
```

## Instalação timescale

**Install the latest Postgres packages**

```bash
apt install gnupg postgresql-common apt-transport-https lsb-release wget
```

**Run the Postgres package setup script**

```bash
/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
```

**Add the TimescaleDB package**

```bash
echo "deb https://packagecloud.io/timescale/timescaledb/debian/ $(lsb_release -c -s) main" | tee /etc/apt/sources.list.d/timescaledb.list
```

**Install the TimescaleDB GPG key**

```bash
wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/timescaledb.gpg
```

**Update your local repository list**

```bash
apt update
```

**Install TimescaleDB**

```bash
apt install timescaledb-2-postgresql-17=2.21.4~debian13 timescaledb-2-loader-postgresql-17=2.21.4~debian13
```

**Caso precise pesquisar quais versões estão disponíveis, faça**

```bash
apt-cache madison timescaledb-2-postgresql-17 | awk '{print $3}'
```

**Impedir que o apt atualize o timescaledb pra não dar conflito de versão**

```bash
apt-mark hold timescaledb-2-postgresql-17 timescaledb-2-loader-postgresql-17
```

**Como Verificar os Pacotes em "Hold":**

Para listar todos os pacotes que estão atualmente "seguros", use o comando:

```bash
apt-mark showhold
```

**Como Remover o "Hold" (para voltar a atualizar):**

Quando você decidir que quer voltar a receber atualizações para esses pacotes no futuro, basta usar o comando `unhold`:

```bash
sudo apt-mark unhold timescaledb-2-postgresql-17 timescaledb-2-loader-postgresql-17
```

**Ajustes finos do timescaledb**

***Será que precisa disso?***

```bash
sudo sed -i "s/max_connections = 50/max_connections = 300/" /etc/postgresql/17/main/postgresql.conf
echo "timescaledb.license=timescale" | sudo tee -a /etc/postgresql/17/main/postgresql.conf
```

**Tune your Postgres instance for TimescaleDB**

```bash
timescaledb-tune --quiet --yes
```

**Restart Postgres**

```bash
systemctl restart postgresql@17-main
```

## Adicione a extensão do TimeScaleDB ao seu banco de dados

**Se essa parte funcionar, não precisa executar os passoa abaixo**

```bash
echo "CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;" | sudo -u postgres psql zabbix 2>/dev/null
```

```bash
cat /usr/share/zabbix/sql-scripts/postgresql/timescaledb/schema.sql | sudo -u zabbix psql zabbix

```

**Acesse o banco** ***Aparentemente não é mais necessário executar os passos abaixo. Mantido apenas pra referência***

```bash
psql -d "postgres://zabbix:MinhaSenhaForte@127.0.0.1:5432/zabbix"
```

**Inclua a extensão**

```bash
CREATE EXTENSION IF NOT EXISTS timescaledb;
```

**Verifique se a extesão foi instalada**

```bash
\dx
```

**Restart Postgres**

```bash
systemctl restart postgresql@17-main
```

# Zabbix - Instalação do backend

## <span style="box-sizing: border-box; outline-color: rgb(39, 131, 53); outline-width: 1px; margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; font-optical-sizing: inherit; font-size-adjust: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; vertical-align: baseline;">1. Introdução</span>

<span style="box-sizing: border-box; outline-color: rgb(39, 131, 53); outline-width: 1px; margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; font-optical-sizing: inherit; font-size-adjust: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; vertical-align: baseline;">Zabbix é uma solução de código aberto para monitoramento de dispositivos e aplicações, sendo capaz de monitorar parâmetros utilizando agentes em diversas plataformas de sistemas operacionais, além de protocolos como SNMP, IPMI, JMX, entre outros.</span>

<span style="box-sizing: border-box; outline-color: rgb(39, 131, 53); outline-width: 1px; margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inherit; font-optical-sizing: inherit; font-size-adjust: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; vertical-align: baseline;">É uma plataforma bastante difundida e não está limitada a ativos de TIC, apenas. Pode ser utilizado em diversas áreas, pois o seu método de coleta é flexível e permitindo personalizá-lo de acordo com necessidades de monitoramento do ambiente.</span>

```bash
vim instalaZabbixApp.sh
```

```bash
#!/bin/bash

# ────────────────────────────────────────────────────────────────
# Script de instalação completa do Zabbix 7 no Debian 13 + Apache
# ────────────────────────────────────────────────────────────────

ZBX_DBHOST='192.168.100.120'
ZABBIX_DB_PASS="MinhaSenhaForte"  # Troque por uma senha forte real

echo "[1/4] INSTALANDO O REPOSITORIO OFICIAL DA ZABBIX SIA..."
wget https://repo.zabbix.com/zabbix/7.0/debian/pool/main/z/zabbix-release/zabbix-release_latest_7.0+debian13_all.deb
dpkg -i zabbix-release_latest_7.0+debian13_all.deb


echo "[2/4] ATUALIZANDO O SISTEMA..."
apt update && apt upgrade -y

echo "[3/4] Instalando pacotes do Zabbix..."
apt install -y zabbix-server-pgsql zabbix-agent2 

echo "[4/4] Configurando Server..."
sed -i "s|^# DBPassword=.*|DBPassword=$ZABBIX_DB_PASS|" /etc/zabbix/zabbix_server.conf
sed -i 's|^#\s*DBHost=.*|DBHost=$ZBX_DBHOST|' /etc/zabbix/zabbix_server.conf
sed -i "s/EnableGlobalScripts=0/EnableGlobalScripts=1/" /etc/zabbix/zabbix_server.conf

echo "[4/4] Configurando Zabbix Agent2..."
sed -i "s/Hostname=Zabbix server/Hostname=ZBXAPP/g" /etc/zabbix/zabbix_agent2.conf


systemctl enable --now zabbix-server zabbix-agent2
systemctl restart zabbix-server zabbix-agent2

echo "✅ Instalação concluída."
```

```bash
chmod +x instalaZabbixApp.sh
./instalaZabbixApp.sh
```

## Conferindo os logs do agent2

```bash
 tail -f /var/log/zabbix/zabbix_agent2.log
```

# Zabbix - Instalação do frontend e Grafana

Zabbix 7 LTS

```bash
vim instalaZabbixFront.sh
```

```bash
#!/bin/bash

# ────────────────────────────────────────────────────────────────
# Script de instalação completa do Zabbix 7 no Debian 13 + Apache
# ────────────────────────────────────────────────────────────────

SERVER_ADDRESS='192.168.100.121'
PHPINI="/etc/php/8.4/apache2/php.ini"
MEU_IP=$(hostname -I | awk '{print $1}')
TIMEZONE="America/Porto_Velho"      # Veja os fusos suportados aqui https://www.php.net/manual/pt_BR/timezones.php


echo "[1/8] INSTALANDO O REPOSITORIO OFICIAL DA ZABBIX SIA..."
wget https://repo.zabbix.com/zabbix/7.0/debian/pool/main/z/zabbix-release/zabbix-release_latest_7.0+debian13_all.deb
dpkg -i zabbix-release_latest_7.0+debian13_all.deb


echo "[2/8] ATUALIZANDO O SISTEMA..."
apt update && apt upgrade -y


echo "[3/8] Instalando pacotes do Zabbix..."
apt install -y zabbix-frontend-php php8.4-pgsql zabbix-apache-conf zabbix-agent2


echo "[4/8] Habilitando e iniciando serviços..."
systemctl enable --now apache2 zabbix-agent2 


echo "[5/8] Ajustando PHP.ini..."
sed -i "s|memory_limit = .*|memory_limit = 512M|" $PHPINI
sed -i "s|max_execution_time = .*|max_execution_time = 600|" $PHPINI
sed -i "s|upload_max_filesize = .*|upload_max_filesize = 100M|" $PHPINI
sed -i "s|;date.timezone =|date.timezone = $TIMEZONE|" $PHPINI


echo "[6/8] Configurando front to server..."
sed -i "s#// \$ZBX_SERVER.*= '';#$ZBX_SERVER = '$SERVER_ADDRESS';#" /etc/zabbix/web/zabbix.conf.php
sed -i "s#// \$ZBX_SERVER_PORT.*= '';#$ZBX_SERVER_PORT = '10051';#" /etc/zabbix/web/zabbix.conf.php
sed -i '/^\s*DocumentRoot \/var\/www\/html/a \ \ \ \ RedirectMatch permanent ^/$ /zabbix' /etc/apache2/sites-available/000-default.conf


echo "[6/8] Configurando Zabbix Agent2..."
sed -i "s/Server=127.0.0.1/Server=$SERVER_ADDRESS/g" /etc/zabbix/zabbix_agent2.conf
sed -i "s/ServerActive=127.0.0.1/ServerActive=$SERVER_ADDRESS/g" /etc/zabbix/zabbix_agent2.conf
sed -i "s/Hostname=Zabbix server/Hostname=ZBXFRONT/g" /etc/zabbix/zabbix_agent2.conf

echo "[8/8] Ajustando timezone do Apache para Zabbix..."
echo "php_value date.timezone $TIMEZONE" >> /etc/zabbix/apache.conf

echo "🔧 Ajustes extras de segurança e desempenho..."
# Apache
a2enmod rewrite headers deflate ssl
sed -i "s|ServerTokens OS|ServerTokens Prod|" /etc/apache2/conf-available/security.conf
sed -i "s|ServerSignature On|ServerSignature Off|" /etc/apache2/conf-available/security.conf

# PHP Opcache
cat <<EOF >> /etc/php/8.4/apache2/conf.d/10-opcache.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
EOF

echo "🔁 Reiniciando serviços..."
systemctl restart apache2 zabbix-agent2


echo "✅ Instalação concluída. Acesse http://$MEU_IP/zabbix no navegador."
echo "Login padrão: Admin / zabbix"
```

Agora dê as pemissões e execute o script

```bash
chmod +x instalaZabbixFront.sh
./instalaZabbixFront.sh
```

## Conferindo os logs do agent2

```bash
 tail -f /var/log/zabbix/zabbix_agent2.log
```