VMware Cloud Community
tekhie
Contributor
Contributor
Jump to solution

Adding vswitch and portgroup information to a stat report

hi - i have a script that details stats for previous 24 hours. I am trying to amend so that it displays the vSwitch name the VMNIC is attached to (ie vSwitch0, vSwitch1 etc etc). I have been trying to get the information to show but i either get a blank column, or i get all the vswitches listed in each row. Any advice ? Thanks in advance

$date = get-date

$vccred = import-pscredential -Path xxxxxx

connect-VIServer -Server xxxxxx -Credential $vccred

$metrics = "net.received.average","net.transmitted.average"

$todayMidnight = Get-Date -Hour 0 -Minute 0 -Second 0

$start = $todayMidnight.AddDays(-1).AddSeconds(1)

$finish = $todayMidnight

foreach ($cluster in Get-cluster | sort-object Name)

{

ConvertTo-Html –body "

$cluster

" | Out-File -append $htmlNICstats

$clusterTmp = @()
write-host " > " $cluster
foreach ($esxImpl in (get-vmhost -Location $cluster | Sort-Object Name))
{
write-host " > " $esxImpl

$ESXHostTMP = @()

$esx = $esxImpl | Get-View

foreach ($vmhost in $esx){

$stats = Get-Stat -Entity $esximpl -Stat $metrics -Start $start -Finish $finish

$stats | Group-Object -Property Instance | where {$_.Name -ne ""} | %{

$row = "" | Select clustername , vswitch , "ESX Name", Date , NIC ,"Max Send MBps","Max Received MBps"

$row.clustername = $cluster.name

$row.vswitch =

$row."ESX Name" = $_.Group[0].Entity.Name

$row.Date = $start.ToShortDateString()

$row.NIC = $_.Group[0].Instance

$row."Max Send MBps" = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.transmitted.average"} | Measure-Object -Property Value -Maximum).Maximum/1KB)

$row."Max Received MBps" = "" -f (($_.Group | where {$_.MetricId -eq "net.received.average"} | Measure-Object -Property Value -Maximum).Maximum/1KB)

$ESXHostTMP += $row

$Report = $Report + $row

}

}

$ESXHostTMP | sort-object Nic | ConvertTo-Html -property clustername , vswitch , "ESX Name" , Date , NIC , "Max Send MBps","Max Received MBps" | Out-File -append $htmlNICStats

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The script only looks at the active NICs.

$pg = $vmhost.Config.Network.Portgroup |where {$_.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -contains $group.group[0].Instance} | %{$_.Spec.Name}

If you want to include the standby NICs as well, that line should be

$pg = $vmhost.Config.Network.Portgroup |where {$_.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -contains $group.group[0].Instance -or $_.ComputedPolicy.NicTeaming.NicOrder.standbyNic -contains $group.group[0].Instance} | %{$_.Spec.Name}

Let me know if that gives the result you expect.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Try this

$vccred = import-pscredential -Path xxxxxx
connect-VIServer -Server xxxxxx -Credential $vccred

$htmlNICstats = "C:\htmlnicstats.html"
$report = @()

$metrics = "net.received.average","net.transmitted.average"
$todayMidnight = Get-Date -Hour 0 -Minute 0 -Second 0
$start = $todayMidnight.AddDays(-1).AddSeconds(1)
$finish = $todayMidnight

foreach ($cluster in Get-cluster | sort-object Name)
{
	ConvertTo-Html –body "
$cluster
	" | Out-File -append $htmlNICstats

	$clusterTmp = @()
	write-host " > " $cluster
	foreach ($esxImpl in (get-vmhost -Location $cluster | Sort-Object Name))
	{
		write-host " > " $esxImpl

		$ESXHostTMP = @()
		$esx = $esxImpl | Get-View

		foreach ($vmhost in $esx){

			$stats = Get-Stat -Entity $esximpl -Stat $metrics -Start $start -Finish $finish
			$stats | Group-Object -Property Instance | where {$_.Name -ne ""} | %{
				$group = $_
				$row = "" | Select clustername , vswitch , "ESX Name", Date , NIC ,"Max Send MBps","Max Received MBps"
				$row.clustername = $cluster.name
				$row.vswitch = (Get-VirtualSwitch  -VMHost $group.Group[0].Entity | where {$_.Nic -contains $group.group[0].Instance}).Name
				$row."ESX Name" = $_.Group[0].Entity.Name
				$row.Date = $start.ToShortDateString()
				$row.NIC = $_.Group[0].Instance
				$row."Max Send MBps" = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.transmitted.average"} | Measure-Object -Property Value -Maximum).Maximum/1KB)
				$row."Max Received MBps" = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.received.average"} | Measure-Object -Property Value -Maximum).Maximum/1KB)
				$ESXHostTMP += $row
				$Report = $Report + $row
			}
		}
		$ESXHostTMP | sort-object Nic | ConvertTo-Html -property clustername , vswitch , "ESX Name" , Date , NIC , "Max Send MBps","Max Received MBps" | Out-File -append $htmlNICStats
	}
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
tekhie
Contributor
Contributor
Jump to solution

Hi Luc - fabulous ... if i want to take it one step further and include the portgroup (as i have some vswitches with 2 portgroups) how would this be accomplished ? I am able to modify the script so that if ony one portgroup on a vSwitch i get the portgroup name listed. However for the vSwitches with more than one portgroup it simply displays blank or system.object[] (or something similar)

tks

LucD <communities-emailer@vmware.com>

21/10/10 12:09

To

Chris SALES/gb/socgen@socgen

cc

Subject

New message: "Re: Adding vswitch and portgroup information to a stat report"

Chris Sales,

A new message was posted in the thread "Adding vswitch and portgroup information to a stat report":

http://communities.vmware.com/message/1632936#1632936

Author : LucD

Profile : http://communities.vmware.com/people/LucD

Message:

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try the attached script.

I had to use the Join function because multiple portgroups on a vswitch can be using the same vnic.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
tekhie
Contributor
Contributor
Jump to solution

hi luc - thanks for the script - i get the follwoing error when running however...

Exception calling "Join" with "2" argument(s): "Value cannot be null.

Parameter name: value"

At :line:55 char:35

+ $row.portgroup = ::Join <<<< (",",($vmhost.Config.Network.Portgroup | where {$_.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -contains $group.group[0].Instance} | %{$_.Spec.Name}))

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that you have a portgroup with no active NICs ?

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
tekhie
Contributor
Contributor
Jump to solution

hi luc - all the vswitches have portgroups with 1 active nic. There are no portgroups with no vmnics assigned to them.

0 Kudos
tekhie
Contributor
Contributor
Jump to solution

hi luc - all the vswitches have portgroups with 1 active nic. There are no portgroups with no vmnics assigned to them.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Finally figured it out.

I got the same error when there are vnics that are not present in any portgroup or that are used in a dvSwitch.

Attached code is updated to catch that.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
tekhie
Contributor
Contributor
Jump to solution

hi luc - thats great - almost there. Not sure if this is specific to my environment but when i run it i get the output as shown in the attached file. For vSwitch 0 i get the portgroup name displayed for both vmnics. For the other vswitches where there is more than one vmnic the portgroup name is only displayed by one of the vmnics. each vswitch is set up with a standby / active vmnic. any ideas as to why ?

(in the attached output you can see that vSwitch1,2 and 4 only have one entry for the portgroup name when there should be 2)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The script only looks at the active NICs.

$pg = $vmhost.Config.Network.Portgroup |where {$_.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -contains $group.group[0].Instance} | %{$_.Spec.Name}

If you want to include the standby NICs as well, that line should be

$pg = $vmhost.Config.Network.Portgroup |where {$_.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -contains $group.group[0].Instance -or $_.ComputedPolicy.NicTeaming.NicOrder.standbyNic -contains $group.group[0].Instance} | %{$_.Spec.Name}

Let me know if that gives the result you expect.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
tekhie
Contributor
Contributor
Jump to solution

Hi Luc - thats perfect - thanks so much for all the help.

0 Kudos