Escaping special characters

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Escaping special characters

Post by WillemDH »

Hello,

I'm having issues with a script that creates calls in our ticket system when there are some characters in the service description such as backticks or exclamation marks. The script that is run with Nagios Reactor called from the actions component does not get started, hence no ticket is created.
Example descriptions not working:

Code: Select all

error 4010 MSExchange Availability: Process Microsoft.Exchange.InfoWorker.Common.Delayed`1[System.String]: Request from S-1-5-21-171585296-393481855-1598175747-74403 failed security checks.
When executed from cli on the Reactor server, it just hangs untill I Ctrl-C

Code: Select all

/var/www/html/nagiosreactor/application/libraries/scripts/naf_create_infra_incident.sh "gateway" "exchserver" "EVT_Application" "1" "error 4010 MSExchange Availability: Process Microsoft.Exchange.InfoWorker.Common.Delayed`1[System.String]: Request from S-1-5-21-171585296-392181855-1598175747-74403 failed security checks." "2015-04-02 11:28:18" "1" "SB-Systeem"
> ^C
Also services with an exclamation mark in the description do get passed to my Powershell script, but the exclamation mark is removed and somehow the nsclient.ini command is appended to the description...

Code: Select all

/var/www/html/nagiosreactor/application/libraries/scripts/naf_create_infra_incident.sh "gateway" "exchserver" "EVT_Application" "2" "Test!" "2015-04-02 11:48:31" "1" "SB-Servers"
2015-04-02 11:48:44 - STEP 1 - got output: scripts\powershell\naf_create_infra_incident.ps1 "exchserver" "EVT_Application" "2" "Test; exit $LastExitCode |'powershell.exe'=0 '/noprofile'=0 '-command'=0 '-'=0 2015-04-02 11:48:44 : Info: Infra Incident creation succeeded for service EVT_Application on exchserver.
Any tips on how to handle these annoying issues? This is the Bash script:

Code: Select all

#!/bin/bash

# Script name:  naf_create_infra_incident.sh
# Version:      1.14.11.18
# Author:       Willem D'Haese
# Created on:   13/08/2014
# Purpose:      Quick action bash script that will call a Powershell script through nrpe to create an Infra call
# History:
#   15/12/2013 => Creation date
#   18/11/2014 => Added Infra Group as parameter
# Copyright:
# This program is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.You should have received a copy of the GNU
# General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

Gateway=$1
Hostname=$2
Service=$3
ServiceStateId=$4
ServiceStatusText=$5
ServiceLastCheck=$6
ServiceCurrentAttempt=$7
InfraGroup=$8

Logfile=/var/log/naf_actions.log

Now=$(date '+%Y-%m-%d -- %H:%M:%S')
echo "$Now : Infra incident creation initiated, parameters Host: $Hostname, Service: $Service, ServiceStateId: $ServiceStateId, ServiceStatusText: $ServiceStatusText, Infragroup: $Infragroup." >> $Logfile
Arg="\"$Hostname\" \"$Service\" \"$ServiceStateId\" \"$ServiceStatusText\" \"$ServiceLastCheck\" \"$ServiceCurrentAttempt\" \"$InfraGroup\""
/usr/local/nagios/libexec/check_nrpe -H $Gateway -t 120 -c naf_create_infra_incident -a "$Arg"
case $? in
    "0")
        Now=$(date '+%Y-%m-%d %H:%M:%S')
        echo "$Now :  Info: Infra Incident creation succeeded for service $Service on $Hostname." >> $Logfile
        echo "$Now :  Info: Infra Incident creation succeeded for service $Service on $Hostname."
        exit 0
        ;;
    "1")
        Now=$(date '+%Y-%m-%d %H:%M:%S')
        echo "$Now :  Error: Infra Incident creation failed for service $Service on $Hostname." >> $Logfile
        echo "$Now :  Error: Infra Incident creation failed for service $Service on $Hostname."
        exit 1
        ;;
    *)
        Now=$(date '+%Y-%m-%d %H:%M:%S')
        echo "$Now :  Error: Infra Incident creation failed for service $Service on $Hostname." >> $Logfile
        echo "$Now :  Error: Infra Incident creation failed for service $Service on $Hostname."
        exit 1
        ;;
esac
The Powershell script is a bit long and has too many sensitive info, but the beginning looks like this:

Code: Select all

# Script name: naf_create_infra_incident.ps1
# Version: 1.15.03.06
# Created on: 13/08/2014
# Authors: Willem D'Haese
# Purpose: Powershell script to make an Infra call based on parameters passed by Nagios XI
# Recent History:
#	21/10/2014 => Change organization to "Stad Gent"
#	18/11/2014 => Added group as parameter
#      06/01/2015 => 3483 = GG - Stad Gent
#	08/03/2015 => Compatibility with Reactor
#	19/03/2015 => Added check for passive services after call creation
# Copyright:
#	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published
#	by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed 
#	in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
#	PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public 
#	License along with this program.  If not, see <http://www.gnu.org/licenses/>.

param(
	[Parameter(Mandatory=$true)][string]$NaHost,
	[Parameter(Mandatory=$true)][string]$NaService,
	[Parameter(Mandatory=$true)][string]$NaServiceStateId,
	[Parameter(Mandatory=$true)][string]$NaServiceStatusText,
	[Parameter(Mandatory=$true)][string]$NaServiceLastCheck,
	[Parameter(Mandatory=$true)][string]$NaServiceCurrentAttempts,
	[Parameter(Mandatory=$true)][string]$NaInfraGroup
)

Add-Type -AssemblyName System.Web
$mod = import-Module "C:\Program Files\NSClient++\scripts\powershell\modules\Get-Infra.ps1"

# Start-Transcript "c:\nagios\dig_create_infra_call.debug.log"
$LogLocal="c:\Nagios\NAF\NAF_Logs\Naf_Actions_$NaHost.log"
$Date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$Date : Initiation of Infra call creation script with parameters:" | Out-File -filepath $LogLocal -Append
"$Date : Host = $NaHost" | Out-File -filepath $LogLocal -Append
"$Date : Service = $NaService" | Out-File -filepath $LogLocal -Append
"$Date : NaServiceStateId = $NaServiceStateId" | Out-File -filepath $LogLocal -Append
"$Date : ServiceStatusText = $NaServiceStatusText" | Out-File -filepath $LogLocal -Append
"$Date : ServiceLastCheck = $NaServiceLastCheck" | Out-File -filepath $LogLocal -Append
"$Date : ServiceCurrentAttempts = $NaServiceCurrentAttempts" | Out-File -filepath $LogLocal -Append
"$Date : InfraGroup = $NaInfraGroup" | Out-File -filepath $LogLocal -Append

$CallDescription = "NAGIOS => "  + [Web.HttpUtility]::HtmlDecode([web.HttpUtility]::UrlDecode($NaServiceLastCheck)) +" : $NaHost : $NaService => " + [Web.HttpUtility]::HtmlDecode([web.HttpUtility]::UrlDecode($NaServiceStatusText))
"$Date : CallDescription = $CallDescription" | Out-File -filepath $LogLocal -Append
The NSClient.ini command looks like this:

Code: Select all

naf_create_infra_incident = cmd /c echo scripts\powershell\naf_create_infra_incident.ps1 $ARG1$; exit $LastExitCode | powershell.exe /noprofile -command -
Grtz

Willem
Nagios XI 5.8.1
https://outsideit.net
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Escaping special characters

Post by ssax »

The issue seems to be that when you are surrounding them with double quotes (backtick and !) it is trying to evaluate them, try to use single quotes.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Escaping special characters

Post by WillemDH »

Already tried that ssax. But I did it again for you: SO I changed it to \'$ServiceStatusText\'

Code: Select all

Arg="\"$Hostname\" \"$Service\" \"$ServiceStateId\" \'$ServiceStatusText\' \"$ServiceLastCheck\" \"$ServiceCurrentAttempt\" \"$InfraGroup\""
/usr/local/nagios/libexec/check_nrpe -H $Gateway -t 120 -c naf_create_infra_incident -a "$Arg"
Submmitted a passive critical result to service and tried, but the script failed:

Code: Select all

