2016-08-03 10:09:49 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-04-28 18:39:39 +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
|
|
|
|
2016-08-11 12:55:45 +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
|
2020-04-28 18:39:39 +02:00
|
|
|
: ${ZBX_SERVER_NAME:="Zabbix docker"}
|
2016-08-03 10:09:49 +02:00
|
|
|
# Default Zabbix server host
|
2020-04-28 18:39:39 +02:00
|
|
|
: ${ZBX_SERVER_HOST:="zabbix-server"}
|
2016-08-03 10:09:49 +02:00
|
|
|
# Default Zabbix server port number
|
2020-04-28 18:39:39 +02:00
|
|
|
: ${ZBX_SERVER_PORT:="10051"}
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2016-08-11 12:55:45 +02:00
|
|
|
# Default timezone for web interface
|
2020-04-28 18:39:39 +02:00
|
|
|
: ${PHP_TZ:="Europe/Riga"}
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
# Default directories
|
2016-08-11 12:55:45 +02:00
|
|
|
# Configuration files directory
|
2016-08-03 10:09:49 +02:00
|
|
|
ZABBIX_ETC_DIR="/etc/zabbix"
|
2016-08-11 12:55:45 +02:00
|
|
|
# Web interface www-root directory
|
2020-04-28 18:39:39 +02:00
|
|
|
ZABBIX_WWW_ROOT="/usr/share/zabbix"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:09:49 +02:00
|
|
|
prepare_system() {
|
|
|
|
echo "** Preparing the system"
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:43:00 +01:00
|
|
|
escape_spec_char() {
|
|
|
|
local var_value=$1
|
|
|
|
|
|
|
|
var_value="${var_value//\\/\\\\}"
|
|
|
|
var_value="${var_value//[$'\n']/}"
|
|
|
|
var_value="${var_value//\//\\/}"
|
|
|
|
var_value="${var_value//./\\.}"
|
|
|
|
var_value="${var_value//\*/\\*}"
|
|
|
|
var_value="${var_value//^/\\^}"
|
|
|
|
var_value="${var_value//\$/\\\$}"
|
|
|
|
var_value="${var_value//\&/\\\&}"
|
|
|
|
var_value="${var_value//\[/\\[}"
|
2018-06-01 05:06:05 +02:00
|
|
|
var_value="${var_value//\]/\\]}"
|
2018-02-23 22:43:00 +01:00
|
|
|
|
2019-07-13 23:50:22 +02:00
|
|
|
echo "$var_value"
|
2018-02-23 22:43:00 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:09:49 +02:00
|
|
|
update_config_var() {
|
|
|
|
local config_path=$1
|
|
|
|
local var_name=$2
|
|
|
|
local var_value=$3
|
|
|
|
local is_multiple=$4
|
|
|
|
|
|
|
|
if [ ! -f "$config_path" ]; then
|
|
|
|
echo "**** Configuration file '$config_path' does not exist"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -n "** Updating '$config_path' parameter \"$var_name\": '$var_value'... "
|
|
|
|
|
|
|
|
# Remove configuration parameter definition in case of unset parameter value
|
|
|
|
if [ -z "$var_value" ]; then
|
2018-02-20 17:23:18 +01:00
|
|
|
sed -i -e "/^$var_name=/d" "$config_path"
|
2016-08-03 10:09:49 +02:00
|
|
|
echo "removed"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove value from configuration parameter in case of double quoted parameter value
|
|
|
|
if [ "$var_value" == '""' ]; then
|
|
|
|
sed -i -e "/^$var_name=/s/=.*/=/" "$config_path"
|
|
|
|
echo "undefined"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2020-04-27 22:19:42 +02:00
|
|
|
# Escaping characters in parameter value and name
|
2018-02-23 22:43:00 +01:00
|
|
|
var_value=$(escape_spec_char "$var_value")
|
2020-04-27 22:19:42 +02:00
|
|
|
var_name=$(escape_spec_char "$var_name")
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
if [ "$(grep -E "^$var_name=" $config_path)" ] && [ "$is_multiple" != "true" ]; then
|
|
|
|
sed -i -e "/^$var_name=/s/=.*/=$var_value/" "$config_path"
|
|
|
|
echo "updated"
|
|
|
|
elif [ "$(grep -Ec "^# $var_name=" $config_path)" -gt 1 ]; then
|
|
|
|
sed -i -e "/^[#;] $var_name=$/i\\$var_name=$var_value" "$config_path"
|
|
|
|
echo "added first occurrence"
|
|
|
|
else
|
|
|
|
sed -i -e "/^[#;] $var_name=/s/.*/&\n$var_name=$var_value/" "$config_path"
|
|
|
|
echo "added"
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:55:45 +02:00
|
|
|
# Check prerequisites for MySQL database
|
2020-04-28 18:39:39 +02:00
|
|
|
check_variables() {
|
|
|
|
: ${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-28 18:39:39 +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-28 18:39:39 +02:00
|
|
|
DB_SERVER_DBNAME=${MYSQL_DATABASE:-"zabbix"}
|
2016-08-03 10:09:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 18:39:39 +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}"
|
2016-08-03 10:09:49 +02:00
|
|
|
fi
|
|
|
|
echo "********************"
|
|
|
|
|
|
|
|
WAIT_TIMEOUT=5
|
|
|
|
|
|
|
|
while [ ! "$(mysqladmin ping -h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} -u ${DB_SERVER_ROOT_USER} \
|
|
|
|
--password="${DB_SERVER_ROOT_PASS}" --silent --connect_timeout=10)" ]; do
|
|
|
|
echo "**** MySQL server is not available. Waiting $WAIT_TIMEOUT seconds..."
|
|
|
|
sleep $WAIT_TIMEOUT
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
prepare_web_server() {
|
2020-04-28 20:22:44 +02:00
|
|
|
APACHE_SITES_DIR="/etc/apache2/sites-enabled"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
echo "** Adding Zabbix virtual host (HTTP)"
|
|
|
|
if [ -f "$ZABBIX_ETC_DIR/apache.conf" ]; then
|
|
|
|
ln -s "$ZABBIX_ETC_DIR/apache.conf" "$APACHE_SITES_DIR/zabbix.conf"
|
|
|
|
else
|
|
|
|
echo "**** Impossible to enable HTTP virtual host"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f "/etc/ssl/apache2/ssl.crt" ] && [ -f "/etc/ssl/apache2/ssl.key" ]; then
|
|
|
|
echo "** Adding Zabbix virtual host (HTTPS)"
|
|
|
|
if [ -f "$ZABBIX_ETC_DIR/apache_ssl.conf" ]; then
|
|
|
|
ln -s "$ZABBIX_ETC_DIR/apache_ssl.conf" "$APACHE_SITES_DIR/zabbix_ssl.conf"
|
|
|
|
else
|
|
|
|
echo "**** Impossible to enable HTTPS virtual host"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "**** Impossible to enable SSL support for Apache2. Certificates are missed."
|
|
|
|
fi
|
2018-03-29 15:28:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:09:49 +02:00
|
|
|
clear_deploy() {
|
|
|
|
echo "** Cleaning the system"
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_zbx_web_config() {
|
|
|
|
local server_name=""
|
|
|
|
|
|
|
|
echo "** Preparing Zabbix frontend configuration file"
|
|
|
|
|
|
|
|
ZBX_WEB_CONFIG="$ZABBIX_ETC_DIR/web/zabbix.conf.php"
|
2020-04-28 18:39:39 +02:00
|
|
|
PHP_CONFIG_FILE="/etc/php/7.2/apache2/conf.d/99-zabbix.ini"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
update_config_var "$PHP_CONFIG_FILE" "max_execution_time" "${ZBX_MAXEXECUTIONTIME:-"600"}"
|
|
|
|
update_config_var "$PHP_CONFIG_FILE" "memory_limit" "${ZBX_MEMORYLIMIT:-"128M"}"
|
|
|
|
update_config_var "$PHP_CONFIG_FILE" "post_max_size" "${ZBX_POSTMAXSIZE:-"16M"}"
|
|
|
|
update_config_var "$PHP_CONFIG_FILE" "upload_max_filesize" "${ZBX_UPLOADMAXFILESIZE:-"2M"}"
|
|
|
|
update_config_var "$PHP_CONFIG_FILE" "max_input_time" "${ZBX_MAXINPUTTIME:-"300"}"
|
|
|
|
update_config_var "$PHP_CONFIG_FILE" "date.timezone" "${PHP_TZ}"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2018-02-23 22:43:00 +01:00
|
|
|
# Escaping characters in parameter value
|
|
|
|
server_name=$(escape_spec_char "${ZBX_SERVER_NAME}")
|
|
|
|
server_user=$(escape_spec_char "${DB_SERVER_ZBX_USER}")
|
|
|
|
server_pass=$(escape_spec_char "${DB_SERVER_ZBX_PASS}")
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
sed -i \
|
|
|
|
-e "s/{DB_SERVER_HOST}/${DB_SERVER_HOST}/g" \
|
|
|
|
-e "s/{DB_SERVER_PORT}/${DB_SERVER_PORT}/g" \
|
|
|
|
-e "s/{DB_SERVER_DBNAME}/${DB_SERVER_DBNAME}/g" \
|
2018-06-28 08:48:31 +02:00
|
|
|
-e "s/{DB_SERVER_SCHEMA}/${DB_SERVER_SCHEMA}/g" \
|
2018-02-23 22:43:00 +01:00
|
|
|
-e "s/{DB_SERVER_USER}/$server_user/g" \
|
|
|
|
-e "s/{DB_SERVER_PASS}/$server_pass/g" \
|
2016-08-03 10:09:49 +02:00
|
|
|
-e "s/{ZBX_SERVER_HOST}/${ZBX_SERVER_HOST}/g" \
|
|
|
|
-e "s/{ZBX_SERVER_PORT}/${ZBX_SERVER_PORT}/g" \
|
|
|
|
-e "s/{ZBX_SERVER_NAME}/$server_name/g" \
|
|
|
|
"$ZBX_WEB_CONFIG"
|
|
|
|
}
|
2016-08-11 12:55:45 +02:00
|
|
|
|
2016-08-03 10:09:49 +02:00
|
|
|
prepare_web() {
|
|
|
|
echo "** Preparing Zabbix web-interface"
|
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
check_variables
|
|
|
|
check_db_connect
|
|
|
|
prepare_web_server
|
|
|
|
prepare_zbx_web_config
|
2016-08-03 10:09:49 +02:00
|
|
|
}
|
2016-08-11 12:55:45 +02:00
|
|
|
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
#################################################
|
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
echo "** Deploying Zabbix web-interface (Apache) with MySQL database"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
prepare_system
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
prepare_web
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
clear_deploy
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
echo "########################################################"
|
|
|
|
|
2018-02-22 21:07:15 +01:00
|
|
|
if [ "$1" != "" ]; then
|
|
|
|
echo "** Executing '$@'"
|
|
|
|
exec "$@"
|
2020-04-28 18:39:39 +02:00
|
|
|
elif [ -f "/usr/sbin/httpd" ]; then
|
|
|
|
echo "** Executing HTTPD"
|
|
|
|
exec /usr/sbin/httpd -D FOREGROUND
|
2018-02-22 21:07:15 +01:00
|
|
|
else
|
|
|
|
echo "Unknown instructions. Exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
#################################################
|