From dab7132d3166b201c494fc1a1a5180f7212c733b Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Mon, 22 Sep 2025 18:34:48 +0200 Subject: [PATCH] reworked the checkmk module --- modules/install_mkagent.sh | 89 ++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 28 deletions(-) diff --git a/modules/install_mkagent.sh b/modules/install_mkagent.sh index d663093..bd23e38 100644 --- a/modules/install_mkagent.sh +++ b/modules/install_mkagent.sh @@ -9,54 +9,87 @@ # ------------------------------------------------------------------------------ # Variable: # * MK_SERVER: Server IP address -# * MK_PORT: Port check_mk agent will use to communicate with server +# * 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.0.7" +export VER_install_mkagent="0.1.0" export DEP_install_mkagent="" install_mkagent() { - wget $MK_URL -O /tmp/check-mk-agent_${MK_VERSION}_all.deb - pkginst xinetd /tmp/check-mk-agent_${MK_VERSION}_all.deb - rm /tmp/check-mk-agent_${MK_VERSION}_all.deb + # 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 - backup_dist /etc/xinetd.d/check_mk - install_file cmk/check_mk /etc/xinetd.d/check_mk - tag_file /etc/xinetd.d/check_mk - sed -i -e "s/@MK_SERVER_IP@/$MK_SERVER_IP/" /etc/xinetd.d/check_mk + # 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 - mkdir -pv /usr/lib/check_mk_agent/plugins/7200 - install_file cmk/mk_apt /usr/lib/check_mk_agent/plugins/7200/mk_apt + # 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 [[ -e /var/lib/cmk-agent/cmk-agent-ctl.gz ]]; then - gunzip /var/lib/cmk-agent/cmk-agent-ctl.gz - chmod +x /var/lib/cmk-agent/cmk-agent-ctl - scp -O $MK_SERVER_IP:/etc/check_mk/agentpwd /tmp/mk-pwd - sleep 1 # Some execution of cmk-agent-ctl have failed with file not found without that line - /var/lib/cmk-agent/cmk-agent-ctl register --hostname $HOSTNAME \ - --server $MK_SERVER_IP --site check_mk --user check_mk --password \ - "$(read /tmp/mk-pwd)" + 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 - svc_restart xinetd } precheck_install_mkagent() { - if [[ -z $MK_VERSION ]]; then - prnt E "Undeclared check_mk version of the agent to install." - die 162 + 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 + prnt E "Undeclared check_mk download URL." + die 162 fi if [[ -z $MK_SERVER_IP ]]; then - prnt E "Undeclared check_mk server." - die 162 + 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 - file_must_exists cmk/check_mk cmk/mk_apt } export -f install_mkagent