Page 1 of 2

check_nrpe unexpected result with powershell script

Posted: Mon May 30, 2016 6:00 am
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.

Re: check_nrpe unexpected result with powershell script

Posted: Mon May 30, 2016 3:49 pm
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 ?

Re: check_nrpe unexpected result with powershell script

Posted: Mon May 30, 2016 11:56 pm
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.

Re: check_nrpe unexpected result with powershell script

Posted: Tue May 31, 2016 3:23 am
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 ?

Re: check_nrpe unexpected result with powershell script

Posted: Tue May 31, 2016 12:13 pm
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.

Re: check_nrpe unexpected result with powershell script

Posted: Wed Jun 01, 2016 8:03 am
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 ?

Re: check_nrpe unexpected result with powershell script

Posted: Wed Jun 01, 2016 5:10 pm
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.

Re: check_nrpe unexpected result with powershell script

Posted: Thu Jun 02, 2016 4:00 am
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

Re: check_nrpe unexpected result with powershell script

Posted: Thu Jun 02, 2016 12:13 pm
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"

Re: check_nrpe unexpected result with powershell script

Posted: Thu Jun 02, 2016 2:02 pm
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