2015-04-02 21:26:28 - STEP 1 - Dispatching task asynchronously
2015-04-02 21:26:28 - STEP 1 - job submitted to queue.
2015-04-02 21:26:29 - STEP 1 - ****** BEGIN RUN 2471 ******
2015-04-02 21:26:30 - STEP 1 - Running block in basic mode.
2015-04-02 21:26:30 - STEP 1 - Running success actions.
2015-04-02 21:26:30 - STEP 1 - Running action: Run a shell script
2015-04-02 21:26:30 - STEP 1 - Translated context variable: /var/www/html/nagiosreactor/application/libraries/scripts/naf_create_infra_incident.sh "%gateway%" "%hostname%" "%service%" "%servicestateid%" "%servicestatustext%" "%servicelastcheck%" "%servicecurrentattempt%" "%infragroup%" = /var/www/html/nagiosreactor/application/libraries/scripts/naf_create_infra_incident.sh "gateway" "server" "EVT_Application" "2" "Test2!" "2015-04-02 21:26:20" "1" "SB-Servers"
2015-04-02 21:26:30 - STEP 1 - got output: No output available from command (cmd /c echo scripts\powershell\naf_create_infra_incident.ps1 $ARG1$; exit $LastExitCode |'powershell.exe'=0 '/noprofile'=0 '-command'=0 '-).'=0 2015-04-02 21:26:30 : Info: Infra Incident creation succeeded for service EVT_Application on server.
2015-04-02 21:26:30 - STEP 1 - cmd exit status: 0
2015-04-02 21:26:30 - STEP 1 - (Run a shell script) finished with result (0)
2015-04-02 21:26:30 - STEP 1 - Run complete (success)
Same result when I put the single quotes in the actions component.. '%servicestatustext%'

The Powershell script did not even run and even problems with no special chars in the description work anymore, so I reverted my changes.

Grtz
Nagios XI 5.8.1
https://outsideit.net
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Escaping special characters

Post by ssax »

Try this from the command line:

Change naf_create_infra_incident.sh:

Code: Select all

Arg="'$Hostname' '$Service' '$ServiceStateId' '$ServiceStatusText' '$ServiceLastCheck' '$ServiceCurrentAttempt' '$InfraGroup'"
Run the command below (make sure to change YOURGATEWAY):

Code: Select all

/var/www/html/nagiosreactor/application/libraries/scripts/naf_create_infra_incident.sh 'YOURGATEWAY' 'exchserver' 'EVT_Application' '1' 'error 4010 MSExchange Availability: Process Microsoft.Exchange.InfoWorker.Common.Delayed`1[System.String]: Request from S-1-5-21-171585296-392181855-1598175747-74403 failed security checks.' '2015-04-02 11:28:18' '1' 'SB-Systeem'
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Escaping special characters

Post by ssax »

I think we are hitting a bug.

As a temporary solution, try the steps below.

Edit this file:

Code: Select all

/var/www/html/nagiosreactor/application/libraries/modules/actions/shell_script_evaluation_action.class.php
After this line (line 31):

Code: Select all

$script = $params['script'];
Add:

Code: Select all

$script = escapeshellcmd($script);
So it looks like this:

Code: Select all

$script = $params['script'];
$script = escapeshellcmd($script);
Go ahead and test it then, if it works you should probably check your other scripts that run shell scripts to make sure they still work too.

I will file a bug report.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Escaping special characters

Post by ssax »

Code: Select all

NEW TASK ID 5411 created - Nagios Reactor Bug Report: Running a chain that calls a shell command, if the text passed into it contains a backtick or dollar sign, the command fails
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Escaping special characters

Post by ssax »

Also, for the exclamation points, it looks like it's nsclient that is failing on them.

I added the code below in your .sh script underneath your Arg= statement:

Code: Select all

Arg=$(echo $Arg | sed -e "s/\!//g")
I can't think of a better way to do it at this moment.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Escaping special characters

Post by WillemDH »

Ssax, I followed your two recommendations and it seems to work for exclamation marks and also for backticks. Please leave this thread open for a week or two. Testing my other Bash scripts will take some time.

Thanks!
Nagios XI 5.8.1
https://outsideit.net
cmerchant
Posts: 546
Joined: Wed Sep 24, 2014 11:19 am

Re: Escaping special characters

Post by cmerchant »

I will keep this thread open. Keep us updated.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Escaping special characters

Post by ssax »

The devs said that it is not a bug and it's working as expected.

Revert the change made to shell_script_evaluation_action.class.php

They recommend you change the event chain shell command from:

From:

Code: Select all

/var/www/html/nagiosreactor/application/libraries/scripts/naf_create_infra_incident.sh "%gateway%" "%hostname%" "%service%" "%servicestateid%" "%servicestatustext%" "%servicelastcheck%" "%servicecurrentattempt%" "%infragroup%"
To:

Code: Select all

/var/www/html/nagiosreactor/application/libraries/scripts/naf_create_infra_incident.sh '%gateway%' '%hostname%' '%service%' '%servicestateid%' '%servicestatustext%' '%servicelastcheck%' '%servicecurrentattempt%' '%infragroup%'
Sorry, I missed that! :oops:
Locked