check_nrpe unexpected result with powershell script

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.
sc00354007
Posts: 8
Joined: Wed Dec 03, 2014 11:21 pm

check_nrpe unexpected result with powershell script

Post by sc00354007 »

Hi All,

I am trying to run a powershell script via NSClient using check_nrpe but it is giving unexpected output. The powershell script is fetching a performance counter from list of servers and based on it the result is send to Nagios.

Below is the software version I am using:


Nagios 3.5.1
Nagios-plugins 2.0.3
NRPE Version - 2.15
NSClient - 0.4.1.90




NSClient.ini:

[/settings/external scripts/wrapped scripts]
alias_check_scripts = powershell\\check_scripts.ps1

[/settings/external scripts/scripts]
alias_check_scripts = cmd /c echo \\scripts\\powershell\\check_scripts.ps1 ; exit($lastexitcode) | powershell.exe -command -



***********************check_scripts.ps1***********************************

Code: Select all


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


$servers = "ABCWPMBX1-ABC","GHIWPMBX3-DEF","DEFWPMBX2-ABC","KLMWPMBX4-DEF","PQRWVJAR2-DEF"
$ErrorActionPreference = 'Stop'

foreach ( $s in $servers )
{
try
{

$c = Get-Counter -ComputerName $s -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName 

}

catch
{
continue
}
}
if($c -Match "MSExchangeIS" )  

		{
		$errorString = "MSExchangeIS counter is enabled for " + $s  
		Write-Host "Critical:"$errorString"" 
		break
		exit $returnStateCritical		
		}

		$errorString = "MSExchangeIS counter is disabled"
		Write-Host "OK:"$errorString""
		exit $returnStateOK
		

Powershell

PS C:\Program Files\NSClient++\scripts\powershell> .\check_scripts.ps1
Critical: MSExchangeIS counter is enabled for PQRWVJAR2-DEF

CMD

C:\>cmd /c echo C:\"Program Files"\NSClient++\scripts\powershell\check_scripts.ps1 ; exit($lastexitcode) | powershell.exe -command -
Critical: MSExchangeIS counter is enabled for SCHWVJAR52-USB.collaboration.lcl


check_nrpe


[nagios@abc ~]$ /usr/lib64/nagios/plugins/check_nrpe -H x.x.x.x -c alias_check_scripts
OK: MSExchangeIS counter is disabled


Please advise where I am going wrong.
sc00354007
Posts: 8
Joined: Wed Dec 03, 2014 11:21 pm

Re: check_nrpe unexpected result with powershell script

Post by sc00354007 »

I also displayed the value of $c outside the loop while calling from check_nrpe and it is blank.

Why is the value of $c as blank when calling via check_nrpe ?

If I call it from Powershell console or cmd, $c = MSExchangeIS

Can anyone help or point me to the right direction ?
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: check_nrpe unexpected result with powershell script

Post by Box293 »

Try changing this:
sc00354007 wrote:NSClient.ini:

[/settings/external scripts/wrapped scripts]
alias_check_scripts = powershell\\check_scripts.ps1

[/settings/external scripts/scripts]
alias_check_scripts = cmd /c echo \\scripts\\powershell\\check_scripts.ps1 ; exit($lastexitcode) | powershell.exe -command -
To this:

Code: Select all

[/settings/external scripts/scripts]
alias_check_scripts = cmd /c echo scripts\powershell\check_scripts.ps1 ; exit($lastexitcode) | powershell.exe -command -
Notice I completely removed one of them and adjusted the other.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
sc00354007
Posts: 8
Joined: Wed Dec 03, 2014 11:21 pm

Re: check_nrpe unexpected result with powershell script

Post by sc00354007 »

Hello,

Thanks for your response.

I did the changes suggested by you and restarted NSCleint service.

Still the same output.

Could there be an issue with the powershell or the NSClient version itself ?
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: check_nrpe unexpected result with powershell script

Post by lmiltchev »

Can you run the following command on your nagios server (from within the plugins directory), and show the output?

Code: Select all

./check_nrpe -H <client ip> -c alias_check_script
Also, post the nsclient.log.
Be sure to check out our Knowledgebase for helpful articles and solutions!
sc00354007
Posts: 8
Joined: Wed Dec 03, 2014 11:21 pm

Re: check_nrpe unexpected result with powershell script

Post by sc00354007 »

If I run it from the Powershell console, it gives me below output:

PS C:\Program Files\NSClient++\scripts\powershell> .\check_scripts.ps1
Critical: MSExchangeIS counter is enabled for PQRWVJAR2-DEF
PS C:\Program Files\NSClient++\scripts\emas>

[nagios@abc ~]$ /usr/lib64/nagios/plugins/check_nrpe -H x.x.x.x -c alias_check_scripts
OK: MSExchangeIS counter is disabled


Below is the NSClient.log:

