I'm trying to execute a bash script in nagios by using nrpe client, but it is always returning "0" value as output.

If i'm using this command in the script:

_VAR=$(atop -r file.atop | grep '^DSK' | tr -s ' ' | grep "sda" | cut -d '|' -f 3 | grep -o '[0-9]+')

echo $_VAR

Then - everything is working properly and on the server i'm receiving correct value by running

/usr/local/nagios/libexec/check_nrpe -H X.X.X.X-c check_asterisk_extensions

But, if i'm changing the command to:

_VAR=$(/usr/sbin/asterisk -rx 'sip show peers' | grep -i 'ok' | wc -l)

echo $_VAR

Then, on the server, I'm receiving permanently "0".

Locally both scripts are returning correct values.

I've tried also by using a php script which converts the variable to the integer:

$strCommand="/usr/sbin/asterisk -rx 'sip show peers' | grep -i 'ok' | wc -l";
//$strCommand="atop -r file.atop | grep '^DSK' | tr -s ' ' | grep "sda" | cut -d '|' -f 3 | grep -o '[0-9]+'";


$str = (int) "$string"; echo $str;

But, result is the same: With the first command is not working - with the second - all good.

Could someone can explain why is this happening?

  • Have you ran /usr/sbin/asterisk -rx 'sip show peers'manually and checked if the output is what you expect? – Gerald Schneider Jun 5 '18 at 11:28
  • Yes, if I'm running the script manually - everything is working fine. – Ilie Soltanici Jun 5 '18 at 11:29
  • Any environment variables that aren't set correctly whan it is run by nagios? – Gerald Schneider Jun 5 '18 at 11:46
  • No, all environment variables are default, and are the same for both commands. – Ilie Soltanici Jun 5 '18 at 11:54
  • What is also interesting, if I'm sending output to the file, and then getting it from there with "cat" - then I'm receiving the correct output. – Ilie Soltanici Jun 5 '18 at 11:59

It was very simple, nagios user didn't have permission to execute command asterisk -rx. Changing command to "sudo asterisk -rx" solved this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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