zabbix-docker/web-nginx-mysql/alpine/docker-entrypoint.sh

231 lines
7.4 KiB
Bash
Raw Normal View History

2016-08-03 10:09:49 +02:00
#!/bin/bash
2020-04-25 21:01:11 +02:00
set -o pipefail
2018-02-18 21:42:44 +01:00
2016-08-03 10:09:49 +02:00
set +e
2016-08-04 07:09:11 +02:00
# Script trace mode
2016-08-04 07:09:11 +02:00
if [ "${DEBUG_MODE}" == "true" ]; then
set -o xtrace
fi
2016-08-03 10:09:49 +02:00
# Default Zabbix installation name
# Used only by Zabbix web-interface
ZBX_SERVER_NAME=${ZBX_SERVER_NAME:-"Zabbix docker"}
# Default Zabbix server host
ZBX_SERVER_HOST=${ZBX_SERVER_HOST:-"zabbix-server"}
# Default Zabbix server port number
ZBX_SERVER_PORT=${ZBX_SERVER_PORT:-"10051"}
# Default timezone for web interface
PHP_TZ=${PHP_TZ:-"Europe/Riga"}
2016-08-03 10:09:49 +02:00
# Default directories
# User 'zabbix' home directory
2016-08-03 10:09:49 +02:00
ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
# Configuration files directory
2016-08-03 10:09:49 +02:00
ZABBIX_ETC_DIR="/etc/zabbix"
# Web interface www-root directory
2016-08-03 10:09:49 +02:00
ZBX_FRONTEND_PATH="/usr/share/zabbix"
2019-07-13 23:50:22 +02:00
# usage: file_env VAR [DEFAULT]
# as example: file_env 'MYSQL_PASSWORD' 'zabbix'
# (will allow for "$MYSQL_PASSWORD_FILE" to fill in the value of "$MYSQL_PASSWORD" from a file)
# unsets the VAR_FILE afterwards and just leaving VAR
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local defaultValue="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo "**** Both variables $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$defaultValue"
if [ "${!var:-}" ]; then
val="${!var}"
echo "** Using ${var} variable from ENV"
elif [ "${!fileVar:-}" ]; then
if [ ! -f "${!fileVar}" ]; then
echo "**** Secret file \"${!fileVar}\" is not found"
exit 1
fi
val="$(< "${!fileVar}")"
echo "** Using ${var} variable from secret file"
fi
export "$var"="$val"
unset "$fileVar"
}
# Check prerequisites for MySQL database
2020-04-25 21:34:09 +02:00
check_variables() {
2020-04-25 22:24:26 +02:00
: ${DB_SERVER_HOST:="mysql-server"}
: ${DB_SERVER_PORT:="3306"}
2016-08-03 10:09:49 +02:00
USE_DB_ROOT_USER=false
CREATE_ZBX_DB_USER=false
2019-07-13 23:50:22 +02:00
file_env MYSQL_USER
file_env MYSQL_PASSWORD
2016-08-03 10:09:49 +02:00
if [ ! -n "${MYSQL_USER}" ] && [ "${MYSQL_RANDOM_ROOT_PASSWORD}" == "true" ]; then
echo "**** Impossible to use MySQL server because of unknown Zabbix user and random 'root' password"
exit 1
fi
if [ ! -n "${MYSQL_USER}" ] && [ ! -n "${MYSQL_ROOT_PASSWORD}" ] && [ "${MYSQL_ALLOW_EMPTY_PASSWORD}" != "true" ]; then
2018-02-18 21:42:44 +01:00
echo "*** Impossible to use MySQL server because 'root' password is not defined and it is not empty"
2016-08-03 10:09:49 +02:00
exit 1
fi
if [ "${MYSQL_ALLOW_EMPTY_PASSWORD}" == "true" ] || [ -n "${MYSQL_ROOT_PASSWORD}" ]; then
USE_DB_ROOT_USER=true
DB_SERVER_ROOT_USER="root"
DB_SERVER_ROOT_PASS=${MYSQL_ROOT_PASSWORD:-""}
fi
2017-10-27 17:53:31 +02:00
[ -n "${MYSQL_USER}" ] && CREATE_ZBX_DB_USER=true
2016-08-03 10:09:49 +02:00
# If root password is not specified use provided credentials
2020-04-25 22:24:26 +02:00
: ${DB_SERVER_ROOT_USER:=${MYSQL_USER}}....
2016-08-03 14:23:39 +02:00
[ "${MYSQL_ALLOW_EMPTY_PASSWORD}" == "true" ] || DB_SERVER_ROOT_PASS=${DB_SERVER_ROOT_PASS:-${MYSQL_PASSWORD}}
2016-08-03 10:09:49 +02:00
DB_SERVER_ZBX_USER=${MYSQL_USER:-"zabbix"}
DB_SERVER_ZBX_PASS=${MYSQL_PASSWORD:-"zabbix"}
2020-04-25 22:24:26 +02:00
DB_SERVER_DBNAME=${MYSQL_DATABASE:-"zabbix"}
2016-08-03 10:09:49 +02:00
}
2020-04-25 21:01:11 +02:00
check_db_connect() {
2016-08-03 10:09:49 +02:00
echo "********************"
echo "* DB_SERVER_HOST: ${DB_SERVER_HOST}"
echo "* DB_SERVER_PORT: ${DB_SERVER_PORT}"
echo "* DB_SERVER_DBNAME: ${DB_SERVER_DBNAME}"
2019-10-31 23:17:26 +01:00
if [ "${DEBUG_MODE}" == "true" ]; then
if [ "${USE_DB_ROOT_USER}" == "true" ]; then
echo "* DB_SERVER_ROOT_USER: ${DB_SERVER_ROOT_USER}"
echo "* DB_SERVER_ROOT_PASS: ${DB_SERVER_ROOT_PASS}"
fi
echo "* DB_SERVER_ZBX_USER: ${DB_SERVER_ZBX_USER}"
echo "* DB_SERVER_ZBX_PASS: ${DB_SERVER_ZBX_PASS}"
echo "********************"
2016-08-03 10:09:49 +02:00
fi
echo "********************"
WAIT_TIMEOUT=5
2020-09-08 21:40:14 +02:00
export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"
2016-08-03 10:09:49 +02:00
while [ ! "$(mysqladmin ping -h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} -u ${DB_SERVER_ROOT_USER} \
2020-09-08 21:40:14 +02:00
--silent --connect_timeout=10)" ]; do
2016-08-03 10:09:49 +02:00
echo "**** MySQL server is not available. Waiting $WAIT_TIMEOUT seconds..."
sleep $WAIT_TIMEOUT
done
2020-09-08 21:40:14 +02:00
unset MYSQL_PWD
2016-08-03 10:09:49 +02:00
}
2020-04-25 21:01:11 +02:00
prepare_web_server() {
2016-08-03 10:09:49 +02:00
NGINX_CONFD_DIR="/etc/nginx/conf.d"
NGINX_SSL_CONFIG="/etc/ssl/nginx"
echo "** Adding Zabbix virtual host (HTTP)"
if [ -f "$ZABBIX_ETC_DIR/nginx.conf" ]; then
ln -s "$ZABBIX_ETC_DIR/nginx.conf" "$NGINX_CONFD_DIR"
else
echo "**** Impossible to enable HTTP virtual host"
fi
if [ -f "$NGINX_SSL_CONFIG/ssl.crt" ] && [ -f "$NGINX_SSL_CONFIG/ssl.key" ] && [ -f "$NGINX_SSL_CONFIG/dhparam.pem" ]; then
echo "** Enable SSL support for Nginx"
if [ -f "$ZABBIX_ETC_DIR/nginx_ssl.conf" ]; then
ln -s "$ZABBIX_ETC_DIR/nginx_ssl.conf" "$NGINX_CONFD_DIR"
else
echo "**** Impossible to enable HTTPS virtual host"
fi
else
echo "**** Impossible to enable SSL support for Nginx. Certificates are missed."
fi
}
prepare_zbx_web_config() {
echo "** Preparing Zabbix frontend configuration file"
2020-04-27 22:19:42 +02:00
PHP_CONFIG_FILE="/etc/php7/php-fpm.d/zabbix.conf"
2016-08-03 10:09:49 +02:00
if [ "$(id -u)" == '0' ]; then
echo "user = zabbix" >> "$PHP_CONFIG_FILE"
echo "group = zabbix" >> "$PHP_CONFIG_FILE"
echo "listen.owner = nginx" >> "$PHP_CONFIG_FILE"
echo "listen.group = nginx" >> "$PHP_CONFIG_FILE"
fi
export ZBX_DENY_GUI_ACCESS=${ZBX_DENY_GUI_ACCESS:-"false"}
export ZBX_GUI_ACCESS_IP_RANGE=${ZBX_GUI_ACCESS_IP_RANGE:-"['127.0.0.1']"}
export ZBX_GUI_WARNING_MSG=${ZBX_GUI_WARNING_MSG:-"Zabbix is under maintenance."}
2020-09-10 14:05:17 +02:00
export ZBX_MAXEXECUTIONTIME=${ZBX_MAXEXECUTIONTIME:-"600"}
export ZBX_MEMORYLIMIT=${ZBX_MEMORYLIMIT:-"128M"}
export ZBX_POSTMAXSIZE=${ZBX_POSTMAXSIZE:-"16M"}
export ZBX_UPLOADMAXFILESIZE=${ZBX_UPLOADMAXFILESIZE:-"2M"}
export ZBX_MAXINPUTTIME=${ZBX_MAXINPUTTIME:-"300"}
export PHP_TZ=${PHP_TZ:-"Europe/Riga"}
export DB_SERVER_TYPE="MYSQL"
export DB_SERVER_HOST=${DB_SERVER_HOST}
export DB_SERVER_PORT=${DB_SERVER_PORT}
export DB_SERVER_DBNAME=${DB_SERVER_DBNAME}
export DB_SERVER_SCHEMA=${DB_SERVER_SCHEMA}
export DB_SERVER_USER=${DB_SERVER_ZBX_USER}
export DB_SERVER_PASS=${DB_SERVER_ZBX_PASS}
export ZBX_SERVER_HOST=${ZBX_SERVER_HOST}
export ZBX_SERVER_PORT=${ZBX_SERVER_PORT:-"10051"}
export ZBX_SERVER_NAME=${ZBX_SERVER_NAME}
2020-10-14 14:49:48 +02:00
FCGI_READ_TIMEOUT=$(expr ${ZBX_MAXEXECUTIONTIME} + 1)
sed -i \
-e "s/{FCGI_READ_TIMEOUT}/${FCGI_READ_TIMEOUT}/g" \
"$ZABBIX_ETC_DIR/nginx.conf"
if [ -f "$ZABBIX_ETC_DIR/nginx_ssl.conf" ]; then
sed -i \
-e "s/{FCGI_READ_TIMEOUT}/${FCGI_READ_TIMEOUT}/g" \
"$ZABBIX_ETC_DIR/nginx_ssl.conf"
fi
if [ "${ENABLE_WEB_ACCESS_LOG:-"true"}" == "false" ]; then
sed -ri \
-e 's!^(\s*access_log).+\;!\1 off\;!g' \
"/etc/nginx/nginx.conf"
sed -ri \
-e 's!^(\s*access_log).+\;!\1 off\;!g' \
"/etc/zabbix/nginx.conf"
sed -ri \
-e 's!^(\s*access_log).+\;!\1 off\;!g' \
"/etc/zabbix/nginx_ssl.conf"
fi
2016-08-03 10:09:49 +02:00
}
#################################################
2020-04-25 19:31:44 +02:00
echo "** Deploying Zabbix web-interface (Nginx) with MySQL database"
2016-08-03 10:09:49 +02:00
2020-05-13 23:12:56 +02:00
check_variables
check_db_connect
prepare_web_server
prepare_zbx_web_config
2016-08-03 10:09:49 +02:00
echo "########################################################"
if [ "$1" != "" ]; then
echo "** Executing '$@'"
exec "$@"
elif [ -f "/usr/bin/supervisord" ]; then
echo "** Executing supervisord"
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
else
echo "Unknown instructions. Exiting..."
exit 1
fi
2016-08-03 10:09:49 +02:00
#################################################