Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rbvmomi'
- require 'trollop'
- def connect(host, user, password)
- vim_opts = {
- :ns => 'urn:vim25',
- :rev => '4.1',
- :host => host,
- :ssl => true,
- :insecure => true,
- :path => '/sdk',
- :port => 443,
- }
- RbVmomi::VIM.new(vim_opts).tap do |vim|
- vim.rev = vim.serviceContent.about.apiVersion
- vim.serviceContent.sessionManager.Login(:userName => user, :password => password)
- end
- end
- def select_set
- [
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsFolder',
- :type => 'Folder',
- :path => 'childEntity',
- :skip => false,
- :selectSet => [
- RbVmomi::VIM.SelectionSpec(:name => 'tsFolder'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToDatastoreFolder'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToHostFolder'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToNetworkFolder'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToVmFolder'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsComputeResourceToHost'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsComputeResourceToResourcePool'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToResourcePool'),
- RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToVm')
- ]
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsDatacenterToDatastoreFolder',
- :type => 'Datacenter',
- :path => 'vmFolder',
- :skip => false,
- :selectSet => [
- RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
- ]
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsDatacenterToHostFolder',
- :type => 'Datacenter',
- :path => 'hostFolder',
- :skip => false,
- :selectSet => [
- RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
- ]
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsDatacenterToNetworkFolder',
- :type => 'Datacenter',
- :path => 'networkFolder',
- :skip => false,
- :selectSet => [
- RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
- ]
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsDatacenterToVmFolder',
- :type => 'Datacenter',
- :path => 'vmFolder',
- :skip => false,
- :selectSet => [
- RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
- ]
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsComputeResourceToHost',
- :type => 'ComputeResource',
- :path => 'host',
- :skip => false,
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsComputeResourceToResourcePool',
- :type => 'ComputeResource',
- :path => 'resourcePool',
- :skip => false,
- :selectSet => [
- RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToResourcePool')
- ]
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsResourcePoolToResourcePool',
- :type => 'ResourcePool',
- :path => 'resourcePool',
- :skip => false,
- :selectSet => [
- RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToResourcePool')
- ]
- ),
- RbVmomi::VIM.TraversalSpec(
- :name => 'tsResourcePoolToVm',
- :type => 'ResourcePool',
- :path => 'vm',
- :skip => false,
- )
- ]
- end
- def object_set(obj)
- [RbVmomi::VIM::ObjectSpec(:obj => obj, :selectSet => select_set)]
- end
- EmsRefreshPropMap = {
- :VirtualMachine => {
- :baseName => "@virtualMachines",
- :keyPath => ['summary', 'config', 'vmPathName'],
- :keyPath2 => ['summary', 'config', 'vmLocalPathName'],
- :props => [
- "availableField",
- "config.cpuAffinity.affinitySet",
- "config.cpuHotAddEnabled",
- "config.cpuHotRemoveEnabled",
- "config.defaultPowerOps.standbyAction",
- "config.hardware.device",
- "config.hardware.numCoresPerSocket",
- "config.hotPlugMemoryIncrementSize",
- "config.hotPlugMemoryLimit",
- "config.memoryHotAddEnabled",
- "config.version",
- "datastore",
- "guest.net",
- "resourceConfig.cpuAllocation.expandableReservation",
- "resourceConfig.cpuAllocation.limit",
- "resourceConfig.cpuAllocation.reservation",
- "resourceConfig.cpuAllocation.shares.level",
- "resourceConfig.cpuAllocation.shares.shares",
- "resourceConfig.memoryAllocation.expandableReservation",
- "resourceConfig.memoryAllocation.limit",
- "resourceConfig.memoryAllocation.reservation",
- "resourceConfig.memoryAllocation.shares.level",
- "resourceConfig.memoryAllocation.shares.shares",
- "snapshot",
- "summary.vm",
- "summary.config.annotation",
- "summary.config.ftInfo.instanceUuids",
- "summary.config.guestFullName",
- "summary.config.guestId",
- "summary.config.memorySizeMB",
- "summary.config.name",
- "summary.config.numCpu",
- "summary.config.template",
- "summary.config.uuid",
- "summary.config.vmPathName",
- "summary.customValue",
- "summary.guest.hostName",
- "summary.guest.ipAddress",
- "summary.guest.toolsStatus",
- "summary.runtime.bootTime",
- "summary.runtime.connectionState",
- "summary.runtime.host",
- "summary.runtime.powerState",
- "summary.storage.unshared",
- "summary.storage.committed"
- ]
- },
- :ComputeResource => {
- :baseName => "@computeResources",
- :keyPath => ['name'],
- :props => [
- "name",
- "host",
- "parent",
- "resourcePool"
- ]
- },
- :ClusterComputeResource => {
- :baseName => "@clusterComputeResources",
- :keyPath => ['name'],
- :props => [
- "configuration.dasConfig.admissionControlPolicy",
- "configuration.dasConfig.admissionControlEnabled",
- "configuration.dasConfig.enabled",
- "configuration.dasConfig.failoverLevel",
- "configuration.drsConfig.defaultVmBehavior",
- "configuration.drsConfig.enabled",
- "configuration.drsConfig.vmotionRate",
- "summary.effectiveCpu",
- "summary.effectiveMemory",
- "host",
- "name",
- "parent",
- "resourcePool"
- ]
- },
- :ResourcePool => {
- :baseName => "@resourcePools",
- :keyPath => nil, # by mor only
- :props => [
- "name",
- "parent",
- "resourcePool",
- "summary.config.cpuAllocation.expandableReservation",
- "summary.config.cpuAllocation.limit",
- "summary.config.cpuAllocation.reservation",
- "summary.config.cpuAllocation.shares.level",
- "summary.config.cpuAllocation.shares.shares",
- "summary.config.memoryAllocation.expandableReservation",
- "summary.config.memoryAllocation.limit",
- "summary.config.memoryAllocation.reservation",
- "summary.config.memoryAllocation.shares.level",
- "summary.config.memoryAllocation.shares.shares",
- "vm"
- ] # childConfiguration currently has a problem updating. See FB3269
- },
- :Folder => {
- :baseName => "@folders",
- :keyPath => ['name'],
- :props => [
- "childEntity",
- "name",
- "parent"
- ]
- },
- :Datacenter => {
- :baseName => "@datacenters",
- :keyPath => ['name'],
- :props => [
- "datastoreFolder",
- "hostFolder",
- "name",
- "parent",
- "vmFolder"
- ]
- },
- :HostSystem => {
- :baseName => "@hostSystems",
- :keyPath => ['summary', 'config', 'name'],
- :props => [
- "config.adminDisabled",
- "config.consoleReservation.serviceConsoleReserved",
- "config.hyperThread.active",
- "config.network.consoleVnic",
- "config.network.dnsConfig.domainName",
- "config.network.dnsConfig.hostName",
- "config.network.ipRouteConfig.defaultGateway",
- "config.network.pnic",
- "config.network.portgroup",
- "config.network.vnic",
- "config.network.vswitch",
- "config.service.service",
- "datastore",
- "hardware.systemInfo.otherIdentifyingInfo",
- "name",
- "summary.host",
- "summary.config.name",
- "summary.config.product.build",
- "summary.config.product.name",
- "summary.config.product.osType",
- "summary.config.product.vendor",
- "summary.config.product.version",
- "summary.config.vmotionEnabled",
- "summary.hardware.cpuMhz",
- "summary.hardware.cpuModel",
- "summary.hardware.memorySize",
- "summary.hardware.model",
- "summary.hardware.numCpuCores",
- "summary.hardware.numCpuPkgs",
- "summary.hardware.numNics",
- "summary.hardware.vendor",
- "summary.quickStats.overallCpuUsage",
- "summary.quickStats.overallMemoryUsage",
- "summary.runtime.connectionState",
- "summary.runtime.inMaintenanceMode"
- ]
- },
- :Datastore => {
- :props => [
- "info",
- "host",
- "capability.directoryHierarchySupported",
- "capability.perFileThinProvisioningSupported",
- "capability.rawDiskMappingsSupported",
- "summary.accessible",
- "summary.capacity",
- "summary.datastore",
- "summary.freeSpace",
- "summary.maintenanceMode",
- "summary.multipleHostAccess",
- "summary.name",
- "summary.type",
- "summary.uncommitted",
- "summary.url",
- "parent"
- ]
- },
- :StoragePod => {
- :props => [
- "summary.capacity",
- "summary.freeSpace",
- "summary.name",
- "childEntity",
- "parent"
- ]
- },
- :DistributedVirtualPortgroup => {
- :props => [
- 'summary.name',
- 'config.key',
- 'config.distributedVirtualSwitch',
- 'config.name',
- 'parent',
- 'host',
- 'tag'
- ]
- },
- :DistributedVirtualSwitch => {
- :props => [
- 'config.uplinkPortgroup',
- 'config.defaultPortConfig',
- 'config.numPorts',
- 'summary.name',
- 'summary.uuid',
- 'summary.host',
- 'summary.hostMember',
- 'parent'
- ]
- }
- }.freeze
- def property_set
- EmsRefreshPropMap.collect do |type, h|
- RbVmomi::VIM::PropertySpec(
- :type => type,
- :all => h[:props].nil?,
- :pathSet => h[:props]
- )
- end
- end
- def main(args)
- vim = connect(args[:host], args[:user], args[:password])
- property_filter_spec = RbVmomi::VIM.PropertyFilterSpec(:objectSet => object_set(vim.serviceContent.rootFolder), :propSet => property_set)
- filter = vim.propertyCollector.CreateFilter(:spec => property_filter_spec, :partialUpdates => true)
- version = ""
- while true
- update_set = vim.propertyCollector.WaitForUpdatesEx(:version => version)
- break if update_set.nil?
- update_set.filterSet.each do |property_filter_update|
- objectSet = property_filter_update.objectSet
- next if objectSet.nil?
- objectSet.each do |object_update|
- name_prop = object_update.changeSet.detect do |prop_change|
- prop_change.name == "name" || prop_change.name == "summary.config.name" || prop_change.name == "summary.name"
- end
- name = name_prop.val unless name_prop.nil?
- puts "#{object_update.kind} #{object_update.obj.class.wsdl_name}:#{object_update.obj._ref} name: #{name}"
- end
- end
- version = update_set.version
- end
- ensure
- filter.DestroyPropertyFilter unless filter.nil?
- vim.serviceContent.sessionManager.Logout unless vim.nil?
- end
- args = Trollop.options do
- banner <<-EOS
- Usage:
- miq_vim_broker.rb [options]
- Options:
- EOS
- opt :host, "host", :type => :string, :short => 'o', :required => true
- opt :user, "username", :type => :string, :short => 'u', :default => 'root', :required => true
- opt :password, "password", :type => :string, :short => 'p', :required => true
- end
- main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement