Skip to content

Instantly share code, notes, and snippets.

@KitsuneAlex
Last active July 26, 2023 22:10
Show Gist options
  • Save KitsuneAlex/a38ebc68deeceb31a9b293227e40ab7f to your computer and use it in GitHub Desktop.
Save KitsuneAlex/a38ebc68deeceb31a9b293227e40ab7f to your computer and use it in GitHub Desktop.
Zabbix Docker Installer
#!/bin/bash
# Simple script for quickly installing and setting up a Zabbix stack.
# This includes database, snmptraps, server and web interface.
# A local agent can be installed optionally.
read -p "Zabbix version (default 'ubuntu-6.4-latest'): " version
if [ -z "${version}" ]; then
echo "Using default Zabbix version"
export ZSTACK_VERSION="ubuntu-6.4-latest"
else
echo "Using Zabbix ${version}"
export ZSTACK_VERSION="${version}"
fi
echo
read -p "Container prefix (default 'zabbix'): " container_prefix
if [ -z "${container_prefix}" ]; then
echo "Using default container prefix"
export ZSTACK_CONTAINER_PREFIX="zabbix"
else
echo "Using container prefix '${container_prefix}'"
export ZSTACK_CONTAINER_PREFIX="${container_prefix}"
fi
echo
read -p "Network name (default 'zabbix'): " network_name
if [ -z "${network_name}" ]; then
echo "Using default network name"
export ZSTACK_NETWORK_NAME="zabbix"
else
echo "Creating network '${network_name}'"
export ZSTACK_NETWORK_NAME="${network_name}"
fi
echo
# Update, upgrade, cleanup before we install packages
echo ">>>>>>>>>>>>>>>>>>>> Updating package sources before starting setup."
sudo apt update -y
sudo apt upgrade -y
sudo apt autoremove -y
sudo apt autoclean -y
echo
# Install Docker, will be skipped if installed
echo ">>>>>>>>>>>>>>>>>>>> Checking for Docker installation"
sudo apt install docker -y
echo
# Create a new internal network for the zabbix stack
echo ">>>>>>>>>>>>>>>>>>>> Creating internal network for containers"
docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 "${ZSTACK_NETWORK_NAME}"
echo
read -p "Database user name (default 'zabbix'): " db_user
if [ -z "${db_username}" ]; then
echo "Using default database user name"
export ZSTACK_DB_USER="zabbix"
else
echo "Creating user $db_username"
export ZSTACK_DB_USER="${db_user}"
fi
echo
read -p "Database name (default 'zabbix'): " db_name
if [ -z "${db_name}" ]; then
echo "Using default database name"
export ZSTACK_DB_NAME="zabbix"
else
echo "Creating database $db_name"
export ZSTACK_DB_NAME="${db_name}"
fi
echo
read -s -p "Database password: " ZSTACK_DB_PASSWORD # Assign directly as it is required
if [ -z "${ZSTACK_DB_PASSWORD}" ]; then
echo "Password must not be empty!"
exit
fi
echo
# Create a PostgreSQL database
echo ">>>>>>>>>>>>>>>>>>>> Creating PostgreSQL database"
docker run --name "${ZSTACK_CONTAINER_PREFIX}-db" --network "${ZSTACK_NETWORK_NAME}" -d \
-e POSTGRES_USER="${ZSTACK_DB_USER}" \
-e POSTGRES_PASSWORD="${ZSTACK_DB_PASSWORD}" \
-e POSTGRES_DB="${ZSTACK_DB_NAME}" \
--restart unless-stopped \
--add-host host.docker.internal:host-gateway \
postgres:latest
echo
read -p "Agent port (default 10051): " agent_port
if [ -z "${agent_port}" ]; then
echo "Using default agent port"
export ZSTACK_AGENT_PORT="10051"
else
echo "Using agent port ${agent_port}"
export ZSTACK_AGENT_PORT="${agent_port}"
fi
echo
read -p "Web port (default 8080): " web_port
if [ -z "${web_port}" ]; then
echo "Using default web port"
export ZSTACK_WEB_PORT="8080"
else
echo "Using web port ${web_port}"
export ZSTACK_WEB_PORT="${web_port}"
fi
echo
read -p "Secure web port (default 8443): " secure_web_port
if [ -z "${secure_web_port}" ]; then
echo "Using default secure web port"
export ZSTACK_SECURE_WEB_PORT="8443"
else
echo "Using secure web port ${secure_web_port}"
export ZSTACK_SECURE_WEB_PORT="${secure_web_port}"
fi
echo
read -p "SNMPT port (default 1162): " snmpt_port
if [ -z "${snmpt_port}" ]; then
echo "Using default SNMPT port"
export ZSTACK_SNMPT_PORT="1162"
else
echo "Using SNMPT port ${snmpt_port}"
export ZSTACK_SNMPT_PORT="${snmpt_port}"
fi
echo
# Create an SNMP traps instance
echo ">>>>>>>>>>>>>>>>>>>> Creating SNMP Traps instance"
docker run --name "${ZSTACK_CONTAINER_PREFIX}-snmptraps" --network "${ZSTACK_NETWORK_NAME}" -d \
-v /zbx_instance/snmptraps:/var/lib/zabbix/snmptraps:rw \
-v /var/lib/zabbix/mibs:/usr/share/snmp/mibs:ro \
-p "162:${ZSTACK_SNMPT_PORT}/udp" \
--restart unless-stopped \
--add-host host.docker.internal:host-gateway \
"zabbix/zabbix-snmptraps:${ZSTACK_VERSION}"
echo
# Create a Zabbix/pgsql server
echo ">>>>>>>>>>>>>>>>>>>> Creating Zabbix server instance"
docker run --name "${ZSTACK_CONTAINER_PREFIX}-server" --network "${ZSTACK_NETWORK_NAME}" -d \
-e DB_SERVER_HOST="${ZSTACK_CONTAINER_PREFIX}-db" \
-e POSTGRES_USER="${ZSTACK_DB_USER}" \
-e POSTGRES_PASSWORD="${ZSTACK_DB_PASSWORD}" \
-e POSTGRES_DB="${ZSTACK_DB_NAME}" \
-e ZBX_ENABLE_SNMP_TRAPS="true" \
-p "10051:${ZSTACK_AGENT_PORT}" \
--volumes-from "${ZSTACK_CONTAINER_PREFIX}-snmptraps" \
--restart unless-stopped \
--add-host host.docker.internal:host-gateway \
"zabbix/zabbix-server-pgsql:${ZSTACK_VERSION}"
echo
# Create a Zabbix Web/pgsql/nginx instance
echo ">>>>>>>>>>>>>>>>>>>> Creating Zabbix Web instance"
docker run --name "${ZSTACK_CONTAINER_PREFIX}-web" --network "${ZSTACK_NETWORK_NAME}" -d \
-e ZBX_SERVER_HOST="${ZSTACK_CONTAINER_PREFIX}-server" \
-e DB_SERVER_HOST="${ZSTACK_CONTAINER_PREFIX}-db" \
-e POSTGRES_USER="${ZSTACK_DB_USER}" \
-e POSTGRES_PASSWORD="${ZSTACK_DB_PASSWORD}" \
-e POSTGRES_DB="${ZSTACK_DB_NAME}" \
-p "8080:${ZSTACK_WEB_PORT}" \
-p "8443:${ZSTACK_SECURE_WEB_PORT}" \
-v /etc/ssl/nginx:/etc/ssl/nginx:ro \
--restart unless-stopped \
--add-host host.docker.internal:host-gateway \
"zabbix/zabbix-web-nginx-pgsql:${ZSTACK_VERSION}"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment