Nagios 4.4.10 Notification Command returns stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Post Reply
k34185
Posts: 2
Joined: Thu Sep 05, 2024 5:50 pm

Nagios 4.4.10 Notification Command returns stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected

Post by k34185 »

I have created a new Python 3.10 script that takes notification arguments and triggers an email and at the same time forwards the notification to another service using and API.

When I try to send a "Custom host notification" to test my notification script I get a

Code: Select all

stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected
and of course the notification is not sent and my script is not executed. I have tried to redirect stdout to a file and stderr to stdout, and it still is not working.

Any idea what may be wrong. Looks like nagios is trying to start /bin/sh and launch the command. The path of Python is correct so as the path to the Python script. I'm starting to believe that it could be the quotes I'm using to wrap each parameter.

/usr/local/nagios/etc/objects/commands.cfg

Code: Select all

...
define command{
        command_name    notify-host-by-email-normal
        command_line    /usr/bin/python3 /usr/local/neo/nagios/notify/notify.py -u "$USER20$" -n "$NOTIFICATIONTYPE$" -d "$HOSTNAME$" -D "$HOSTSTATE$" -i "$HOSTADDRESS$" -o "$HOSTOUTPUT$" -c "$NOTIFICATIONCOMMENT$" -t "$LONGDATETIME$" "$CONTACTEMAIL$" >> /usr/local/neo/nagios/notify/logs/notify.log 2>&1
        }
...
/usr/local/nagios/var/nagios.log

Code: Select all

...
[1725576091] EXTERNAL COMMAND: SEND_CUSTOM_HOST_NOTIFICATION;17KNK9K3CS11;3;Network Engineering NEO;custom 3
[1725576091] HOST NOTIFICATION: monitoring-normal;********;CUSTOM (UP);notify-host-by-email-normal;PING OK - Packet loss = 0%, RTA = 2.42 ms;Network Engineering NEO;custom 3
[1725576091] HOST NOTIFICATION: networke-normal;********;CUSTOM (UP);notify-host-by-email-normal;PING OK - Packet loss = 0%, RTA = 2.42 ms;Network Engineering NEO;custom 3
[1725576091] wproc: NOTIFY job 0 from worker Core Worker 557827 is a non-check helper but exited with return code 2
[1725576091] wproc:   host=********; service=(none); contact=network-normal
[1725576091] wproc:   early_timeout=0; exited_ok=1; wait_status=512; error_code=0;
[1725576091] wproc:   stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected
[1725576091] wproc: NOTIFY job 0 from worker Core Worker 557828 is a non-check helper but exited with return code 2
[1725576091] wproc:   host=********; service=(none); contact=monitoring-normal
[1725576091] wproc:   early_timeout=0; exited_ok=1; wait_status=512; error_code=0;
[1725576091] wproc:   stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected
...
/usr/local/nagios/var/nagios.debug

Code: Select all

...
[1725576091.501592] [256.1] [pid=47302] Read raw external command '[1725576091] SEND_CUSTOM_HOST_NOTIFICATION;********;3;Network Engineering NEO;custom 3'
[1725576091.502759] [032.0] [pid=47302] ** Host Notification Attempt ** Host: '********', Type: CUSTOM, Options: 3, Current State: 0, Last Notification: Thu Jan  1 00:00:00 1970
[1725576091.502918] [032.1] [pid=47302] This is a forced host notification, so we'll send it out.
[1725576091.503030] [032.0] [pid=47302] Notification viability test passed.
[1725576091.503163] [032.1] [pid=47302] Current notification number: 0 (unchanged)
[1725576091.503272] [032.1] [pid=47302] Host notification will NOT be escalated.
[1725576091.503374] [032.1] [pid=47302] This notification will be BROADCAST to all (escalated and normal) contacts...
[1725576091.503490] [032.1] [pid=47302] Adding contacts from host escalation(s) to notification list.
[1725576091.503595] [032.1] [pid=47302] Adding normal contacts for host to notification list.
[1725576091.504688] [032.0] [pid=47302] 2 contacts were notified.
...
jsimon
Posts: 339
Joined: Wed Aug 23, 2023 11:27 am

Re: Nagios 4.4.10 Notification Command returns stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected

Post by jsimon »

Hi @k34185,

The first thing I would look at is making sure your script opens with "/bin/python" and not "/bin/sh" as you're trying to run a python script, not a shell script.
k34185
Posts: 2
Joined: Thu Sep 05, 2024 5:50 pm

Re: Nagios 4.4.10 Notification Command returns stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected

Post by k34185 »

Thank you for your reply. In the command_line part of the command I have /usr/bin/python3 as the command and my script and all its parameters are part of the arguments for /usr/bin/python3.

I believe I found the issue with my command_line. My $USER20$ variable holds extra HTML code depending on the level of notification and is defined something similar as the following:

/usr/local/nagios/etc/resource.cfg

Code: Select all

...
$USER20$="<dl><dt>Procedure:</dt><ul><li>Please verify the error in <a href=\"https://****/\">Nagios</a>.</li><li>Please check the <a href=\"https://****\">Activity Calendar</a> for scheduled maintenance.</li><li>If the error persists outside a scheduled maintenance window, please call the On-Call Network Engineer at ###-###-#### or email to <a href=\"mailto:****?subject=****\">Support</a>.</li></ul></dl>"
...
I believe here is where Nagios command is finding the redirection unexpected. I think I'll try to escape all the '<', '>', ':' and '/'
jsimon
Posts: 339
Joined: Wed Aug 23, 2023 11:27 am

Re: Nagios 4.4.10 Notification Command returns stderr line 01: /bin/sh: 1: Syntax error: redirection unexpected

Post by jsimon »

I understand that your command configuration specifies "/usr/bin/python3", however the error message you are getting back states that it occurs on line 1. If Line 1 of your script opens with

Code: Select all

#!/bin/sh
or something like this, that will cause this redirect error. You'll want

Code: Select all

#!/usr/bin/env python
, or whatever the path to your python binary is here. I would recommend taking a look at this in addition to your escape efforts to see if it helps the issue.
Post Reply