Hostname translation for piggyback

Hi
I want to create a “Hostname translation for piggyback” for my Docker Containers, that are created automatically.
Can I create a generic rule or do I need to create one for each host.
I read something about creating a rule like this, but not getting it to work.
Also, I would have preferred if I in replacement could use a dynamic hostname

I have created a script that adds a rule to the piggyback translation ruleset for each Docker node:

You can install it via the “helper” MKP found here: check_mk_extensions/helper at cmk2.3 · HeinleinSupport/check_mk_extensions · GitHub and the additional REST API wrapper found here: check_mk_extensions/check_mk_api at cmk2.3 · HeinleinSupport/check_mk_extensions · GitHub

1 Like

So if I understand this correct, I install this Extension package and this will run in the background and create a new “Hostname translation for piggybacked hosts” for every host that have docker installed?
Or how do I understand this?

It installs the script to $OMD_ROOT/local/bin.

You can then run it manually or via a cronjob.

Im total new to checkmk and also most of linux, so sorry to ask again.

I have installed the package, but not enabled it.

When I Enable it, it will not execute any thing?
When enabled, I will get the script in bin folder?

I then execute the script container_piggyback_translation.py, just not sure how?
Also is there a readme file for the other scripts, so I can understand if they can help with other things?

If you are in the EU or Switzerland we can provide you with professional services regarding Checkmk and this extension.

Hi,

another way to get the piggyback-name with your necessary informations would be a small change to the mk_docker.py Plugin:

Add the following to Line 278:

        elif config["container_id"] == "combined":
            self.all_containers = {
                f'{os.uname()[1].split(".")[0]}_{c.attrs["Name"].lstrip("/")}': c for c in all_containers
            }

so the whole __init__ of MKDockerClient looks like this:

__init__
    def __init__(self, config):
        super().__init__(config["base_url"], version=MKDockerClient.API_VERSION)
        all_containers = _robust_inspect(self, "containers")
        if config["container_id"] == "name":
            self.all_containers = {c.attrs["Name"].lstrip("/"): c for c in all_containers}
        elif config["container_id"] == "long":
            self.all_containers = {c.attrs["Id"]: c for c in all_containers}
        elif config["container_id"] == "combined":
            self.all_containers = {
                f'{os.uname()[1].split(".")[0]}_{c.attrs["Name"].lstrip("/")}': c for c in all_containers
            }
        else:
            self.all_containers = {c.attrs["Id"][:12]: c for c in all_containers}
        self._env = {"REMOTE": os.getenv("REMOTE", "")}
        self._container_stats = {}
        self._device_map = None
        self.node_info = self.info()

        self._df_caller = ParallelDfCall(call=super().df)

Afterwards you can use the the change in the docker.cfg with

[DOCKER]
container_id: combined

The piggyback-Host is now <name_of_dockerhost>_<container_name>.
If you want any other formatting, you can of course change it in the elif-block.

This way you don’t need any cronjobs or renaming rules.

Cheers Jeronimo

3 Likes

If I do this, will I lose the change on every upgrade or how will this work?

The mk_docker.py is a plugin that runs on the docker host.
So unless you’re using the agent bakery, you don’t have to touch it after the initial rollout and it will be unaffected by Checkmk Updates.

That is the implementation of this idea:
https://ideas.checkmk.com/suggestions/532086/include-hostname-in-container-name

I use bakery, so will update. But thanks for the idea.

Thanks for this, have voted on it.
Would make it extremely easy for me to not do anything extra, but just config in checkmk for all my docker and containers to automatic deploy.

I added this small change as a pull request, so maybe it gets mainlined in the future:

If you have any comments or improvements, make sure to drop them in there as well.

Cheers Jeronimo

3 Likes

This is much needed. Currently I use the container “name” option in the bakery rather than the “short” name so I can actually tell what container is what.

I set all my container names are either of the form “app” or app-host" depending on whether I have the same sort of container on different Docker hosts. For example all my docker hosts nave an Nginx proxy manager and so I set container names to be like “nginx-server001”, “nginx-server002” (“container_name: nginx-server001” in the compose.yml). I only have one container for UniFi controller, so that one is just named “unifi”.

I’d love it if I didn’t have to change container names for uniqueness, and each container could show up in CheckMK automatically and without clashing by just having CheckMK append the host on each one.

What happened to the pull request? It is closed but does not contain a hint why.

There is a new one Added 'combined' to container_id options by jwiederh · Pull Request #793 · Checkmk/checkmk · GitHub
But this is stale at the moment as there are some minor formatting issues.
@jwiederh if you can fix these then it is possible that it goes further.

Hi, thanks for the heads up, the formatting difference has been fixed and all checks have passed now.

1 Like