Skip to main content

Zabbix & Zabbix Agent 2 - Monitoring Setup, PostgreSQL & MySQL Setup

1086 words·
Zabbix Monitoring Nginx PostgreSQL MySQL
Table of Contents

Zabbix Server
#

PostgreSQL & Nginx Version
#

The following tutorial installs Zabbix Server with PostgreSQL and Nginx.

Install Zabbix
#

# Change directory
cd /tmp

# Download .deb package
sudo wget https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.4-1+ubuntu22.04_all.deb

# Install .deb package
sudo dpkg -i zabbix-release_6.4-1+ubuntu22.04_all.deb

# Update package index
sudo apt update

# Install Zabbix Server, Zabbix Frontend, Zabbix Agent & dependencies
sudo apt install zabbix-server-pgsql zabbix-frontend-php php8.1-pgsql zabbix-nginx-conf zabbix-sql-scripts zabbix-agent -y

PostgreSQL
#

  • Install PostgreSQL
# Install PostgreSQL
sudo apt install postgresql -y

# Check status
sudo systemctl status postgresql

# Check if service start is enabled
sudo systemctl is-enabled postgresql
  • Create User & DB
# Create "zabbix" user and prompt for pw
# Note: "postgres" is a default administrative user 
sudo -u postgres createuser --pwprompt zabbix

# Shell output:
Enter password for new role: # Define pw for zabbix user
Enter it again: # Reenter pw


# Create "zabbix" db and define "zabbix" user as owner
sudo -u postgres createdb -O zabbix zabbix 
  • Import Data
# Import initial schema and data
zcat /usr/share/zabbix-sql-scripts/postgresql/server.sql.gz | sudo -u zabbix psql zabbix

Note: The zcat command reads compressed (gzip )files and outputs their contents.


Zabbix Configuration
#

# Open Zabbix main configuration
sudo vi /etc/zabbix/zabbix_server.conf
  • Define PostgreSQL PW
# Define the PostgreSQL pw for the zabbix user
DBPassword=password
  • Default log settings
# Default Zabbix log
/var/log/zabbix/zabbix_server.log

# Disable log rotation
LogFileSize=0

Nginx
#

sudo vi /etc/zabbix/nginx.conf
server {
        listen          443 ssl;
        server_name     zabbix.jklug.work;

        ssl_certificate         /etc/letsencrypt/live/zabbix.jklug.work/fullchain.pem;
        ssl_certificate_key     /etc/letsencrypt/live/zabbix.jklug.work/privkey.pem;


        root    /usr/share/zabbix;

        index   index.php;

        location = /favicon.ico {
                log_not_found   off;
        }

        location / {
                try_files       $uri $uri/ =404;
        }

        location /assets {
                access_log      off;
                expires         10d;
        }

        location ~ /\.ht {
                deny            all;
        }

        location ~ /(api\/|conf[^\.]|include|locale) {
                deny            all;
                return          404;
        }

        location /vendor {
                deny            all;
                return          404;
        }

        location ~ [^/]\.php(/|$) {
                fastcgi_pass    unix:/var/run/php/zabbix.sock;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index   index.php;

                fastcgi_param   DOCUMENT_ROOT   /usr/share/zabbix;
                fastcgi_param   SCRIPT_FILENAME /usr/share/zabbix$fastcgi_script_name;
                fastcgi_param   PATH_TRANSLATED /usr/share/zabbix$fastcgi_script_name;

                include fastcgi_params;
                fastcgi_param   QUERY_STRING    $query_string;
                fastcgi_param   REQUEST_METHOD  $request_method;
                fastcgi_param   CONTENT_TYPE    $content_type;
                fastcgi_param   CONTENT_LENGTH  $content_length;

                fastcgi_intercept_errors        on;
                fastcgi_ignore_client_abort     off;
                fastcgi_connect_timeout         60;
                fastcgi_send_timeout            180;
                fastcgi_read_timeout            180;
                fastcgi_buffer_size             128k;
                fastcgi_buffers                 4 256k;
                fastcgi_busy_buffers_size       256k;
                fastcgi_temp_file_write_size    256k;
        }
}
# Check nginx configuration
sudo nginx -t

Restart Services
#

# Restart services
sudo systemctl restart zabbix-server zabbix-agent nginx php8.1-fpm

# Enable services autostart (Should be enabled by default on Ubuntu 22.04)
sudo systemctl enable zabbix-server zabbix-agent nginx php8.1-fpm

MySQL & Nginx Version
#

The following tutorial installs Zabbix Server with MySQL and Nginx.

Install Zabbix
#

# Change directory
cd /tmp

# Download .deb package
sudo wget https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.4-1+ubuntu22.04_all.deb

# Install .deb package
sudo dpkg -i zabbix-release_6.4-1+ubuntu22.04_all.deb

# Update package index
sudo apt update

# Install Zabbix Server, Zabbix Frontend, Zabbix Agent & dependencies
sudo apt install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent -y

MySQL
#

  • Install MySQL
# Install MySQL server
sudo apt install mysql-server -y

# Connect to server with MySQL client
sudo mysql

# Set root pw
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'definerootpw';

# Exit MySQL client
exit

# Run secure installation script
sudo mysql_secure_installation
  • Create DB & User
# Connect to server with MySQL client: Promts for root user pw
mysql -u root -p

# Create "zabbix" db, define character encoding, define db collation
create database zabbix character set utf8mb4 collate utf8mb4_bin;

# Create "zabbix" user and define pw
create user zabbix@localhost identified by 'password';

