Advertisement
Guest User

Untitled

a guest
May 31st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.24 KB | None | 0 0
  1. require 'rbvmomi'
  2. require 'trollop'
  3.  
  4. def connect(host, user, password)
  5. vim_opts = {
  6. :ns => 'urn:vim25',
  7. :rev => '4.1',
  8. :host => host,
  9. :ssl => true,
  10. :insecure => true,
  11. :path => '/sdk',
  12. :port => 443,
  13. }
  14.  
  15. RbVmomi::VIM.new(vim_opts).tap do |vim|
  16. vim.rev = vim.serviceContent.about.apiVersion
  17. vim.serviceContent.sessionManager.Login(:userName => user, :password => password)
  18. end
  19. end
  20.  
  21. def select_set
  22. [
  23. RbVmomi::VIM.TraversalSpec(
  24. :name => 'tsFolder',
  25. :type => 'Folder',
  26. :path => 'childEntity',
  27. :skip => false,
  28. :selectSet => [
  29. RbVmomi::VIM.SelectionSpec(:name => 'tsFolder'),
  30. RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToDatastoreFolder'),
  31. RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToHostFolder'),
  32. RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToNetworkFolder'),
  33. RbVmomi::VIM.SelectionSpec(:name => 'tsDatacenterToVmFolder'),
  34. RbVmomi::VIM.SelectionSpec(:name => 'tsComputeResourceToHost'),
  35. RbVmomi::VIM.SelectionSpec(:name => 'tsComputeResourceToResourcePool'),
  36. RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToResourcePool'),
  37. RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToVm')
  38. ]
  39. ),
  40. RbVmomi::VIM.TraversalSpec(
  41. :name => 'tsDatacenterToDatastoreFolder',
  42. :type => 'Datacenter',
  43. :path => 'vmFolder',
  44. :skip => false,
  45. :selectSet => [
  46. RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
  47. ]
  48. ),
  49. RbVmomi::VIM.TraversalSpec(
  50. :name => 'tsDatacenterToHostFolder',
  51. :type => 'Datacenter',
  52. :path => 'hostFolder',
  53. :skip => false,
  54. :selectSet => [
  55. RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
  56. ]
  57. ),
  58. RbVmomi::VIM.TraversalSpec(
  59. :name => 'tsDatacenterToNetworkFolder',
  60. :type => 'Datacenter',
  61. :path => 'networkFolder',
  62. :skip => false,
  63. :selectSet => [
  64. RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
  65. ]
  66. ),
  67. RbVmomi::VIM.TraversalSpec(
  68. :name => 'tsDatacenterToVmFolder',
  69. :type => 'Datacenter',
  70. :path => 'vmFolder',
  71. :skip => false,
  72. :selectSet => [
  73. RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
  74. ]
  75. ),
  76. RbVmomi::VIM.TraversalSpec(
  77. :name => 'tsComputeResourceToHost',
  78. :type => 'ComputeResource',
  79. :path => 'host',
  80. :skip => false,
  81. ),
  82. RbVmomi::VIM.TraversalSpec(
  83. :name => 'tsComputeResourceToResourcePool',
  84. :type => 'ComputeResource',
  85. :path => 'resourcePool',
  86. :skip => false,
  87. :selectSet => [
  88. RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToResourcePool')
  89. ]
  90. ),
  91. RbVmomi::VIM.TraversalSpec(
  92. :name => 'tsResourcePoolToResourcePool',
  93. :type => 'ResourcePool',
  94. :path => 'resourcePool',
  95. :skip => false,
  96. :selectSet => [
  97. RbVmomi::VIM.SelectionSpec(:name => 'tsResourcePoolToResourcePool')
  98. ]
  99. ),
  100. RbVmomi::VIM.TraversalSpec(
  101. :name => 'tsResourcePoolToVm',
  102. :type => 'ResourcePool',
  103. :path => 'vm',
  104. :skip => false,
  105. )
  106. ]
  107. end
  108.  
  109. def object_set(obj)
  110. [RbVmomi::VIM::ObjectSpec(:obj => obj, :selectSet => select_set)]
  111. end
  112.  
  113. EmsRefreshPropMap = {
  114. :VirtualMachine => {
  115. :baseName => "@virtualMachines",
  116. :keyPath => ['summary', 'config', 'vmPathName'],
  117. :keyPath2 => ['summary', 'config', 'vmLocalPathName'],
  118. :props => [
  119. "availableField",
  120. "config.cpuAffinity.affinitySet",
  121. "config.cpuHotAddEnabled",
  122. "config.cpuHotRemoveEnabled",
  123. "config.defaultPowerOps.standbyAction",
  124. "config.hardware.device",
  125. "config.hardware.numCoresPerSocket",
  126. "config.hotPlugMemoryIncrementSize",
  127. "config.hotPlugMemoryLimit",
  128. "config.memoryHotAddEnabled",
  129. "config.version",
  130. "datastore",
  131. "guest.net",
  132. "resourceConfig.cpuAllocation.expandableReservation",
  133. "resourceConfig.cpuAllocation.limit",
  134. "resourceConfig.cpuAllocation.reservation",
  135. "resourceConfig.cpuAllocation.shares.level",
  136. "resourceConfig.cpuAllocation.shares.shares",
  137. "resourceConfig.memoryAllocation.expandableReservation",
  138. "resourceConfig.memoryAllocation.limit",
  139. "resourceConfig.memoryAllocation.reservation",
  140. "resourceConfig.memoryAllocation.shares.level",
  141. "resourceConfig.memoryAllocation.shares.shares",
  142. "snapshot",
  143. "summary.vm",
  144. "summary.config.annotation",
  145. "summary.config.ftInfo.instanceUuids",
  146. "summary.config.guestFullName",
  147. "summary.config.guestId",
  148. "summary.config.memorySizeMB",
  149. "summary.config.name",
  150. "summary.config.numCpu",
  151. "summary.config.template",
  152. "summary.config.uuid",
  153. "summary.config.vmPathName",
  154. "summary.customValue",
  155. "summary.guest.hostName",
  156. "summary.guest.ipAddress",
  157. "summary.guest.toolsStatus",
  158. "summary.runtime.bootTime",
  159. "summary.runtime.connectionState",
  160. "summary.runtime.host",
  161. "summary.runtime.powerState",
  162. "summary.storage.unshared",
  163. "summary.storage.committed"
  164. ]
  165. },
  166. :ComputeResource => {
  167. :baseName => "@computeResources",
  168. :keyPath => ['name'],
  169. :props => [
  170. "name",
  171. "host",
  172. "parent",
  173. "resourcePool"
  174. ]
  175. },
  176. :ClusterComputeResource => {
  177. :baseName => "@clusterComputeResources",
  178. :keyPath => ['name'],
  179. :props => [
  180. "configuration.dasConfig.admissionControlPolicy",
  181. "configuration.dasConfig.admissionControlEnabled",
  182. "configuration.dasConfig.enabled",
  183. "configuration.dasConfig.failoverLevel",
  184. "configuration.drsConfig.defaultVmBehavior",
  185. "configuration.drsConfig.enabled",
  186. "configuration.drsConfig.vmotionRate",
  187. "summary.effectiveCpu",
  188. "summary.effectiveMemory",
  189. "host",
  190. "name",
  191. "parent",
  192. "resourcePool"
  193. ]
  194. },
  195. :ResourcePool => {
  196. :baseName => "@resourcePools",
  197. :keyPath => nil, # by mor only
  198. :props => [
  199. "name",
  200. "parent",
  201. "resourcePool",
  202. "summary.config.cpuAllocation.expandableReservation",
  203. "summary.config.cpuAllocation.limit",
  204. "summary.config.cpuAllocation.reservation",
  205. "summary.config.cpuAllocation.shares.level",
  206. "summary.config.cpuAllocation.shares.shares",
  207. "summary.config.memoryAllocation.expandableReservation",
  208. "summary.config.memoryAllocation.limit",
  209. "summary.config.memoryAllocation.reservation",
  210. "summary.config.memoryAllocation.shares.level",
  211. "summary.config.memoryAllocation.shares.shares",
  212. "vm"
  213. ] # childConfiguration currently has a problem updating. See FB3269
  214. },
  215. :Folder => {
  216. :baseName => "@folders",
  217. :keyPath => ['name'],
  218. :props => [
  219. "childEntity",
  220. "name",
  221. "parent"
  222. ]
  223. },
  224. :Datacenter => {
  225. :baseName => "@datacenters",
  226. :keyPath => ['name'],
  227. :props => [
  228. "datastoreFolder",
  229. "hostFolder",
  230. "name",
  231. "parent",
  232. "vmFolder"
  233. ]
  234. },
  235. :HostSystem => {
  236. :baseName => "@hostSystems",
  237. :keyPath => ['summary', 'config', 'name'],
  238. :props => [
  239. "config.adminDisabled",
  240. "config.consoleReservation.serviceConsoleReserved",
  241. "config.hyperThread.active",
  242. "config.network.consoleVnic",
  243. "config.network.dnsConfig.domainName",
  244. "config.network.dnsConfig.hostName",
  245. "config.network.ipRouteConfig.defaultGateway",
  246. "config.network.pnic",
  247. "config.network.portgroup",
  248. "config.network.vnic",
  249. "config.network.vswitch",
  250. "config.service.service",
  251. "datastore",
  252. "hardware.systemInfo.otherIdentifyingInfo",
  253. "name",
  254. "summary.host",
  255. "summary.config.name",
  256. "summary.config.product.build",
  257. "summary.config.product.name",
  258. "summary.config.product.osType",
  259. "summary.config.product.vendor",
  260. "summary.config.product.version",
  261. "summary.config.vmotionEnabled",
  262. "summary.hardware.cpuMhz",
  263. "summary.hardware.cpuModel",
  264. "summary.hardware.memorySize",
  265. "summary.hardware.model",
  266. "summary.hardware.numCpuCores",
  267. "summary.hardware.numCpuPkgs",
  268. "summary.hardware.numNics",
  269. "summary.hardware.vendor",
  270. "summary.quickStats.overallCpuUsage",
  271. "summary.quickStats.overallMemoryUsage",
  272. "summary.runtime.connectionState",
  273. "summary.runtime.inMaintenanceMode"
  274. ]
  275. },
  276. :Datastore => {
  277. :props => [
  278. "info",
  279. "host",
  280. "capability.directoryHierarchySupported",
  281. "capability.perFileThinProvisioningSupported",
  282. "capability.rawDiskMappingsSupported",
  283. "summary.accessible",
  284. "summary.capacity",
  285. "summary.datastore",
  286. "summary.freeSpace",
  287. "summary.maintenanceMode",
  288. "summary.multipleHostAccess",
  289. "summary.name",
  290. "summary.type",
  291. "summary.uncommitted",
  292. "summary.url",
  293. "parent"
  294. ]
  295. },
  296. :StoragePod => {
  297. :props => [
  298. "summary.capacity",
  299. "summary.freeSpace",
  300. "summary.name",
  301. "childEntity",
  302. "parent"
  303. ]
  304. },
  305. :DistributedVirtualPortgroup => {
  306. :props => [
  307. 'summary.name',
  308. 'config.key',
  309. 'config.distributedVirtualSwitch',
  310. 'config.name',
  311. 'parent',
  312. 'host',
  313. 'tag'
  314. ]
  315. },
  316. :DistributedVirtualSwitch => {
  317. :props => [
  318. 'config.uplinkPortgroup',
  319. 'config.defaultPortConfig',
  320. 'config.numPorts',
  321. 'summary.name',
  322. 'summary.uuid',
  323. 'summary.host',
  324. 'summary.hostMember',
  325. 'parent'
  326. ]
  327. }
  328. }.freeze
  329.  
  330. def property_set
  331. EmsRefreshPropMap.collect do |type, h|
  332. RbVmomi::VIM::PropertySpec(
  333. :type => type,
  334. :all => h[:props].nil?,
  335. :pathSet => h[:props]
  336. )
  337. end
  338. end
  339.  
  340. def main(args)
  341. vim = connect(args[:host], args[:user], args[:password])
  342.  
  343. property_filter_spec = RbVmomi::VIM.PropertyFilterSpec(:objectSet => object_set(vim.serviceContent.rootFolder), :propSet => property_set)
  344. filter = vim.propertyCollector.CreateFilter(:spec => property_filter_spec, :partialUpdates => true)
  345.  
  346. version = ""
  347. while true
  348. update_set = vim.propertyCollector.WaitForUpdatesEx(:version => version)
  349. break if update_set.nil?
  350.  
  351. update_set.filterSet.each do |property_filter_update|
  352. objectSet = property_filter_update.objectSet
  353. next if objectSet.nil?
  354.  
  355. objectSet.each do |object_update|
  356. name_prop = object_update.changeSet.detect do |prop_change|
  357. prop_change.name == "name" || prop_change.name == "summary.config.name" || prop_change.name == "summary.name"
  358. end
  359.  
  360. name = name_prop.val unless name_prop.nil?
  361.  
  362. puts "#{object_update.kind} #{object_update.obj.class.wsdl_name}:#{object_update.obj._ref} name: #{name}"
  363. end
  364. end
  365.  
  366. version = update_set.version
  367. end
  368. ensure
  369. filter.DestroyPropertyFilter unless filter.nil?
  370. vim.serviceContent.sessionManager.Logout unless vim.nil?
  371. end
  372.  
  373. args = Trollop.options do
  374. banner <<-EOS
  375. Usage:
  376. miq_vim_broker.rb [options]
  377.  
  378. Options:
  379. EOS
  380. opt :host, "host", :type => :string, :short => 'o', :required => true
  381. opt :user, "username", :type => :string, :short => 'u', :default => 'root', :required => true
  382. opt :password, "password", :type => :string, :short => 'p', :required => true
  383. end
  384.  
  385. main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement