Create host via API automatically

Hello,
I would like to automatically create hosts via the API. Since I never used an API before or automated such things (sorry, I’m a bloody beginner) I’m asking here for help.

My idea was, through my software deployment system I roll out the agent → works

It also registers the agent for TLS via a script, which also works
(“C:\Program Files (x86)\checkmk\service\cmk-agent-ctl.exe” register --hostname %COMPUTERNAME%.DOMAIN.TLD --server HOST.DOMAIN.TLD:8000 --site SITNAME --user automation --password AUTOMATIONPASSWORD)

Now it would be awesome to create the host in a kind of quarantine folder (a bit like in an AD).
So I created a folder in the main folder called “AutomaticHostCreationQuarantine”
(URL: https://HOST.DOMAIN.TLD/SITENAME/check_mk/wato.py?folder=automatic_host_creation_quarantine&mode=folder)

How can I create the host dynamically from the client where the agent is installed?
Basically just create the host in the folder with the hostname (FQDN like at the agent registration above , no attributes like IP etc…)
And I guess that would also be easily adaptable to Linux.

Thanks in advance!
Greetings
Max

Hi Max,

the easiest way would be to use the cloud edition, that includes an agent with auto registration features that does exactly what you want :slight_smile:

Otherwise you can just trigger a powershell script on the monitored host, read its local hostname and create the host via the rest api. Alternatively the software deployment could trigger the script, if you can give it the hostname of the target host.

An example is e.g. this, but there are several examples in this forum.

In the checkmk help (lower left) in the checkmk gui there is the complete API documentation.

This is an example via curl from the API documentation:

#!/bin/bash

HOST_NAME="localhost"
SITE_NAME="yoursite"
PROTO="http" #[http|https]
API_URL="$PROTO://$HOST_NAME/$SITE_NAME/check_mk/api/1.0"

USERNAME="automation"
PASSWORD="password"

curl \
  -G \
  --request POST \
  --write-out "\nxxx-status_code=%{http_code}\n" \
  --header "Authorization: Bearer $USERNAME $PASSWORD" \
  --header "Accept: application/json" \
  --header "Content-Type: application/json" \
  --data '{
          "attributes": {
            "ipaddress": "192.168.0.123"
          },
          "folder": "/",
          "host_name": "example.com"
        }' \
  "$API_URL/domain-types/host_config/collections/all"

And I should mention that this is also possible with the checkmk Ansible Collection that fully automates the whole process.

Thank you for the help.
After doing a lot of scripting the whole day (partially thanks to Copilot) I got it working
It is primarily for my homelab, so I don’t want the cloud version.

I created 8 scripts, 4 for manual setup, 4 for automatic silent install with more variables

1_createcurrenthost.ps1
2_activatependingchanges.ps1
3_setdowntime.ps1
4_register-agent.cmd

→ All scripts are designed to be run from the host in question. There is a variable which reads the FQDN of the host it is run on (e.g. HOST.AD.DOMAIN.CORP for an AD environment)

1 creates the host in Checkmk
MANUAL:
has variables in it which you have to set (Checkmk data, and the folder where host should be created)
AUTOMATIC:
Basically the same, but shows a popup with link to the folder
2 activates the changes and saves the host
(you have to wait a bit, it shows they are applied, but at the moment of the message the y aren't yet !!!)
-> AUTOMATIC closes after finishing. MANUAL stays open and shows message
3 sets downtime for host during setup
MANUAL:
Shows popup for duration of downtime and comment, afterwards shows link to downtimes of host
AUTOMATIC:
Just sets downtime + comment according to the variables (Downtime variable only accepts hours, so at least one hour)
4 TLS registration
-> AUTOMATIC closes after finishing. MANUAL stays open and shows message

Thank you for the help.
Greetings
Max
1_automatic_createcurrenthost.ps1 (2.7 KB)
2_automatic_activatependingchanges.ps1 (2.0 KB)
3_automatic_setdowntime.ps1 (2.0 KB)
4_automatic_register-agent.bat (228 Bytes)
1_manual_createcurrenthost.ps1 (3.9 KB)
2_manual_activatependingchanges.ps1 (2.0 KB)
3_manual_setdowntime.ps1 (7.3 KB)
4_manual_register-agent.bat (373 Bytes)

1 Like

Nice, thanks for sharing

No problem.
Since it was a LOT of work (at least for me…) I thought sharing would be a good idea

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.