Self Service API without Icinga for Windows

Hi.
I would like an automatic registration of windows host without Icinga for Windows installed.
It it possible?

Thanks a lot
Mario

icinga2 - The Icinga 2 network monitoring daemon (version: r2.14.2-1)
Ubuntu 22.04
Enabled features: api checker command graphite icingadb mainlog notification
Icinga Web 2 Version 2.12.1
Git commit cd2daeb2cb8537c633d343a29eb76c54cd2ebbf2
PHP Version 7.4.3-4ubuntu2.23
Git commit date 2023-11-15
Get Icinga Support
Icinga Community
Report a bug
Icinga Documentation
Loaded Libraries
icinga/icinga-php-thirdparty 0.12.1
icinga/icinga-php-library 0.14.1
Loaded Modules
businessprocess 2.5.0
director master
fileshipper 1.2.0
icingadb 1.1.3
graphite 1.2.4
incubator 0.20.0
migrate 2.12.1

You could create a script that automates several steps that are needed to add new hosts like the old and deprecated powershell-modul.

Or you could DevOps tools e.g. ansible, puppet, saltstack to do the same via e.g. playbook. You’ll find many of this approaches in the net e.g. blog or other blog or Telekom.

1 Like

I use the self service API to register Linux hosts so yes, it’s possible.

1 Like

Could I use Powershell client as mentioned here: Icinga2 Api - Icinga 2

combined with attributes for host creation like here: Icinga2 Api - Icinga 2
?

Btw, the above approach doesn’t use Director, I think

The self service API is director exclusive.

https://icinga.com/docs/icinga-director/latest/doc/74-Self-Service-API/#windows-agents

https://icinga.com/docs/icinga-director/latest/doc/70-REST-API/#self-service-api

Here is some of my code to get you started:

# 2. install icinga and plugins

# refresh repos
apt-get update
# make packages installable
apt-mark unhold icinga2
apt-mark unhold linuxfabrik-monitoring-plugins
# install packages
apt-get install \
	curl \
	icinga2=${ICINGA_VERSION}${VERSION_ID} \
	linuxfabrik-monitoring-plugins=${MONITORING_PLUGINS_VERSION} -y
# prevent updates to incompatible versions
apt-mark hold icinga2
apt-mark hold linuxfabrik-monitoring-plugins


# 3. get key from director

# use HOST Template API Key to create host and get self-service Host API Key
HOST_KEY=$(curl --request POST \
  --url $ICINGA_URL'/icingaweb2/director/self-service/register-host?name='$FQDN'&key='$DIRECTOR_KEY \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{
	"display_name": "'"$DISPLAY_NAME"'",
	"address": "'"$IP"'"
}'
	)

# remove surrounding double quotes
HOST_KEY="${HOST_KEY%\"}"
HOST_KEY="${HOST_KEY#\"}"

# if error in HOST_KEY try to find key in /var/lib/icinga2/certs/ticket
if [[ $HOST_KEY == *"error"* ]]; then
    if [ -f "${HOST_KEY_FILE}" ]; then
	HOST_KEY=$(<"${HOST_KEY_FILE}")
    else
	echo -e '\033[0;31mError: could not create or load host key for self service ticket API to get Cert signed by Icinga PKI!'
	echo -e 'Remove host from director or check self service token of director host-template.\033[0m'
	exit 1
    fi
fi

# use Host API Key to get Icinga2 PKI ticket
ICINGA_TICKET=$(curl --request GET \
  --url $ICINGA_URL'/icingaweb2/director/self-service/ticket?key='$HOST_KEY \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
	     )

# remove surrounding double quotes
ICINGA_TICKET="${ICINGA_TICKET%\"}"
ICINGA_TICKET="${ICINGA_TICKET#\"}"

# if error in HOST_KEY try to find key in /var/lib/icinga2/certs/ticket
if [[ $ICINGA_TICKET == *"error"* ]]; then
    if [ -f "${TICKET_FILE}" ]; then
	ICINGA_TICKET=$(<"${TICKET_FILE}")
    else
	echo -e '\033[0;31mError: could not create or load ticket to get Cert signed by Icinga PKI!'
	echo -e 'Remove host from director or check self service token of director host-template.\033[0m'
	exit 1
    fi
fi

# 4. Setup Icinga2 Agent
#make sure the directory /var/lib/icinga2/certs exists and has the correct permissions
install --owner=nagios \
	--group=nagios \
	--mode=700 \
	--directory /var/lib/icinga2/certs

#save host key
echo "$HOST_KEY" > $HOST_KEY_FILE

#save PKI cert from Icinga config master while still
#fd3 will preserve output to stdout while capturing stdout for later checking of fingerprint
{ PKI_OUTPUT=$(icinga2 pki save-cert \
	--host $ICINGA_MASTER1 \
	--port 5665 \
	--key local.key \
	--cert local.crt \
	--trustedcert /var/lib/icinga2/certs/master.crt | tee /dev/fd/3 ); } 3>&1

if [[ $PKI_OUTPUT = *${FINGERPRINT}* ]]; then
    echo "Icinga PKI: fingerprint matched"
else
    echo -e "\033[0;31mError: Icinga PKI fingerprint didn't match! Man in the middle attack?\033[0m"
    exit 1
fi


# execute icinga2 node setup
icinga2 node setup --zone $ICINGA_MASTER1 \
	--endpoint ${ICINGA_MASTER1},${ICINGA_MASTER1},5665 \
	--endpoint ${ICINGA_MASTER2},${ICINGA_MASTER2},5665 \
	--parent_host ${ICINGA_MASTER1},5665 \
	--parent_zone master \
	--cn $FQDN \
	--accept-config \
	--accept-commands \
	--disable-confd  \
	--trustedcert /var/lib/icinga2/certs/master.crt \
	--ticket $ICINGA_TICKET

# enable the icinga2 service
systemctl enable --now icinga2.service
# restart the icinga2 service to ensure new configuration is enabled
systemctl restart icinga2.service

# 5. Install sudoers file for monitoring-plugins
echo "$LINUXFABRIK_SUDOERS" > $LINUXFABRIK_SUDOERS_PATH
chmod 0640 $LINUXFABRIK_SUDOERS_PATH
2 Likes