Compare commits

..

1 Commits

Author SHA1 Message Date
CanbiZ (MickLesk)
50731fc941 Improve Zabbix agent service detection
Use systemctl cat to reliably detect zabbix-agent2 or zabbix-agent units, add fallback when no agent is present, and warn instead of failing. Guard stop/start calls so they only run when an agent service was found, and use [[ ]] for safer string comparisons. Adjust package upgrade logic to handle both agent2 and agent cases accordingly.
2026-02-16 08:32:19 +01:00
3 changed files with 46 additions and 7 deletions

View File

@@ -35,15 +35,18 @@ function update_script() {
exit
fi
if systemctl list-unit-files | grep -q zabbix-agent2.service; then
if systemctl cat zabbix-agent2.service &>/dev/null; then
AGENT_SERVICE="zabbix-agent2"
else
elif systemctl cat zabbix-agent.service &>/dev/null; then
AGENT_SERVICE="zabbix-agent"
else
AGENT_SERVICE=""
msg_warn "No Zabbix Agent service found, skipping agent actions"
fi
msg_info "Stopping Services"
systemctl stop zabbix-server
systemctl stop "$AGENT_SERVICE"
[[ -n "$AGENT_SERVICE" ]] && systemctl stop "$AGENT_SERVICE"
msg_ok "Stopped Services"
read -rp "Choose Zabbix version [1] 7.0 LTS [2] 7.4 (Latest Stable) [3] Latest available (default: 2): " ZABBIX_CHOICE
@@ -83,13 +86,13 @@ function update_script() {
$STD apt install --only-upgrade zabbix-server-pgsql zabbix-frontend-php php8.4-pgsql
if [ "$AGENT_SERVICE" = "zabbix-agent2" ]; then
if [[ "$AGENT_SERVICE" == "zabbix-agent2" ]]; then
$STD apt install --only-upgrade zabbix-agent2 zabbix-agent2-plugin-postgresql
if [ -f /etc/zabbix/zabbix_agent2.d/plugins.d/nvidia.conf ]; then
sed -i 's|^Plugins.NVIDIA.System.Path=.*|# Plugins.NVIDIA.System.Path=/usr/libexec/zabbix/zabbix-agent2-plugin-nvidia-gpu|' \
/etc/zabbix/zabbix_agent2.d/plugins.d/nvidia.conf
fi
else
elif [[ "$AGENT_SERVICE" == "zabbix-agent" ]]; then
$STD apt install --only-upgrade zabbix-agent
fi
@@ -105,7 +108,7 @@ function update_script() {
msg_info "Starting Services"
systemctl start zabbix-server
systemctl start "$AGENT_SERVICE"
[[ -n "$AGENT_SERVICE" ]] && systemctl start "$AGENT_SERVICE"
systemctl restart apache2
msg_ok "Started Services"
msg_ok "Updated successfully!"

View File

@@ -34,6 +34,42 @@ EOF
fi
}
set -Eeuo pipefail
trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR
trap on_exit EXIT
trap on_interrupt INT
trap on_terminate TERM
error_handler() {
local exit_code="$1"
local line_number="$2"
local command="$3"
if [[ "$exit_code" -eq 0 ]]; then
return 0
fi
printf "\e[?25h"
echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n"
exit "$exit_code"
}
on_exit() {
local exit_code="$?"
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
exit "$exit_code"
}
on_interrupt() {
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
exit 130
}
on_terminate() {
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
exit 143
}
# This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection
setting_up_container() {
msg_info "Setting up Container OS"

View File

@@ -172,7 +172,7 @@ network_check() {
fi
set -e
trap 'error_handler' ERR
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
}
# ==============================================================================