Obtain Service and Host labels from REST API

CheckMK Version: 2.2.0p32 Enterprise Edition

Hello,
I currently have some services and hosts with labels assigned to them, e.g.:

My intent is to retrieve these labels by using the REST API, but I haven’t been able to find any solutions yet.
I don’t see any dedicated endpoint in the documentation, nor I can find it in others.
The only way I’ve found is to recall the ruleset /domain-type/rule/collections/all?ruleset_name=service_label_rules and host_label_rules, but that’s limiting and way more complicated to integrate as I would need to recreate some other association logics compared to just obtaining the label.

In previous versions of CheckMK I would have been able to export a CSV and obtain the labels from the WebAPI, but since its deprecation I don’t know where to look.

Any advice?

I’m not sure if this is what you want but you can get labels from hosts via API:

Open HELP > REST API Documentation in your CheckMK Site

Klick on
GET Show a Host
or
GET Show all Hosts

Response 200 > Response Schema > extensions > atributes > labels.

Kind regards, Marc

Hi Marcs,

I did try that, but it’s not showing into the attributes.
Here’s an example rule:

By using python requests I make the call

uri = f'{API_URL}/objects/host_config/{host}'
headers = {
    'accept': 'application/json',
    'Authorization': f'Bearer {USERNAME} ' + PASSWORD
}
response = requests.get(uri, headers=headers, verify=False, params={"effective_attributes": False})

What I obtain is something that look like this:
image
The only attributes that I get are the alias, meta_data and tags.

Am I missing something?

Oh, I think this will work for Hostlabels.
You first Screeshot shows Service Labels, i didn’t see that before…

In this case maybe you have to query the “GET Show all monitored services…”
But the documentations shows no labels here :confused:

I need to get the labels from both hosts and services, as there’s both host labels and service labels rules ^^
The endpoint you gave me should return the host labels, but it’s not, and I have no idea how I could recall the service labels.

I’m at a dead end :smiley:

So, i tested the API myself:
Created two labels:

One per rule:

One explicit on a host:
grafik

The API Call for the Host:
f"{API_URL}/objects/host_config/HOSTNAME",

Returns only the explicit entered label, not the one from the rule:

But you can query the host label rules:
grafik

and get the labels from the rules:
grafik

This also works with “service_label_rules”

I hope this will help you further :slight_smile:

Hi MarcS,
just a couple things that I want to note:

  • The first method will only return explicit rules of a host, that’s a nono for me
  • The second method is the original “solution” I came up with, but querying the ruleset directly is also a bigger nono for me. I’d have to parse the entire ruleset, adapt the regex to the explicit services, and do additional API calls to retrieve for example the host tag a service belongs to. I’d basically have to rewrite the entire association logic that’s already in CheckMK.

After banging my head to the wall for so long, I’ve found a last resort solution that doesn’t involve using the REST API.

I’m going to send a POST request by recalling the login.py and passing inside the direct link that will export a service/host stats in CSV, that’s where I can easily grab the host/service label and call it a day. The last thing that I have to do is to make a function that will convert the hosts and services names to an encoded URL and pass it inside another function:

def ready_for_url(a):
    a.replace(" ", "%2B")
    return a

def obtain_service_labels(host, service):
    ready_host = ready_for_url(host)
    ready_service = ready_for_url(service)
    csv_url = f"https://checkmk.mysite.com/cmk/check_mk/login.py?_origtarget=%2Fcmk%2Fcheck_mk%2Fview.py%3Fhost%3D{ready_host}%26output_format%3Dcsv_export%26service%3D{ready_service}%26site%3Dcmk%26view_name%3Dservice&_username=API_USER&_password=mysupercoolpassword&_login=1"
    
    with requests.Session() as s:
        download = s.post(csv_url, verify=False)
        content = download.content.decode('utf-8')
        cr = csv.reader(content.splitlines(), delimiter=';')
        print(list(cr)[1][3])

obtain_service_labels("hostname", "servicename")

And the result will look like this:

I wish it were possible to do it with the REST API, but I think this will do for now.
I’m leaving this issue open for the time being, as I’d like to know what the devs think about this situation, and perhaps plan some of those mentioned features in a future release of CheckMK.

Cheers :melting_face:

I think you can request all labels by using livestatus requests (Statusdaten abrufen via Livestatus). You can ask for the column “labels” there. Haven’t tried it mysself, but should work.

At the time I did a workaround to get all labels, by getting them as json from the web gui interface using the dev tools of a browser. (-;

@gstolz suggested a faster solution using live query instead:

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.