Files
homelab-scripts/checkmk.sh

113 lines
3.7 KiB
Bash

#!/bin/bash
# make sure we are running as sudo
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
# get OS ditro and version
OS=$(hostnamectl | awk '/Operating System/ {print $3}')
VM=$(hostnamectl | awk '/Virtualization/ {print $2}')
if [ ${OS} == 'Ubuntu' ]; then
OS_VERSION=$(hostnamectl | awk '/Operating System/ {print $4}')
else
OS_VERSION=$(hostnamectl | awk '/Operating System/ {print $5}')
fi
if [ "${VM}" == "vmware" ]; then
VM="true"
else
VM="false"
fi
set -e
# set -x
CHECK_MK_SERVER="gauntcheckmk.home.johnhgaunt.com"
SITE_NAME="gaunt"
API_URL="http://${CHECK_MK_SERVER}/${SITE_NAME}/check_mk/api/1.0"
USERNMAE="automation"
PASSWORD="$1"
AUTHORIZATION_HEADER="Authorization: Bearer ${USERNMAE} ${PASSWORD}"
read -e -p "Enter Hostname: " -i "$(cat /etc/hostname)" HOSTNAME
tmpdir=$(mktemp -d)
if [ -x /usr/bin/dpkg ]; then
tmpfile=${tmpdir}/checkmkagent.deb
wget -O ${tmpfile} --header="${AUTHORIZATION_HEADER}" "${API_URL}/domain-types/agent/actions/download/invoke?os_type=linux_deb"
if dpkg -l check-mk-agent > /dev/null; then
dpkg -P check-mk-agent
fi
apt-get install xinetd curl
dpkg -i ${tmpfile}
# Apt
wget -O /usr/lib/check_mk_agent/plugins/mk_apt http://${CHECK_MK_SERVER}/${SITE_NAME}/check_mk/agents/plugins/mk_apt
chmod +x /usr/lib/check_mk_agent/plugins/mk_apt
elif [ -x /usr/bin/rpm -o -x /bin/rpm ]; then
tmpfile=${tmpdir}/checkmkagent.rpm
wget -O ${tmpfile} --header="${AUTHORIZATION_HEADER}" "${API_URL}/domain-types/agent/actions/download/invoke?os_type=linux_rpm"
if rpm -q check-mk-agent > /dev/null; then
rpm -e check-mk-agent
# RPM
#wget -O /usr/lib/check_mk_agent/plugins/mk_apt http://${CHECK_MK_SERVER}/${SITE_NAME}/check_mk/agents/plugins/mk_apt
#chmod +x /usr/lib/check_mk_agent/plugins/mk_apt
fi
if [ -x /usr/bin/zypper ]; then
zypper install xinetd curl
elif [ -x /usr/bin/yum ]; then
yum install xinetd curl
fi
rpm -i ${tmpfile}
else
echo "Unknown package system"
exit 1
fi
rm -rfv ${tmpdir}
rm -fv /etc/cmk-update-agent.state
curl --header "Content-Type: application/json" \
--header "${AUTHORIZATION_HEADER}" \
--request POST \
--data "{\"host_name\": \"${HOSTNAME}\", \"folder\": \"~servers\"}" \
${API_URL}/domain-types/host_config/collections/all
ETAG=$(curl --header "Content-Type: application/json" \
--header "${AUTHORIZATION_HEADER}" \
--request GET \
-I \
${API_URL}/domain-types/activation_run/collections/pending_changes | \
grep -Fi etag | sed -r 's/.*"(.*)".*/\1/')
curl --header "Content-Type: application/json" \
--header "${AUTHORIZATION_HEADER}" \
--header "If-Match: ${ETAG}" \
--request POST \
${API_URL}/domain-types/activation_run/actions/activate-changes/invoke
# test if not a VM and if so, install the smartctl stuff
if [ "${VM}" == "false" ]; then
# hardware inventory script
wget -O /usr/lib/check_mk_agent/plugins/mk_inventory.linux http://${CHECK_MK_SERVER}/${SITE_NAME}/check_mk/agents/plugins/mk_inventory.linux
chmod +x /usr/lib/check_mk_agent/plugins/mk_inventory.linux
# Smart script for hard drives
wget -O /usr/lib/check_mk_agent/plugins/smart http://${CHECK_MK_SERVER}/${SITE_NAME}/check_mk/agents/plugins/smart
chmod +x /usr/lib/check_mk_agent/plugins/smart
fi
/usr/bin/cmk-agent-ctl register --hostname ${HOSTNAME} \
--server ${CHECK_MK_SERVER} --site ${SITE_NAME} \
--user ${USERNMAE} --password ${PASSWORD} \
--trust-cert
sleep 15s
curl --header "Content-Type: application/json" \
--header "${AUTHORIZATION_HEADER}" \
--request POST \
--data "{\"host_name\": \"${HOSTNAME}\", \"mode\": \"refresh\"}" \
${API_URL}/domain-types/service_discovery_run/actions/start/invoke