Command path different between machines

On Icinga2 v2.12.3, I’m writing a CheckCommand for a plugin that I want to execute on a number of machines. The problem is that they run different distros, and the plugin I want lives in a distro-specific place: PluginDir on some and LocalPluginDir on others.

I started with something like this:

Object CheckCommand "eth" {
    if (vars.distro == "distro1") {
        command = [ PluginDir + "/check_eth" ]
    } else {
        command = [ LocalPluginDir + "/check_eth" ]
    }

That didn’t work, I guess because vars.distro wasn’t being evaluated on a per-host basis. I then wasted a couple of hours trying various things, without success. I finally succeeded with a truly ugly shell-based hack:

    command = [ "sh", "-c", "if [ -x " + LocalPluginDir + "/check_eth ]; then " + LocalPluginDir + "/check_eth -b $$*; else " + PluginDir + "/check_eth -b $$*; fi" ]

That has two problems. The first is that it’s just horrible. The second is the “-b” after check_eth; for some reason the first argument in the (sorted) argument list isn’t getting passed. I think that’s probably a bug; I verified that the following produces the expected results, with the “-b” switch included:

    command = [ "/bin/echo" ]

Anyway, so I’ve figured out a solution (modulo the bug). But is there a better way? (Yes, I’ve considered putting a symlink to the plugin in LocalPluginDir for the machines that use PluginDir, but that’s painful.)

I don’t have LocalPluginDir defined anywhere, but on each host I have PluginContribDir defined in /etc/icinga2/constants.conf. This points to the location applicable for that specific host.

Thanks! I guess that’d work (with a bit of adaptation), but it’s about the same as creating a symlink: it requires separate configuration on every host. I was hoping for something that could be done entirely on the master (which my shell hack accomplishes, but what a hack).

It may be worth evaluating why your plugins are installed in different locations on every host in the first place. There are several config management tools out there too that can make it pretty simple to update constants.conf.

I’m pretty sure I already explained that the reason the plugins are in different places is because the machines run different distros. I would hope that it’s obvious that there are reasons for that fact.

Sometimes our brains fade. My shell hack be better if I had simply remembered PATH:

    command = [ "sh", "-c", "PATH=" + LocalPluginDir + ":" + PluginDir + ":$$PATH check_eth -b $$*" ]

which is still ugly but not as ugly.

(Still no clue why the -b is necessary, though.)