Page 1 of 1

NCPA and return codes

Posted: Wed Apr 12, 2017 9:11 am
by cbeattie-unitrends
NCPA 2.0.3 on Windows Server 2008 R2 Standard
PowerShell 2.0
Nagios XI 5.3.3 on CentOS 7
NCPA Wizard 1.4.4, check_ncpa.py 1.1.0

I've written a custom plugin in PowerShell to check the free space on vSphere datastores. When I check the plugin's output when running it directly on the Windows box, I see the expected return codes:

Code: Select all

PS C:\Users\cbeattie> & 'C:\Program Files (x86)\Nagios\NCPA\plugins\Get-DatastoreSingle.ps1' -VIServer myviserver -Datastore mydatastore -Warn 85 -Crit 95 -Verbose ; $LASTEXITCODE
VERBOSE: myviserver
VERBOSE: mydatastore
VERBOSE: 85
VERBOSE: 95
VERBOSE: myviserver
VERBOSE: 4/12/2017 7:50:55 AM Get-Datastore Started execution
VERBOSE: 4/12/2017 7:50:55 AM Get-Datastore Finished execution
VERBOSE: mydatastore
CRITICAL: mydatastore - 98.14% used
2
PS C:\Users\cbeattie>
However, when I run it from the command line from my Nagios XI server, I always get a return code of 0:

Code: Select all

[nagios@dnagios libexec]$ ./check_ncpa.py --hostname=myhost --token=mytoken --metric='plugins/Get-DatastoreSingle.ps1' --arguments='-VIServer myviserver -Datastore mydatastore -Warn 85 -Crit 95 -Verbose' --verbose --debug ; echo $?
Connecting to: https://myhost:5693/api/plugins/Get-DatastoreSingle.ps1/-VIServer/myviserver/-Datastore/mydatastore/-Warn/85/-Crit/95/-Verbose?token=mytoken&check=1
File returned contained:
{
    "returncode": 0,
    "stdout": "VERBOSE: myviserver\nVERBOSE: mydatastore\nVERBOSE: 85\nVERBOSE: 95\nVERBOSE: myviserver\nVERBOSE: 4/12/2017 7:52:44 AM Get-Datastore Started execution\nVERBOSE: 4/12/2017 7:52:47 AM Get-Datastore Finished execution\nVERBOSE: mydatastore\nCRITICAL: mydatastore - 98.14% used"
}
VERBOSE: myviserver
VERBOSE: mydatastore
VERBOSE: 85
VERBOSE: 95
VERBOSE: myviserver
VERBOSE: 4/12/2017 7:52:44 AM Get-Datastore Started execution
VERBOSE: 4/12/2017 7:52:47 AM Get-Datastore Finished execution
VERBOSE: mydatastore
CRITICAL: mydatastore - 98.14% used
0
[nagios@nagios libexec]$
Any idea what I'm doing wrong or missing?

Re: NCPA and return codes

Posted: Wed Apr 12, 2017 12:30 pm
by lmiltchev
How are you exiting the script?

I haven' been able to recreate the issue. Here's what I tried:

my PS script

Code: Select all

$cpu = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average).Average

$warn = $args[0]
$crit = $args[1]

if ($cpu -lt $warn) {
  Write-Host "OK - CPU usage is at $cpu Percent|CPU=$cpu;$warn;$crit"
  exit 0
} elseif ($cpu -ge $warn -and $cpu -lt $crit) {
  Write-Host "WARNING - CPU usage is at $cpu Percent|CPU=$cpu;$warn;$crit"
  exit 1
} elseif ($cpu -ge $crit) {
  Write-Host "CRITICAL - CPU usage is at $cpu Percent|CPU=$cpu;$warn;$crit"
  exit 2
} else {
  Write-Host "UNKNOWN - Something has gone wrong with the CPU check"
  exit 3
}
exit 0
Running it locally on the Windows box

Code: Select all

PS C:\Program Files (x86)\Nagios\NCPA\plugins> .\cpu_usage3.ps1 90 95
OK - CPU usage is at 13 Percent|CPU=13;90;95
PS C:\Program Files (x86)\Nagios\NCPA\plugins> echo $?
True
PS C:\Program Files (x86)\Nagios\NCPA\plugins> .\cpu_usage3.ps1 1 2
CRITICAL - CPU usage is at 5 Percent|CPU=5;1;2
PS C:\Program Files (x86)\Nagios\NCPA\plugins> echo $?
False
Running the check_ncpa plugin on the Nagios XI server

Code: Select all

[root@main-nagios-xi libexec]# ./check_ncpa.py -H x.x.x.x -t 'mytoken' -M 'plugins/cpu_usage3.ps1/90/95'; echo $?
OK - CPU usage is at 7 Percent|CPU=7;90;95
0
[root@main-nagios-xi libexec]# ./check_ncpa.py -H x.x.x.x -t 'mytoken' -M 'plugins/cpu_usage3.ps1/1/2'; echo $?
CRITICAL - CPU usage is at 4 Percent|CPU=4;1;2
2

Re: NCPA and return codes

Posted: Wed Apr 12, 2017 4:10 pm
by cbeattie-unitrends
I figured it out. It's a bug (or misfeature at best) in PowerShell. There is something about using CmdletBinding() and Param() that triggers it. You can mark this thread as solved and lock it.

Here are the simplest examples I could make up.

critical.ps1

Code: Select all

"CRITICAL: Everything is wrong!"
exit 2
From Windows:

Code: Select all

PS C:\Program Files (x86)\Nagios\NCPA\plugins> .\critical.ps1 ; $LASTEXITCODE
CRITICAL: Everything is wrong!
2
From Nagios:

Code: Select all

[nagios@xxx libexec]$ ./check_ncpa.py --hostname=xxx--token=xxx--metric='plugins/critical.ps1' --verbose --debug ; echo $?
Connecting to: https://xxx:5693/api/plugins/critical.ps1/?token=xxx&check=1
File returned contained:
{
    "returncode": 2,
    "stdout": "CRITICAL: Everything is wrong!"
}
CRITICAL: Everything is wrong!
2
ok-critical.ps1:

Code: Select all

[CmdletBinding()]
Param()
"CRITICAL: Everything is wrong!"
exit 2
From Windows:

Code: Select all

PS C:\Program Files (x86)\Nagios\NCPA\plugins> .\ok-critical.ps1 ; $LASTEXITCODE
CRITICAL: Everything is wrong!
2
From Nagios:

Code: Select all

[nagios@xxx libexec]$ ./check_ncpa.py --hostname=xxx--token=xxx--metric='plugins/ok-critical.ps1' --verbose --debug ; echo $?
Connecting to: https://xxx:5693/api/plugins/ok-critical.ps1/?token=xxx&check=1
File returned contained:
{
    "returncode": 0,
    "stdout": "CRITICAL: Everything is wrong!"
}
CRITICAL: Everything is wrong!
0