VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

get-stat network script

I'm looking to use get-stat to get some network related info.  Here is what I need to do:

select a group of vms

for each vm, select only virtual network adapter 1 (vnic1) not vnic 2. (some vms have 2 or more vnics)

find the avg peak throughput in Mbps for that virtual network adapter only.  Make it as accurate as I can based on statistics level 1.   Peak of last 24 hours is OK. (I'd like to plan for the peaks)

Create a table such as below I can export to csv.

Ideas?

Thanks!

VirtualMachineNameNetworkAdapater1PeakThroughputMbps
vm1136
vm2234
vm316
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

In your case you would get all the VMs in the cluster.

$vmName = 'ClusterVM*'

$vnicName = 'Network adapter 1'

$vms = Get-VM -Name $vmName

$vnic = Get-NetworkAdapter -VM $vm -Name $vnicName

$stat = 'net.transmitted.average','net.received.average'

$report = Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 6 -Instance $vnic.ExtensionData.Key |

Group-Object -Property Entity,Timestamp |

Select @{N='Timestamp';E={$_.Group[0].Timestamp}},

  @{N='Entity';E={$_.Group[0].Entity}},

  @{N='Received';E={$_.Group | where{$_.MetricId -match 'received'} | Select -ExpandProperty Value}},

  @{N='Transmitted';E={$_.Group | where{$_.MetricId -match 'transmitted'} | Select -ExpandProperty Value}},

  @{N='Throughput (KBps)';E={$_.Group | Measure-Object -Property Value -Sum | Select -ExpandProperty Sum}}

# Total

$totals = $report | Measure-Object -Property Received,Transmitted,'Throughput (KBps)' -Sum

$report += New-Object PSObject -Property @{

  Entity = 'Totals'

  Received = $totals | where{$_.Property -eq 'Received'} | Select -ExpandProperty Sum

  Transmitted = $totals | where{$_.Property -eq 'Transmitted'} | Select -ExpandProperty Sum

  'Throughput (KBps)' = $totals | where{$_.Property -eq 'Throughput (KBps)'} | Select -ExpandProperty Sum

}

$report | Format-Table -AutoSize


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

View solution in original post

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

The counter net.throughput.usage.average per instance is statistics level 4.

You can go for the summation of net.received.average and net.transmitted.average, but these 2 are statistics level 3 for instances.

So I don't get what you mean by your statistics level 1 comment.

And there is an issue with these counters in vSphere 5.5, see KB2083590


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

TheVMinator
Expert
Expert
Jump to solution

OK thanks.  I wrongly assumed I could do this with only statistics level 1 enabled.  What if I were to be willing to be content with just realtime metrics, could I run a script that would create a snapshot of the above requirements just for the present moment and get a rough idea what was happening at a given moment?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, these are available in the Realtime interval (for +/- 1 hour), but only transmitted and received, not the aggregated throughput.

As you can see with my Stats Toolbox :smileygrin:

To get the throughput value, just do a summation of these 2

net-stat-realtime.png


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

TheVMinator
Expert
Expert
Jump to solution

OK great.  Is net.received.average measured in units of percentage, or in units of Kbps?  (In order for this to work I have to be able to get the value in Kbps / Mbps)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In KBps, see the PerformanceManager table.

net-stat-kbps.png


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

Reply
0 Kudos
TheVMinator
Expert
Expert
Jump to solution

OK thanks - problem though:  In the "entity" column, the only metrics that give throughput for virtual NICS appear to be level 3 metrics.

I don't see any level 1 metrics for vitul nics.  The level 1 metrics all appear to be for the entire virtual machine, and don't break out the statistics on a single vm by vnic.

http://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.wssdk.apiref.doc/network_counters.html

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

For the Realtime interval the Statistics Levels do not play, those are only for the historical intervals.

Every counter, except for the aggregated ones, are available in the Realtime interval


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

Reply
0 Kudos
TheVMinator
Expert
Expert
Jump to solution

OK thanks.  It appears not to work though.

$vnic1s = get-vm vm100  | get-networkadapter | where {$_.name -eq 'Network adapter 1'}

get-stat -entity $vnic1s -stat net.received.average -realtime

get-stat : 5/23/2015 12:55:06 AM    Get-Stat        Invalid entity type. Performance statistics are collected only for Clusters, Hosts, Virtual Machines, Resource Pools, Datastores,

Datastore Clusters, Datacenters, VApps and VIServers.   

At  char:1

+ get-stat -entity $vnic1s -stat net.received.average -realtime

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-Stat], InvalidArgument

    + FullyQualifiedErrorId : Core_GetStat_TryValidateParameterList_InvalidType,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats

Are you sure why I'm trying to do is possible (compare net.received.average between network adapter 1 and network adapter 2 on the same vm)?  It seems like this error is saying that virtual machines are valid entities but virtual nics on those VMs are not. 

thoughts?

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should retrieve those counters on a VM or an ESXi node, not on the network adapter.

With the Instance property you can filter which vnic you want to report on.


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

Reply
0 Kudos
TheVMinator
Expert
Expert
Jump to solution

Capture - Copy.PNG

OK thanks - ran this code and looked at the instance property

$a = get-stat -entity (get-vm vm100) -stat net.received.average -realtime

Under $a -> syncroot -> [1] -> instance

I see this: vmnic0

vmnic0 uplink that provides connectivity to a port group.  I'm not sure it would help to get statistics on that because that vmnic carries traffic for every VM on the port group.

what I am trying to find is the traffic statistics for the virtual machines own vmxnet3 network adapter 1, and distinguish that from networkadapter2.

For example, I see the network adapter 1 in the following location:

$a -> syncroot -> [1] -> entity -> networkadapters -> [1]

Is it possible to get traffic just on this networkadapter1?

I see a "value" field for the object as a whole in the property inspector, but I don't see a "value" field associated with the individual network adapter. 

Any ideas?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should see one or more instances with a number (for example 4000).

This number corresponds with the Key of a vNIC on your VM.

Something like this for example (looks at the last 2 minutes)

$vmName = 'MyVM'

$vnicName = 'Network adapter 1'

$vm = Get-VM -Name $vmName

$vnic = Get-NetworkAdapter -VM $vm -Name $vnicName

$stat = 'net.transmitted.average','net.received.average'

Get-Stat -Entity $vm -Stat $stat -Realtime -MaxSamples 6 -Instance $vnic.ExtensionData.Key |

Group-Object -Property Entity,Timestamp |

Select @{N='Timestamp';E={$_.Group[0].Timestamp}},

  @{N='Entity';E={$_.Group[0].Entity}},

  @{N='Received';E={$_.Group | where{$_.MetricId -match 'received'} | Select -ExpandProperty Value}},

  @{N='Transmitted';E={$_.Group | where{$_.MetricId -match 'transmitted'} | Select -ExpandProperty Value}},

  @{N='Throughput (KBps)';E={$_.Group | Measure-Object -Property Value -Sum | Select -ExpandProperty Sum}} |

Format-Table -AutoSize


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

Reply
0 Kudos
TheVMinator
Expert
Expert
Jump to solution

OK great  - thanks.  One more thing - what if I want to do this for a group of VMs and create a total?  Should create a loop and run this for each Vm or can I put a variable representing a collection of VMs into a single get-stat call?

For example if I'm looking to get the total throughput of all "networkadapter1's" and network adapter2s put together for all VMs in cluster1

VMgroup
Total_Throughput_NetworkAdapter1
TotalThroughputNetworkAdapter2
Cluster13450Mbps2000Mbps
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

In your case you would get all the VMs in the cluster.

$vmName = 'ClusterVM*'

$vnicName = 'Network adapter 1'

$vms = Get-VM -Name $vmName

$vnic = Get-NetworkAdapter -VM $vm -Name $vnicName

$stat = 'net.transmitted.average','net.received.average'

$report = Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 6 -Instance $vnic.ExtensionData.Key |

Group-Object -Property Entity,Timestamp |

Select @{N='Timestamp';E={$_.Group[0].Timestamp}},

  @{N='Entity';E={$_.Group[0].Entity}},

  @{N='Received';E={$_.Group | where{$_.MetricId -match 'received'} | Select -ExpandProperty Value}},

  @{N='Transmitted';E={$_.Group | where{$_.MetricId -match 'transmitted'} | Select -ExpandProperty Value}},

  @{N='Throughput (KBps)';E={$_.Group | Measure-Object -Property Value -Sum | Select -ExpandProperty Sum}}

# Total

$totals = $report | Measure-Object -Property Received,Transmitted,'Throughput (KBps)' -Sum

$report += New-Object PSObject -Property @{

  Entity = 'Totals'

  Received = $totals | where{$_.Property -eq 'Received'} | Select -ExpandProperty Sum

  Transmitted = $totals | where{$_.Property -eq 'Transmitted'} | Select -ExpandProperty Sum

  'Throughput (KBps)' = $totals | where{$_.Property -eq 'Throughput (KBps)'} | Select -ExpandProperty Sum

}

$report | Format-Table -AutoSize


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

Reply
0 Kudos
TheVMinator
Expert
Expert
Jump to solution

This is great MUCH appreciated!

Reply
0 Kudos