1- #! /bin/bash
2-
1+ #! /usr/bin/env bash
32# https://docs.checkmk.com/latest/en/agent_linux_legacy.html#unencrypted
3+ # set -euo pipefail
4+
5+ # ---------------------------------------------------------------------------
6+ # Resolve agent package path from argument
7+ # ---------------------------------------------------------------------------
8+ readonly AGENT_REFS_DIR=" /omd/sites/cmk/var/check_mk/agents/linux_deb/references"
49
510case " ${1:- host} " in
611 vanilla)
7- AGENT_DEB=" /omd/sites/cmk/var/check_mk/agents/linux_deb/references/_VANILLA"
12+ AGENT_DEB=" ${AGENT_REFS_DIR} /_VANILLA"
13+ ;;
14+ host)
15+ AGENT_DEB=" ${AGENT_REFS_DIR} /$( hostname) "
816 ;;
9- localhost|host )
10- AGENT_DEB=" /omd/sites/cmk/var/check_mk/agents/linux_deb/references /localhost"
17+ localhost)
18+ AGENT_DEB=" ${AGENT_REFS_DIR} /localhost"
1119 ;;
1220 * )
1321 echo " Usage: $0 [vanilla|localhost|host]" >&2
1422 exit 1
1523 ;;
1624esac
25+ readonly AGENT_DEB
26+
27+ if [[ ! -f " $AGENT_DEB " ]]; then
28+ echo " ERROR: Checkmk agent deb package not found: $AGENT_DEB " >&2
29+ exit 1
30+ fi
31+
32+ # ---------------------------------------------------------------------------
33+ # Install
34+ # ---------------------------------------------------------------------------
35+ echo " ▹ Installing Checkmk agent from: $AGENT_DEB "
36+
37+ # In devcontainers systemctl exists but systemd is not the init system.
38+ # The package's preinst script calls systemctl and fails, causing dpkg to
39+ # abort before unpacking any files. Stub systemctl with a no-op for the
40+ # duration of dpkg so maintainer scripts don't break the installation.
41+ if command -v systemctl > /dev/null 2>&1 && ! systemctl is-system-running > /dev/null 2>&1 ; then
42+ _STUB_DIR=$( mktemp -d)
43+ printf ' #!/bin/sh\nexit 0\n' > " $_STUB_DIR /systemctl"
44+ chmod +x " $_STUB_DIR /systemctl"
45+ export PATH=" $_STUB_DIR :$PATH "
46+ trap ' rm -rf "$_STUB_DIR"' EXIT
47+ fi
48+
49+ dpkg -i " $AGENT_DEB "
50+
51+ ensure_user_shell () {
52+ # in the devcontainer, the non-root agent user is not allowed to have an interactive shell, which prevents the
53+ # scheduler to start properly. We fix this here.
54+ local user=" $1 "
55+ local desired_shell=" /bin/bash"
56+ local current_shell
1757
18- echo " ▹ Installing the Checkmk agent..."
19-
20- dpkg -i " $AGENT_DEB " 2> /dev/null
21-
22- if [ -d /opt/checkmk/agent/ ]; then
23- echo " USER-Agent detected: running "
24- echo " - /opt/checkmk/agent/default/package/scripts/super-server/1_xinetd/setup deploy"
25- echo " - /opt/checkmk/agent/default/package/scripts/super-server/1_xinetd/setup trigger"
26- SETUP=/opt/checkmk/agent/default/package/scripts/super-server/1_xinetd/setup
27- bash " $SETUP " deploy
28- bash " $SETUP " trigger
29- if [[ -f /opt/checkmk/agent/default/package/config/robotmk.json ]]; then
30- echo " ▹ robotmk.json found — starting RobotMK scheduler in the background..."
31- pkill -f " robotmk_scheduler" 2> /dev/null && echo " - killed existing robotmk_scheduler instance"
32- /opt/checkmk/agent/default/package/robotmk/robotmk_scheduler /opt/checkmk/agent/default/package/config/robotmk.json &
58+ current_shell=$( getent passwd " $user " | cut -d: -f7 || true)
59+ if [[ -z " $current_shell " ]]; then
60+ echo " ERROR: user '$user ' not found in /etc/passwd" >&2
61+ exit 1
3362 fi
34- else
35- echo " ROOT-Agent detected: running "
36- echo " - /var/lib/cmk-agent/scripts/super-server/1_xinetd/setup deploy"
37- echo " - /var/lib/cmk-agent/scripts/super-server/1_xinetd/setup trigger"
38- SETUP=/var/lib/cmk-agent/scripts/super-server/1_xinetd/setup
39- bash " $SETUP " deploy
40- bash " $SETUP " trigger
41- if [[ -f /etc/check_mk/robotmk.json ]]; then
42- echo " ▹ robotmk.json found — starting RobotMK scheduler in the background..."
43- pkill -f " robotmk_scheduler" 2> /dev/null && echo " - killed existing robotmk_scheduler instance"
44- /usr/lib/check_mk_agent/robotmk/robotmk_scheduler /etc/check_mk/robotmk.json &
63+
64+ if [[ " $current_shell " != " $desired_shell " ]]; then
65+ echo " - updating shell for user '$user ' from '$current_shell ' to '$desired_shell '"
66+ if command -v usermod > /dev/null 2>&1 ; then
67+ usermod -s " $desired_shell " " $user "
68+ else
69+ sed -i -E " s#^($user :[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:).*#\1$desired_shell #" /etc/passwd
70+ fi
4571 fi
46- fi
72+ }
73+
74+ # ---------------------------------------------------------------------------
75+ # Configure xinetd and optionally start the RobotMK scheduler
76+ # ---------------------------------------------------------------------------
77+ setup_agent () {
78+ local setup_script=" $1 "
79+ local robotmk_config=" $2 "
80+ local scheduler_bin=" $3 "
81+ local run_as_user=" ${4:- } "
82+
83+ echo " - $setup_script deploy"
84+ bash " $setup_script " deploy
4785
86+ echo " - $setup_script trigger"
87+ bash " $setup_script " trigger
88+
89+ if [[ -f " $robotmk_config " ]]; then
90+ echo " ▹ robotmk.json found — starting RobotMK scheduler in the background..."
91+ if pkill -f " robotmk_scheduler" 2> /dev/null; then
92+ echo " - killed existing robotmk_scheduler instance"
93+ fi
94+ if [[ -n " $run_as_user " ]]; then
95+ ensure_user_shell " $run_as_user "
96+ echo " - starting scheduler as user: $run_as_user "
97+ su -l " $run_as_user " -c " \" $scheduler_bin \" \" $robotmk_config \" " &
98+ else
99+ " $scheduler_bin " " $robotmk_config " &
100+ fi
101+ fi
102+ }
103+
104+ if [[ -d /opt/checkmk/agent/ ]]; then
105+ echo " ▹ USER-agent detected"
106+ _RUNTIME_DIR=" /opt/checkmk/agent/default/runtime"
107+ _AGENT_USER=$( stat -c ' %U' " $_RUNTIME_DIR " 2> /dev/null)
108+ if [[ -z " $_AGENT_USER " ]]; then
109+ echo " ERROR: could not determine owner of $_RUNTIME_DIR " >&2
110+ exit 1
111+ fi
112+ echo " - runtime dir owner: $_AGENT_USER "
113+ setup_agent \
114+ " /opt/checkmk/agent/default/package/scripts/super-server/1_xinetd/setup" \
115+ " /opt/checkmk/agent/default/package/config/robotmk.json" \
116+ " /opt/checkmk/agent/default/package/robotmk/robotmk_scheduler" \
117+ " $_AGENT_USER "
118+ else
119+ echo " ▹ ROOT-agent detected"
120+ setup_agent \
121+ " /var/lib/cmk-agent/scripts/super-server/1_xinetd/setup" \
122+ " /etc/check_mk/robotmk.json" \
123+ " /usr/lib/check_mk_agent/robotmk/robotmk_scheduler"
124+ fi
0 commit comments