REST API Service Discovery for single host

CMK version:
Checkmk Raw Edition 2.2.0p12
OS version:
Ubuntu 22.04 LTS
Error message:
Via the Swagger UI: Failed to fetch.
Via Powershell: {“title”: “Unauthorized”, “status”: 401, “detail”: “Couldn’t log in.”}

Code used

$SERVER_NAME="checkmk.domain.local"
$SITE_NAME="monitoring"
$API_URL="http://$SERVER_NAME/$SITE_NAME/check_mk/api/1.0/domain-types/service_discovery_run/actions/start/invoke";


$HEADER = @{
    "Authorization" = "Bearer $USERNAME $PASSWORD"
    "Accept" = "application/json"
    "Content-Type" = "application/json"
}

$BODY = @{
    "host_name" = $COMPUTERNAME;
    "mode"= "refresh";
} | ConvertTo-Json

Invoke-RestMethod -Uri "$API_URL" -Method Post -Headers $HEADER -Body $BODY

This part works for creating the host via REST API, I’ve only changed the URL and BODY part for the service discovery.
Doing this via curl works just fine.

Hello @sieuwe , first of all welcome to the forum :partying_face:

I see you are using $COMPUTERNAME, however i do not see it defined in the code you posted.
Also if it is filled via other means, does it contain the FQDN ?

I have tested a triggered refresh on/via Postman with the following config:

With payload on the body:

{
    "host_name": "test01.mydomain.tld",
    "mode": "refresh"
}

Result was a 200: OK
Interesting returns from the call:

"domainType": "service_discovery_run",
    "id": "service_discovery-test01.mydomain.tld",
    "title": "Service discovery background job service_discovery-test01.mydomain.tld is running",
    "members": {},
    "extensions": {
        "active": true,
        "state": "running",
        "logs": {
            "result": [],
            "progress": [
                "Starting job..."
            ]
        }
    }

This was performed on a 2.1.0p36 setup.

Now i’m not ‘into’ poweshell, as i have a Linux-only env, so i’m trying to reproduce/learn as i go.

First of all, as soon as i take the last part ( the $BODY -part out of the final call ( in a command-line-only call i dont get issues with unauthorised.

Invoke-RestMethod https://monitor.mydomain.tld/mysite/check_mk/api/1.0/domain-types/service_discovery_run/actions/start/invoke -method Post -Headers @{ "Authorization" = "Bearer automation MySuperSecretPAssword";"Accept" = "application/json";"Content-Type" = "application/json"}

Instead i get(as it should):

Invoke-RestMethod : {"title": "Bad Request", "status": 400, "detail": "These fields have problems: host_name", "fields": {"host_name": ["Missing data for required field."]}}
At line:1 char:1

If i extend the call with the needed/wanted/required payload in the body:

Invoke-RestMethod https://monitormydomain.tld/mysite/check_mk/api/1.0/domain-types/service_discovery_run/actions/start/invoke -method Post -Headers @{ "Authorization" = "Bearer automation MySuperSecretPAssword";"Accept" = "application/json";"Content-Type" = "application/json"} -Body (@{ "host_name" = "test01.mydomain.tld"}|ConvertTo-Json)

i get back a 200:OK
with additional info:

links      : {@{domainType=link; rel=self; href=http://monitor.mydomain.tld/mysite/check_mk/api/1.0/objects/service_discovery/service_discovery-test01.mydomain.tld; method=GET;
             type=application/json}}
domainType : service_discovery
id         : service_discovery-test01.mydomain.tld
title      : Service discovery result of host test01.mydomain.tld
members    :
extensions : @{check_table=; host_labels=; vanished_labels=; changed_labels=}

However as soon as i add the mode to the commandline:

Invoke-RestMethod https://monitor.mydomain.tld/comsolve/check_mk/api/1.0/domain-types/service_discovery_run/actions/start/invoke -method Post -Headers @{ "Authorization" = "Bearer automation MySuperSecretPAssword";"Accept" = "application/json";"Content-Type" = "application/json"} -Body (@{ "host_name"="test01.mydomain.tld";"mode"="refresh" }|ConvertTo-Json)

It will throw me the authentication-error:

Invoke-RestMethod : {"title": "You need to be authenticated to use the REST API.", "status": 401}
At line:1 char:1
+ Invoke-RestMethod https://monitor.mydomain.tld/comsolve/check_mk/api/1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

So somewhere in this breakdown it seems that PowerShell is not playing nicely (if it were my guess)

… out of curiosity, as the default mode behaviour is fix_all … (apart from the fact is should work) why are you only going for a refresh?

  • Glowsome

Hi @Glowsome

$COMPUTERNAME was something I forgot in my post, that’s my bad.
I copied the mode=refresh from the walkthrough on the website.
I removed it from my API call and tried again.
And it worked :grin:
So I’ll go ahead and work with the default fix_all mode.

1 Like