VMware Cloud Community
MikeR3
Contributor
Contributor

Retrieving portgroup traffic shaping values

I've written a script to modify the traffic shaping values and run it against our vSphere environment (standard switches). It works fine to set the values, but given the size of our environment, I'd like to run an audit in case any hosts were missed.

Thus far, I haven't been able to figure out how to obtain those values, if anyone has a code sample, please post it, thanks.

 

 

 

 

0 Kudos
8 Replies
LucD
Leadership
Leadership

You can use the Get-Stat cmdlet with the Realtime switch, but be aware that the values returned are averages over a 20 second interval.
Something like this

$esxName = 'MyEsx'

$esx = Get-VMHost -Name $esxName

Get-VirtualSwitch -VMHost $esx -Standard -PipelineVariable vss |
ForEach-Object -Process {
    $pnic = (Get-VMHostNetworkAdapter -VMHost $esx -VirtualSwitch $vss).Name

    Get-Stat -Entity $esx -Stat 'net.transmitted.average', 'net.received.average' -Realtime -MaxSamples 1 |
    Where-Object { $_.Instance -in $pnic } |
    Group-Object -Property Instance |
    ForEach-Object -Process {
        New-Object -TypeName PSObject -Property ([ordered]@{
                VMHost = $esx.Name
                VSS = $vss.Name
                PNic = $_.Name
                ReceivedKBps = $_.Group.Where{ $_.MetricId -eq 'net.received.average' }.Value
                TransmittedKBps = $_.Group.Where{ $_.MetricId -eq 'net.transmitted.average' }.Value
            })
    }
}

You can also use the Get-EsxTop cmdlet, which will give you values over a 5 second interval, similar to what esxtop is showing.

$esxName = 'MyEsx'

$esx = Get-VMHost -Name $esxName

