Add tags/labels based on host name?

Hi,

I’m evaluating CheckMK for our environment. I was wondering about the possibility to automatically apply tags and/or labels based on the host name.

We use FQDN host names and they have a pattern like so: ...domain.com.

I would like to automatically tag or label a host with the environment derived from the FQDN - is this possible?

1 Like

This is the FQDN pattern I tried to express, which the forum omitted:

<name>.<system>.<environment>.domain.com

That is not possible with checkmk means in the GUI.

There is a rule to attach host labels to hosts, but you would have to create N rules for N environments to set N labels, and more or less hardcode the environments, for example:

  • Hosts with name (regex) ^[^.]+\.[^.]+\.env-1\.domain\.com get the label environment:env-1
  • Hosts with name (regex) ^[^.]+\.[^.]+\.env-2\.domain\.com get the label environment:env-2
  • Hosts with name (regex) ^[^.]+\.[^.]+\.env-3\.domain\.com get the label environment:env-3

and so on.

While this possible, it is not really feasable because it does not take new environments into account and is very error prone.

Another possibility would be to use the REST-API and some Python script to automate that process.

If all your hosts are “regular” hosts with the checkmk agent installed (i.e. no SNMP devices), then it might be the easiest to put a file on each host with its own environment label. Just put a file similar to the following in the directory /var/lib/check_mk_agent/spool on the host(s). The actual filename doesn’t matter (as long as it does not start with a number), e.g.

Filename: /var/lib/check_mk_agent/spool/env-labels

Content:

<<<labels:sep(0)>>>
{"environment": "env-1"}

The first line is important and the second line is in JSON format and must be all in one line. If you want to set more labels, do it like so:

<<<labels:sep(0)>>>
{"environment": "env-1", "other_label": "other_value"}

When you then do a discovery for that host, it will get the new label "environment:env-1". Nothing else is required on the server side.

5 Likes

Sweet, thank you for the quick reply!

1 Like

Just tried this on Windows and works after 2nd rescan of that host:
C:\ProgramData\checkmk\agent\spool\discovery-host-labels.txt

image

Cool!

3 Likes

So if i treat all of this right it would be possible to write an agent-plugin to be distributed via the agent-bakery analysing something on the host and create an output using this data simmilar to:

<<<labels:sep(0)>>>
{“labelkey”: “what-ever-the-plugin-found”}

and the host will get the label labelkey:what-ever-the-plugin-found during service discovery.

Am i right so far?

That’s exactly how it works, yes.

You can either put that section in a spool file (for example when the host is rolled out with Ansible or something similar) or you can write an agent plugin that outputs this section.

Depending on how expensive it is to “analyse something on the host” you might want to run this agent plugin only every few hours or so instead of every minute.