title: Custom written Nagios plugin always returns incorrect value but works on command line
body: <p>I wrote a plugin that would check to see if two hosts are online at the same time and if so, return a critical. When I run this command locally at the command line the logic works correctly and the echo statements all match up depending on the state of the hosts checked (e.g. "CRITICAL - Both testbed controllers online" or "OK - $VM1 is the only testbed controller online." etc). The problem is that when I run these commands via <code>./check_nrpe -H &lt;NRPEHost&gt; -c "controller_check"</code> (the same values also show up on the Nagios webpage) It always returns the same value no matter what the status of the hosts: <strong>"CRITICAL - Both testbed controllers currently offline"</strong> echoing the actual values of $VM1 and $VM2 shows that the initial if check is <em>always</em> being set to 0.</p>
  
  <p>The script works by creating binary values from a <code>ping -c 1 -W 1 $HOSTNAME</code> if check first and then using those values to create the actual alert/exit value. Here's the if statements creating binary values for host online states:</p>
  
  <pre><code>if ping -c 1 -W 1 $VM1HOSTNAME; then
    VM1=1
  else
    VM1=0
  fi
  
  if ping -c 1 -W 1 $VM2HOSTNAME; then
    VM2=1
  else
    VM2=0
  fi
  </code></pre>
  
  <p>And the actual logic that creates the NRPE return:</p>
  
  <pre><code>if [ $VM1 -ne $VM2 ]; then
    if [ $VM1 -gt $VM2 ]; then
      echo "OK - $VM1 is currently the only testbed controller online."
      exit 0
    else
      echo "OK - $VM2 is currently the only testbed controller online."
      exit 0
    fi
  elif [ $VM1 -eq $VM2 ]; then
    if [ $VM1 -eq 0 ]; then
      echo "CRITICAL - Both testbed controllers currently offline"
      exit 2
    else
      echo "CRITICAL - Both testbed controllers currently online."
      exit 2
    fi
  else
    echo "UNKNOWN - Unable to read output."
    exit 3
  fi
  </code></pre>
  
  <p>I've never written my own NRPE plugin before so I'm assuming I'm doing something simple wrong here but the NRPE plugin writing tutorials I've seen online seem to match up with what I've written. As a sidenote if I use <code>check_ping</code> instead of <code>ping -c 1 -W 1</code> the values returned are correct <strong>but</strong> the only value that shows up on the Nagios webpage is the output of the first check_ping command.</p>
  
  <p>For instance (this is correct):</p>
  
  <pre><code>./check_nrpe -H ikor -c "check_testbed_controller_status"
  PING OK - Packet loss = 0%, RTA = 0.81 ms|rta=0.811000ms;10.000000;20.000000;0.000000 pl=0%;2;5;0
  PING OK - Packet loss = 0%, RTA = 0.79 ms|rta=0.787000ms;10.000000;20.000000;0.000000 pl=0%;2;5;0
  CRITICAL - Both testbed controllers currently online.
  </code></pre>
  
  <p>But the Nagios status information only shows <code>PING OK - Packet loss = 0%, RTA = 0.79 ms</code> instead of the echo statement that I want. </p>
  
  <p>So I guess if I can either A) fix what's wrong with using /usr/bin/ping for the if check that causes NRPE to always read these hosts as offline (the if check always returns 0) or B) use check_ping but return only the third stdout line to Nagios that has the actual status information. Does anyone have any ideas or reading recommendations for me here? Thanks so much. </p>