CheckMK Ansible Module for "HTTP Service Checks"

Hello,
I have been looking over the Ansible Collection for CheckMK, but wasn’t able to find modules regarding rules for HTTP Service Checks.
Is currently CheckMK automation supporting this ?

Kind Regards

Hi,

Ansible can’t do that yet but this is exactly an Example I documented here how to automate that:
https://cmdbsyncer.readthedocs.io/en/latest/checkmk/recipe_multiple_http_rules/

If you want to build something for that, you will find the requests here in the source:

Line 77 the code starts, Line 146 the API Requests

Also you can check the swagger documentation found in Checkmk

1 Like

Hi,
Im looking in the Swagger but I cannot find the “HTTP Service Rules”, is it possible that there is no REST API for it ?

Hi,

AFAIK there is no special API-Endpoint to create a HTTP Service Check. But there is a gernal Endpoint for creating a rule using a given ruleset.
The name of the ruleset would be active_checks:http

You can use this Endpoit to create Rules to apply HTTP Checks.
For this Endpoint there even is a Module in the Ansible Collection.

Hi, Thanks for reply @masamon,
I have tried this module but it seems it creates a General Rule but not HTTP Service Rule, as there is no reference for HTTP Service Rule, I’m not sure how should I structure parameters

Maybe there is other way how I could implement this :
This is what I’m trying to automate as I have many different URI’s that i need to fetch to check if service is okay

I need do BASIC AUTH / CERT which is added with HTTP Service Check to reach my service endpoints, and then I set a condition if in response there is specific string “Healthy” its all good

As Currently I have few endpoints like:
https://example.com/api/v1/service1/status
https://example.com/api/v1/service2/status
https://example.com/api/v1/service3/status
https://example.com/api/v1/service4/status
… service 50
All these endpoints I’m currently manually adding inside HTTP SERVICE Rule 1 by 1 , but I wonder if there could be other way to automate it.

Hi @ronald_sp,

the example from module does not create an http service Check as it uses the ruleset: “checkgroup_parameters:memory_percentage_used”
Check the ruleset attribute in your code :slight_smile:

To create a http Service Check you need to use the ruleset “active_checks:http”, change that in your code.
Furthermore you need to set the conditions and properties as needed - just like you would in the GUI.
And most important the value_raw, those values define the rule. The easiest way to get them is to create an example rule which fits your needs and export the value_raw in the GUI:
image
Use that example to build a general value_raw for your http Checks.

In the end your module args should look something like that including the copied value_raw from the GUI:

"module_args": {
      "server_url": "https://<Your-checkmk-Server>",
      "site": "<Your-Site>",
      "automation_user": "<USER>",
      "automation_secret": "<SECRET>",
      "ruleset": "active_checks:http",
      "rule": {
        "conditions": {
          "host_labels": [],
          "host_name": {
            "match_on": [
              "<Piggyback-host>"
            ],
            "operator": "one_of"
          },
          "host_tags": [],
          "service_labels": []
        },
        "properties": {
          "comment": "automation: created new rule check_http by ansible",
          "description": "New http Check",
          "disabled": false
        },
        "value_raw": "{'host': {'address': 'example.com'}, 'mode': ('url', {'method': 'GET', 'onredirect': 'follow', 'ssl': 'auto', 'uri': 'api/v1/service1/status'}), 'name': 'Example'}",
        "location": {
          "folder": "/",
          "position": "top",
          "rule_id": null
        },
        "folder": "/"
      },
      "state": "present",
      "validate_certs": true
    }
  }

Unfortunately i can not provide the complete ansible module call for your, as we are using roles including the rule module.

Thanks @masamon !!,
I was able to setup it like this It is working and creating HTTP service Rules.
However each time I run the play it will create 4 new HTTP service it wont skip it , and if I try to put state : absent it will say “Rule does not exist” , any ideas what I might change, to delete service I just created and also not create new ones if they already exists.

Usually the modul would only create a new rule, if the a rule with matching conditions, folder, value_raw and enabled/disabled does not already exist - as you also expect.
I tested it and it works as described.

Also deleting a rule should work if you only change the state :thinking:

Are you sure the conditions, folder, value_raw and enabled/disabled are allways the same?
If thats the case, what Version of the Ansible collection are you using? Maybe that could give a hint.
Otherwise I am then unfortunately also somewhat out of the question :frowning_face:

1 Like

@masamon thank you for the help, I have found the solution.
In my jinja rule template I have left empty “Properties : {}”, turns out I have to set at least some value like for module to find the correct rule:
“properties”: {
** “comment”: “automation: created new rule check_http by ansible”,**
** “description”: “New http Check”,**
** “disabled”: false**
** }**

1 Like

@masamon
I might have one more question, sorry to bother you again.
Do you think there is also a module which I might use to create K8s rule

@ronald_sp no problem :slight_smile:

The Ansilbe Module “rule” can create a rule of any type. You just need to know the name of the ruleset.
In this case special_agents:kube and the value_raw, which can be generated in the GUI.

I’m using the GUI urls to find the name of the Ruleset.
For Kubernetes Rule the uri is something like:
wato.py%3Fmode%3Dedit_ruleset%26varname%3Dspecial_agents%253Akube
The important Part is behind varname: special_agents%253Akube.
If you decode the String you get the ruleset name: special_agents:kube

Maybe someone else knows a way to get all the ruleset names in a easier way :slight_smile:

A colleague made me aware of the fact, that the ruleset name is show in every Rule under the “Rule Properties” - press “show more” if needed:

With that rulename an the Ansbile Module you can create every rule type.

1 Like

Thanks @masamon I wasn’t aware of it , this helps a lot ! :slight_smile: