2

I'm using NSCLient to run some NRPE checks on Windows Server boxes. The checks run Powershell scripts but the problem I'm having is NSClient is adding random line breaks after 79 characters. I'm guessing this is because the default Powershell/CMD console width is 80 characters (Is this right?) but I need the check to not add these new lines (They screw up the performance data and Nagios get's upset).

I've tried changing the console size from a standard Powershell instance but that didn't work, is there any way to specify the console size with a command line flag? The help files weren't much use.

An example of the output I'm getting from Nagios' check_nrpe

vmxnet3 ethernet adapter _2:728.370961294234isatap.{1c6435bc-27c9-452b-a6fe-668^M
dced31461}:0| 'Network Usage (Bytes/sec) (vmxnet3 ethernet adapter _2)'=728.370^M
961294234 'Network Usage (Bytes/sec) (isatap.{1c6435bc-27c9-452b-a6fe-668dced31^M
461})'=0

(The issue isn't with the control characters, Nagios doesn't seem to notice them, but the new-lines themselves)

Powershell script;

#
# Check Network Usage
#

#Check for argument variables
if ( $($args.count) -lt 2 ) {
    "Usage: ./check_network.ps1 [warn level] [crit level]"
    exit 3
}

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

#Create buffers for the Nagios output
$ExitStatus = 0
$TextOP = ""
$PerfDataOP = ""

#Get the average network usage for the last 5 seconds
$PerfData = Get-Counter -Counter "\network interface(*)\bytes total/sec" -SampleInterval 5 -MaxSamples 1

foreach($InterfacePerf in $PerfData.CounterSamples) {
    #Write the outputs
    $TextOP = %{"{0}" -f $TextOP,$InterfacePerf.InstanceName,$InterfacePerf.CookedValue}
    $PerfDataOP = %{"{0} 'Network Usage (Bytes/sec) ({1})'={2}" -f $PerfDataOP,$InterfacePerf.InstanceName,$InterfacePerf.CookedValue}

    #Trigger alerts

    #If level is higher than crit, and the status is 'OK' or 'Warn'
    if ( $InterfacePerf.CookedValue -gt $crit -and $ExitStatus -lt 2 ) {
        $ExitStatus = 2
    } elseif ( $InterfacePerf.CookedValue -gt $warn -and $ExitStatus -eq 0 ) {#warning
        $ExitStatus = 1
    }
}

#Exit and return status
"$TextOP|$PerfDataOP"
exit $ExitStatus
5
  • could you post the powershell that is causing the issue? I suspect that using Out-String is the solution, but seeing the code would help.
    – EBGreen
    Aug 8, 2011 at 14:13
  • @EBGreen Updated
    – user80776
    Aug 8, 2011 at 14:15
  • The first thing I would question is why you have this apparently extraneous loop here: $TextOP = %{"{0}" -f $TextOP,$InterfacePerf.InstanceName,$InterfacePerf.CookedValue} I don't think that is causing your problem but you may want to look at it.
    – EBGreen
    Aug 8, 2011 at 14:39
  • @EBGreen I come from a UNIX background, this is the first Powershell script I've ever written so there are probably some issues =) What would be a better way to go about adding data to the end of that string?
    – user80776
    Aug 8, 2011 at 14:57
  • If I understand your goal exactly then I think this would work: $TextOP += "{0}" -f $TextOP,$InterfacePerf.InstanceName,$InterfacePerf.CookedValue My comments were not meant as a criticism by the way. I'm still always learning PS, so there may have been some perfectly valid reason for what you did that I just didn't know :)
    – EBGreen
    Aug 8, 2011 at 15:28

2 Answers 2

0

I had success with the following code, with a test sample to generate a long string...

if( $Host -and $Host.UI -and $Host.UI.RawUI ) {
  $rawUI = $Host.UI.RawUI
  $oldSize = $rawUI.BufferSize
  $typeName = $oldSize.GetType( ).FullName
  $newSize = New-Object $typeName (101, $oldSize.Height)
  $rawUI.BufferSize = $newSize
}

$longstring = ""
1..200 | %{$longstring += "a"}

$longstring

Obviously the most important bit is the top section. $longstring is just me making a long string. You could also shorten this to:

$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (101, 25)

This removes any kind of testing before setting values.

I set the width to 101 so I could see a clear line wrap.

0

I don't have a way to test your situation, but this will hopefully address it:

"$TextOP|$PerfDataOP" | Out-String -Width 1024
2
  • Nope, same thing =(
    – user80776
    Aug 8, 2011 at 14:59
  • Hmmm...I need to find a way to replicate your issue. I suck at figuring things out in my head.
    – EBGreen
    Aug 8, 2011 at 15:28

You must log in to answer this question.