<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NIC Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/category/vsphere/nic/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/category/vsphere/nic/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Sat, 21 Apr 2012 19:05:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.7</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>NIC Archives - LucD notes</title>
	<link>https://www.lucd.info/category/vsphere/nic/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>vNIC transmit and receive rates</title>
		<link>https://www.lucd.info/2012/04/21/vnic-transmit-and-receive-rates/</link>
					<comments>https://www.lucd.info/2012/04/21/vnic-transmit-and-receive-rates/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sat, 21 Apr 2012 18:59:32 +0000</pubDate>
				<category><![CDATA[NIC]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[Virtual Machine]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[VM]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=3991</guid>

					<description><![CDATA[The VMTN PowerCLI Community is a constant source of inspiration for blog posts. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>The <a href="https://communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli?view=discussions&amp;start=0" target="_blank">VMTN PowerCLI Community</a> is a constant source of inspiration for blog posts. User <a href="https://communities.vmware.com/people/roswellevent" target="_blank">roswellevent</a> raised, in the thread <a href="https://communities.vmware.com/message/2029742#2029742">Any way to get virtual machine Nic card usage?</a>, the question if it was possible to get the <strong>transmit</strong> and <strong>receive</strong> rate for each <strong>vNIC</strong> in <strong>virtual machines</strong>.</p>
<p>Since I&#8217;m interested in all things statistics in vSphere I decided to tackle the question. Finding the metrics to use for this kind of report is not too difficult. Under the <a href="https://pubs.vmware.com/vsphere-50/topic/com.vmware.wssdk.apiref.doc_50/network_counters.html" target="_blank">PerformanceManager Network</a> section we find the metrics <strong>net.received.average</strong> and <strong>net.transmitted.average</strong>. And, provided your <strong>Statistics Level</strong> is set to <strong>3</strong> for the timeframe you want to report on, the metrics capture statistics on the <strong>device level</strong>.</p>
<p>Great, exactly what we need! A quick check in the <strong>Performance</strong> tab in the <strong>vSphere Client</strong> showed an additional problem to solve. The instance didn&#8217;t mention <strong>Network Adapter 1</strong> but a number.</p>
<p><img fetchpriority="high" decoding="async" class="alignnone  wp-image-3994" title="nic-stat1" src="https://lucd.info/wp-content/uploads/2012/04/nic-stat1.png" alt="" width="578" height="284" srcset="https://www.lucd.info/wp-content/uploads/2012/04/nic-stat1.png 825w, https://www.lucd.info/wp-content/uploads/2012/04/nic-stat1-300x147.png 300w" sizes="(max-width: 578px) 100vw, 578px" /></p>
<p><span id="more-3991"></span></p>
<h2>Background</h2>
<p>It turns out that this number is the internal <strong>Key</strong> used for the vNICs. Could have been a bit more user-friendly in the vSphere Client <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The solution is quite simple, the script fetches all the vNICs of a VM and then stores the Key together with the human-understandable name in a <strong>hash table</strong>. This hash table allows the script to replace the <strong>Instance</strong> value by the <strong>Name</strong> of the vNIC.</p>
<h2>The script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Get-VmNicStat{
&lt;#
.SYNOPSIS  Retrieve network rates per vNIC per VM
.DESCRIPTION The function will calculate the average
  transmit receive rate, over a select time interval,
  for each vNIC on the VMs selected.
.NOTES  Author:  Luc Dekens
.PARAMETER VM
  The Virtual Machine(s)
  This is a required parameter.
.PARAMETER Start
  A DateTime value that specifies the start of the timeframe
  you want to use.
  This is a required parameter.
.PARAMETER Finish
  A DateTime value that specifies the end of the timeframe
  you want to use.
  The default is the current time.
.EXAMPLE
  PS&gt; Get-VM | Get-VmNicStat -Start $start
#&gt;

  param(
  [CmdletBinding()]
  [parameter(Mandatory = $true, ValueFromPipeline = $true)]
  [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl[]]$VM,
  [parameter(Mandatory = $true)]
  [System.DateTime]$Start,
  [System.DateTime]$Finish = (Get-Date)
  )

  begin{
    $metrics = &quot;net.received.average&quot;,&quot;net.transmitted.average&quot;
  }

  process{
    $stats = Get-Stat -Entity $VM -Stat $metrics -Start $Start

    foreach($vm in ($stats | Group-Object -Property {$_.Entity.Name})){
      $nicTab = @{}
      $vm.Group[0].Entity.NetworkAdapters | %{
        $key = [string]$_.ExtensionData.Key
        $value = $_.Name
        $nicTab.Add($key,$value)
      }

      $vm.Group | where {$nicTab.ContainsKey($_.Instance)} |
      Group-Object -Property Instance | %{
        New-Object PSObject -Property @{
          VM = $vm.Name
          Start = $Start
          Finish = $Finish
          NIC = $nicTab[$_.Group[0].Instance]
          &quot;Avg Received (KBps)&quot; = [Math]::Round(($_.Group |
            where {$_.MetricId -eq &quot;net.received.average&quot;} |
            Measure-Object -Property Value -Average).Average,1)
          &quot;Avg Transmitted (KBps)&quot; = [Math]::Round(($_.Group |
            where {$_.MetricId -eq &quot;net.transmitted.average&quot;} |
            Measure-Object -Property Value -Average).Average,1)
        }
      }
    }
  }
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 33</strong>: The metrics the functions uses.</p>
<p><strong>Line 37</strong>: Retrieve the statistical data for the virtual machines over the selected timeframe.</p>
<p><strong>Line 39</strong>: The function uses the Group-Object cmdlet to split the returned statistical by VM.</p>
<p><strong>Line 40-45</strong>: Look at all the vNICs connected to the VM and create a hash table, linking the Key with the Name of the vNIC.</p>
<p><strong>Line 47</strong>: The Where-clause will filter out the statistical data for the vNICs of the VM</p>
<p><strong>Line 48</strong>: Use the Group-Object to to split the statistical data per vNIC</p>
<p><strong>Line 49-60</strong>: Create an object per vNIC per VM</p>
<p><strong>Line 53</strong>: Use the hash table to fetch the &#8220;Network Adapter x&#8221; name of the vNIC</p>
<p><strong>Line 54-59</strong>: Calculate the average transmit and recieve values over the selected timeframe.</p>
<h2>Sample usage</h2>
<p>You can use the function like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$vms = Get-VM -Name vm*
Get-VmNicStat -VM $vms -Start (Get-Date).AddHours(-1)</pre><p></p>
<p>And you can also use it in a pipeline, like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VM -Name vm* |
Get-VmNicStat -Start (Get-Date).AddHours(-1) |
Select VM,Start,Finish,NIC,&quot;Avg Received (KBps)&quot;,&quot;Avg Transmitted (KBps)&quot; |
Format-Table -AutoSize</pre><p></p>
<p>The resulting output looks like this</p>
<p><img decoding="async" class="alignnone  wp-image-4000" title="nic-stat2" src="https://lucd.info/wp-content/uploads/2012/04/nic-stat2.png" alt="" width="698" height="82" srcset="https://www.lucd.info/wp-content/uploads/2012/04/nic-stat2.png 872w, https://www.lucd.info/wp-content/uploads/2012/04/nic-stat2-300x35.png 300w" sizes="(max-width: 698px) 100vw, 698px" /></p>
<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2012/04/21/vnic-transmit-and-receive-rates/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
