Custom check by ssh command

Hello, I’ve been trying to monitor a service (zookeeper) through ssh with a linux command and i encountered a bit of a problem, first of all i would like to know if it was correct to do as i’ve done :

and i get :

i don’t understand why it only print half of the command, everything after the “|” is ignored, i’ve ran the command manualy through ssh and it work (i get “imok”), and i would like to know how to customise this service test to get it to show critical on icingaweb when after the check it’s different of “imok” and ok when it’s “imok”.

i’ve tried to run a command by ckeck_by_ssh but i get an error :
./usr/lib64/nagios/plugins/check_by_ssh -v -i $HOME/.ssh/id_rsa -H icinga@10.1.1.2 -C “ls”
check_by_ssh: Invalid hostname/address

i can log with ssh without password.
Thanks for reading

Hi @dendro

have you tried to define the content of vars.by_ssh_command as elements of an array?

vars.by_ssh_command = [ "sudo", "echo", "ruok" , "|", "nc", "127.0.0.1", "5181"]

Also if you want to use any other user than the icinga user you can use the var vars.by_ssh_logname.

hi ritzgu thanks for your answer i’ll try it later, right now i’m trying :
./usr/lib64/nagios/plugins/check_by_ssh -l icinga -H 10.1.1.2 -C “ls” (the “ls” is just to test i’ll replace it by an another command when it work)
in order to try to pass the command by check_by_ssh i’ve managed to solve the invalid hostname with the -l flags, but now i have permission denied.
I can log in with ssh (without password) into the remote host and launch the command without problem but when i try it by using icinga i have permission denied … try again.

a couple things to try and i apologize if they’ve been tried before:

  • make sure the command is run as “nagios” user or “icinga” depending on set-up
  • for the by_ssh_command try it as a string without the [ ]. alternatively, if the pipe “|” is giving you issues, then possibly use a “wrapper” script that calls the by_ssh and then parses the entire output.
  • there might be nesting quoting issues so makes sure the echo is quoted

by_ssh_command = [ “sudo --command ‘echo ruok | nc…’”]

Thanks for your answer Pete, i’ve tried :

but i get :
image

while when i try to execute the command in the terminal (as user icinga) it work fine :

result :

When I try to put everything in the vars.by_ssh_command i get :
“Remote command execution failed: bash: /usr/lib64/nagios/plugins/check_by_ssh -l icinga -H 10.1.1.2 -C ‘whoami’: No such file or directory”

I’ve managed to send system command through icinga, i didn’t need the bracket :

Hi Alex,
I’m afraid I don’t understand your last statement “I’ve managed to send system command…”, but I think I can help with the others.

If the statement works, when it is executed on the target host, you should check:

  • Did you execute it as the same user as icinga agent is running? (would explain the permission issue)
  • Is the service executed on the correct host?
    This might sound strange, but I had an issue when I forgot to tick “execute on agent” in the GUI, so Icinga executed the statement on the Icinga server, instead of the client. To verify this you can just replace your command string with “hostname”.

The bash error when trying to put the whole string into “vars.by_ssh_command” is reproducable. If you are in a bash session and execute “ls something” (with quotes), it will not be interpreted as command “ls” and option “something”, but as command “ls something”, which can’t be found in the search path. So this is not the way to go. I didn’t check by myself, but I think that ritzgu’s version with the array might be correct.

Regards,
Dirk

Hi Dirk thanks for your participation, it executed the command on the right agent i tested it, and for the permission issue it was because the user icinga wasn’t in the sudoers file but was trying to use sudo command.