0

Can anyone clarify for me why the following code is not running when I place it in command_line for a custom Nagios command? It works when I run it in terminal.

command_name     notify-host-by-sms
commnad_line     echo -e "AT+CMGS="$CONTACTPAGER$"\r" | socat -t3 - TCP:XXX.XXX.XXX.XXX:10002 && echo -e "$HOSTNAME$ $HOSTSTATE$ $HOSTADDRESS$ $LONGDATETIME$\x1A" | socat - TCP:XXX.XXX.XXX.XXX:10002
1
  • The existing answer is already good but i’ll give you two general pointers: 1) always use full paths, /bin/foo not simply foo, and 2) for anything but simple one liners with no pipes, write a script instead and use that
    – pzkpfw
    Commented Sep 16, 2023 at 13:42

1 Answer 1

1

It is not possible to use | and && in Nagios command_line, so you would have to wrap this as a script. (Also, it would not work with the commnad_line typo.)

command_name  notify-host-by-sms
commnad_line  /usr/local/bin/notify-host-by-sms.sh $CONTACTPAGER$ $HOSTNAME$ $HOSTSTATE$ $HOSTADDRESS$ $LONGDATETIME$

And then use those command line arguments in the script, e.g.,

#!/bin/bash

echo -e "AT+CMGS=\"${1}\"\r" \
  | socat -t3 - TCP:192.0.2.1:10002 \
  || exit 1

echo -e "${2} ${3} ${4} ${5}\x1A" \
  | socat - TCP:192.0.2.1:10002 \
  || exit 1

(I have added extra quotation, because the AT+CMGS= might need them around the value. I believe it was your intention but they were just unescaped.)

3
  • Ahh, I understand now. I've configured all of this, but the script still doesn't run. Do I need to somehow elevate the nagios user to run the script with root priveledges? Because I have to run the script in sudo mode when I test it in terminal.
    – DesignerJT
    Commented Sep 16, 2023 at 14:06
  • Neither echo nor socat would require sudo. Maybe there's a problem with the permissions of your script? Try sudo chmod 755 /usr/local/bin/notify-host-by-sms.sh. Commented Sep 16, 2023 at 15:38
  • Ahh, I figured it out. It was either the full process kill and start instead of a restart or it was switching to using Address2 instead of Pager in the contact. It may have been a formatting issue with the pager value or it wasn't loading the config files on soft restart. Either was, it's working now, thank you very much for the help.
    – DesignerJT
    Commented Sep 18, 2023 at 14:26

You must log in to answer this question.

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