Thursday, August 18, 2011

Pulling CDP info from each vmnic


I dont trust the ‘Network Hint’ info you get from the vSphere client. Having said that, if you do not have access to the physical switches to query the config, it can be the only thing we have from the ESX side.
I needed to try to ascertain whether the VLAN trunks on the physical switch ports for my hosts were configured with the same VLANs and so threw this together.


Basically it will go to each host, and each pNic in each host and pull the observed VLANs and Subnets and store them in some tables.

########################################################################### 
#  
# NAME: ShowpNicCPD.ps1
#
# AUTHOR:  winter@shonkyholdings.com
#
# COMMENT:     gets the VLANs and Subnets observed for each pnic in each ESX host.
#             shoves all this into a bunch of hashtables described below.
#
#    $Report = @{"hostname","$HostElement"}
#    $HostElement = @{"vmnic","$pNicElement"}
#    $pNicElement = @{"vlan","subnet"}
#
# VERSION HISTORY:
# 1.0 18/08/2011 - Initial release
#
###########################################################################
$vCenter = "my.vcenter.server"
Connect-VIServer $vCenter 
$Report = @{}

$HostElement = @{}

$pNicElement = @{}
$AllHostsView = Get-View -ViewType HostSystem

foreach ($HostView in $AllHostsView){
    $HostNetView = Get-View $HostView.ConfigManager.NetworkSystem
    foreach ($pNic in $HostNetView.NetworkInfo.Pnic){
        $HostNicCDP = $HostNetView.QueryNetworkHint($pNic.Device)
        foreach ($Subnet in $HostNicCDP.SyncRoot){
            foreach ($NetHint in $Subnet.Subnet){
                $pNicElement.set_Item($NetHint.VlanID,$NetHint.IpSubnet)
            }
        }
        $HostElement.set_Item($pNic.Device,$pNicElement.Clone())
        $pNicElement = @{}
    }
    $Report.set_Item($HostView.Name,$HostElement.Clone())
    $HostElement = @{}
}


You will end up with a nice table with all your VLAN & Subnet data. Of course you would want to do *something* with it.

You could put a little wrapper over it to ask the user which host/nic they want detail on:

$HostList = Get-View -ViewType HostSystem 
[int]$num = 0 
Write-Host Select the ESX Host by choosing its number 
foreach ($esx in $HostList){ 
    Write-Host $num : $esx.Name 
    $num++ 
} 
[int]$hostnum = Read-Host Choose..... 
$SelectedHost = $HostList[$hostnum] 
$SelectedNic = Read-Host Please type in vmnic to report on, e.g. vmnic3 
write-host Vlans Observed for $SelectedHost.Name on pNic $SelectedNic 
$Report[$SelectedHost.Name].get_Item($selectednic)


Select the ESX Host by choosing its number
0 : esx01.shonkyholdings.com
1 : esx02.shonkyholdings.com
2 : esx03.shonkyholdings.com
Choose.....: 1
Please type in vmnic to report on e.g. vmnic3: vmnic2
Vlans Observed for esx02.shonkyholdings.com on pNic vmnic2
Key : 50
Value : 10.50.20.128-10.50.20.254
Name : 50

Key : 145
Value : 10.38.91.32-10.38.91.33
Name : 145

Key : 200
Value : 10.6.200.1-10.6.200.127
Name : 200


Im a little lazy in the formatting, as I spend 99% of my time in my IDE, which does all this for me :)

Or you could dump the whole lot out, ugly as it may be...

foreach ($VMHost in $Report.Keys){ 
    foreach ($NicSet in $Report.get_Item($VMHost)){ 
        foreach ($vmnic in $NicSet.Keys){ 
        Write-Host $VMHost $vmnic Vlans and Observed IP Ranges 
        $NicSet[$vmnic] 
        Write-Host 
        } 
    } 
}


Or of course you could do something more interesting with it, like group the hosts by cluster, then look for any entries that are not similiar, or map the nics to the vSwitches they support and compare across that...

If anyone wants I'll write that up ;)

2 comments:

winter said...

remember that all this stuff is what has been observed, and does not represent the actual config. If you see differences in the data, this may not be an issue, but rather something to have a closer look at....

vmware jobs in hyderabad said...

I admire the valuable information you offer in your articles. I will bookmark your blog and have my friends check up here often. I am quite sure they will learn lots of new stuff here than anybody else! Regards, vmware jobs in hyderabad

Labels

blogs I read

Authors

My photo

Enjoys spending time in layer 6 & 7. Passionate about delivering quality education.
Based in Sydney.

certz