# Grants all privileges for "zabbix" db to "zabbix" user
grant all privileges on zabbix.* to zabbix@localhost;

# Allow the creation of stored functions and triggers without requiring the SUPER privilege
set global log_bin_trust_function_creators = 1;

# Exit MySQL client
quit; 

Note: utf8mb4 character encoding supports more unicode characters than the standard utf8 encoding, including emojis. utf8mb4_bin is a very strict binary collation, which means it compares text data based on the binary values of the characters.

  • Import Data
# Import initial schema and data: Prompts for "zabbix" user pw
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix 

Note: The zcat command reads compressed (gzip )files and outputs their contents.

# Connect to server with MySQL client: Promts for root user pw
mysql -u root -p

# Disable log_bin_trust_function_creators option
set global log_bin_trust_function_creators = 0;

# Exit MySQL client
exit

Zabbix Configuration
#

# Open Zabbix main configuration
sudo vi /etc/zabbix/zabbix_server.conf
  • Define MySQL PW
# Define the PostgreSQL pw for the zabbix user
DBPassword=password
  • Default log settings
# Default Zabbix log
/var/log/zabbix/zabbix_server.log

# Disable log rotation
LogFileSize=0

Nginx
#

sudo vi /etc/zabbix/nginx.conf
server {
        listen          443 ssl;
        server_name     zabbix.jklug.work;

        ssl_certificate         /etc/letsencrypt/live/zabbix.jklug.work/fullchain.pem;
        ssl_certificate_key     /etc/letsencrypt/live/zabbix.jklug.work/privkey.pem;


        root    /usr/share/zabbix;

        index   index.php;

        location = /favicon.ico {
                log_not_found   off;
        }

        location / {
                try_files       $uri $uri/ =404;
        }

        location /assets {
                access_log      off;
                expires         10d;
        }

        location ~ /\.ht {
                deny            all;
        }

        location ~ /(api\/|conf[^\.]|include|locale) {
                deny            all;
                return          404;
        }

        location /vendor {
                deny            all;
                return          404;
        }

        location ~ [^/]\.php(/|$) {
                fastcgi_pass    unix:/var/run/php/zabbix.sock;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index   index.php;

                fastcgi_param   DOCUMENT_ROOT   /usr/share/zabbix;
                fastcgi_param   SCRIPT_FILENAME /usr/share/zabbix$fastcgi_script_name;
                fastcgi_param   PATH_TRANSLATED /usr/share/zabbix$fastcgi_script_name;

                include fastcgi_params;
                fastcgi_param   QUERY_STRING    $query_string;
                fastcgi_param   REQUEST_METHOD  $request_method;
                fastcgi_param   CONTENT_TYPE    $content_type;
                fastcgi_param   CONTENT_LENGTH  $content_length;

                fastcgi_intercept_errors        on;
                fastcgi_ignore_client_abort     off;
                fastcgi_connect_timeout         60;
                fastcgi_send_timeout            180;
                fastcgi_read_timeout            180;
                fastcgi_buffer_size             128k;
                fastcgi_buffers                 4 256k;
                fastcgi_busy_buffers_size       256k;
                fastcgi_temp_file_write_size    256k;
        }
}
# Check nginx configuration
sudo nginx -t

Restart Services
#

# Restart services
sudo systemctl restart zabbix-server zabbix-agent nginx php8.1-fpm

# Enable services autostart (Should be enabled by default on Ubuntu 22.04)
sudo systemctl enable zabbix-server zabbix-agent nginx php8.1-fpm

Zabbix Frontend
#

Setup
#

# Open Zabbix webinterface
zabbix.jklug.work
  • Define PW for “zabbix” SQL user
# Path to Zabbix GUI configuration file
/etc/zabbix/web/zabbix.conf.php

# Path to Zabbix GUI configuration file: Symlink
/usr/share/zabbix/conf/zabbix.conf.php

Login
#

# Default user:
Admin

# Default password:
zabbix

Zabbix Agent
#

Zabbix Agent: Written in C and is a mature and stable choice, supports basic monitoring capabilities.

Zabbix Agent 2: Written in GO, offers more advanced features and capabilities. Has a plugin-based architecture.

Zabbix Agent 2 Setup
#

# Change directory
cd /tmp

# Download .deb package
sudo wget https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.4-1+ubuntu22.04_all.deb

# Install .deb package
sudo dpkg -i zabbix-release_6.4-1+ubuntu22.04_all.deb

# Update package index
sudo apt update

# Install Zabbix Agent 2
sudo apt install zabbix-agent2 zabbix-agent2-plugin-*

Zabbix Agent 2 Configuration
#

# Open configuration file
sudo vi /etc/zabbix/zabbix_agent2.conf
  • Connection to Zabbix server
# Define IP of the Zabbix server
Server=192.168.30.90
ServerActive=192.168.30.90

# Define host name: Must match the hostname used to add the server to the Zabbix frontend
Hostname=UbuntuServer1
  • Default log settings
# Default Zabbix log
LogFile=/var/log/zabbix/zabbix_agent2.log

# Disable log rotation
LogFileSize=0

Restart Agent
#

# Restart Zabbix Agent 2
sudo systemctl restart zabbix-agent2

# Check status
sudo systemctl status zabbix-agent2

Links #

# Official Zabbix Documentation
https://www.zabbix.com/download?zabbix=6.4&os_distribution=ubuntu&os_version=22.04&components=server_frontend_agent&db=pgsql&ws=nginx