Execute check of a custom plugin remotely

Hello. I’m trying to implement a plugin I wrote in python.
In this python-script a subprocess is opened with ps = subprocess.Popen(('/usr/NX/bin/nxserver', '--list'), stdout=subprocess.PIPE).
I want this line to be executed on an agent, but I get an error message in icinga2-web, that the file can’t be found. By the python version displayed in the error message I realised, that this line is executed on the master node.
My services.conf in the global-templates zone looks like this:

apply Service "systemd status" {
    import "generic-service" 
    check_command = "check_systemd"
    command_endpoint = host.vars.agent_endpoint

    assign where host.vars.os == "linux"
    ignore where host.vars.noagent
}

apply Service "NoMachine status" {
    import "generic-service"
    check_command = "check_nomachine_sessions"
    command_endpoint = host.vars.client_endpoint

    assign where host.vars.os == "linux" && host.vars.Services == "NoMachine"
    assign where host.name == "sc-030175l.intra.dlr.de"
    ignore where host.vars.noagent
}                                     

The systemd status is working correctly but for my own script I get the FileNotFoundException.
Any ideas what I could’ve configured wrong?

Thanks in advance.

Hi and welcome,

on your working systemd status service you defined the command_endpoint with host.vars.agent_endpoint and on the NoMachine status service with host.vars.client_endpoint - maybe a typo?

Greetz

Thanks for your answer.
I tried to change this, because I read about this somewhere. But even with host.vars.agent_endpoint it’s not working.

It depends on how you have set things up, But besure to check if you have vars.agent_endpoint = name in the host definition

Thanks, when I add this the file does not get found. So I’ll just have to move the script to the remote machine.

But now all I get is the execvpe(…) failed: No such file or directory error. The help I found in this forum didn’t work for me.
When I run the command sudo -u nagios /bin/sudo /usr/lib/nagios/plugins/check_nomachine_sessions.py locally on the machine, it works exactly the way I want. But on Icinga-web I get the error mentioned above.
Any ideas?

Right, If you ge that error it might be time to check if you have setup the check properly.

for example:

// {{ ansible_managed }}
object CheckCommand "check-yum" {
  import "plugin-check-command"
  command = [ PluginDir + "/check_yum.py" ]
  arguments = {
    "-W" = {
      set_if =  "$yum_warn_on_any$"
    }
  }
}

Needs to be defined in your case it might be something like this:

 // {{ ansible_managed }}
 object CheckCommand "check-yum" {
   import "plugin-check-command"
   command = [ PluginDir + "/check_nomachine_sessions.py" ]
 }

plugindir is set by default to:
constants.conf:const PluginDir = "/usr/lib64/nagios/plugins"

So be sure to double check :slight_smile:

Thanks for guiding me through this.
It’s partially working now. The script is getting executed, but I need sudo-privileges to be able to get the output correctly.
I’ve tried

 object CheckCommand "check-nomachine_sessions" {
   import "plugin-check-command"
   command = [ "/bin/sudo " + PluginDir + "/check_nomachine_sessions.py" ]
 }

but if I do it like this, I get a No such file or directory error again.
I enabled the nagios User to execute the script and the target-file in the script as sudo without a password.

Why put sudo?
this: "/bin/sudo " will break it :frowning:
you can check some of the other files in the
/usr/lib/nagios/plugins/
directory
perhaps you need to adjust your file permissions so that this file can be ran without sudo.
you can copy the permissions of a different file in that directory like the check_disk file
If you need help you can calculate them here:

and change them with the chmod commando.

Both files: check_nomachine_sessions.py and the /usr/NX/bin/nxserver have the 755 permissions. So they are executable. But /usr/NX/bin/nxserver --list will return -1 if not run with sudo privileges.

755 seems propper :slight_smile:

So your problem is not with icinga at this point in time but with the:
check_nomachine_sessions.py
script trying to get a valid output from the:
/usr/NX/bin/nxserver
executable.

So, short answer is take a look at sudoers files and allow the nagios user to run this command. or create create a simple shell script instead. Or rewrite the python script to a shell script entirely.
test.sh

#!/bin/sh
sudo /usr/bin/python3 /usr/lib/nagios/plugins/check_nomachine_sessions.py

it aint pretty, but it will work :slight_smile:

Thanks for your help!
Everything is working the way I wanted it to. :+1: