Skip to content

Instantly share code, notes, and snippets.

@toobulkeh
Last active January 3, 2024 09:08
Show Gist options
  • Save toobulkeh/6124975 to your computer and use it in GitHub Desktop.
Save toobulkeh/6124975 to your computer and use it in GitHub Desktop.
rbvmomi performance manager usage
vim = RbVmomi::VIM.connect host: vcenter.hostname, user: vcenter.username, password: vcenter.password, insecure: true
pm = vim.serviceInstance.content.perfManager
# this is for a single cluster with hosts directly in one cluster. 'first' returns the cluster object.
hosts = vim.serviceInstance.find_datacenter.hostFolder.children.first.host
pm.retrieve_stats([hosts[0]], ['cpu.usagemhz'], {interval: '300', start_time: (Time.now - 6.hours)}).first[1][:metrics]['cpu.usagemhz']
# This above query queries for the 'cpu.usagemhz' counter, which by default matches the (none) rollup counter (NOT THE AVERAGE) in vSphere API.
# This is a problem with the RbVmomi gem helper function "retrieve_stats" only. To override this, use the following manual counter selection:
pqs = RbVmomi::VIM::PerfQuerySpec(maxSample: 1, entity: hosts[0], metricId: [{counterId: 6, instance: '*'}], intervalId: '300', startTime: (Time.now-1.hour).to_datetime, endTime: Time.now.to_datetime)
# => #<RbVmomi::VIM::PerfQuerySpec:0x00000004f24e38 @props={:maxSample=>1, :entity=>HostSystem("host-13"), :metricId=>[{:counterId=>6, :instance=>"*"}], :intervalId=>"300", :startTime=>Thu, 01 Aug 2013 22:00:27 +0000, :endTime=>Thu, 01 Aug 2013 23:00:27 +0000}>
pm.QueryPerf({querySpec: [pqs]})
# => [#<RbVmomi::VIM::PerfEntityMetric:0x00000004c01a58 @props={:dynamicProperty=>[], :sampleInfo=>[#<RbVmomi::VIM::PerfSampleInfo:0x0000000404a188 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:05:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x00000004051190 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:10:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x00000004057928 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:15:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x000000040549d0 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:20:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x0000000405c4c8 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:25:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x000000040629e0 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:30:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x00000004bec0b8 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:35:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x00000004be91d8 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:40:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x00000004bee598 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:45:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x00000004bf5780 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:50:00 UTC, :interval=>300}>, #<RbVmomi::VIM::PerfSampleInfo:0x00000004bfc5a8 @props={:dynamicProperty=>[], :timestamp=>2013-08-01 22:55:00 UTC, :interval=>300}>], :value=>[#<RbVmomi::VIM::PerfMetricIntSeries:0x00000004c02020 @props={:dynamicProperty=>[], :value=>[1064, 1026, 1001, 1124, 1061, 1072, 1068, 1065, 1069, 1081, 0], :id=>#<RbVmomi::VIM::PerfMetricId:0x00000004bfafc8 @props={:dynamicProperty=>[], :counterId=>6, :instance=>""}>}>], :entity=>HostSystem("host-13")}>]
# I will work this into a gem pull request in the coming weeks
# => [1721, 1797, 2041, 2983, 1807, 1814, 1870, 1816, 1799, 1882, 1963, 1886, 1770, 1753, 3673, 3459,
# 1828, 1812, 1807, 1869, 2026, 1888, 2085, 1809, 1826, 1785, 2330, 3315, 1776, 1861, 1807, 1842,
# 1797, 1858, 1836, 1922, 1782, 1843, 3906, 3413, 1867, 2273, 1961, 1736, 1833, 1813, 1846, 2063,
# 1852, 1854, 2208, 3276, 1933, 1776, 2544, 1751, 1815, 1830, 1740, 1764, 1703, 1766, 3661, 2981,
# 1769, 1890, 2273, 1764, 1747, 1827, 0]
# Notes:
# - that that last result is always 0 in this vcenter I've seen
# - sometimes the entire metrics is {} when statistics are not being saved in the vCenter performance database properly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment