How to Report number of hosts per checkmk site

Hello,

I hope my request finds you all well.
I am running checkmk raw edition 2.2.0p5 with one master and multiple instances (distributed monitoring).
each cmk instance may have one or many sites.
monitored hosts are located in different regions, grouped by environment.
So to monitor hosts located in regionA in dev environment we have a pool of connections(sites) with the following
naming convention:

regionA_dev_1
regionA_dev_2

regionA_dev_n

we want to use ansible and cmk rest API to automatically add an agent to the monitoring system.
To ensure a better distribution of load between sites in the same region and the same environment,
we need to scrub the number of hosts per site in the same connection pool in order to always provision the least crowded site.
I didn’t found a way in checkmk to get this figures.
Any help is much appreciated.

checkmk edition: raw
checkmk version: 2.2.0p5

cheers
Alfred

Hi Alfred,
not sure, if the checkmk raw edition provides the same information, but setup > licensing in the free and cloud edition shows all sites and the number of hosts and services

See this page in the manual

Hope this helps

Tom

The raw edition will not give these licensing infos, as there is no license limit.

However, since 2.1 all sites will provide a “Site <name> statistics” service if you monitor the underlying linux with a check_mk_agent.
The performance data of that check will tell you how many hosts are on that site:

cmk_hosts_up=139;;;; cmk_hosts_down=1;;;; cmk_hosts_unreachable=0;;;; cmk_hosts_in_downtime=0;;;; cmk_services_ok=4715;;;; cmk_services_in_downtime=0;;;; cmk_services_on_down_hosts=44;;;; cmk_services_warning=38;;;; cmk_services_unknown=35;;;; cmk_services_critical=68;;;;

however, that will only be sufficient if all hosts (or at least most) create the same load in monitoring, if you use a lot of different special agents / snmp access to devices, it would be better to also look into CPU/Memory resources and the “OMD <name> Performance” service.

Tank you gstolz for your reply.
but as i’m new to checkmk, may you please provide a sample command to retrieve site name statistics ?

I do not know if this is exactly what you are after, but if you purely want to get the number of hosts known in your setup then:

I’ve been playing around with the API, and in retrieving info (i use Postman), i did the below call:

https://{{HOST_NAME}}/{{SITE_NAME}}/{{API_URL}}/domain-types/host_config/collections/all

Adjacent to the info that was returned i added this to the ‘test’-tab:

var body = JSON.parse(responseBody);
  tests["Count: "  + body.value.length] = true;

The result is i get on the ‘test’-pane the exact number of hosts on/in my setup.

With the right call in Ansible (i assume you are using ansible.builtin.uri) you should be able to register the outcome and achieve the same.

Now as i do not have a multisite-setup i can not determine if this on a per-connection site, but maybe you can test it to see if it does.

i made an example which will reflect the correct number of hosts via an API call in Ansible:

---
- name: URI test
  hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Make a Native REST call using URI to get information from CheckMK
      ansible.builtin.uri:
        url: https://your_CheckMK-Host/your_site/check_mk/api/1.0/domain-types/host_config/collections/all
        validate_certs: false
        headers:
          Content-Type: "application/json"
          Accept: "*/*"
          Authorization: "Bearer automation your_SuperSecretPassword"
        method: GET
        return_content: true
      register: response

    - debug:
        var: response | json_query('json.value[*].id') | length

The outcome :

ok: [localhost] => {
    "response | json_query('json.value[*].id') | length": "65"
}

The number at Length is the number of hosts defined (in my case its 65)

  • Glowsome

I tested setting up a distributed way, and i noticed that if you were to do that an additional host ( host counter+1) is created. (in my case i went from 65 → 66 hosts registered)
This is i guess a consequence of having a standalone test-site monitored by the standalone main site.

As i have not yet examined the output of the api-call i have used in my test i am still unsure as to filtering this ‘duplicate’ out on the main site.

As the (distributed-server-)host showed up in a different section(not seen before) when looking at all hosts i do think there is a distinct feature where i should be able to filter on, and thus exclude it.

  • Glowsome

Update,
Even after establishing a /the distributed connection (details as to how below)

When using the exact same playbook as shown above the number of hosts ( even tho in the UI its +1 ) has not increased.

So i am curious (still have to test) what the outcome will be when i query the 2nd site regarding host-count.

  • Glowsome

if it can help someone else, as i was using distributed monitoring, i solve the problem by retrieving hostlist for each site on the monitoring host:

su -c 'cmk -l' -l {{ item.site }}

and after ,i used 'wc -l ’ command to get the number of monitored hosts for a given site

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.