0

Script is at Server:

#!/bin/bash


if [ ! $# == 1 ]; then
echo "Usage check_cluster "
fi;
clu_srv=$1
error="stopped"
error1="disabled"
error2="recoverable"
host1=`sudo /usr/sbin/clustat|grep $1| awk {'print $2'}`
host2=`sudo /usr/sbin/clustat|grep $1| awk {'print $3'}`
service1=`sudo /usr/sbin/clustat|grep $clu_srv| awk {'print $1'}`
if [[ "$host2" == "$error" ]] || [[ "$host2" == "$error1" ]]; then
echo "CRITICAL - Cluster $clu_srv service failover on $host1 and state is '$host2'"
else
echo "OK - Cluster  $clu_srv service is on $host1 and state is '$host2'"
fi;

##--EndScript


It receives thee argument from script correctly. When I running this script manually at Server from command line it returns correct information, for example:

# /usr/local/nagios/libexec/check_rhcs-ERS NFSService
OK - Cluster  NFSService service is on NODE1 and state is 'started'

But when I tried with the script (check_nrpe) remotely with following command its showing incorrect information:

# ./check_nrpe -H localhost -c check_rhcs-ERS
OK - Cluster  NFSService service is on  and state is ''

nrpe.cfg:

# command[check_rhcs-ERS]=/usr/local/nagios/libexec/check_rhcs-ERS  NFSService

What is Wrong with the script, How to fix it?

2
  • Modify the script to log its output so you can see what it's doing. For example: Add -x to the first line (#!/bin/bash -x) and immediately after that add a new line after that to capture the stderr+stdout output to a logfile (exec > /tmp/nrpetest.$$.log 2>&1) then call the script via NRPE again. Review the log file that is generated to see where the script is failing.
    – Gene
    Aug 16, 2016 at 7:55
  • Additionally, nrpe checks rely on the return code (or "exit code") of the script/command to determine whether the check returned OK, WARNING, or CRITICAL status. Your last if statement should include appropriate exit commands to return the desired return code. nagios-plugins.org/doc/guidelines.html#AEN78
    – Gene
    Aug 16, 2016 at 8:17

2 Answers 2

1

If you have configured sudo correctly its probably a problem with Requiretty, you should tell sudo not to require for nrpe.

See sudoers: how to disable requiretty per user

0
1

Your NRPE user most likely doesn't have permissions to run commands with sudo access.

To allow this, you can add the line below to your /etc/sudoers file using visudo. You can also omit the NFSService part if you don't want to restrict that part.

nrpe ALL=(ALL) NOPASSWD: /usr/sbin/clustat NFSService

That said, your script does need improving. It also only takes one parameter, not three - the $2 and $3 variables are awk arguments, not bash parameters.

My partially-edited version is below:

#!/bin/bash


if [ $# -ne 1 ]; then
  echo "Usage check_cluster " >&2
  exit 1
fi

clu_srv=$1

error="stopped"
error1="disabled"
error2="recoverable"

host1=$(sudo /usr/sbin/clustat | grep "${clu_srv}" | awk '{ print $2 }')
host2=$(sudo /usr/sbin/clustat | grep "${clu_srv}" | awk '{ print $3 }')
service1=$(sudo /usr/sbin/clustat | grep "${clu_srv}" | awk '{ print $1}')

The error-handling part of your script needs clarifying - what conditions do you want to catch? Your OK output gives the status for host2, but says that the service is on host1.

5
  • Thanks for the help..Already Given the permission to nrpe user. May be something wrong with the script.
    – Gaurav
    Aug 16, 2016 at 7:49
  • 1
    If it works using your user, there is nothing wrong with the script. You can elevate yourself to the nrpe user's shell by running su nrpe as root - if you get bumped back to your root shell, you can change the user's shell in /etc/passwd - just remember to change it back when you're done debugging. Aug 16, 2016 at 7:51
  • I already debugged before using root user, its showing same result.I thing there is no issue for permissions.
    – Gaurav
    Aug 16, 2016 at 7:57
  • You said that you "running this script manually at Server from command line it returns correct information". Can you please clarify? Aug 16, 2016 at 8:01
  • Using nrpe user I can run the script manually at server from cmd line...Actually I gives all right to nrpe user but still same result occured.
    – Gaurav
    Aug 16, 2016 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.