Created
November 15, 2012 17:07
-
-
Save hroi/4079804 to your computer and use it in GitHub Desktop.
util to help switchport configuration in a vmware environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env jruby | |
| require 'java' | |
| require 'dom4j-1.6.1.jar' | |
| require 'vijava51b20120923.jar' | |
| (server, username, password, clustername) = ARGV | |
| url = "https://#{server}/sdk" | |
| import java.net.URL | |
| # Creating ViJava Module so I don't have to import each com.vmware.vim25.mo class individually. | |
| module VIJava | |
| include_package "com.vmware.vim25.mo" | |
| end | |
| si = VIJava::ServiceInstance.new(URL.new(url), username, password, true) | |
| rootFolder = si.getRootFolder() | |
| clusters = VIJava::InventoryNavigator.new(rootFolder).searchManagedEntities("ClusterComputeResource") | |
| clusters = clusters.find_all {|cluster| cluster.name.match clustername} if clustername | |
| puts "!!" * 40 | |
| puts "!! VM count per VPG in each cluster:" | |
| puts "!!" * 40 | |
| clusters.sort{|a,b| a.name <=> b.name }.each do |cluster| | |
| puts "#{cluster.name}:" | |
| cluster.networks.sort{|a,b| | |
| a.name <=> b.name}.each{|net| | |
| printf("\t%20s: %10d VMs\n",net.name, net.vms.count) } | |
| end | |
| puts | |
| puts "!!" * 40 | |
| puts "!! Unique VLAN ids of all PGs on hosts in each cluster:" | |
| puts "!!" * 40 | |
| clusters.sort{|a,b| a.name <=> b.name }.each do |cluster| | |
| puts "#{cluster.name}:" | |
| # native vlans | |
| vlans_on_hosts = cluster.hosts.collect do |host| | |
| pnics = host.host_network_system.network_config.pnic.collect {|pnic| pnic.device} | |
| host.host_network_system.query_network_hint(pnics).collect do |network_hint| | |
| if network_hint.connected_switch_port.nil? | |
| puts "\t Warning: no connnected switch info on #{host.name}, #{network_hint.device}" | |
| 0 | |
| else | |
| network_hint.connected_switch_port.vlan | |
| end | |
| end | |
| end | |
| vlans_on_hosts << cluster.hosts.collect do |host| | |
| host.host_network_system.network_info.portgroup.collect{|pg| pg.spec.vlanId} | |
| end | |
| vlans_on_hosts.flatten!.sort!.uniq! | |
| printf "\t%s\n\n", vlans_on_hosts.find_all{|vlan| vlan > 0}.join(",") | |
| end | |
| puts | |
| puts "!!" * 40 | |
| puts "!! Cluster: switchport connections" | |
| puts "!!" * 40 | |
| clusters.sort{|a,b| a.name <=> b.name }.each do |cluster| | |
| puts "#{cluster.name}:" | |
| switchmap = {} | |
| cluster.hosts.each do |host| | |
| pnics = host.host_network_system.network_config.pnic.collect {|pnic| pnic.device} | |
| host.host_network_system.query_network_hint(pnics).each do |network_hint| | |
| if network_hint.connected_switch_port.nil? | |
| puts "\tWarning: no connnected switch info on #{host.name}, #{network_hint.device}" | |
| else | |
| dev_id = network_hint.connected_switch_port.dev_id | |
| port_id = network_hint.connected_switch_port.port_id | |
| vlan_id = network_hint.connected_switch_port.vlan | |
| switchmap[dev_id] = [] unless switchmap.include? dev_id | |
| switchmap[dev_id] << port_id | |
| end | |
| end | |
| end | |
| switchmap.each do |dev_id,ports| | |
| printf "\t%s\n", dev_id | |
| ports.sort{|x,y| x.gsub(/[a-zA-Z]/, '').split("/").collect{|x| x.to_i} <=> y.gsub(/[a-zA-Z]/, '').split("/").collect{|x| x.to_i}}.each do |port_id| | |
| printf "\t\t%s\n", port_id | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment