2020-07-14 21:35:15 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-07-14 21:45:09 +02:00
|
|
|
set -o pipefail
|
2020-07-14 21:35:15 +02:00
|
|
|
|
|
|
|
set +e
|
|
|
|
|
|
|
|
# Script trace mode
|
2021-04-13 12:07:14 +02:00
|
|
|
if [ "${DEBUG_MODE,,}" == "true" ]; then
|
2020-07-14 21:35:15 +02:00
|
|
|
set -o xtrace
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Default Zabbix installation name
|
|
|
|
# Used only by Zabbix web-interface
|
2021-02-18 19:22:41 +01:00
|
|
|
: ${ZBX_SERVER_NAME:="Zabbix docker"}
|
2020-07-14 21:35:15 +02:00
|
|
|
# Default Zabbix server host
|
2021-02-18 19:22:41 +01:00
|
|
|
: ${ZBX_SERVER_HOST:="zabbix-server"}
|
2020-07-14 21:35:15 +02:00
|
|
|
# Default Zabbix server port number
|
2021-02-18 19:22:41 +01:00
|
|
|
: ${ZBX_SERVER_PORT:="10051"}
|
2020-07-14 21:35:15 +02:00
|
|
|
|
|
|
|
# Default timezone for web interface
|
2021-02-18 19:22:41 +01:00
|
|
|
: ${PHP_TZ:="Europe/Riga"}
|
2020-07-14 21:35:15 +02:00
|
|
|
|
|
|
|
# Default directories
|
|
|
|
# Configuration files directory
|
|
|
|
ZABBIX_ETC_DIR="/etc/zabbix"
|
|
|
|
# Web interface www-root directory
|
2021-02-18 19:22:41 +01:00
|
|
|
ZABBIX_WWW_ROOT="/usr/share/zabbix"
|
2020-07-14 21:35:15 +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-07-14 21:45:09 +02:00
|
|
|
check_variables() {
|
2023-07-14 07:15:26 +02:00
|
|
|
if [ ! -n "${DB_SERVER_SOCKET}" ]; then
|
|
|
|
: ${DB_SERVER_HOST:="mysql-server"}
|
|
|
|
else
|
|
|
|
DB_SERVER_HOST="localhost"
|
|
|
|
fi
|
2020-07-14 21:35:15 +02:00
|
|
|
: ${DB_SERVER_PORT:="3306"}
|
2022-02-26 20:16:20 +01:00
|
|
|
|
2020-07-14 21:35:15 +02:00
|
|
|
file_env MYSQL_USER
|
|
|
|
file_env MYSQL_PASSWORD
|
|
|
|
|
|
|
|
DB_SERVER_ZBX_USER=${MYSQL_USER:-"zabbix"}
|
|
|
|
DB_SERVER_ZBX_PASS=${MYSQL_PASSWORD:-"zabbix"}
|
|
|
|
|
|
|
|
DB_SERVER_DBNAME=${MYSQL_DATABASE:-"zabbix"}
|
2023-05-02 08:33:33 +02:00
|
|
|
|
|
|
|
if [ ! -n "${DB_SERVER_SOCKET}" ]; then
|
|
|
|
mysql_connect_args="-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT}"
|
|
|
|
else
|
|
|
|
mysql_connect_args="-S ${DB_SERVER_SOCKET}"
|
|
|
|
fi
|
2020-07-14 21:35:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 18:57:19 +02:00
|
|
|
db_tls_params() {
|
|
|
|
local result=""
|
|
|
|
|
2021-04-13 12:07:14 +02:00
|
|
|
if [ "${ZBX_DB_ENCRYPTION,,}" == "true" ]; then
|
2020-09-08 23:24:29 +02:00
|
|
|
result="--ssl-mode=required"
|
2020-08-27 18:57:19 +02:00
|
|
|
|
|
|
|
if [ -n "${ZBX_DB_CA_FILE}" ]; then
|
|
|
|
result="${result} --ssl-ca=${ZBX_DB_CA_FILE}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${ZBX_DB_KEY_FILE}" ]; then
|
|
|
|
result="${result} --ssl-key=${ZBX_DB_KEY_FILE}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${ZBX_DB_CERT_FILE}" ]; then
|
|
|
|
result="${result} --ssl-cert=${ZBX_DB_CERT_FILE}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $result
|
|
|
|
}
|
|
|
|
|
2020-07-14 21:35:15 +02:00
|
|
|
check_db_connect() {
|
|
|
|
echo "********************"
|
|
|
|
echo "* DB_SERVER_HOST: ${DB_SERVER_HOST}"
|
|
|
|
echo "* DB_SERVER_PORT: ${DB_SERVER_PORT}"
|
2023-05-02 08:33:33 +02:00
|
|
|
if [ -n "${DB_SERVER_SOCKET}" ]; then
|
|
|
|
echo "* DB_SERVER_SOCKET: ${DB_SERVER_SOCKET}"
|
|
|
|
fi
|
2020-07-14 21:35:15 +02:00
|
|
|
echo "* DB_SERVER_DBNAME: ${DB_SERVER_DBNAME}"
|
2021-04-13 12:07:14 +02:00
|
|
|
if [ "${DEBUG_MODE,,}" == "true" ]; then
|
2020-07-14 21:35:15 +02:00
|
|
|
echo "* DB_SERVER_ZBX_USER: ${DB_SERVER_ZBX_USER}"
|
|
|
|
echo "* DB_SERVER_ZBX_PASS: ${DB_SERVER_ZBX_PASS}"
|
|
|
|
fi
|
|
|
|
echo "********************"
|
2020-07-14 21:45:09 +02:00
|
|
|
|
2020-07-14 21:35:15 +02:00
|
|
|
WAIT_TIMEOUT=5
|
2020-07-14 21:45:09 +02:00
|
|
|
|
2020-08-27 18:57:19 +02:00
|
|
|
ssl_opts="$(db_tls_params)"
|
2020-07-14 21:45:09 +02:00
|
|
|
|
2022-02-26 20:16:20 +01:00
|
|
|
export MYSQL_PWD="${DB_SERVER_ZBX_PASS}"
|
2020-09-08 20:46:03 +02:00
|
|
|
|
2023-05-02 08:33:33 +02:00
|
|
|
while [ ! "$(mysqladmin ping $mysql_connect_args -u ${DB_SERVER_ZBX_USER} \
|
2020-09-08 20:46:03 +02:00
|
|
|
--silent --connect_timeout=10 $ssl_opts)" ]; do
|
2020-07-14 21:35:15 +02:00
|
|
|
echo "**** MySQL server is not available. Waiting $WAIT_TIMEOUT seconds..."
|
|
|
|
sleep $WAIT_TIMEOUT
|
|
|
|
done
|
2020-09-08 20:46:03 +02:00
|
|
|
|
|
|
|
unset MYSQL_PWD
|
2020-07-14 21:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
prepare_web_server() {
|
|
|
|
NGINX_CONFD_DIR="/etc/nginx/conf.d"
|
|
|
|
NGINX_SSL_CONFIG="/etc/ssl/nginx"
|
|
|
|
|
2022-07-20 20:53:46 +02:00
|
|
|
if [ ! -f "/proc/net/if_inet6" ]; then
|
|
|
|
sed -i '/listen \[::\]/d' "$ZABBIX_ETC_DIR/nginx.conf"
|
|
|
|
sed -i '/listen \[::\]/d' "$ZABBIX_ETC_DIR/nginx_ssl.conf"
|
|
|
|
fi
|
|
|
|
|
2020-07-14 21:35:15 +02:00
|
|
|
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"
|
|
|
|
|
|
|
|
PHP_CONFIG_FILE="/etc/php-fpm.d/zabbix.conf"
|
|
|
|
|
2022-02-26 22:28:25 +01:00
|
|
|
export PHP_FPM_PM=${PHP_FPM_PM:-"dynamic"}
|
|
|
|
export PHP_FPM_PM_MAX_CHILDREN=${PHP_FPM_PM_MAX_CHILDREN:-"50"}
|
|
|
|
export PHP_FPM_PM_START_SERVERS=${PHP_FPM_PM_START_SERVERS:-"5"}
|
|
|
|
export PHP_FPM_PM_MIN_SPARE_SERVERS=${PHP_FPM_PM_MIN_SPARE_SERVERS:-"5"}
|
|
|
|
export PHP_FPM_PM_MAX_SPARE_SERVERS=${PHP_FPM_PM_MAX_SPARE_SERVERS:-"35"}
|
|
|
|
export PHP_FPM_PM_MAX_REQUESTS=${PHP_FPM_PM_MAX_REQUESTS:-"0"}
|
|
|
|
|
2020-07-14 21:45:09 +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
|
|
|
|
|
2021-04-13 14:10:43 +02:00
|
|
|
: ${ZBX_DENY_GUI_ACCESS:="false"}
|
2021-04-13 12:07:14 +02:00
|
|
|
export ZBX_DENY_GUI_ACCESS=${ZBX_DENY_GUI_ACCESS,,}
|
2020-09-11 16:07:55 +02:00
|
|
|
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-11 08:49:29 +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"}
|
2021-04-13 14:10:43 +02:00
|
|
|
export PHP_TZ=${PHP_TZ}
|
2020-09-11 08:49:29 +02:00
|
|
|
|
|
|
|
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}
|
2021-11-17 01:45:24 +01:00
|
|
|
export ZBX_SERVER_PORT=${ZBX_SERVER_PORT}
|
2020-09-11 08:49:29 +02:00
|
|
|
export ZBX_SERVER_NAME=${ZBX_SERVER_NAME}
|
|
|
|
|
2021-04-13 14:10:43 +02:00
|
|
|
: ${ZBX_DB_ENCRYPTION:="false"}
|
2021-04-13 12:07:14 +02:00
|
|
|
export ZBX_DB_ENCRYPTION=${ZBX_DB_ENCRYPTION,,}
|
2020-09-11 08:49:29 +02:00
|
|
|
export ZBX_DB_KEY_FILE=${ZBX_DB_KEY_FILE}
|
|
|
|
export ZBX_DB_CERT_FILE=${ZBX_DB_CERT_FILE}
|
|
|
|
export ZBX_DB_CA_FILE=${ZBX_DB_CA_FILE}
|
2021-04-13 14:10:43 +02:00
|
|
|
: ${ZBX_DB_VERIFY_HOST:="false"}
|
2021-04-13 12:07:14 +02:00
|
|
|
export ZBX_DB_VERIFY_HOST=${ZBX_DB_VERIFY_HOST,,}
|
2020-09-11 08:49:29 +02:00
|
|
|
|
2021-04-13 14:10:43 +02:00
|
|
|
: ${DB_DOUBLE_IEEE754:="true"}
|
2021-04-13 12:07:14 +02:00
|
|
|
export DB_DOUBLE_IEEE754=${DB_DOUBLE_IEEE754,,}
|
2020-09-11 08:49:29 +02:00
|
|
|
|
|
|
|
export ZBX_HISTORYSTORAGEURL=${ZBX_HISTORYSTORAGEURL}
|
|
|
|
export ZBX_HISTORYSTORAGETYPES=${ZBX_HISTORYSTORAGETYPES:-"[]"}
|
|
|
|
|
|
|
|
export ZBX_SSO_SETTINGS=${ZBX_SSO_SETTINGS:-""}
|
2020-07-14 21:35:15 +02:00
|
|
|
|
2020-07-14 21:45:09 +02:00
|
|
|
if [ -n "${ZBX_SESSION_NAME}" ]; then
|
2021-02-18 19:22:41 +01:00
|
|
|
cp "$ZABBIX_WWW_ROOT/include/defines.inc.php" "/tmp/defines.inc.php_tmp"
|
|
|
|
sed "/ZBX_SESSION_NAME/s/'[^']*'/'${ZBX_SESSION_NAME}'/2" "/tmp/defines.inc.php_tmp" > "$ZABBIX_WWW_ROOT/include/defines.inc.php"
|
2020-07-14 21:45:09 +02:00
|
|
|
rm -f "/tmp/defines.inc.php_tmp"
|
|
|
|
fi
|
2020-07-14 21:35:15 +02:00
|
|
|
|
2020-10-14 15:06:02 +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
|
|
|
|
|
2021-04-13 14:10:43 +02:00
|
|
|
: ${ENABLE_WEB_ACCESS_LOG:="true"}
|
2021-04-13 12:07:14 +02:00
|
|
|
|
|
|
|
if [ "${ENABLE_WEB_ACCESS_LOG,,}" == "false" ]; then
|
2020-07-14 21:45:09 +02:00
|
|
|
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
|
2020-07-14 21:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#################################################
|
|
|
|
|
|
|
|
echo "** Deploying Zabbix web-interface (Nginx) with MySQL database"
|
|
|
|
|
2020-07-14 21:45:09 +02:00
|
|
|
check_variables
|
|
|
|
check_db_connect
|
|
|
|
prepare_web_server
|
|
|
|
prepare_zbx_web_config
|
2020-07-14 21:35:15 +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
|
|
|
|
|
|
|
|
#################################################
|