116 lines
3.9 KiB
Bash
116 lines
3.9 KiB
Bash
#!/bin/bash
|
|
|
|
# make sure we are running as sudo
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "Please run as root"
|
|
exit
|
|
fi
|
|
|
|
# ask for the domain, username, and password
|
|
read -e -p "Enter Checkmk Server: " -i "gauntcheckmk.home.johnhgaunt.com" CHECK_MK_SERVER
|
|
read -e -p "Enter Checkmk Site Name: " -i "gaunt" SITE_NAME
|
|
read -e -p "Enter Checkmk Username: " -i "automation" USERNAME
|
|
read -e -s -p "Enter ${USERNAME}'s Password: " PASSWORD
|
|
|
|
|
|
# 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" ] || [ "${VM}" == "kvm" ] ; then
|
|
VM="true"
|
|
else
|
|
VM="false"
|
|
fi
|
|
|
|
set -e
|
|
# set -x
|
|
|
|
API_URL="http://${CHECK_MK_SERVER}/${SITE_NAME}/check_mk/api/1.0"
|
|
AUTHORIZATION_HEADER="Authorization: Bearer ${USERNAME} ${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 -y
|
|
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 ${USERNAME} --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
|