0

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]+'";

$string=shell_exec("$strCommand");

$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?

6
  • Have you ran /usr/sbin/asterisk -rx 'sip show peers'manually and checked if the output is what you expect? Jun 5, 2018 at 11:28
  • Yes, if I'm running the script manually - everything is working fine. Jun 5, 2018 at 11:29
  • Any environment variables that aren't set correctly whan it is run by nagios? Jun 5, 2018 at 11:46
  • No, all environment variables are default, and are the same for both commands. Jun 5, 2018 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. Jun 5, 2018 at 11:59

1 Answer 1

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .