REST API Service Creation

Hello,

First of all im pretty new, im working as a Student in a Company and Icinga is my current Project.
I had the following Problem as i cannot create a Service via the REST API, a host can be created but a service isnt working.

As creation of an host i make a post call to

http://10.3.0.233/icingaweb2/director/host

with

curl -i -X POST \
   -H "Accept:application/json" \
   -H "Authorization:Basic Hash=" \
   -H "Content-Type:application/json" \
   -d \
'{
  "address" : "127.0.0.1",
  "object_name" : "apitest",
  "object_type" : "object",
  "vars" : {"location" : "Berlin"},
  "display_name" : "apitest",
  "groups" : ["Switche"],
  "imports" : ["standard-host"]
}' \
 'http://10.3.0.233/icingaweb2/director/host'

I tried to create a Service at

http://10.3.0.233/icingaweb2/director/services

with

curl -i -X POST \
   -H "Accept:application/json" \
   -H "Authorization:Basic Hash=" \
   -H "Content-Type:application/json" \
   -d \
'{
  "display_name": "TestService",
  "host_name" : "apitest",
  "vars" : {"testvar" : "1", "testvar" : "2"},
  "check_command" : "Ping"
}' \
 'http://10.3.0.233/icingaweb2/director/services'

But after everything i tried i get the Response 200 everytime, and only a list of already present Services.
Thanks for the help. :sweat_smile:

Icinga Web 2 Version 2.10.1
PHP Version 7.4.3
Director Version - Master (i didnt find any other version number but i installed 2 months ago so i think 1.9.1)
Icinga2 Version : r2.13.3-1
Ubuntu : Platform version: 20.04.4 LTS (Focal Fossa)
Apache : Apache/2.4.41

try to post it to '/icingaweb2/director/services?host=apitest
with this json:

'{
  "host": "apitest",
  "display_name": "someOtherNameForTestService",
  "object_name" : "TestService",
  "vars" : {"testvar" : "1", "testvar" : "2"},
    "imports": [
        "your service template that uses check ping",
    ],
    "object_type": "object",
}'

Same problem there:

{
  "host": "apitest",
  "display_name": "someOtherNameForTestService",
  "object_name" : "TestService",
  "vars" : {"testvar" : "1", "testvar" : "2"},
    "imports": [
        "PING-SNMP",
    ],
    "object_type": "object",
}

as JSON

and the answer was

{
"objects":[]
}

which director version do you use?

please post the exact curl command without login and password obfuscated

I think 1.9.1

but i solved it, in github there is a new documentation of examples.

https://github.com/Icinga/icingaweb2-module-director/pull/2097/files/d4a3e608656de8e11f686c43b8aef406cba15ef1#diff-e084696320f69cb5cbcd658fba28f8e51f80b876c70f82b7e8495b59f2519b43

that helped alot.

the curl that worked is (For an Apply Service) (the %22 is equivalent to ")

./director_curl.sh POST director/service \
'{
    "object_name": "d_ssh",
    "object_type": "apply",
    "imports": ["service_5m"],
    "check_command": "ssh",
    "assign_filter": "host.vars.os=%22Linux%22"
}'

If it is a service, I wouldn’t change “check_interval”,“retry_interval” because these settings are in your template and should’t be changed via api because you can’t set them in gui for the services individually.

in the first post you want to create a service, now you want to create a service template.

Yeah sorry, i had too many thoughts at once, at the moment i cannot create a service via Rest, im sure it works but even in the new Documentation there is no Example for this.
Until i know more i work with the Apply service, its basically the same for me.

I got a single service to work :sweat_smile:

./director_curl.sh POST director/service \
'{
    "object_name": "d_ssh",
    "object_type": "object",
	"host" : "testhost"
    "imports": ["service_5m"],
    "check_command": "ssh"
	}'

Thanks for your help ^^
2 Likes