Issue on Rest API for creating Host with contactgroups

Hi Everyone,

I’m new to the forum and it’s my first post. :slight_smile: I’m trying to automate stuff for our checkmk apps and its a good thing with v2 rest api is introduced. But, I find it quite troublesome and quite hard to follow. If there could be more example that checkmk team can provide to run these actions. Hehe.

Anyway, hope anyone could help me with my issue. I’m trying out the rest api for checkmk v2 and I got stuck when I tried to add a contactgroup in the mix. It tells me that my entry is not a valid string. Anyone encounter the same issue when creating their hosts? See the code below.

out=$(

curl
–request POST
–write-out “\nxxx-status_code=%{http_code}\n”
–header “Authorization: Bearer autouser $automation_secret”
–header “Accept: application/json”
–header “Content-Type: application/json”
–data ‘{ “attributes”: { “site”: “mastertest”, “contactgroups”: { “groups”: [ “contactgroup1”, “contactgroup2”, “server1-cg” ] }, “tag_os”: “lin” }, “folder”: “/servers/site1”, “host_name”: “server1.example.com” }’
“$uri/domain-types/host_config/collections/all”)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 387 100 161 100 226 1226 1721 --:–:-- --:–:-- --:–:-- 1725

echo $out

{“title”: “Bad Request”, “status”: 400, “detail”: “These fields have problems: attributes”, “fields”: {“attributes”: {“contactgroups”: [“Not a valid string.”]}}} xxx-status_code=400

echo $uri

https://test-monitoring.example.com/mastertest/check_mk/api/1.0

Thanks!

I’m still stuck with this. Anyone has an idea?

I just discovered this bug myself and was able to figure out roughly what’s happening.

When creating/updating hosts via the REST api, the contactgroups attribute is validated incorrectly. It expects a string when it should expect a list of strings.

Things got really weird when I submitted the string “z” in the attributes:

{'contactgroups': 'z',
 'ipaddress': '8.8.8.8',
 'tag_agent': 'no-agent'}

Strings get through validation and are treated as if they are the list/tuple checkmk actually expects. Here’s the exception in the json response:

Traceback (most recent call last):
  File "/omd/sites/DEV/lib/python3/cmk/gui/wsgi/applications/rest_api.py", line 403, in _wsgi_app
    return wsgi_app(environ, start_response)
  File "/omd/sites/DEV/lib/python3/cmk/gui/wsgi/applications/rest_api.py", line 207, in __call__
    wsgi_app = self.func(ParameterDict(path_args))
  File "/omd/sites/DEV/lib/python3/cmk/gui/plugins/openapi/restful_objects/decorators.py", line 520, in _validating_wrapper
    response = self.func(param)
  File "/omd/sites/DEV/lib/python3/cmk/gui/plugins/openapi/endpoints/host_config.py", line 82, in create_host
    body['folder'].create_hosts([(host_name, body['attributes'], None)])
  File "/omd/sites/DEV/lib/python3/cmk/gui/watolib/hosts_and_folders.py", line 1872, in create_hosts
    self.create_validated_hosts(entries, bake_hosts)
  File "/omd/sites/DEV/lib/python3/cmk/gui/watolib/hosts_and_folders.py", line 1881, in create_validated_hosts
    self.save_hosts()
  File "/omd/sites/DEV/lib/python3/cmk/gui/watolib/hosts_and_folders.py", line 845, in save_hosts
    self._save_hosts_file()
  File "/omd/sites/DEV/lib/python3/cmk/gui/watolib/hosts_and_folders.py", line 923, in _save_hosts_file
    cgconfig = convert_cgroups_from_tuple(host.attribute("contactgroups"))
  File "/omd/sites/DEV/lib/python3/cmk/gui/watolib/utils.py", line 77, in convert_cgroups_from_tuple
    "groups": value[1],
IndexError: string index out of range

Oddly enough adding users with contact groups works fine: a list of strings works (ReDoc also says it should be an “array of strings”).

It’s been 2 months and no one from checkmk has even replied back to this ticket… very frustrating.

Have you tried the latest patch release?

BTW: This is a forum post, not a ticket for the devs. You can send them an email via feedback@checkmk.com.

Still happens with the daily master build from today:

Thanks! I still encountering this error when I try to call the api. I test using checkmk raw 2p9 version.

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.