updated and tested script

This commit is contained in:
2024-01-15 16:38:13 -05:00
parent 2995fc413c
commit 20aa41dbc5

View File

@@ -24,25 +24,27 @@ fi
set -e set -e
# set -x # set -x
url="gauntcheckmk.home.johnhgaunt.com/Gaunt" CHECK_MK_SERVER="gauntcheckmk.home.johnhgaunt.com"
user="automation" SITE_NAME="Gaunt"
pass="$1" API_URL="http://${CHECK_MK_SERVER}/${SITE_NAME}/check_mk/api/1.0"
header="Authorization: Bearer ${user} ${pass}" USERNMAE="automation"
read -e -p "Enter Hostname: " -i "$(cat /etc/hostname)" host PASSWORD="$1"
AUTHORIZATION_HEADER="Authorization: Bearer ${USERNMAE} ${PASSWORD}"
read -e -p "Enter Hostname: " -i "$(cat /etc/hostname)" HOSTNAME
tmpdir=$(mktemp -d) tmpdir=$(mktemp -d)
if [ -x /usr/bin/dpkg ]; then if [ -x /usr/bin/dpkg ]; then
tmpfile=$tmpdir/checkmkagent.deb tmpfile=${tmpdir}/checkmkagent.deb
wget -O $tmpfile --header="${header}" "${url}/check_mk/api/1.0/domain-types/agent/actions/download/invoke?os_type=linux_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 if dpkg -l check-mk-agent > /dev/null; then
dpkg -P check-mk-agent dpkg -P check-mk-agent
fi fi
apt-get install xinetd curl apt-get install xinetd curl
dpkg -i $tmpfile dpkg -i ${tmpfile}
elif [ -x /usr/bin/rpm -o -x /bin/rpm ]; then elif [ -x /usr/bin/rpm -o -x /bin/rpm ]; then
tmpfile=$tmpdir/checkmkagent.rpm tmpfile=${tmpdir}/checkmkagent.rpm
wget -O $tmpfile --header="${header}" "${url}/check_mk/api/1.0/domain-types/agent/actions/download/invoke?os_type=linux_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 if rpm -q check-mk-agent > /dev/null; then
rpm -e check-mk-agent rpm -e check-mk-agent
fi fi
@@ -51,69 +53,52 @@ elif [ -x /usr/bin/rpm -o -x /bin/rpm ]; then
elif [ -x /usr/bin/yum ]; then elif [ -x /usr/bin/yum ]; then
yum install xinetd curl yum install xinetd curl
fi fi
rpm -i $tmpfile rpm -i ${tmpfile}
else else
echo "Unknown package system" echo "Unknown package system"
exit 1 exit 1
fi fi
rm -rfv $tmpdir rm -rfv ${tmpdir}
rm -fv /etc/cmk-update-agent.state rm -fv /etc/cmk-update-agent.state
echo curl --header "Content-Type: application/json" \
read -p "Agent installed" --header "${AUTHORIZATION_HEADER}" \
--request POST \
--data "{\"host_name\": \"${HOSTNAME}\", \"folder\": \"~servers\"}" \
${API_URL}/domain-types/host_config/collections/all
wget --header="${header}" --header="Content-Type: application/json" "${url}/check_mk/api/1.0/domain-types/host_config/collections/all" --post-data="{\"host_name\": \"$host\", \"folder\": \"Servers\"}" 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 -v "${url}/check_mk/webapi.py?action=add_host&_username=$user&_secret=$pass" -d "request={\"hostname\": \"$host\", \"folder\": \"Servers\"}" 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
echo
read -p "Host created"
curl -v "${url}/check_mk/webapi.py?action=activate_changes&_username=$user&_secret=$pass"
# test if not a VM and if so, install the smartctl stuff # test if not a VM and if so, install the smartctl stuff
if [ "${VM}" == "false" ]; then if [ "${VM}" == "false" ]; then
# hardware inventory script # hardware inventory script
wget -O /usr/lib/check_agent/plugins/mk_inventory.linux https://checkmk.johnhgaunt.com/Gaunt/check_mk/agents/plugins/mk_inventory.linux 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_agent/plugins/mk_inventory.linux chmod +x /usr/lib/check_mk_agent/plugins/mk_inventory.linux
# Smart script for hard drives # Smart script for hard drives
wget -O /usr/lib/check_agent/plugins/smart https://checkmk.johnhgaunt.com/Gaunt/check_mk/agents/plugins/smart 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_agent/plugins/smart chmod +x /usr/lib/check_mk_agent/plugins/smart
fi fi
echo /usr/bin/cmk-agent-ctl register --hostname ${HOSTNAME} \
read -p "Changes activated" --server ${CHECK_MK_SERVER} --site ${SITE_NAME} \
--user ${USERNMAE} --password ${PASSWORD} \
/usr/bin/cmk-agent-ctl register --hostname $host \
--server gauntcheckmk.home.johnhgaunt.com --site Gaunt \
--user $user --password $pass \
--trust-cert --trust-cert
echo curl --header "Content-Type: application/json" \
read -p "Agent registered" --header "${AUTHORIZATION_HEADER}" \
--request POST \
#/usr/bin/cmk-update-agent register -v -U $user -S $pass -H $host --data "{\"host_name\": \"${HOSTNAME}\", \"mode\": \"refresh\"}" \
${API_URL}/domain-types/service_discovery_run/actions/start/invoke
#echo
#read -p "Agent Updater registered"
#curl -v "$url/check_mk/webapi.py?action=bake_agents&_username=$user&_secret=$pass"
#echo
#read -p "Agents baked"
#/usr/bin/cmk-update-agent -v
#echo
#read -p "Agent updated"
curl -v "$url/check_mk/webapi.py?action=discover_services&_username=$user&_secret=$pass" -d "request={\"hostname\": \"$host\"}"
echo
read -p "Host services discovered"
curl -v "$url/check_mk/webapi.py?action=activate_changes&_username=$user&_secret=$pass"
echo
read -p "Changes activated"