Page 1 of 1

NRPE plugin only returns first entry

Posted: Thu Jul 23, 2015 10:20 am
by Brick
I have a powershell script that checks the total bytes/sec for all of the connected network cards of a server, problem is, when its ran though NRPE it only returns the first entry and its perfdata

Its not a buffer limit or an output limit as it perf data is all at the very end of the string, here is my code-

Code: Select all

$adaptername = @()
$adapterdevice = @()
$adaptercount = @()
$num = 0
$out = ""

#Get any warning or critical levels
foreach ($arg in $args) {

$split = $arg.split('=')

if ($split[0] -like "warn") {
    $warn = $split[1]
    $warn = [int]$warn
    }
elseif ($split[0] -like "crit") {
    $crit = $split[1]
    $crit = [int]$crit
    }

}

#Get all connected network devices
$adapterdevice += Get-WmiObject -Class Win32_NetworkAdapter -filter "NetconnectionStatus = '2'" | Select -expand Name

#Find their names
foreach ($adapter in $adapterdevice) {

$adaptername += Get-WmiObject -Class Win32_NetworkAdapter -filter "Name = '$adapter'" | Select -expand NetconnectionID

}


#Find the total bytes per second
foreach ($device in $adapterdevice) {

#Perfmon doesnt like certain characters
$device = $device -replace "[(]","["
$device = $device -replace "[)]","]"
$device = $device -replace "#","_"

$getcount = Get-counter -Counter "\Network Interface($device)\Bytes Total/sec"

$getcount = $getcount.CounterSamples | Select -expand CookedValue

#Turn it into kb
$getcount = ($getcount)/1kb

#round up to nearest whole number
$getcount = "{0:N0}" -f $getcount

$adaptercount += $getcount

#Build the output string
$out += $adaptername[$num] + " : " + $adaptercount[$num] + "<BR>"

$num++

}

$num = 0

#Check if any of them should alert and create perfdata
foreach ($adapter in $adaptercount) {

$adapter = [int]$adapter

$perfdata += "|'" + $adaptername[$num] + "'=" + $adapter + ";" + $warn + ";" + $crit

$num++

if ($warn) {
    if ($adapter -ge $warn) { $exitcode = 1 }
    }
if ($crit) {
    if ($adapter -ge $crit) { $exitcode = 2 }
    }
if (!$exitcode) { $exitcode = 0 }

}


#Add perfdata to the end of the return
$out += $perfdata

echo $out

exit $exitcode

This is my command in NSC.ini:

Code: Select all

check_network_adapter = cmd /c echo scripts\\networkadapters.ps1 "$ARG1$" "$ARG2$"; | powershell.exe -command -
And my command in nagios:

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H bcdvmgt01 -t 60 -c check_network_adapter -a warn=100 crit=1000
When I run it locally in powershell I get the following result:

Code: Select all

Production Adapter - Secondary : 28<BR>Production Adapter - Primary : 44<BR>|'Production Adapter - Secondary'=28;100;1000|'Production Adapter - Primary'=44;100;1000
But when I run it though NRPE I get the following result:

Code: Select all

Production Adapter - Primary : 4<BR>|'Production Adapter - Primary'=4;100;1000
It just ignores the second adapter...

Any thoughts?

Re: NRPE plugin only returns first entry

Posted: Thu Jul 23, 2015 11:58 am
by jdalrymple
Incidentally, the way I read this it's ignoring the first adapter, not the second.

I don't think this issue is nrpe or nscp related per-se, what I think is that for some reason when your PS1 is being run in the context of the SYSTEM user without the full shell environment you have - the results are different. To test this theory, try running your PS1 as a scheduled task (SYSTEM user) and instead of echoing the output dump it into a file. Because it's the 2nd and because you're getting perfdata I truly think this will be a matter of debugging some WMI and/or NTFS stuff.

Also look for clues in your event viewer.