External Powershell Scripts return code

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
hales8181
Posts: 33
Joined: Thu Jan 19, 2017 11:15 am

External Powershell Scripts return code

Post by hales8181 »

I'm having difficulty getting an externally run Powershell script to return the correct exit code.
The script evaluates a URL (which is passed in as an argument) and considers a status description of "IP address not allowed" as a success, exit code 0, a status description of "OK" as Critical, and anything else as a warning.
The script is:

Code: Select all

param ([string]$url)
try
{
	$response = Invoke-WebRequest -Uri $url
}
catch
{
	$response = $_.Exception.Response
}
if ($response.statusDescription -eq "IP address not allowed")
{
	Write-Output "OK, IP Filtering is working |"; exit 0
}
if ($response.StatusDescription -eq "OK")
{
	Write-Output "Critical, IP Filtering is not in place for $url |"; exit 2
}
else { write-Output "Warning, Unknown response |"; exit 1 }
If I run this locally and pass in a URL as an argument I get:

Code: Select all

PS C:\Program Files\NSClient++\scripts\PowerShell> .\IPFiltercheck.ps1 google.com
Critical, IP Filtering is not in place for google.com 
And I can see that the $LASTEXITCODE has the correct value of 2

If I run using nscp test I get:
nscptest.png
So it looks like it should return the correct exit code - however, if I try to run from my Nagios box I get:

[[email protected] ~]$ /usr/local/nagios/libexec/check_nrpe -H 10.12.0.4 -t 30 -n -c check_ip_filter -a google.com
Warning, Unknown response |

And in my nsclient.log I can see the correct command:
2017-02-13 15:22:44: debug:c:\source\master\modules\CheckExternalScripts\CheckExternalScripts.cpp:607: Command line: cmd /c echo scripts\powershell\IPFiltercheck.ps1 google.com; exit $LastExitCode | powershell.exe /noprofile -command -

When I run that command line it returns exit code 2, so I'm not sure why the Nagios box is getting back the unknown response which is the exit 1?
You do not have the required permissions to view the files attached to this post.
Last edited by hales8181 on Tue Feb 14, 2017 7:46 am, edited 1 time in total.
askewdread
Posts: 69
Joined: Wed Nov 16, 2016 4:54 pm

Re: External Powershell Scripts return code

Post by askewdread »

not sure if it does anything differently but we use the following for our exit part in the config..... its slightly different than yours so may make a difference?

Code: Select all

exit($lastexitcode)
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: External Powershell Scripts return code

Post by rkennedy »

Does the account running NSClient++ have all the correct permissions for executing your script? Might be worth having it debug a temp file to check if it's actually being executed at all, to debug it further.
Former Nagios Employee
hales8181
Posts: 33
Joined: Thu Jan 19, 2017 11:15 am

Re: External Powershell Scripts return code

Post by hales8181 »

exit($lastexitcode) fixed it! Thank you!
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: External Powershell Scripts return code

Post by dwhitfield »

It sounds like this issue has been resolved. Is it okay if we lock this thread? Thanks for choosing the Nagios forums!
hales8181
Posts: 33
Joined: Thu Jan 19, 2017 11:15 am

Re: External Powershell Scripts return code

Post by hales8181 »

Turned out I needed to do both - exit code and change the nsclient service to run as admin.

Thread can be locked
Locked