2019-11-14 06:28:37 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
set +e
|
|
|
|
|
|
|
|
# Script trace mode
|
2021-04-13 13:34:31 +02:00
|
|
|
if [ "${DEBUG_MODE,,}" == "true" ]; then
|
2019-11-14 06:28:37 +01:00
|
|
|
set -o xtrace
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Default Zabbix installation name
|
|
|
|
# Default Zabbix server host
|
|
|
|
: ${ZBX_SERVER_HOST:="zabbix-server"}
|
|
|
|
# Default Zabbix server port number
|
|
|
|
: ${ZBX_SERVER_PORT:="10051"}
|
|
|
|
|
|
|
|
# Default directories
|
|
|
|
# User 'zabbix' home directory
|
|
|
|
ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
|
|
|
|
# Configuration files directory
|
|
|
|
ZABBIX_ETC_DIR="/etc/zabbix"
|
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"
|
2019-11-14 06:28:37 +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//\[/\\[}"
|
|
|
|
var_value="${var_value//\]/\\]}"
|
|
|
|
|
|
|
|
echo "$var_value"
|
|
|
|
}
|
|
|
|
|
|
|
|
update_config_var() {
|
|
|
|
local config_path=$1
|
|
|
|
local var_name=$2
|
|
|
|
local var_value=$3
|
|
|
|
local is_multiple=$4
|
|
|
|
|
2020-06-23 15:28:09 +02:00
|
|
|
local masklist=("TLSPSKIdentity")
|
|
|
|
|
2019-11-14 06:28:37 +01:00
|
|
|
if [ ! -f "$config_path" ]; then
|
|
|
|
echo "**** Configuration file '$config_path' does not exist"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2020-06-23 15:28:09 +02:00
|
|
|
if [[ " ${masklist[@]} " =~ " $var_name " ]] && [ ! -z "$var_value" ]; then
|
|
|
|
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
|
2019-11-14 06:28:37 +01:00
|
|
|
|
|
|
|
# Remove configuration parameter definition in case of unset parameter value
|
|
|
|
if [ -z "$var_value" ]; then
|
|
|
|
sed -i -e "/^$var_name=/d" "$config_path"
|
|
|
|
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
|
2019-11-14 06:28:37 +01:00
|
|
|
var_value=$ZABBIX_USER_HOME_DIR/enc/$var_value
|
|
|
|
fi
|
|
|
|
|
2020-08-05 23:21:01 +02:00
|
|
|
# Escaping characters in parameter value and name
|
2019-11-14 06:28:37 +01:00
|
|
|
var_value=$(escape_spec_char "$var_value")
|
2020-08-05 23:21:01 +02:00
|
|
|
var_name=$(escape_spec_char "$var_name")
|
2019-11-14 06:28:37 +01: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"
|
2020-08-05 23:21:01 +02:00
|
|
|
elif [ "$(grep -Ec "^[#;] $var_name=" $config_path)" -gt 0 ]; then
|
2019-11-14 06:28:37 +01:00
|
|
|
sed -i -e "/^[#;] $var_name=/s/.*/&\n$var_name=$var_value/" "$config_path"
|
|
|
|
echo "added"
|
2020-08-05 23:21:01 +02:00
|
|
|
else
|
|
|
|
sed -i -e '$a\' -e "$var_name=$var_value" "$config_path"
|
|
|
|
echo "added at the end"
|
2019-11-14 06:28:37 +01:00
|
|
|
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
|
|
|
|
update_config_var $config_path "$var_name" "$file_name"
|
|
|
|
}
|
|
|
|
|
2019-11-14 06:28:37 +01:00
|
|
|
prepare_zbx_agent_config() {
|
|
|
|
echo "** Preparing Zabbix agent configuration file"
|
|
|
|
ZBX_AGENT_CONFIG=$ZABBIX_ETC_DIR/zabbix_agent2.conf
|
|
|
|
|
|
|
|
: ${ZBX_PASSIVESERVERS:=""}
|
|
|
|
: ${ZBX_ACTIVESERVERS:=""}
|
|
|
|
|
|
|
|
[ -n "$ZBX_PASSIVESERVERS" ] && ZBX_PASSIVESERVERS=","$ZBX_PASSIVESERVERS
|
|
|
|
|
|
|
|
ZBX_PASSIVESERVERS=$ZBX_SERVER_HOST$ZBX_PASSIVESERVERS
|
|
|
|
|
|
|
|
[ -n "$ZBX_ACTIVESERVERS" ] && ZBX_ACTIVESERVERS=","$ZBX_ACTIVESERVERS
|
|
|
|
|
|
|
|
ZBX_ACTIVESERVERS=$ZBX_SERVER_HOST":"$ZBX_SERVER_PORT$ZBX_ACTIVESERVERS
|
|
|
|
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "PidFile"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "LogType" "console"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "LogFile"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "LogFileSize"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "DebugLevel" "${ZBX_DEBUGLEVEL}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "SourceIP"
|
|
|
|
|
|
|
|
: ${ZBX_PASSIVE_ALLOW:="true"}
|
2021-04-13 13:34:31 +02:00
|
|
|
if [ "${ZBX_PASSIVE_ALLOW,,}" == "true" ]; then
|
2019-11-14 06:28:37 +01:00
|
|
|
echo "** Using '$ZBX_PASSIVESERVERS' servers for passive checks"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "Server" "${ZBX_PASSIVESERVERS}"
|
|
|
|
else
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "Server"
|
|
|
|
fi
|
|
|
|
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "ListenPort" "${ZBX_LISTENPORT}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "ListenIP" "${ZBX_LISTENIP}"
|
|
|
|
|
|
|
|
: ${ZBX_ACTIVE_ALLOW:="true"}
|
2021-04-13 13:34:31 +02:00
|
|
|
if [ "${ZBX_ACTIVE_ALLOW,,}" == "true" ]; then
|
2019-11-14 06:28:37 +01:00
|
|
|
echo "** Using '$ZBX_ACTIVESERVERS' servers for active checks"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "ServerActive" "${ZBX_ACTIVESERVERS}"
|
|
|
|
else
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "ServerActive"
|
|
|
|
fi
|
2023-01-18 06:26:02 +01:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "HeartbeatFrequency" "${ZBX_HEARTBEAT_FREQUENCY}"
|
2023-08-16 09:55:59 +02:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "ForceActiveChecksOnStart" "${ZBX_FORCEACTIVECHECKSONSTART}"
|
2019-11-14 06:28:37 +01:00
|
|
|
|
2021-04-13 13:34:31 +02:00
|
|
|
if [ "${ZBX_ENABLEPERSISTENTBUFFER,,}" == "true" ]; then
|
2020-08-05 23:21:01 +02:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "EnablePersistentBuffer" "1"
|
2021-08-25 15:00:35 +02:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "PersistentBufferFile" "$ZABBIX_USER_HOME_DIR/buffer/agent2.db"
|
2020-08-05 23:21:01 +02:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "PersistentBufferPeriod" "${ZBX_PERSISTENTBUFFERPERIOD}"
|
|
|
|
else
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "EnablePersistentBuffer" "0"
|
|
|
|
fi
|
|
|
|
|
2021-04-13 13:34:31 +02:00
|
|
|
if [ "${ZBX_ENABLESTATUSPORT,,}" == "true" ]; then
|
2020-08-05 23:21:01 +02:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "StatusPort" "31999"
|
|
|
|
fi
|
|
|
|
|
2022-02-15 12:07:20 +01:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "HostInterface" "${ZBX_HOSTINTERFACE}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "HostInterfaceItem" "${ZBX_HOSTINTERFACEITEM}"
|
2020-08-05 23:21:01 +02:00
|
|
|
|
2019-11-14 06:28:37 +01:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "Hostname" "${ZBX_HOSTNAME}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "HostnameItem" "${ZBX_HOSTNAMEITEM}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "HostMetadata" "${ZBX_METADATA}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "HostMetadataItem" "${ZBX_METADATAITEM}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "RefreshActiveChecks" "${ZBX_REFRESHACTIVECHECKS}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "BufferSend" "${ZBX_BUFFERSEND}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "BufferSize" "${ZBX_BUFFERSIZE}"
|
|
|
|
# Please use include to enable Alias feature
|
|
|
|
# update_config_multiple_var $ZBX_AGENT_CONFIG "Alias" ${ZBX_ALIAS}
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "Timeout" "${ZBX_TIMEOUT}"
|
2022-04-20 16:18:46 +02:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "Include" "/etc/zabbix/zabbix_agent2.d/plugins.d/*.conf"
|
2022-05-10 18:37:02 +02:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "Include" "/etc/zabbix/zabbix_agentd.d/*.conf" "true"
|
2019-11-14 06:28:37 +01:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "UnsafeUserParameters" "${ZBX_UNSAFEUSERPARAMETERS}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "TLSConnect" "${ZBX_TLSCONNECT}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "TLSAccept" "${ZBX_TLSACCEPT}"
|
2024-08-21 13:26:24 +02:00
|
|
|
file_process_from_env $ZBX_AGENT_CONFIG "TLSCAFile" "${ZBX_TLSCAFILE}" "${ZBX_TLSCA}"
|
|
|
|
file_process_from_env $ZBX_AGENT_CONFIG "TLSCRLFile" "${ZBX_TLSCRLFILE}" "${ZBX_TLSCRL}"
|
2019-11-14 06:28:37 +01:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "TLSServerCertIssuer" "${ZBX_TLSSERVERCERTISSUER}"
|
|
|
|
update_config_var $ZBX_AGENT_CONFIG "TLSServerCertSubject" "${ZBX_TLSSERVERCERTSUBJECT}"
|
2024-08-21 13:26:24 +02:00
|
|
|
file_process_from_env $ZBX_AGENT_CONFIG "TLSCertFile" "${ZBX_TLSCERTFILE}" "${ZBX_TLSCERT}"
|
|
|
|
file_process_from_env $ZBX_AGENT_CONFIG "TLSKeyFile" "${ZBX_TLSKEYFILE}" "${ZBX_TLSKEY}"
|
2019-11-14 06:28:37 +01:00
|
|
|
update_config_var $ZBX_AGENT_CONFIG "TLSPSKIdentity" "${ZBX_TLSPSKIDENTITY}"
|
2024-08-21 13:26:24 +02:00
|
|
|
file_process_from_env $ZBX_AGENT_CONFIG "TLSPSKFile" "${ZBX_TLSPSKFILE}" "${ZBX_TLSPSK}"
|
2019-11-14 06:28:37 +01:00
|
|
|
|
2020-08-05 23:21:01 +02:00
|
|
|
update_config_multiple_var $ZBX_AGENT_CONFIG "DenyKey" "${ZBX_DENYKEY}"
|
|
|
|
update_config_multiple_var $ZBX_AGENT_CONFIG "AllowKey" "${ZBX_ALLOWKEY}"
|
2019-11-14 06:28:37 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 08:33:01 +01:00
|
|
|
prepare_zbx_agent_plugin_config() {
|
|
|
|
echo "** Preparing Zabbix agent plugin configuration files"
|
|
|
|
|
2024-06-01 08:10:31 +02:00
|
|
|
update_config_var "/etc/zabbix/zabbix_agent2.d/plugins.d/mongodb.conf" "Plugins.MongoDB.System.Path" "/usr/sbin/zabbix-agent2-plugin/mongodb"
|
|
|
|
update_config_var "/etc/zabbix/zabbix_agent2.d/plugins.d/postgresql.conf" "Plugins.PostgreSQL.System.Path" "/usr/sbin/zabbix-agent2-plugin/postgresql"
|
|
|
|
update_config_var "/etc/zabbix/zabbix_agent2.d/plugins.d/mssql.conf" "Plugins.MSSQL.System.Path" "/usr/sbin/zabbix-agent2-plugin/mssql"
|
2022-12-14 08:33:01 +01:00
|
|
|
}
|
|
|
|
|
2024-08-20 11:09:56 +02:00
|
|
|
clear_zbx_env() {
|
|
|
|
[[ "${ZBX_CLEAR_ENV}" == "false" ]] && return
|
|
|
|
|
|
|
|
for env_var in $(env | grep -E "^ZBX_"); do
|
|
|
|
unset "${env_var%%=*}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2019-11-14 06:28:37 +01:00
|
|
|
prepare_agent() {
|
|
|
|
echo "** Preparing Zabbix agent"
|
|
|
|
prepare_zbx_agent_config
|
2022-12-14 08:33:01 +01:00
|
|
|
prepare_zbx_agent_plugin_config
|
2024-08-20 11:09:56 +02:00
|
|
|
clear_zbx_env
|
2019-11-14 06:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#################################################
|
|
|
|
|
|
|
|
if [ "${1#-}" != "$1" ]; then
|
|
|
|
set -- /usr/sbin/zabbix_agent2 "$@"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" == '/usr/sbin/zabbix_agent2' ]; then
|
|
|
|
prepare_agent
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec "$@"
|
|
|
|
|
|
|
|
#################################################
|