Skip to content

Commit

Permalink
Merge pull request #111 from sensu-plugins/fix/metrics-windows-networ…
Browse files Browse the repository at this point in the history
…k.ps1

WIP: fix for metric-windows-network.ps1 to work for wider range of interface names
  • Loading branch information
derekgroh authored Jul 13, 2020
2 parents 0c096cb + 5ac9dbd commit 17122e8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ This CHANGELOG follows the format listed at [Our CHANGELOG Guidelines ](https://
Which is based on [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]
### Changes
- update tests to favor Pester instead of Ruby (@derekgroh)
### Breaking Changes
- update metrics-windows-network.ps1 -Interface argument handling to work with Interfaces names that contain underscores
### Added
- Add new -ListInterfaces option to metrics-windows-network.ps1 to get a list of valid Network Addresses
- Add new ability to metrics-windows-network.psi to output metrics for all Interfaces if no Interface is specified.

## [3.0.0] - 2019-03-04
### Security
Expand Down
33 changes: 29 additions & 4 deletions bin/metric-windows-network.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

param(
[string[]]$Interfaces,
[switch]$UseFullyQualifiedHostname
[switch]$UseFullyQualifiedHostname,
[switch]$ListInterfaces
)

$ThisProcess = Get-Process -Id $pid
Expand All @@ -40,18 +41,41 @@ if ($UseFullyQualifiedHostname -eq $false) {
}

$perfCategoryID = Get-PerformanceCounterByID -Name 'Network Interface'
if ($perfCategoryID.Length -eq 0 ) {
Write-Host "perfCategoryID: is Null"
Exit 2
}
$localizedCategoryName = Get-PerformanceCounterLocalName -ID $perfCategoryID

for($i = 0; $i -lt $Interfaces.Count; $i+=1) {
$tmp = $Interfaces[$i]
$Interfaces[$i] = $tmp.Replace("_"," ")
$Interfaces[$i] = $tmp.Replace(" ","_")
}

if ($ListInterfaces -eq $true) {
Write-Host "List of Available Interface Names"
Write-Host "Full Name :: Underscore Modified Name"
Write-Host "-------------------------------------"
}
foreach ($ObjNet in (Get-Counter -Counter "\$localizedCategoryName(*)\*").CounterSamples)
{
$instanceName=$ObjNet.InstanceName.ToString().Replace(" ","_")
if ($ListInterfaces -eq $true) {
$str = $ObjNet.InstanceName.ToString()
Write-Host "$str :: $instanceName"
Break
}

if ($Interfaces.Contains($ObjNet.InstanceName)) {
$include = $false
if ($Interfaces.Count -eq 0) {
$include = $true
} else {
if ($Interfaces.Contains($instanceName)) {
$include = $true
}
}

if ( $include -eq $true ) {
$Measurement = ($ObjNet.Path).Trim("\\") -replace "\\","." -replace " ","_" -replace "[(]","." -replace "[)]","" -replace "[\{\}]","" -replace "[\[\]]",""

$Measurement = $Measurement.Remove(0,$Measurement.IndexOf("."))
Expand All @@ -72,4 +96,5 @@ foreach ($ObjNet in (Get-Counter -Counter "\$localizedCategoryName(*)\*").Counte

}

}
}

0 comments on commit 17122e8

Please sign in to comment.