File: ComputeResource.rb

package info (click to toggle)
ruby-rbvmomi 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,756 kB
  • sloc: ruby: 5,590; sh: 36; makefile: 7
file content (51 lines) | stat: -rw-r--r-- 1,591 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class RbVmomi::VIM::ComputeResource
  # Aggregate cluster information.
  #
  # @note Values are returned in a hash.
  #
  # @return [Mhz] totalCPU: Sum of the frequencies of each CPU in the cluster.
  # @return [Mhz] usedCPU:  CPU cycles used across the cluster.
  # @return [MB]  totalMem: Total RAM.
  # @return [MB]  usedMem:  Used RAM.
  def stats
    filterSpec = RbVmomi::VIM.PropertyFilterSpec(
      :objectSet => [{
        :obj => self,
        :selectSet => [
          RbVmomi::VIM.TraversalSpec(
            :name => 'tsHosts',
            :type => 'ComputeResource',
            :path => 'host',
            :skip => false
          )
        ]
      }],
      :propSet => [{
        :pathSet => %w(name overallStatus summary.hardware.cpuMhz
                    summary.hardware.numCpuCores summary.hardware.memorySize
                    summary.quickStats.overallCpuUsage
                    summary.quickStats.overallMemoryUsage),
        :type => 'HostSystem'
      }]
    )

    result = _connection.propertyCollector.RetrieveProperties(:specSet => [filterSpec])

    stats = {
      :totalCPU => 0,
      :totalMem => 0,
      :usedCPU => 0,
      :usedMem => 0,
    }

    result.each do |x|
      next if x['overallStatus'] == 'red'
      stats[:totalCPU] += x['summary.hardware.cpuMhz'] * x['summary.hardware.numCpuCores']
      stats[:totalMem] += x['summary.hardware.memorySize'] / (1024*1024)
      stats[:usedCPU] += x['summary.quickStats.overallCpuUsage'] || 0
      stats[:usedMem] += x['summary.quickStats.overallMemoryUsage'] || 0
    end

    stats
  end
end