Question

Retrieving Host Groups and Associated Hosts via Centreon API

  • 29 November 2023
  • 2 replies
  • 123 views

Badge +6

"Hello,

I am working on generating KPIs on our Centreon platform (prod and dev) using the Centreon API. My goal is to retrieve the list of host groups with their associated hosts, both enabled and disabled.

I have followed the documentation provided here: link to documentation. However, I am facing difficulties. I am working with Centreon 22.04.7.

Is it possible to achieve this operation with API v1? I have successfully followed the steps in the documentation to generate a token; however, retrieving the list of host groups is not working as expected.

Below is an excerpt of the Python script I used to generate the token,


If you have any advice or guidance on how to obtain the list of host groups with their associated hosts, I would appreciate your assistance.

Thank you,
 


2 replies

Userlevel 5
Badge +11

Hello,

You can do it with APIv1 but you have to make multiple request

  • First you need to authenticate like you did (btw we don’t see your Python script on your post). It will give you a token
  • Then you need to list the hostgroups with a request like this (i will use curl for my request, but you can use them to create your Python script) 
    curl --location 'http://CENTREON_ADDRESS/centreon/api/index.php?action=action&object=centreon_clapi' \
    --header 'Content-Type: application/json' \
    --header 'centreon-auth-token: YOUR_AUTH_TOKEN' \
    --data '{
    "action": "show",
    "object": "HG"
    }'

    You will get a JSON like this with all your hostgroups
     

    {
    "result": [
    {
    "id": "1",
    "name": "Infra",
    "alias": "Infra"
    },
    {
    "id": "2",
    "name": "Dev",
    "alias": "Dev"
    }
    ]
    }

     

  • With this lists, you will need for each hostgroup to make a “getmember” request
    curl --location 'http://CENTREON_ADDRESS/centreon/api/index.php?action=action&object=centreon_clapi' \
    --header 'Content-Type: application/json' \
    --header 'centreon-auth-token: YOUR_AUTH_TOKEN' \
    --data '{
    "action": "getmember",
    "object": "HG",
    "values": "Infra"
    }'

    You will get the list of the member, like this

    {
    "result": [
    {
    "id": "14",
    "name": "HOST1"
    },
    {
    "id": "13",
    "name": "HOST2"
    }
    ]
    }

This is how you can do it with APIv1, but i think you should take a look with APIv2 method, it’s will be more efficient i think

https://docs-api.centreon.com/api/centreon-web/#tag/Host-group/paths/~1monitoring~1hostgroups/get

 

 

Badge +6

Hello @vcoum,

Thank you very much for your response. I'll test all of that, and I'll let you know if it works.

Reply