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

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.