Skip to content

Commit ddf121b

Browse files
authored
Merge pull request #15 from elabit/bridge-lab
feat: Bridge lab
2 parents 1e3ed6d + 84ca5e2 commit ddf121b

84 files changed

Lines changed: 1831 additions & 142 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 109 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,124 @@
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

510
case "${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
;;
1624
esac
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

_dev/_devcontainers/cmk25/template/.devcontainer/oncreate.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,8 @@ apt-get update -qq
6060
apt-get install -y --no-install-recommends firefox-esr
6161
ok "Firefox installed."
6262

63+
# install additional tools
64+
apt-get install -y --no-install-recommends jq curl wget vim htop sudo python3-pip
65+
6366
echo ""
6467
echo -e "${GREEN}${BOLD}Container creation complete.${RESET}"

_dev/_devcontainers/cmk25/template/.devcontainer/postcreate.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ disown
7373

7474
echo ""
7575
echo -e "${GREEN}${BOLD}Setup complete.${RESET} Open a new terminal to activate the PATH."
76+
77+
useradd cmk-agent -m -s /bin/bash
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
_subdirectory: template
2+
_answers_file: .copier-env-answers.yml
3+
4+
rf_version:
5+
type: str
6+
default: "7.1.1"
7+
8+
python_version:
9+
type: str
10+
default: "3.12"
11+
12+
pip_version:
13+
type: str
14+
default: "23.2.1"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
channels:
2+
- conda-forge
3+
dependencies:
4+
- python={{ python_version }}
5+
- pip={{ pip_version }}
6+
- pip:
7+
- robotframework=={{ rf_version }}
8+
- robotmk-bridge-testdatagenerator
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Library | Version |
2+
|---|---|
3+
| Python | `{{ python_version }}` |
4+
| Robot Framework | `{{ rf_version }}` |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Robotmk Bridge Lab
2+
3+
4+
5+
## About this Repository
6+
7+
This repository provides a lab that can be used to work through the examples in the following blog posts:
8+
9+
- TODO
10+
- TODO
11+
12+
It includes the following components:
13+
14+
- [Checkmk](https://checkmk.com) 2.5
15+
- [Robotmk Bridge](https://github.com/elabit/robotmk-bridge-plugin)
16+
- A [test data generator](https://pypi.org/project/robotmk-bridge-testdatagenerator/) which fakes results from 3rd party tools
17+
18+
{% include 'how-to-run-lab.partial.md' %}
19+
20+
21+
## Links
22+
23+
TODO
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
_subdirectory: template
3+
_answers_file: .copier-answers.yml
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
populate:
2+
3+
- src: templates/rf-skeleton
4+
dst: suites/simpletest
5+
exclude:
6+
- "*.pyc"
7+
- "__pycache__/"
8+
9+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file is managed by Copier.
2+
# Do NOT edit manually. To regenerate: task generate
3+
_src_path: {{ _copier_conf.src_path }}
4+
_commit: {{ _copier_conf.commit or 'none' }}

0 commit comments

Comments
 (0)