check_by_ssh doesn't work with remote python script
check_by_ssh doesn't work with remote python script
I am using check_by_ssh on my Nagios server (running Nagios Core) to check my remote host.
I have a python script on the remote host to report back the status of a service running there.
The python script runs perfectly locally on the remote host.
However, when using check_by_ssh to run the python script, it returned UNKNOWN
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check.py" -E
UNKNOWN - check_by_ssh: Remote command service_check.py' returned status 127
If the script is a shell script instead, check_by_ssh would run perfectly.
Is there a way to get check_by_ssh to work properly with the python script?
Thanks!
I have a python script on the remote host to report back the status of a service running there.
The python script runs perfectly locally on the remote host.
However, when using check_by_ssh to run the python script, it returned UNKNOWN
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check.py" -E
UNKNOWN - check_by_ssh: Remote command service_check.py' returned status 127
If the script is a shell script instead, check_by_ssh would run perfectly.
Is there a way to get check_by_ssh to work properly with the python script?
Thanks!
-
- Support Tech
- Posts: 3457
- Joined: Mon May 15, 2017 5:00 pm
Re: check_by_ssh doesn't work with remote python script
Hi, @yomiko.
Try putting the service_check.py command(as you'd run it locally) in a shell script.
And then run the shell script via check_by_ssh instead.
Let me know if it works or not.
Try putting the service_check.py command(as you'd run it locally) in a shell script.
Code: Select all
#!/bin/bash
/usr/local/nagios/libexec/service_check.py
Let me know if it works or not.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Re: check_by_ssh doesn't work with remote python script
That does not work.
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check_bash.sh" -E
UNKNOWN - check_by_ssh: Remote command 'service_check_bash.sh' returned status 127
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check_bash.sh"
Remote command execution failed: ***************************************************************************
I have restricted shell setup to allow a handful of scripts/commands that the user could run.
I placed commands like check_disk, check_uptime, service_check.py as links in the remote host's user's homedir.
The service_check.py script will work if I run it locally.
Remotely with check_by_ssh, it doesn't work. However, if I add a link of /usr/bin/python and /usr/bin/systemctl
to the remote host, then check_by_ssh will work.
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check_bash.sh" -E
UNKNOWN - check_by_ssh: Remote command 'service_check_bash.sh' returned status 127
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check_bash.sh"
Remote command execution failed: ***************************************************************************
I have restricted shell setup to allow a handful of scripts/commands that the user could run.
I placed commands like check_disk, check_uptime, service_check.py as links in the remote host's user's homedir.
The service_check.py script will work if I run it locally.
Remotely with check_by_ssh, it doesn't work. However, if I add a link of /usr/bin/python and /usr/bin/systemctl
to the remote host, then check_by_ssh will work.
-
- DevOps Engineer
- Posts: 19396
- Joined: Tue Nov 15, 2011 3:11 pm
- Location: Nagios Enterprises
- Contact:
Re: check_by_ssh doesn't work with remote python script
You need to supply the FULL path to the service_check.py file on the client server, like:yomiko wrote:Code: Select all
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check.py" -E UNKNOWN - check_by_ssh: Remote command service_check.py' returned status 127
Code: Select all
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "/usr/local/nagios/libexec/service_check.py" -E
Re: check_by_ssh doesn't work with remote python script
I have the other checks (check_load, check_uptime, etc) as links in the remote host & checks_by_ssh worked on these commands.
The remote user has executable permission on the custom script on the remote host.
Both the full path and linked path worked ONLY if I also create links for "/usr/bin/python" and /usr/bin/systemctl" in the
remote user's home dir.
E.g.
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "python /usr/local/nagios/libexec/service_check.py" -E
Service OK - active (running) since Mon 2018-04-23 18:37:52 GMT
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check.py" -E
Service OK - active (running) since Mon 2018-04-23 18:37:52 GMT
The remote user has executable permission on the custom script on the remote host.
Both the full path and linked path worked ONLY if I also create links for "/usr/bin/python" and /usr/bin/systemctl" in the
remote user's home dir.
E.g.
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "python /usr/local/nagios/libexec/service_check.py" -E
Service OK - active (running) since Mon 2018-04-23 18:37:52 GMT
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "service_check.py" -E
Service OK - active (running) since Mon 2018-04-23 18:37:52 GMT
-
- DevOps Engineer
- Posts: 19396
- Joined: Tue Nov 15, 2011 3:11 pm
- Location: Nagios Enterprises
- Contact:
Re: check_by_ssh doesn't work with remote python script
could you share service_check.py this plugin so we can see it?
also
This looks like it is working
also
This looks like it is working
yomiko wrote:Code: Select all
# /usr/lib64/nagios/plugins/check_by_ssh -l <USERNAME> -i ~/.ssh/id_rsa -H <remote_host_IP> -C "python /usr/local/nagios/libexec/service_check.py" -E Service OK - active (running) since Mon 2018-04-23 18:37:52 GMT
Re: check_by_ssh doesn't work with remote python script
Here is the custom check.
# cat allow/service_check.py
#!/usr/bin/python
import os, sys
import re
code=os.popen("systemctl status httpd").read().replace('\n', '')
code = code.split("Active:")[1].split(";")[0].lstrip()
if "active (running)" in code:
print "Service OK - ",code
sys.exit(0)
elif "inactive (dead)" or "failed" in code:
print "Service CRITICAL - ",code
sys.exit(2)
# cat allow/service_check.py
#!/usr/bin/python
import os, sys
import re
code=os.popen("systemctl status httpd").read().replace('\n', '')
code = code.split("Active:")[1].split(";")[0].lstrip()
if "active (running)" in code:
print "Service OK - ",code
sys.exit(0)
elif "inactive (dead)" or "failed" in code:
print "Service CRITICAL - ",code
sys.exit(2)
-
- DevOps Engineer
- Posts: 19396
- Joined: Tue Nov 15, 2011 3:11 pm
- Location: Nagios Enterprises
- Contact:
Re: check_by_ssh doesn't work with remote python script
You will likely need the full path to systemctl in your plugin as well
Code: Select all
#!/usr/bin/python
import os, sys
import re
code=os.popen("/usr/bin/systemctl status httpd").read().replace('\n', '')
code = code.split("Active:")[1].split(";")[0].lstrip()
if "active (running)" in code:
print "Service OK - ",code
sys.exit(0)
elif "inactive (dead)" or "failed" in code:
print "Service CRITICAL - ",code
sys.exit(2)
Re: check_by_ssh doesn't work with remote python script
Resolved...
I have restricted shell setup. The python script runs "systemctl". After adding "systemctl" as an allowed
command to run in the restricted shell, check_by_ssh ran fine.
Thanks for the tips from everyone.
I have restricted shell setup. The python script runs "systemctl". After adding "systemctl" as an allowed
command to run in the restricted shell, check_by_ssh ran fine.
Thanks for the tips from everyone.
Re: check_by_ssh doesn't work with remote python script
Did you have further (related) questions or are we good to lock this up?
Former Nagios employee