Checkmk Agent für Alpine Linux


Da gibt es leider nichts fertiges. Im Checkmk Forum bin ich auf diesen älteren Beitrag gestossen. CheckMK Agent on Alpine Linux
Die einzelnen Schritte hab ich alle mal in ein installscript gepackt. Damit ist der Agent in einem Rutsch installiert.

mein Setup:

  • Checkmk CRE 2.2.0p20
  • Raspberry Pi mit Alpine Linux
  • Proxmox LXC Container mit Alpine Linux

Variante A mit socat

install-checkmk-agent-alpinelinux_socat.sh

#!/bin/ash
#
# use curl --insecure for self signed certificates
#

#
# ADJUST THESE VARIABLES!
#
cmk_server="192.168.50.153" # CHANGE - checkmk server ip or fqdn
cmk_site="test"             # CHANGE - checkmk site

#
# check if we are root
#
if [[ ! $USER = root ]]; then
  echo " This script must be run as root!" ; echo ; exit 1
fi

#
# check if we are running Alpine Linux
#
if [ -f "/etc/alpine-release" ]; then

# install needed packages
apk add socat ethtool curl

# create directories
mkdir /etc/check_mk
mkdir -p /var/lib/check_mk_agent/spool
mkdir /var/lib/check_mk_agent/cache
mkdir -p /usr/lib/check_mk_agent/plugins
mkdir /usr/lib/check_mk_agent/local

# download check_mk agent from our cmk server
curl -s -L http://$cmk_server/$cmk_site/check_mk/agents/check_mk_agent.openwrt -o /usr/local/bin/check_mk_agent
chmod 755 /usr/local/bin/check_mk_agent

# download waitmax binary from our checkmk server
curl -s -L http://$cmk_server/$cmk_site/check_mk/agents/waitmax -o /usr/local/bin/waitmax
chmod 755 /usr/local/bin/waitmax

# create init script
cat > /etc/init.d/checkmk_agent << EOF
#!/sbin/openrc-run
description="Check_MK Agent on Alpine Linux"
name=check_mk_agent
command="/usr/bin/socat"
command_args="-U TCP-LISTEN:6556,fork,reuseaddr EXEC:/usr/local/bin/check_mk_agent"
command_user="root"
pidfile="/run/$RC_SVCNAME.pid"
command_background="yes"

depend() {
 need net
}
EOF

# make script executable
chmod 755 /etc/init.d/checkmk_agent

# modify check_mk_agent <<<check_mk>> section
sed -i 's/openwrt/alpine linux/' /usr/local/bin/check_mk_agent

# modify check_mk_agent <<<df>>> section
sed -i s"/df -kPT.*/df -kPT | egrep -v '(Filesystem|none|udev|tmpfs)'/" /usr/local/bin/check_mk_agent
sed -i s"/df -PTi.*/df -PTi | egrep -v '(Filesystem|none|udev|tmpfs)'/" /usr/local/bin/check_mk_agent

#
# create cronjob
#
cat > /etc/periodic/hourly/get.alpinelinux.online.version << EOF
#!/bin/ash
curl -s https://www.alpinelinux.org/downloads/ | grep "Current Alpine Version" | sed 's/>/ /g;s/</ /g' | awk '{print \$6}' > /var/lib/check_mk_agent/cache/alpinelinux.version
EOF

# make script executable
chmod 755 /etc/periodic/hourly/get.alpinelinux.online.version

# get current version for check_os script
/etc/periodic/hourly/get.alpinelinux.online.version

#
# create check_os script
#
cat > /usr/lib/check_mk_agent/local/check_os << EOF
#!/bin/ash

# get versions
version_online=\$(cat /var/lib/check_mk_agent/cache/alpinelinux.version)
version_installed=\$(cat /etc/alpine-release)

# check if newer version is online available
if [[ \$version_online = \$version_installed ]]; then
        echo "0 Check_os" - "Alpine Linux \$(cat /etc/alpine-release) - \$(uname -rvm)"
          exit 0
           else
          echo "1 Check_os" - Alpine Linux \$version_installed - update to \$version_online available'\n'"https://www.alpinelinux.org/downloads/" "Alpine Linux downloads"
        exit 1
