How to list all assigned labels

This is actually a follow up question to my previous topic How to label multiple users to a host, but it could also stand on it’s own hence new topic. Not sure if desirable or not.

Using 2.0.0p16 (CRE), assigning labels manually to hosts at the moment, but might do so dynamically and / or to services as well in the future. Till made dynamically, labels are made manually on the fly, typos are possible, so duplicate labels might form.

To keep labels organised and in same syntax, is there a page or view that lists all available or assigned labels to hosts or services?

I would think there should be one, since the filter does an autocomplete on available labels.

You can add the column “Host Label” to a host view to see all labels assigned to hosts.

I had already done that, but that doesn’t pop out any typo duplicates or syntax goof ups.

I was hoping for a big list of unique labels sorted alphabetical. With standard functionality of a link on each label to filtered hosts or services using specific label, so you can edit them at specific host or service configuration.

If there isn’t such a place, then thinking outside of the box… :thinking:

Maybe inspect what sources the autocomplete in the filter uses, perhaps this information is stored somewhere in OMD.
Or alternatively I can go for an all host view and try to alter the view with user styles so only the labels remain.

Options to investigate tomorrow.

Did a bit of investigation and the best workaround I found so far was to use the inspect tool inside the browser (using Firefox 91 ESR) to get Host label JSON response visible.

  • Go to any host view you want the labels visible as JSON

  • Filter results, add filter Host labels

  • Inspect the empty field Host labels
    image

  • Developer tools, tab Network, clear results if needed

  • In Host labels field, type a character and directly backspace to remove it. This will search for all labels, instead of a wildcard search of text you typed.

  • In Developer tools click the latest entry of ajax_autocomplete_labels.py?_ajaxid=

  • In Response tab, you get your list of unique host labels, copy all JSON data

{
	"result_code": 0,
	"result": [
		{
			"value": "cmk/os_family:linux"
		},
		{
			"value": "cmk/device_type:switch"
		},
		{
			"value": "user/username1:yes"
		},
		{
			"value": "user/username2:yes"
		},
		{
			"value": "user/username3:true"
		},
		{
			"value": "user/username4:yes"
		},
		{
			"value": "cmk/check_mk_server:yes"
		}
	],
	"severity": "success"
}
  • Process JSON to get your overview, find typos or syntax goof ups.
    Example user/username3:true doesn’t follow the syntax of the other user labels.
  • Use previous host view with filter on Host labels to find the hosts with the messed up labels by editing them in the host configuration.

Similar process can be done for Service labels too. And Chrome will probably work too to inspect it.

3 Likes

Marked my workaround as solution until something better comes around. (-:

1 Like

the following works (if you are not using distributed monitoring)

lq "GET hosts\nColumns: labels" | tr , '\n' | sort -u
3 Likes

Been out of the running for quite a while. Only did self health monitoring and failing most of the time. :wink:

Where does one use your command, is this linux CLI or API code?

linux cli :slight_smile: executed as the checkmk/omd site user

1 Like

I just had to look a little bit longer before replying, just found this:

Your solution is faster and simpler than mine, so marking it as solution instead.

1 Like