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
|
2021-04-12 03:43:41 +02:00
|
|
|
if [ "${DEBUG_MODE,,}" == "true" ]; then
|
2016-08-04 07:09:11 +02:00
|
|
|
set -o xtrace
|
|
|
|
fi
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
# Default directories
|
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"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
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
|
|
|
|
|
2020-06-23 14:42:13 +02:00
|
|
|
local masklist=("TLSPSKIdentity")
|
|
|
|
|
2016-08-03 10:09:49 +02: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
|
2016-08-03 10:09:49 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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
|
2016-08-03 10:09:49 +02: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")
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
local config_path=$1
|
|
|
|
local var_name=$2
|
|
|
|
local file_name=$3
|
|
|
|
local var_value=$4
|
|
|
|
|
|
|
|
if [ ! -z "$var_value" ]; then
|
|
|
|
echo -n "$var_value" > "${ZABBIX_INTERNAL_ENC_DIR}/$var_name"
|
|
|
|
file_name="${ZABBIX_INTERNAL_ENC_DIR}/${var_name}"
|
|
|
|
fi
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
if [ -n "$var_value" ]; then
|
|
|
|
export "$var_name"="$file_name"
|
2016-08-03 10:09:49 +02:00
|
|
|
fi
|
2024-12-23 13:22:01 +01:00
|
|
|
# Remove variable with plain text data
|
|
|
|
unset "${var_name%%FILE}"
|
|
|
|
}
|
2019-04-11 16:02:48 +02:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
update_zbx_config() {
|
2023-01-16 09:01:06 +01:00
|
|
|
: ${ZBX_USE_NODE_NAME_AS_DB_NAME:="false"}
|
|
|
|
if [ "${ZBX_USE_NODE_NAME_AS_DB_NAME,,}" == "false" ]; then
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_DB_NAME="${ZABBIX_USER_HOME_DIR}/db_data/${ZBX_HOSTNAME:-"zabbix-proxy-sqlite3"}.sqlite"
|
2023-01-16 09:01:06 +01:00
|
|
|
else
|
|
|
|
node_name=$(uname -n)
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_DB_NAME="${ZABBIX_USER_HOME_DIR}/db_data/$node_name.sqlite"
|
2016-08-03 10:09:49 +02:00
|
|
|
fi
|
2024-12-23 13:22:01 +01:00
|
|
|
unset ZBX_USE_NODE_NAME_AS_DB_NAME
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_SERVER_HOST="${ZBX_SERVER_HOST:="zabbix-server"}"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
if [ ! -z "${ZBX_HOSTNAMEITEM}" ]; then
|
|
|
|
export ZBX_HOSTNAME=""
|
2016-08-03 10:09:49 +02:00
|
|
|
else
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_HOSTNAME="${ZBX_HOSTNAME:-"zabbix-proxy-sqlite3"}"
|
2016-08-03 10:09:49 +02:00
|
|
|
fi
|
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
: ${ZBX_ENABLE_SNMP_TRAPS:="false"}
|
|
|
|
[[ "${ZBX_ENABLE_SNMP_TRAPS,,}" == "true" ]] && export ZBX_STARTSNMPTRAPPER=1
|
|
|
|
unset ZBX_ENABLE_SNMP_TRAPS
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
update_config_multiple_var "${ZABBIX_CONF_DIR}/zabbix_proxy_modules.conf" "LoadModule" "${ZBX_LOADMODULE}"
|
2016-08-03 10:09:49 +02: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}"
|
2016-08-03 10:09:49 +02: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}"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2024-12-23 13:22:01 +01:00
|
|
|
file_process_from_env "ZBX_TLSPSKFILE" "${ZBX_TLSPSKFILE}" "${ZBX_TLSPSK}"
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
if [ "$(id -u)" != '0' ]; then
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_USER="$(whoami)"
|
2016-08-03 10:09:49 +02:00
|
|
|
else
|
2024-12-23 13:22:01 +01:00
|
|
|
export ZBX_ALLOWROOT=1
|
2016-08-03 10:09:49 +02:00
|
|
|
fi
|
|
|
|
}
|
2016-08-11 12:55:45 +02:00
|
|
|
|
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_"); do
|
2024-08-20 11:09:56 +02:00
|
|
|
unset "${env_var%%=*}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:09:49 +02:00
|
|
|
prepare_proxy() {
|
|
|
|
echo "Preparing Zabbix proxy"
|
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
update_zbx_config
|
2024-08-20 11:09:56 +02:00
|
|
|
clear_zbx_env
|
2016-08-03 10:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#################################################
|
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
if [ "${1#-}" != "$1" ]; then
|
|
|
|
set -- /usr/sbin/zabbix_proxy "$@"
|
2016-08-03 10:09:49 +02:00
|
|
|
fi
|
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
if [ "$1" == '/usr/sbin/zabbix_proxy' ]; then
|
|
|
|
prepare_proxy
|
2018-02-22 21:07:15 +01:00
|
|
|
fi
|
2016-08-03 10:09:49 +02:00
|
|
|
|
2020-04-28 18:39:39 +02:00
|
|
|
exec "$@"
|
|
|
|
|
2016-08-03 10:09:49 +02:00
|
|
|
#################################################
|