Get-VirtualSwitch -VMHost $esx -Standard -PipelineVariable vss |
ForEach-Object -Process {
  $pnic = (Get-VMHostNetworkAdapter -VMHost $esx -VirtualSwitch $vss -Server esx1.local.lab).Name
  $ports = (Get-EsxTop -CounterName PNIC -Server esx1.local.lab | where{$_.PNICName -in $pnic}).UplinkPort
  Get-EsxTop -CounterName NetPort -Server esx1.local.lab | where{$_.PortId -in $ports} |
  ForEach-Object -Process {
    New-Object -TypeName PSObject -Property ([ordered]@{
      VMHost = $esx.Name
      VSS = $vss.Name
      PNic = $_.ClientName
      ReceivedMBps = [math]::Round($_.NumOfRecvBytes/1MB/5,0)
      TransmittedMBps = [math]::Round($_.NumOfSendBytes / 1MB/5, 0)
    })
  }
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MikeR3
Contributor
Contributor

Thanks Luc, that looks like useful code to have in my library.

Actually, what I'm looking to obtain here is the traffic shaping settings per portgroup, screen shot below.

I have a script to change from default, but when I tried to modify the code to grab what the current settings are, the values are blank.

0 Kudos
LucD
Leadership
Leadership

Ok, seems I misread your question, I thought you wanted performance data to check the shaping.

If you just want the configuration of the Shaping Policy per Portgroup you could do something like this.

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
  $netSys = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem
  $netSys.NetworkConfig.Portgroup |
  ForEach-Object -Process {
    $obj = New-Object -TypeName PSObject -Property ([ordered]@{
        VMHost = $esx.Name
        VSS = $_.Spec.vSwitchName
        Portgroup = $_.Spec.Name
        ShapingEnabled = 'na'
        AvgBandwidth = 'na'
        PeakBandWidth = 'na'
        BurstSize = 'na'
      })
    if ($_.Spec.Policy.ShapingPolicy -ne $null) {
      $obj.ShapingEnabled = $_.Spec.Policy.ShapingPolicy.Enabled
      $obj.AvgBandwidth = $_.Spec.Policy.ShapingPolicy.AverageBandwidth
      $obj.PeakBandwidth = $_.Spec.Policy.ShapingPolicy.PeakBandwidth
      $obj.BurstSize = $_.Spec.Policy.ShapingPolicy.BurstSize
    }
    $obj
  }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MikeR3
Contributor
Contributor

Thanks Luc, that worked great. It's interesting that the default portgroups are still showing blank for values, but for the ones which I've explicitly overridden, it reports the settings.

 

 

0 Kudos
LucD
Leadership
Leadership

I suspect that the ones for which you see blanks are not overriding the vSwitch settings for traffic shaping.
We could look at the NetworkInfo.Portgroup.ComputedPolicy, this should also show the values for the Portgroups that do not override the vSwitch settings.

 

Get-VMHost -Name MyEsx -PipelineVariable esx |
ForEach-Object -Process {
  $netSys = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem
  $netSys.NetworkInfo.Portgroup |
  ForEach-Object -Process {
    $obj = New-Object -TypeName PSObject -Property ([ordered]@{
        VMHost = $esx.Name
        VSS = $_.Spec.vSwitchName
        Portgroup = $_.Spec.Name
        OverrideVSS = 'na'
        ShapingEnabled = 'na'
        AvgBandwidth = 'na'
        PeakBandWidth = 'na'
        BurstSize = 'na'
      })
    if ($_.Spec.Policy.ShapingPolicy -ne $null) {
      $obj.OverrideVSS = $true
      $obj.ShapingEnabled = $_.Spec.Policy.ShapingPolicy.Enabled
      $obj.AvgBandwidth = $_.Spec.Policy.ShapingPolicy.AverageBandwidth
      $obj.PeakBandwidth = $_.Spec.Policy.ShapingPolicy.PeakBandwidth
      $obj.BurstSize = $_.Spec.Policy.ShapingPolicy.BurstSize
    }
    elseif($_.ComputedPolicy.ShapingPolicy -ne $null){
      $obj.OverrideVSS = $false
      $obj.ShapingEnabled = $_.ComputedPolicy.ShapingPolicy.Enabled
      $obj.AvgBandwidth = $_.ComputedPolicy.ShapingPolicy.AverageBandwidth
      $obj.PeakBandwidth = $_.ComputedPolicy.ShapingPolicy.PeakBandwidth
      $obj.BurstSize = $_.ComputedPolicy.ShapingPolicy.BurstSize
    }
    $obj
  }
}

 




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MikeR3
Contributor
Contributor

Thank you. One more question, is there a way to filter this for only a specific vSwitch on a host? I've tried a couple of tweaks, but no luck.

0 Kudos
LucD
Leadership
Leadership

You can insert a Where-clause, something like this

Get-VMHost -Name MyEsx -PipelineVariable esx |
ForEach-Object -Process {
  $netSys = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem
  $netSys.NetworkInfo.Portgroup |
  where{$_.Spec.vSwitchName -eq 'MyVSS'} |
  ForEach-Object -Process {
    $obj = New-Object -TypeName PSObject -Property ([ordered]@{
        VMHost = $esx.Name
        VSS = $_.Spec.vSwitchName
        Portgroup = $_.Spec.Name
        OverrideVSS = 'na'
        ShapingEnabled = 'na'
        AvgBandwidth = 'na'
        PeakBandWidth = 'na'
        BurstSize = 'na'
      })
    if ($_.Spec.Policy.ShapingPolicy -ne $null) {
      $obj.OverrideVSS = $true
      $obj.ShapingEnabled = $_.Spec.Policy.ShapingPolicy.Enabled
      $obj.AvgBandwidth = $_.Spec.Policy.ShapingPolicy.AverageBandwidth
      $obj.PeakBandwidth = $_.Spec.Policy.ShapingPolicy.PeakBandwidth
      $obj.BurstSize = $_.Spec.Policy.ShapingPolicy.BurstSize
    }
    elseif($_.ComputedPolicy.ShapingPolicy -ne $null){
      $obj.OverrideVSS = $false
      $obj.ShapingEnabled = $_.ComputedPolicy.ShapingPolicy.Enabled
      $obj.AvgBandwidth = $_.ComputedPolicy.ShapingPolicy.AverageBandwidth
      $obj.PeakBandwidth = $_.ComputedPolicy.ShapingPolicy.PeakBandwidth
      $obj.BurstSize = $_.ComputedPolicy.ShapingPolicy.BurstSize
    }
    $obj
  }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MikeR3
Contributor
Contributor

Thank you! That worked great.

0 Kudos