diff --git a/checkmk.sh b/checkmk.sh index 08c15ff..68c5da0 100644 --- a/checkmk.sh +++ b/checkmk.sh @@ -24,25 +24,27 @@ fi set -e # set -x -url="gauntcheckmk.home.johnhgaunt.com/Gaunt" -user="automation" -pass="$1" -header="Authorization: Bearer ${user} ${pass}" -read -e -p "Enter Hostname: " -i "$(cat /etc/hostname)" host +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="${header}" "${url}/check_mk/api/1.0/domain-types/agent/actions/download/invoke?os_type=linux_deb" + 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 + dpkg -i ${tmpfile} elif [ -x /usr/bin/rpm -o -x /bin/rpm ]; then - 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" + 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 fi @@ -51,69 +53,52 @@ elif [ -x /usr/bin/rpm -o -x /bin/rpm ]; then elif [ -x /usr/bin/yum ]; then yum install xinetd curl fi - rpm -i $tmpfile + rpm -i ${tmpfile} else echo "Unknown package system" exit 1 fi -rm -rfv $tmpdir +rm -rfv ${tmpdir} rm -fv /etc/cmk-update-agent.state -echo -read -p "Agent installed" +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 -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 if [ "${VM}" == "false" ]; then # 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 - chmod +x /usr/lib/check_agent/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_mk_agent/plugins/mk_inventory.linux # Smart script for hard drives - wget -O /usr/lib/check_agent/plugins/smart https://checkmk.johnhgaunt.com/Gaunt/check_mk/agents/plugins/smart - chmod +x /usr/lib/check_agent/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_mk_agent/plugins/smart fi -echo -read -p "Changes activated" - -/usr/bin/cmk-agent-ctl register --hostname $host \ - --server gauntcheckmk.home.johnhgaunt.com --site Gaunt \ - --user $user --password $pass \ +/usr/bin/cmk-agent-ctl register --hostname ${HOSTNAME} \ + --server ${CHECK_MK_SERVER} --site ${SITE_NAME} \ + --user ${USERNMAE} --password ${PASSWORD} \ --trust-cert -echo -read -p "Agent registered" - -#/usr/bin/cmk-update-agent register -v -U $user -S $pass -H $host - -#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" +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