fi
EOF

#
# create check_apk script
#
cat > /usr/lib/check_mk_agent/local/check_apk << EOF
#!/bin/ash
UPDATES=\$(apk -uU list | wc -l)

if [[ \$UPDATES -gt 0 ]]; then
  LIST=\$(apk -u list | sed 's/\$/\\\n/' >/var/lib/check_mk_agent/cache/alpinelinux.packages)
   echo "1 Check_apk" - \$UPDATES packages available for upgrade'\n'Package List:'\n'\$(cat /var/lib/check_mk_agent/cache/alpinelinux.packages)
  exit 1
else
  echo "0 Check_apk" - no packages available for upgrade
fi
EOF

# make these scripts executable
chmod +x /usr/lib/check_mk_agent/local/*

# enable autostart and start checkmk_agent
rc-update add checkmk_agent
/etc/init.d/checkmk_agent start

# status
/etc/init.d/checkmk_agent status

else 
    echo "ERROR - Your are not running Alpine Linux!"
fi

Variante B mit inetd

install-checkmk-agent-alpinelinux_inetd.sh

#!/bin/ash
#
# use curl --insecure for self signed certificates
#

#
# ADJUST THESE VARIABLES!
#
cmk_server="192.168.50.153" # CHANGE - checkmk server ip or fqdn
cmk_site="test"             # CHANGE - checkmk site

#
# check if we are root
#
if [[ ! $USER = root ]]; then
  echo " This script must be run as root!" ; echo ; exit 1
fi

#
# check if we are running Alpine Linux
#
if [ -f "/etc/alpine-release" ]; then

# install needed packages
apk add busybox-extras ethtool curl

# create directories
mkdir /etc/check_mk
mkdir -p /var/lib/check_mk_agent/spool
mkdir /var/lib/check_mk_agent/cache
mkdir -p /usr/lib/check_mk_agent/plugins
mkdir /usr/lib/check_mk_agent/local

# download check_mk agent from our cmk server
curl -s -L http://$cmk_server/$cmk_site/check_mk/agents/check_mk_agent.openwrt -o /usr/local/bin/check_mk_agent
chmod 755 /usr/local/bin/check_mk_agent

# download waitmax binary from our checkmk server
curl -s -L http://$cmk_server/$cmk_site/check_mk/agents/waitmax -o /usr/local/bin/waitmax
chmod 755 /usr/local/bin/waitmax

# register checkmk as a service
echo "checkmk-agent   6556/tcp                        # Checkmk monitoring agent" >> /etc/services

# create inetd.conf
cat > /etc/inetd.conf << EOF
checkmk-agent stream tcp nowait root /usr/local/bin/check_mk_agent
#checkmk-agent stream tcp6 nowait root /usr/local/bin/check_mk_agent
EOF

# create inetd init script
cat > /etc/init.d/inetd << EOF
#!/sbin/openrc-run
name="busybox \$SVCNAME"
command="/usr/sbin/\$SVCNAME"
command_args="-f \$INETD_OPTS"
pidfile="/var/run/\$SVCNAME.pid"
command_background=true

depend() {
need net localmount
after firewall
}
EOF

# make script executable
chmod 755 /etc/init.d/inetd

# modify check_mk_agent <<<check_mk>> section
sed -i 's/openwrt/alpine linux/' /usr/local/bin/check_mk_agent

# modify check_mk_agent <<<df>>> section
sed -i s"/df -kPT.*/df -kPT | egrep -v '(Filesystem|none|udev|tmpfs)'/" /usr/local/bin/check_mk_agent
sed -i s"/df -PTi.*/df -PTi | egrep -v '(Filesystem|none|udev|tmpfs)'/" /usr/local/bin/check_mk_agent

#
# create cronjob
#
cat > /etc/periodic/hourly/get.alpinelinux.online.version << EOF
#!/bin/ash
curl -s https://www.alpinelinux.org/downloads/ | grep "Current Alpine Version" | sed 's/>/ /g;s/</ /g' | awk '{print \$6}' > /var/lib/check_mk_agent/cache/alpinelinux.version
EOF

# make script executable
chmod 755 /etc/periodic/hourly/get.alpinelinux.online.version

# get current version for check_os script
/etc/periodic/hourly/get.alpinelinux.online.version

#
# create check_os script
#
cat > /usr/lib/check_mk_agent/local/check_os << EOF
#!/bin/ash

# get versions
version_online=\$(cat /var/lib/check_mk_agent/cache/alpinelinux.version)
version_installed=\$(cat /etc/alpine-release)

# check if newer version is online available
if [[ \$version_online = \$version_installed ]]; then
        echo "0 Check_os" - "Alpine Linux \$version_installed - \$(uname -rvm)"
          exit 0
           else
          echo "1 Check_os" - Alpine Linux \$version_installed - update to \$version_online available'\n'"https://www.alpinelinux.org/downloads/" "Alpine Linux downloads"
        exit 1
fi
EOF

#
# create check_apk script
#
cat > /usr/lib/check_mk_agent/local/check_apk << EOF
#!/bin/ash
UPDATES=\$(apk -uU list | wc -l)

if [[ \$UPDATES -gt 0 ]]; then
  LIST=\$(apk -u list | sed 's/\$/\\\n/' >/var/lib/check_mk_agent/cache/alpinelinux.packages)
   echo "1 Check_apk" - \$UPDATES packages available for upgrade'\n'Package List:'\n'\$(cat /var/lib/check_mk_agent/cache/alpinelinux.packages)
  exit 1
else
  echo "0 Check_apk" - no packages available for upgrade
fi
EOF

# make these scripts executable
chmod +x /usr/lib/check_mk_agent/local/*

# enable autostart and start inetd
rc-update add inetd
/etc/init.d/inetd start

# status
/etc/init.d/inetd status

else 
    echo "ERROR - Your are not running Alpine Linux!"
fi

Installation

### socat
wget https://codeberg.org/blan/checkmk-agent-alpinelinux/raw/branch/main/install-checkmk-agent-alpinelinux_socat.sh
chmod +x install-checkmk-agent-alpinelinux_socat.sh
# Variablen anpassen!
./install-checkmk-agent-alpinelinux_socat.sh

### inetd
wget https://codeberg.org/blan/checkmk-agent-alpinelinux/raw/branch/main/install-checkmk-agent-alpinelinux_inetd.sh
chmod +x install-checkmk-agent-alpinelinux_inetd.sh
# Variablen anpassen!
./install-checkmk-agent-alpinelinux_inetd.sh

Host hinzufügen

Check Intervall

Lässt sich mit dem Agent nicht anpassen. Als Workaround hole ich mir die Daten über einen cronjob, speichere sie in eine Datei und werte diese dann aus. (cache)
Mein check_os script muss z.B. nicht alle 60 Sekunden die online verfügbare Alpine Version abfragen.

Ja, kann man auch alles in Checkmk in den Parametern für einen Service einstellen.

Check_os

Check_apk

Uninstall

Lässt sich auch alles wieder deinstallieren.

remove-checkmk-agent-alpinelinux_socat.sh

#!/bin/ash
clear
echo
echo removing checkmk-agent
echo
service checkmk_agent stop
rc-update del checkmk_agent
rm /etc/init.d/checkmk_agent
rm -r /etc/check_mk
rm -r /var/lib/check_mk_agent
rm -r /usr/lib/check_mk_agent
rm /usr/local/bin/check_mk_agent
rm /usr/local/bin/waitmax
rm /etc/periodic/hourly/get.alpinelinux.online.version
apk del socat
echo
echo done - reboot the machine

remove-checkmk-agent-alpinelinux_inetd.sh

#!/bin/ash
clear
echo
echo removing checkmk-agent
echo
service inetd stop
rc-update del inetd
rm /etc/init.d/inetd
rm /etc/inetd.conf
rm -r /etc/check_mk
rm -r /var/lib/check_mk_agent
rm -r /usr/lib/check_mk_agent
rm /usr/local/bin/check_mk_agent
rm /usr/local/bin/waitmax
rm /etc/periodic/hourly/get.alpinelinux.online.version
sed -i '/6556/d' /etc/services
apk del busybox-extras
echo
echo done - reboot the machine

Schreibe einen Kommentar