Files
init.sh/modules/install_mkagent.sh

99 lines
3.3 KiB
Bash

# ------------------------------------------------------------------------------
# Install check_mk agent using xinetd superserver
# This file is part of the init.sh project
# Copyright (c) 2019-2023 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# Variable:
# * MK_SERVER: Server IP address
# * MK_SITE: The check_mk site (or instance) to use
# * MK_URL: The URL to use to download the agent
# * MK_SECRET: The secret to use to register the agent
# * MK_USER: The user to use to register
# ------------------------------------------------------------------------------
export VER_install_mkagent="0.1.0"
export DEP_install_mkagent=""
install_mkagent()
{
# Download and install agent
wget "$MK_URL" -O /tmp/check-mk-agent_latest_all.deb
pkginst /tmp/check-mk-agent_latest_all.deb
rm /tmp/check-mk-agent_latest_all.deb
# Activate correct service depending on system configuration
if pidof systemd >/dev/null; then
systemctl enable --now check-mk-agent.socket
else
pkginst xinetd
backup_dist /etc/xinetd.d/check-mk-agent
install_file cmk/check_mk /etc/xinetd.d/check-mk-agent
tag_file /etc/xinetd.d/check-mk-agent
sed -i -e "s/@MK_SERVER_IP@/$MK_SERVER_IP/" /etc/xinetd.d/check-mk-agent
svc_restart xinetd
fi
# Install apt plugin (for Debian)
if [[ $PKG_MAN == "apt-get" ]]; then
mkdir -pv /usr/lib/check_mk_agent/plugins/3600
install_file cmk/mk_apt /usr/lib/check_mk_agent/plugins/3600/mk_apt
fi
# Cmk > 2.1, configure agent
if [[ -n $MK_SECRET ]]; then
local secret
secret=$(fetch_secret "$MK_SECRET")
if [[ -e /var/lib/cmk-agent/cmk-agent-ctl.gz ]]; then
gunzip -f /var/lib/cmk-agent/cmk-agent-ctl.gz
chmod +x /var/lib/cmk-agent/cmk-agent-ctl
fi
if [[ -e /var/lib/cmk-agent/cmk-agent-ctl ]]; then
/var/lib/cmk-agent/cmk-agent-ctl register \
--hostname "$HOSTNAME" \
--server "$MK_SERVER_IP" \
--site "$MK_SITE" \
--user "$MK_USER" \
--password "$secret"
fi
unset secret
else
prnt W "No secret configured, agent cannot be registered."
fi
}
precheck_install_mkagent()
{
if [[ -z $MK_SITE ]]; then
prnt E "Undeclared check_mk site to use."
die 162
fi
if [[ -z $MK_URL ]]; then
prnt E "Undeclared check_mk download URL."
die 162
fi
if [[ -z $MK_SERVER_IP ]]; then
prnt E "Undeclared check_mk server."
die 162
fi
if [[ $PKG_MAN == "apt-get" ]]; then
file_must_exists cmk/check_mk cmk/mk_apt
fi
if [[ -z $MK_SECRET ]]; then
prnt W "No secret set for CheckMK, registration won't be possible."
if [[ -z $MK_USER ]]; then
prnt E "A CheckMK user is required to register."
die 162
fi
fi
}
export -f install_mkagent
export -f precheck_install_mkagent
# EOF