Add hosts/services to host/service groups via Check_MK HTTP API

I am trying to figure out how to assign a host to a host group and a service to a service group via the Check_MK HTTP API.

I am able to create new host groups and service groups with the API:

  • action=add_hostgroup
  • action=add_servicegroup

Now, I want to be able to add hosts/services to those host/service groups via the API.
With the GUI it’s easy:

  • WATO -> Host & Service Parameters -> Grouping -> Assignment of hosts to host groups / Assignment of services to service groups -> Create a new rule/Edit an existing rule

How can I simulate this behaviour with the Check_MK API ?

I saw that there is an action=set_ruleset, but I wasn’t able to find the exact ruleset names and what to provide to to them (when I checked with action=get_rulesets_info).

Can someone give an example with curl on how to add/edit these rules ?

Check_MK Enterprise Server Version: 1.6.0p8

check this:

ANd you have to use get_ruleset and then set_ruleset, you need the configuration_hash ID its very important

https://checkmk.com/cms_web_api_references.html#get_rulset

1 Like

You can find all possible commands inside
https://checkmk.com/cms_web_api_references.html

and also inside the code in the file

 ~/lib/python/cmk/gui/plugins/webapi/webapi.py

The assignment of hosts to host groups is a normal ruleset.

First define a ruleset manually inside WATO, then use the function “get_rulesets_info” to retrieve all available rulesets and then use the “get_ruleset” for your ruleset you want to modify.

I would try to use a python binding like https://github.com/brennerm/check-mk-web-api/tree/master/check_mk_web_api for such doing. It is easier then the very big curl commands.

3 Likes

I have 1 rule in the “Assignment of hosts to host groups” section.

I can see it if I use action=get_ruleset and request the correct ruleset_name:

curl … &action=get_ruleset" -d ‘request={“ruleset_name”:“host_groups”}’

RESULT

{‘result’: {‘ruleset’: {’’: [{‘condition’: {‘host_tags’: {‘environment’: ‘env_prod’, ‘operating_system’: ‘linux_os’}}, ‘value’: ‘Linux-HG’, ‘options’: {‘description’: u’Linux-Host_Grouping’}}]}, ‘configuration_hash’: ‘311bd86e3517e82349f888aacdf5d241’}, ‘result_code’: 0}

Trying to change the description:

curl … &action=set_ruleset" -d “request={‘ruleset_name’:‘host_groups’,‘ruleset’: {’’: [{‘conditions’: {‘host_tags’: {‘environment’: ‘env_prod’, ‘operating_system’: ‘linux_os’}}, ‘options’: {‘description’: u’test123’}, ‘value’: ‘Linux-HG’, ‘options’: {‘description’: u’Linux-Host_Grouping’}}]},‘configuration_hash’: ‘311bd86e3517e82349f888aacdf5d241’}”

RESULT

{‘result’: u"Check_MK exception: Invalid rule {‘conditions’: {‘host_tags’: {‘environment’: ‘env_prod’, ‘operating_system’: ‘linux_os’}}, ‘options’: {‘description’: u’Linux-Host_Grouping’}, ‘value’: ‘Linux-HG’}", ‘result_code’: 1}

Trying to create a new rule:

curl … &action=set_ruleset" -d “request={‘ruleset_name’:‘host_groups’,‘ruleset’: {’’: [{‘conditions’: {‘host_tags’: {‘environment’: ‘env_test’, ‘operating_system’: ‘linux_os’}}, ‘options’: {‘description’: u’test123’}, ‘value’: ‘Linux-HG’, ‘options’: {‘description’: u’test123_Grouping’}}]}}”

RESULT

{‘result’: u"Check_MK exception: Invalid rule {‘conditions’: {‘host_tags’: {‘environment’: ‘env_test’, ‘operating_system’: ‘linux_os’}}, ‘options’: {‘description’: u’test123_Grouping’}, ‘value’: ‘Linux-HG’}", ‘result_code’: 1}

I tried mutiple combinations and checked if the brackets close correctly. I am not able to find a problem. The syntax is really hard to read/write.

Ok, here are my findings after doing several tests.

Let’s say that we have multiple rules created in “Assignment of hosts to host groups”.

  1. We need to find out the Check_MK internal name of the ruleset:

curl … &action=get_rulesets_info"

If we search through this enormous pile of text we should find the ruleset name. In our case - “host_groups”.

  1. We can get all the currently existing rules in this ruleset in an appropriate form by running:

curl … &action=get_ruleset" -d ‘request={“ruleset_name”:“host_groups”}’

{‘result’: {‘ruleset’: {’’: [{‘condition’: {‘host_name’: [‘serv1’, ‘serv2’, ‘serv3’]}, ‘value’: ‘H2’, ‘options’: {‘description’: u’test3’}}, {‘condition’: {‘host_tags’: {‘environment’: ‘env_prod’, ‘operating_system’: ‘linux_os’}}, ‘value’: ‘Linux-HG’, ‘options’: {‘description’: u’Linux Host Grouping’}}, {‘condition’: {‘host_name’: [‘windows_serv’]}, ‘value’: ‘H1’, ‘options’: {‘description’: u’win’}}]}, ‘configuration_hash’: ‘f0371a8ab4e95cfb42c0fc1e25ff3aba’}, ‘result_code’: 0}

  1. Now we want to create/edit rules. The problem here is that there is no way to differentiate one rule from another. There doesn’t seem to be a rule ID. I am not sure how Check_MK decides if it needs to create a new rule or edit an existing one. Yes {‘condition’: {‘host_name’: [‘serv1’, ‘serv2’, ‘serv3’]}, ‘value’: ‘H2’, ‘options’: {‘description’: u’test3’}} represents 1 rule, but there is no rule name, no rule ID, nothing.

Let’s create a new rule. We copy and paste all the conditions from above and add another condition which represents a new rule:

curl … &action=set_ruleset" -d “request={‘ruleset_name’:‘host_groups’,‘ruleset’: {’’: [{‘condition’: {‘host_name’: [‘serv1’, ‘serv2’, ‘serv3’]}, ‘value’: ‘H2’, ‘options’: {‘description’: u’test3’}}, {‘condition’: {‘host_tags’: {‘environment’: ‘env_prod’, ‘operating_system’: ‘linux_os’}}, ‘value’: ‘Linux-HG’, ‘options’: {‘description’: u’Linux Host Grouping’}}, {‘condition’: {‘host_name’: [‘windows_serv’]}, ‘value’: ‘H1’, ‘options’: {‘description’: u’win’}}, {‘condition’: {‘host_tags’: {‘environment’: ‘env_test’, ‘operating_system’: ‘linux_os’}}, ‘value’: ‘Linux-TEST-HG’, ‘options’: {‘description’: u’Linux Test Hosts’}}]}}”

Conclusion:

  • I am not sure if there is a more elegant way of doing this at the moment.
  • You need to copy and paste ALL conditions that you get with get_ruleset or you will accidentally delete every single one of your rules in the ruleset. I didn’t find a way to “Just create a new rule”. Specifying/Not specifying configuration_hash doesn’t do anything - Rules are always deleted.
  • As a consequence of what was said previously, if you have 500 rules your curl will be a kilometer long and if you do some kind of mistake or an error occurs somehow there is a chance that all your existing rules will get deleted.

This is the reason why i said - please use a language binding like the Python API from https://github.com/brennerm/check-mk-web-api

If you want to use PHP there is also something available https://github.com/bheisig/checkmkwebapi (set_ruleset is missing there at the moment)

What you get back from the get_ruleet is nothing else than a Python dictionary.
You can modify or search the dictionary for conditions and add your own entries. At the end you use the API functions to write the modified dictionary back to the system.

I would say it is not recommended to have so many rules inside one ruleset. The system will get very slow at activation time and if you search for rules inside WATO.

If i look at my bigger systems then the maximum i have is around 100-200 rules in one set and this some active check like HTTP.

2 Likes

Okay,

Thanks for sharing this information Andreas. I will give the python programs a try since it’s more integrated with what Check_MK returns.

Thanks to neeloj as well :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.