2018-02-18 21:45:33 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-04-26 18:45:42 +02:00
|
|
|
set -o pipefail
|
2018-02-18 21:45:33 +01:00
|
|
|
|
|
|
|
set +e
|
|
|
|
|
|
|
|
# Script trace mode
|
2021-04-12 03:43:41 +02:00
|
|
|
if [ "${DEBUG_MODE,,}" == "true" ]; then
|
2018-02-18 21:45:33 +01:00
|
|
|
set -o xtrace
|
|
|
|
fi
|
|
|
|
|
2024-08-21 13:26:24 +02:00
|
|
|
# Internal directory for TLS related files, used when TLS*File specified as plain text values
|
|
|
|
ZABBIX_INTERNAL_ENC_DIR="${ZABBIX_USER_HOME_DIR}/enc_internal"
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2022-08-27 20:40:18 +02:00
|
|
|
: ${DB_CHARACTER_SET:="utf8mb4"}
|
|
|
|
: ${DB_CHARACTER_COLLATE:="utf8mb4_bin"}
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-18 21:45:33 +01:00
|
|
|
update_config_var() {
|
|
|
|
local config_path=$1
|
|
|
|
local var_name=$2
|
|
|
|
local var_value=$3
|
|
|
|
local is_multiple=$4
|
|
|
|
|
2020-06-23 14:42:13 +02:00
|
|
|
local masklist=("DBPassword TLSPSKIdentity")
|
|
|
|
|
2018-02-18 21:45:33 +01:00
|
|
|
if [ ! -f "$config_path" ]; then
|
|
|
|
echo "**** Configuration file '$config_path' does not exist"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2020-06-23 15:24:22 +02:00
|
|
|
if [[ " ${masklist[@]} " =~ " $var_name " ]] && [ ! -z "$var_value" ]; then
|
2020-06-23 14:42:13 +02:00
|
|
|
echo -n "** Updating '$config_path' parameter \"$var_name\": '****'. Enable DEBUG_MODE to view value ..."
|
|
|
|
else
|
|
|
|
echo -n "** Updating '$config_path' parameter \"$var_name\": '$var_value'..."
|
|
|
|
fi
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2024-12-25 06:41:51 +01:00
|
|
|
# Remove configuration parameter definition in case of unset or empty parameter value
|
2018-02-18 21:45:33 +01:00
|
|
|
if [ -z "$var_value" ]; then
|
2018-02-20 17:23:18 +01:00
|
|
|
sed -i -e "/^$var_name=/d" "$config_path"
|
2018-02-18 21:45:33 +01:00
|
|
|
echo "removed"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2024-12-25 06:41:51 +01:00
|
|
|
# Remove value from configuration parameter in case of set to double quoted parameter value
|
|
|
|
if [[ "$var_value" == '""' ]]; then
|
|
|
|
if [ "$(grep -E "^$var_name=" $config_path)" ]; then
|
|
|
|
sed -i -e "/^$var_name=/s/=.*/=/" "$config_path"
|
|
|
|
else
|
|
|
|
sed -i -e "/^[#;] $var_name=/s/.*/&\n$var_name=/" "$config_path"
|
|
|
|
fi
|
2018-02-18 21:45:33 +01:00
|
|
|
echo "undefined"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Use full path to a file for TLS related configuration parameters
|
2021-11-22 14:32:57 +01:00
|
|
|
if [[ $var_name =~ ^TLS.*File$ ]] && [[ ! $var_value =~ ^/.+$ ]]; then
|
2018-02-18 21:45:33 +01:00
|
|
|
var_value=$ZABBIX_USER_HOME_DIR/enc/$var_value
|
|
|
|
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")
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2024-12-25 06:41:51 +01:00
|
|
|
if [ "$(grep -E "^$var_name=$var_value" $config_path)" ]; then
|
|
|
|
echo "exists"
|
|
|
|
elif [ "$(grep -E "^$var_name=" $config_path)" ] && [ "$is_multiple" != "true" ]; then
|
2018-02-18 21:45:33 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
update_config_multiple_var() {
|
|
|
|
local config_path=$1
|
|
|
|
local var_name=$2
|
|
|
|
local var_value=$3
|
|
|
|
|
|
|
|
var_value="${var_value%\"}"
|
|
|
|
var_value="${var_value#\"}"
|
|
|
|
|
|
|
|
local IFS=,
|
|
|
|
local OPT_LIST=($var_value)
|
|
|
|
|
|
|
|
for value in "${OPT_LIST[@]}"; do
|
|
|
|
update_config_var $config_path $var_name $value true
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-08-21 13:26:24 +02:00
|
|
|
file_process_from_env() {
|
2024-12-23 13:22:01 +01:00
|
|
|
local var_name=$1
|
|
|
|
local file_name=$2
|
|
|
|
local var_value=$3
|
2024-08-21 13:26:24 +02:00
|
|
|
|
|
|
|
if [ ! -z "$var_value" ]; then
|
|
|
|
echo -n "$var_value" > "${ZABBIX_INTERNAL_ENC_DIR}/$var_name"
|
|
|
|
file_name="${ZABBIX_INTERNAL_ENC_DIR}/${var_name}"
|
|
|
|
fi
|
2024-12-23 13:22:01 +01:00
|
|
|
|
|
|
|
if [ -n "$var_value" ]; then
|
|
|
|
export "$var_name"="$file_name"
|
|
|
|
fi
|
|
|
|
# Remove variable with plain text data
|
|
|
|
unset "${var_name%%FILE}"
|
2024-08-21 13:26:24 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 21:45:33 +01:00
|
|
|
# Check prerequisites for MySQL database
|
|
|
|
check_variables_mysql() {
|
2023-05-02 08:33:33 +02:00
|
|
|
if [ ! -n "${DB_SERVER_SOCKET}" ]; then
|
|
|
|
: ${DB_SERVER_HOST:="mysql-server"}
|
|
|
|
: ${DB_SERVER_PORT:="3306"}
|
|
|
|
fi
|
|
|
|
|
2018-02-18 21:45:33 +01: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
|
|
|
|
|
2022-02-26 20:16:20 +01:00
|
|
|
file_env MYSQL_ROOT_USER
|
2020-04-26 18:45:42 +02:00
|
|
|
file_env MYSQL_ROOT_PASSWORD
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2021-04-12 03:43:41 +02:00
|
|
|
if [ ! -n "${MYSQL_USER}" ] && [ "${MYSQL_RANDOM_ROOT_PASSWORD,,}" == "true" ]; then
|
2018-02-18 21:45:33 +01:00
|
|
|
echo "**** Impossible to use MySQL server because of unknown Zabbix user and random 'root' password"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-04-12 03:43:41 +02:00
|
|
|
if [ ! -n "${MYSQL_USER}" ] && [ ! -n "${MYSQL_ROOT_PASSWORD}" ] && [ "${MYSQL_ALLOW_EMPTY_PASSWORD,,}" != "true" ]; then
|
2018-02-18 21:45:33 +01:00
|
|
|
echo "*** Impossible to use MySQL server because 'root' password is not defined and it is not empty"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-04-12 03:43:41 +02:00
|
|
|
if [ "${MYSQL_ALLOW_EMPTY_PASSWORD,,}" == "true" ] || [ -n "${MYSQL_ROOT_PASSWORD}" ]; then
|
2018-02-18 21:45:33 +01:00
|
|
|
USE_DB_ROOT_USER=true
|
2022-02-26 20:16:20 +01:00
|
|
|
DB_SERVER_ROOT_USER=${MYSQL_ROOT_USER:-"root"}
|
2018-02-18 21:45:33 +01:00
|
|
|
DB_SERVER_ROOT_PASS=${MYSQL_ROOT_PASSWORD:-""}
|
|
|
|
fi
|
|
|
|
|
2019-11-05 14:32:23 +01:00
|
|
|
[ -n "${MYSQL_USER}" ] && [ "${USE_DB_ROOT_USER}" == "true" ] && CREATE_ZBX_DB_USER=true
|
2018-02-18 21:45:33 +01:00
|
|
|
|
|
|
|
# If root password is not specified use provided credentials
|
2020-04-26 18:45:42 +02:00
|
|
|
: ${DB_SERVER_ROOT_USER:=${MYSQL_USER}}
|
2021-04-12 03:43:41 +02:00
|
|
|
[ "${MYSQL_ALLOW_EMPTY_PASSWORD,,}" == "true" ] || DB_SERVER_ROOT_PASS=${DB_SERVER_ROOT_PASS:-${MYSQL_PASSWORD}}
|
2024-12-23 13:22:01 +01:00
|
|
|
|
2018-02-18 21:45:33 +01:00
|
|
|
DB_SERVER_ZBX_USER=${MYSQL_USER:-"zabbix"}
|
|
|
|
DB_SERVER_ZBX_PASS=${MYSQL_PASSWORD:-"zabbix"}
|
|
|
|
|
2020-04-26 18:45:42 +02:00
|
|
|
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
|
2018-02-18 21:45:33 +01:00
|
|
|
}
|
|
|
|
|
2020-08-27 18:58:54 +02:00
|
|
|
db_tls_params() {
|
|
|
|
local result=""
|
|
|
|
|
|
|
|
if [ -n "${ZBX_DBTLSCONNECT}" ]; then
|
2020-09-08 23:41:01 +02:00
|
|
|
ssl_mode=${ZBX_DBTLSCONNECT//verify_full/verify_identity}
|
|
|
|
result="--ssl-mode=$ssl_mode"
|
2020-08-27 18:58:54 +02:00
|
|
|
|
|
|
|
if [ -n "${ZBX_DBTLSCAFILE}" ]; then
|
|
|
|
result="${result} --ssl-ca=${ZBX_DBTLSCAFILE}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${ZBX_DBTLSKEYFILE}" ]; then
|
|
|
|
result="${result} --ssl-key=${ZBX_DBTLSKEYFILE}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${ZBX_DBTLSCERTFILE}" ]; then
|
|
|
|
result="${result} --ssl-cert=${ZBX_DBTLSCERTFILE}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $result
|
|
|
|
}
|
|
|
|
|
2018-02-18 21:45:33 +01:00
|
|
|
check_db_connect_mysql() {
|
|
|
|
echo "********************"
|
2023-05-02 08:33:33 +02:00
|
|
|
if [ ! -n "${DB_SERVER_SOCKET}" ]; then
|
|
|
|
echo "* DB_SERVER_HOST: ${DB_SERVER_HOST}"
|
|
|
|
echo "* DB_SERVER_PORT: ${DB_SERVER_PORT}"
|
|
|
|
else
|
|
|
|
echo "* DB_SERVER_SOCKET: ${DB_SERVER_SOCKET}"
|
|
|
|
fi
|
2018-02-18 21:45:33 +01:00
|
|
|
echo "* DB_SERVER_DBNAME: ${DB_SERVER_DBNAME}"
|
2021-04-12 03:43:41 +02:00
|
|
|
if [ "${DEBUG_MODE,,}" == "true" ]; then
|
2019-10-31 23:17:26 +01:00
|
|
|
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}"
|
2018-02-18 21:45:33 +01:00
|
|
|
fi
|
|
|
|
echo "********************"
|
|
|
|
|
|
|
|
WAIT_TIMEOUT=5
|
|
|
|
|
2020-08-27 18:58:54 +02:00
|
|
|
ssl_opts="$(db_tls_params)"
|
2020-04-28 16:21:46 +02:00
|
|
|
|
2020-09-08 21:40:14 +02:00
|
|
|
export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"
|
|
|
|
|
2023-05-02 08:33:33 +02:00
|
|
|
while [ ! "$(mysqladmin ping $mysql_connect_args -u ${DB_SERVER_ROOT_USER} \
|
2020-09-08 23:41:01 +02:00
|
|
|
--silent --connect_timeout=10 $ssl_opts)" ]; do
|
2018-02-18 21:45:33 +01: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
|
2018-02-18 21:45:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mysql_query() {
|
|
|
|
query=$1
|
|
|
|
local result=""
|
|
|
|
|
2020-08-27 18:58:54 +02:00
|
|
|
ssl_opts="$(db_tls_params)"
|
2020-04-28 16:21:46 +02:00
|
|
|
|
2020-09-08 21:40:14 +02:00
|
|
|
export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"
|
|
|
|
|
2023-05-02 08:33:33 +02:00
|
|
|
result=$(mysql --silent --skip-column-names $mysql_connect_args \
|
2020-09-08 23:41:01 +02:00
|
|
|
-u ${DB_SERVER_ROOT_USER} -e "$query" $ssl_opts)
|
2020-09-08 21:40:14 +02:00
|
|
|
|
|
|
|
unset MYSQL_PWD
|
2018-02-18 21:45:33 +01:00
|
|
|
|
|
|
|
echo $result
|
|
|
|
}
|
|
|
|
|
2022-08-27 20:40:18 +02:00
|
|
|
exec_sql_file() {
|
|
|
|
sql_script=$1
|
|
|
|
|
|
|
|
local command="cat"
|
|
|
|
|
|
|
|
ssl_opts="$(db_tls_params)"
|
|
|
|
|
|
|
|
export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"
|
|
|
|
|
|
|
|
if [ "${sql_script: -3}" == ".gz" ]; then
|
|
|
|
command="zcat"
|
|
|
|
fi
|
|
|
|
|
|
|
|
$command "$sql_script" | mysql --silent --skip-column-names \
|
|
|
|
--default-character-set=${DB_CHARACTER_SET} \
|
2023-05-02 08:33:33 +02:00
|
|
|
$mysql_connect_args \
|
2022-08-27 20:40:18 +02:00
|
|
|
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
|
|
|
|
${DB_SERVER_DBNAME} 1>/dev/null
|
|
|
|
|
|
|
|
unset MYSQL_PWD
|
|
|
|
}
|
|
|
|
|
2018-02-18 21:45:33 +01:00
|
|
|
create_db_user_mysql() {
|
|
|
|
[ "${CREATE_ZBX_DB_USER}" == "true" ] || return
|
|
|
|
|
|
|
|
echo "** Creating '${DB_SERVER_ZBX_USER}' user in MySQL database"
|
|
|
|
|
|
|
|
USER_EXISTS=$(mysql_query "SELECT 1 FROM mysql.user WHERE user = '${DB_SERVER_ZBX_USER}' AND host = '%'")
|
|
|
|
|
|
|
|
if [ -z "$USER_EXISTS" ]; then
|
|
|
|
mysql_query "CREATE USER '${DB_SERVER_ZBX_USER}'@'%' IDENTIFIED BY '${DB_SERVER_ZBX_PASS}'" 1>/dev/null
|
|
|
|
else
|
2019-04-10 00:21:23 +02:00
|
|
|
mysql_query "ALTER USER ${DB_SERVER_ZBX_USER} IDENTIFIED BY '${DB_SERVER_ZBX_PASS}';" 1>/dev/null
|
2018-02-18 21:45:33 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
mysql_query "GRANT ALL PRIVILEGES ON $DB_SERVER_DBNAME. * TO '${DB_SERVER_ZBX_USER}'@'%'" 1>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
create_db_database_mysql() {
|
|
|
|
DB_EXISTS=$(mysql_query "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME='${DB_SERVER_DBNAME}'")
|
|
|
|
|
|
|
|
if [ -z ${DB_EXISTS} ]; then
|
|
|
|
echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..."
|
2022-08-27 20:40:18 +02:00
|
|
|
mysql_query "CREATE DATABASE ${DB_SERVER_DBNAME} CHARACTER SET ${DB_CHARACTER_SET} COLLATE ${DB_CHARACTER_COLLATE}" 1>/dev/null
|
2018-02-18 21:45:33 +01:00
|
|
|
# better solution?
|
|
|
|
mysql_query "GRANT ALL PRIVILEGES ON $DB_SERVER_DBNAME. * TO '${DB_SERVER_ZBX_USER}'@'%'" 1>/dev/null
|
|
|
|
else
|
|
|
|
echo "** Database '${DB_SERVER_DBNAME}' already exists. Please be careful with database COLLATE!"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-08-27 20:40:18 +02:00
|
|
|
apply_db_scripts() {
|
|
|
|
db_scripts=$1
|
|
|
|
|
|
|
|
for sql_script in $db_scripts; do
|
|
|
|
[ -e "$sql_script" ] || continue
|
|
|
|
echo "** Processing additional '$sql_script' SQL script"
|
|
|
|
|
|
|
|
exec_sql_file "$sql_script"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2018-02-18 21:45:33 +01:00
|
|
|
create_db_schema_mysql() {
|
|
|
|
DBVERSION_TABLE_EXISTS=$(mysql_query "SELECT 1 FROM information_schema.tables WHERE table_schema='${DB_SERVER_DBNAME}' and table_name = 'dbversion'")
|
|
|
|
|
|
|
|
if [ -n "${DBVERSION_TABLE_EXISTS}" ]; then
|
|
|
|
echo "** Table '${DB_SERVER_DBNAME}.dbversion' already exists."
|
|
|
|
ZBX_DB_VERSION=$(mysql_query "SELECT mandatory FROM ${DB_SERVER_DBNAME}.dbversion")
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${ZBX_DB_VERSION}" ]; then
|
|
|
|
echo "** Creating '${DB_SERVER_DBNAME}' schema in MySQL"
|
|
|
|
|
2022-08-27 20:40:18 +02:00
|
|
|
exec_sql_file "/usr/share/doc/zabbix-server-mysql/create.sql.gz"
|
2020-09-08 21:40:14 +02:00
|
|
|
|
2024-12-26 07:52:37 +01:00
|
|
|
apply_db_scripts "${ZABBIX_USER_HOME_DIR}/dbscripts/*.sql"
|
2018-02-18 21:45:33 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
update_zbx_config() {
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_DB_HOST="${DB_SERVER_HOST}"
|
|
|
|
export ZBX_DB_PORT="${DB_SERVER_PORT}"
|
|
|
|
export ZBX_DB_SOCKET="${DB_SERVER_SOCKET}"
|
2020-04-28 16:21:46 +02:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_DB_NAME="${DB_SERVER_DBNAME}"
|
|
|
|
export ZBX_DB_USER="${DB_SERVER_ZBX_USER}"
|
|
|
|
export ZBX_DB_PASSWORD="${DB_SERVER_ZBX_PASS}"
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2020-04-26 18:45:42 +02:00
|
|
|
: ${ZBX_ENABLE_SNMP_TRAPS:="false"}
|
2024-12-23 13:22:01 +01:00
|
|
|
[[ "${ZBX_ENABLE_SNMP_TRAPS,,}" == "true" ]] && export ZBX_STARTSNMPTRAPPER=1
|
|
|
|
unset ZBX_ENABLE_SNMP_TRAPS
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
update_config_multiple_var "${ZABBIX_CONF_DIR}/zabbix_server_modules.conf" "LoadModule" "${ZBX_LOADMODULE}"
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
file_process_from_env "ZBX_TLSCAFILE" "${ZBX_TLSCAFILE}" "${ZBX_TLSCA}"
|
|
|
|
file_process_from_env "ZBX_TLSCRLFILE" "${ZBX_TLSCRLFILE}" "${ZBX_TLSCRL}"
|
2019-11-22 06:01:14 +01:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
file_process_from_env "ZBX_TLSCERTFILE" "${ZBX_TLSCERTFILE}" "${ZBX_TLSCERT}"
|
|
|
|
file_process_from_env "ZBX_TLSKEYFILE" "${ZBX_TLSKEYFILE}" "${ZBX_TLSKEY}"
|
2024-05-29 08:54:52 +02:00
|
|
|
|
2021-11-17 09:31:10 +01:00
|
|
|
if [ "${ZBX_AUTOHANODENAME}" == 'fqdn' ] && [ ! -n "${ZBX_HANODENAME}" ]; then
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_HANODENAME="$(hostname -f)"
|
2021-11-17 09:31:10 +01:00
|
|
|
elif [ "${ZBX_AUTOHANODENAME}" == 'hostname' ] && [ ! -n "${ZBX_HANODENAME}" ]; then
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_HANODENAME="$(hostname)"
|
2021-11-17 09:31:10 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
: ${ZBX_NODEADDRESSPORT:="10051"}
|
|
|
|
if [ "${ZBX_AUTONODEADDRESS}" == 'fqdn' ] && [ ! -n "${ZBX_NODEADDRESS}" ]; then
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_NODEADDRESS="$(hostname -f):${ZBX_NODEADDRESSPORT}"
|
2021-11-17 09:31:10 +01:00
|
|
|
elif [ "${ZBX_AUTONODEADDRESS}" == 'hostname' ] && [ ! -n "${ZBX_NODEADDRESS}" ]; then
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_NODEADDRESS="$(hostname):${ZBX_NODEADDRESSPORT}"
|
2021-11-17 09:31:10 +01:00
|
|
|
fi
|
|
|
|
|
2020-04-26 18:45:42 +02:00
|
|
|
if [ "$(id -u)" != '0' ]; then
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_USER="$(whoami)"
|
2018-02-18 21:45:33 +01:00
|
|
|
else
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_ALLOWROOT=1
|
2018-02-18 21:45:33 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:09:56 +02:00
|
|
|
clear_zbx_env() {
|
|
|
|
[[ "${ZBX_CLEAR_ENV}" == "false" ]] && return
|
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
for env_var in $(env | grep -E "^(ZABBIX|DB|MYSQL)_"); do
|
2024-08-20 11:09:56 +02:00
|
|
|
unset "${env_var%%=*}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-12-29 15:56:38 +01:00
|
|
|
prepare_db() {
|
|
|
|
echo "** Preparing database"
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2020-04-26 18:45:42 +02:00
|
|
|
check_variables_mysql
|
|
|
|
check_db_connect_mysql
|
|
|
|
create_db_user_mysql
|
|
|
|
create_db_database_mysql
|
|
|
|
create_db_schema_mysql
|
2022-12-29 15:56:38 +01:00
|
|
|
}
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2022-12-29 15:56:38 +01:00
|
|
|
prepare_server() {
|
|
|
|
echo "** Preparing Zabbix server"
|
|
|
|
|
|
|
|
prepare_db
|
2020-04-26 18:45:42 +02:00
|
|
|
update_zbx_config
|
2024-08-20 11:09:56 +02:00
|
|
|
clear_zbx_env
|
2018-02-18 21:45:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#################################################
|
|
|
|
|
2020-04-26 18:45:42 +02:00
|
|
|
if [ "${1#-}" != "$1" ]; then
|
|
|
|
set -- /usr/sbin/zabbix_server "$@"
|
2022-12-29 15:56:38 +01:00
|
|
|
fi
|
2018-02-18 21:45:33 +01:00
|
|
|
|
2020-04-26 18:45:42 +02:00
|
|
|
if [ "$1" == '/usr/sbin/zabbix_server' ]; then
|
|
|
|
prepare_server
|
2018-02-18 21:45:33 +01:00
|
|
|
fi
|
|
|
|
|
2022-12-29 15:56:38 +01:00
|
|
|
if [ "$1" == "init_db_only" ]; then
|
|
|
|
prepare_db
|
|
|
|
else
|
|
|
|
exec "$@"
|
|
|
|
fi
|
2018-02-18 21:45:33 +01:00
|
|
|
|
|
|
|
#################################################
|