Windows Service names with spaces fail to parse correctly

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
intelXIadmin
Posts: 65
Joined: Mon Mar 25, 2013 9:10 am

Windows Service names with spaces fail to parse correctly

Post 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
vAJ
Posts: 456
Joined: Thu Nov 08, 2012 5:09 pm
Location: Austin, TX

Re: Windows Service names with spaces fail to parse correctl

Post 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/
Andrew J. - Do you even grok?
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: Windows Service names with spaces fail to parse correctl

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
vAJ
Posts: 456
Joined: Thu Nov 08, 2012 5:09 pm
Location: Austin, TX

Re: Windows Service names with spaces fail to parse correctl

Post 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:
You do not have the required permissions to view the files attached to this post.
Andrew J. - Do you even grok?
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Windows Service names with spaces fail to parse correctl

Post 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! :)
vAJ
Posts: 456
Joined: Thu Nov 08, 2012 5:09 pm
Location: Austin, TX

Re: Windows Service names with spaces fail to parse correctl

Post 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.
Andrew J. - Do you even grok?
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Windows Service names with spaces fail to parse correctl

Post by slansing »

Agreed, that is the most sure fire way to get around modifying anything you would normally not need to!
intelXIadmin
Posts: 65
Joined: Mon Mar 25, 2013 9:10 am

Re: Windows Service names with spaces fail to parse correctl

Post 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
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Windows Service names with spaces fail to parse correctl

Post 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?
intelXIadmin
Posts: 65
Joined: Mon Mar 25, 2013 9:10 am

Re: Windows Service names with spaces fail to parse correctl

Post 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
Locked