Page 1 of 2

Windows Service names with spaces fail to parse correctly

Posted: Wed May 01, 2013 9:57 am
by intelXIadmin
Hello,

I was following the doc Nagios XI - Restarting a Windows Service with NRPE. I am able to sucessfully start services on a remote machine with the following command:

./check_nrpe -H 192.168.5.180 -p 5666 -c runcmd -a spooler.

This works. Now I am trying to restart a service with the same command:

./check_nrpe -H 192.168.5.180 -p 5666 -c runcmd -a Klocwork 9.2 Server

The spaces are parsed out and it only passes through the string Klocwork. Of course this is not a real service name so it fails. Is there some type of quoting I will need? I've tried double quotes and single quotes.

Regards,

Randy

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 01, 2013 10:51 am
by vAJ
Is "Klocwork 9.2 Server" the display name or the actual service name. Service Names usually don't have spaces.

And this may be of help: http://www.groundtactics.com/2010/04/06 ... th-spaces/

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 01, 2013 11:02 am
by lmiltchev
intelXIadmin,

Try changing the following line in the NSC.ini (or nsclient.ini) from this:

Code: Select all

runcmd=scripts\runcmd.bat $ARG1$
to this:

Code: Select all

runcmd=scripts\runcmd.bat "$ARG1$"
Restart the nsclinet++ service and run a check from the nagios server:

Code: Select all

./check_nrpe -H 192.168.5.180 -p 5666 -c runcmd -a 'Klocwork 9.2 Server'
Note: The service name with spaces is wrapped in single quotes.

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 01, 2013 11:03 am
by vAJ
Take a look at this screencap.

To start this windows service, you can use the service name:

Code: Select all

net start aspnet_state
Or the display name:

Code: Select all

net start "ASP.NET State Service"
When peforming these operations from a Linux monitoring solution, try to avoid using Windows names... Not that it can't be done (and I don't know the syntax for passing the display name) but it's just easier. K-I-S-S... :mrgreen:

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 01, 2013 11:08 am
by slansing
The issue here is not that he cannot restart the service locally but that he is having issues passing the name with spaces through NSclient into a batch script. Ludmil posted the solution, and I have updated the documentation accordingly, we should be good to go! :)

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 01, 2013 12:45 pm
by vAJ
I understand, I was using the local example to illustrate that there is a difference in using the service name and the display name.

All of my remote commands from LInux systems to Windows systems in regards to services use the Service Name, which do not have spaces. This solves the problem equally well, without the need to update the nsc.ini on each Windows host.

In an environment like mine with hundreds of Windows servers, an ounce of prevention at the Nagios side prevents a pound of labor/scripting to correct something on the monitored system side.

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 01, 2013 1:00 pm
by slansing
Agreed, that is the most sure fire way to get around modifying anything you would normally not need to!

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 08, 2013 5:01 pm
by intelXIadmin
Sorry for the delay. I tried the change that was recommended. I modified the line in nsc.ini to be

Cust_Restart_Service=cmd /c echo scripts\Cust_Restart_Service.ps1 | powershell.exe -command .\scripts\Cust_Restart_Service.ps1 "$ARG1$"

Then I went to the Nagios server and typed in the check_nrpe command:

./check_nrpe -H x.x.x.x -p 5666 -c cust_restart_service -a 'Klocwork 9.2 Server' and got back the following response. It appears it is still parsing at the first space.

Stop-Service : Cannot find any service with service name 'Klocwork'.
At C:\Program Files\NSClient++\scripts\Cust_Restart_Service.ps1:104 char:13
+ Stop-Service <<<< $StrServiceName
+ CategoryInfo : ObjectNotFound: (Klocwork:String) [Stop-Service]
, ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.
Commands.StopServiceCommand

Start-Service : Cannot find any service with service name 'Klocwork'.
At C:\Program Files\NSClient++\scripts\Cust_Restart_Service.ps1:112 char:14
+ Start-Service <<<< $StrServiceName
+ CategoryInfo : ObjectNotFound: (Klocwork:String) [Start-Service
], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.
Commands.StartServiceCommand

Re: Windows Service names with spaces fail to parse correctl

Posted: Thu May 09, 2013 9:04 am
by slansing
It may be ignoring single quotes try with double's. ( "Klocwork 9.2 Server" ).

Also, can you share the contents this .ps1 file you are executing?

Re: Windows Service names with spaces fail to parse correctl

Posted: Wed May 15, 2013 11:08 am
by intelXIadmin
slansing wrote:It may be ignoring single quotes try with double's. ( "Klocwork 9.2 Server" ).

Double quotes have no effect.

Also, can you share the contents this .ps1 file you are executing?

#
#--- Set up Nagios return status codes.
#

$returnStateOK = 0
$returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3
$msg = ""

#***********************************************************************************************************
# Function Definitions
#***********************************************************************************************************

#
#--- Set up logging.
#

Set-Variable logFile -Scope Script
function LogInfo($message)
{
$date= Get-Date
$outContent = "[$date]`tInfo`t`t$message`n"
Add-Content "Log\$Script:logFile" $outContent
}

function LogError($message)
{
$date= Get-Date
$outContent = "[$date]`tError`t`t $message`n"
Add-Content "Log\$Script:logFile" $outContent
}

function ConfigureLogger()
{

if((Test-Path Log) -eq $false)
{
$LogFolderCreationObj=New-Item -Name Log -type directory
}
$date= Get-Date -UFormat "%Y-%m-%d %H-%M-%S"
$Script:logFile="Cust_Restart_Service $date.log"
Add-Content "Log\$logFile" "Date`t`t`tCategory`t`tDetails"
}

ConfigureLogger


#***********************************************************************************************************
# Main Code starts Here.
#***********************************************************************************************************

#
# --- Get positional parameters.
#

# Write-Host "Num Args:" $args.Length;
# foreach ($arg in $args)
# {
# Write-Host "Arg: $arg";
# }
#Write-Host "Arg1: " $args[0]
#Write-Host "Arg2: " $args[1]


$StrServiceName = $args[0]

if (-not $StrServiceName) {
write-Host "No input parameter!"
exit $returnStateOK}
#
#--- Set up a service object for formatting and output
#

#
#--- Stop Service
#

Stop-Service $StrServiceName
$msg="Stopping Service $StrServiceName"
LogInfo ($msg)

#
#---Start Service
#

Start-Service $StrServiceName
$msg="Starting Service $StrServiceName"
LogInfo ($msg)

Exit $ReturnStateOK