1

I'm trying to have NCPA execute a PowerShell script on a server while providing it with arguments. I have done so before and have 3 different scripts that behave as expected. This one doesn't.

To keep this short, I'm trying to use Get-Counter to get a performance counter and provide the path as an argument.

The script currently looks like this:

$count = $args[0]
$warn = 1500
$crit = 2000
$Data = Get-Counter -Counter $count
$Raw = $Data.CounterSamples.CookedValue

Write-Host "$Raw | 'Counter'=$Raw;$warn;$crit;0;3000"

if ($Raw -gt $crit) {
    exit 2
} elseif ($Raw -gt $warn) {
    exit 1
} else {
    exit 0
}

This gets called from the Nagios server via:

/usr/local/nagios/libexec/check_ncpa.py -H 192.168.**.** -t 'randomkeystringstuff' -P 5693 -M plugins/counter.ps1 -a "'\Processor Information(_Total)\% Processor Time'"

Filling the count variable directly in the script works. I can then also execute that through Nagios without errors. But using the argument returns

Get-Counter : Internal performance counter API call failed. Error: c0000bc4.

Which is the identical error when you provide a non existing path.

Since I've got the argument method working in three other scripts perfectly, I'm assuming some kind of string or type mismatch or such.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Domsch
  • 33
  • 2
  • 2
    On my machine (Windows 10) the -a is interpreted as the first argument, not as an option. When I leave out the -a and use `"\Processor Information(_Total)\% Processor Time"` as arguments it works fine. But things can be different in your situation, considering you seem te be running a Pyhon script on a Linux system – Gert Jan Kraaijeveld Jan 30 '19 at 16:40
  • Thy Python script is "just" a connector to the NCPA Client on the Windows Server. -a in that case instructs it to run the Powershell command with everything after -a in double quotes as arguments. To clarify: running `-M plugins/counter.ps1 -a "'\Processor Information(_Total)\% Processor Time'"` through this script is the same as running `'counter.ps1 '\Processor Information(_Total)\% Processor Time'` localy on the machine. – Domsch Jan 31 '19 at 10:56

1 Answers1

0

A simple guess is to remove the single quote in the argument

"'\Processor Information(_Total)\% Processor Time'"
->
"\Processor Information(_Total)\% Processor Time"

-->'\Processor Information(_Total)\% Processor Time'<-- is not a valid counter appearly.

Larry Song
  • 1,086
  • 9
  • 13
  • I do need the single quotes. If i remove them, the Shell interprets the spaces. I can strip the Single Quotes in the Powershell script, but this doesn't change the output. Also, when i put the Counter with single quotes directly into the script, that works. It's neede even, because otherwise, PS doesn't recognize it as a string and interprets the first space as the end of the counter Name. – Domsch Jan 31 '19 at 10:57
  • @Domsch in your ps script, add a line write-host "-->$count<--", there has to be something wrong about the $count variable. – Larry Song Feb 01 '19 at 00:07