Escaping special characters
Posted: Thu Apr 02, 2015 4:58 am
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:
When executed from cli on the Reactor server, it just hangs untill I Ctrl-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...
Any tips on how to handle these annoying issues? This is the Bash script:
The Powershell script is a bit long and has too many sensitive info, but the beginning looks like this:
The NSClient.ini command looks like this:
Grtz
Willem
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.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
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.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
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 -AppendCode: Select all
naf_create_infra_incident = cmd /c echo scripts\powershell\naf_create_infra_incident.ps1 $ARG1$; exit $LastExitCode | powershell.exe /noprofile -command -Willem