016-06-01 08:55:40: d:..\..\..\trunk\service\NSClient++.cpp:1361: Starting: DONE
2016-06-01 08:55:47: d:D:\source\nscp\trunk\include\nrpe/server/protocol.hpp:66: Accepting connection from: ::ffff:x.x.x.x
2016-06-01 08:55:47: d:..\..\..\trunk\service\NSClient++.cpp:960: Injecting: alias_...
2016-06-01 08:55:47: d:..\..\..\..\trunk\modules\CheckExternalScripts\CheckExternalScripts.cpp:257: Arguments: /c echo scripts\powershell\check_scripts.ps1 ; exit($lastexitcode) | powershell.exe -command -
2016-06-01 08:55:47: d:..\..\..\trunk\service\NSClient++.cpp:985: Result alias_check_scripts: OK




$c = (Get-Counter -ComputerName $s -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName)

I also tried printing the value of $c while calling the script from check_nrpe and the value is blank whereas one of the server is giving MSExchangeIS.

Does check_nrpe does not understand the "-ea stop" option ?
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: check_nrpe unexpected result with powershell script

Post by ssax »

Please try this code change and see if it works for you.

Code: Select all

$commandString = "Get-Counter -ComputerName $s -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName"
Write-Host $commandString
$c = Invoke-Expression $commandString
Please post the output.
sc00354007
Posts: 8
Joined: Wed Dec 03, 2014 11:21 pm

Re: check_nrpe unexpected result with powershell script

Post by sc00354007 »

Hi,

Thanks for your response.

Please see below:

Updated code :

Code: Select all


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


$servers = "ABCWPMBX1-ABC","GHIWPMBX3-DEF","DEFWPMBX2-ABC","KLMWPMBX4-DEF","PQRWVJAR2-DEF"
$ErrorActionPreference = 'Stop'

foreach ( $s in $servers )
{
try
{

$commandString = "Get-Counter -ComputerName $s -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName" 
write-host $commandString
$c = Invoke-Expression $commandString
}

catch
{
continue
}
}
if($c -Match "MSExchangeIS" )  

      {
      $errorString = "MSExchangeIS counter is enabled for " + $s  
      Write-Host "Critical:"$errorString"" 
      break
      exit $returnStateCritical      
      }

      $errorString = "MSExchangeIS counter is disabled"
      Write-Host "OK:"$errorString""
      exit $returnStateOK
      

Below is the output when I execute it from the Nagios via check_nrpe:

[nagios@abc ~]$ /usr/lib64/nagios/plugins/check_nrpe -H x.x.x.x -c alias_check_scripts
Get-Counter -ComputerName ABCWPMBX1-ABC -ListSet MSExchangeIS -ea stop|'select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'GHIWPMBX3-DEF'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'DEFWPMBX2-ABC'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'KLMWPMBX4-DEF'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'PQRWVJAR2-DEF'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
OK:'=0 'MSExchangeIS'=0 'counter'=0 'is'=0 'disabled'=0





Below is the output when executed from the PowerShell console:

PS C:\Program Files\NSClient++\scripts\powershell> .\check_scripts.ps1
Get-Counter -ComputerName ABCWPMBX1-ABC -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName
Get-Counter -ComputerName GHIWPMBX3-DEF -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName
Get-Counter -ComputerName DEFWPMBX2-ABC -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName
Get-Counter -ComputerName KLMWPMBX4-DEF -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName
Get-Counter -ComputerName PQRWVJAR2-DEF -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName
Critical: MSExchangeIS counter is enabled for PQRWVJAR2-DEF



Nsclient.log:

2016-06-02 04:54:19: d:D:\source\nscp\trunk\include\nrpe/server/protocol.hpp:66: Accepting connection from: ::ffff:x.x.x.x
2016-06-02 04:54:19: d:..\..\..\trunk\service\NSClient++.cpp:960: Injecting: alias_check_scripts...
2016-06-02 04:54:19: d:..\..\..\..\trunk\modules\CheckExternalScripts\CheckExternalScripts.cpp:257: Arguments: /c echo scripts\powershell\check_scripts.ps1 ; exit($lastexitcode) | powershell.exe -command -
2016-06-02 04:54:20: d:..\..\..\trunk\service\NSClient++.cpp:985: Result alias_check_scripts: OK
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: check_nrpe unexpected result with powershell script

Post by ssax »

Maybe it has something to do with the dashes in the names, try changing it to this:

Code: Select all

$commandString = "Get-Counter -ComputerName '$s' -ListSet MSExchangeIS -ea stop|select-object -ExpandProperty CounterSetName"
sc00354007
Posts: 8
Joined: Wed Dec 03, 2014 11:21 pm

Re: check_nrpe unexpected result with powershell script

Post by sc00354007 »

Please see the below output after doing the changes:

[nagios@abc ~]$ /usr/lib64/nagios/plugins/check_nrpe -H x.x.x.x -c alias_check_scripts
Get-Counter -ComputerName 'ABCWPMBX1-ABC' -ListSet MSExchangeIS -ea stop|'select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'GHIWPMBX3-DEF'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'DEFWPMBX2-ABC'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'KLMWPMBX4-DEF'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
Get-Counter'=0 '-ComputerName'=0 'PQRWVJAR2-DEF'=0 '-ListSet'=0 'MSExchangeIS'=0 '-ea'=0 'stop|select-object'=0 '-ExpandProperty'=0 'CounterSetName
OK:'=0 'MSExchangeIS'=0 'counter'=0 'is'=0 'disabled'=0
Locked