Class: VSphereMonitoring

Inherits:
Object
  • Object
show all
Defined in:
lib/vspheremonitoring.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ VSphereMonitoring

Returns a new instance of VSphereMonitoring.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vspheremonitoring.rb', line 9

def initialize (config)
  @config = config
  @verbose = config[:opts][:verbose]

  speak("Building connection to vsphere")
  @vim = RbVmomi::VIM.connect :host     => config[:host],
                             :user     => config[:user],
                             :password => config[:password],
                             :insecure => true

  p = Hash.new
  p[:vsphere] = process_all_datacenters()
  puts p.to_json
end

Instance Method Details

#cluster_cpu(cluster) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/vspheremonitoring.rb', line 65

def cluster_cpu (cluster)
  speak("gathering cluster cpu stats for #{cluster.name}")
  data = Hash.new
  data['totalCpu']      = cluster.summary.totalCpu
  data['numCpuCores']   = cluster.summary.numCpuCores
  data['numCpuThreads'] = cluster.summary.numCpuThreads
  data['effectiveCpu']  = cluster.summary.effectiveCpu
  data
end

#cluster_memory(cluster) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/vspheremonitoring.rb', line 57

def cluster_memory (cluster)
  speak("gathering cluster memory stats for #{cluster.name}")
  data = Hash.new
  data['totalMemory']     = cluster.summary.totalMemory
  data['effectiveMemory'] = cluster.summary.effectiveMemory
  data
end

#cluster_stats(cluster) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vspheremonitoring.rb', line 75

def cluster_stats (cluster)
  speak("gathering cluster stats for #{cluster.name}")
  data = Hash.new
  data['cpu']               = cluster_cpu(cluster)
  data['memory']            = cluster_memory(cluster)
  data['numVmotions']       = cluster.summary.numVmotions
  data['numHosts']          = cluster.summary.numHosts
  data['numEffectiveHosts'] = cluster.summary.numEffectiveHosts
  data['targetBalance']     = cluster.summary.targetBalance
  data['currentBalance']    = cluster.summary.currentBalance
  data
end

#datastore_stats(datastore) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vspheremonitoring.rb', line 88

def datastore_stats (datastore)
  speak("gathering datastore stats for #{datastore.name}")
  data = Hash.new
  datastore.RefreshDatastore
  capacity = datastore.summary.capacity
  freeSpace = datastore.summary.freeSpace
  uncommitted = datastore.summary.uncommitted
  data['capacityM']    = ((capacity / 1024) / 1024) if capacity.class != NilClass
  data['freeSpaceM']   = ((freeSpace / 1024) / 1024) if freeSpace.class != NilClass
  data['uncommittedM'] = ((uncommitted / 1024) / 1024) if uncommitted.class != NilClass
  data
end

#host_cpu(host) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/vspheremonitoring.rb', line 38

def host_cpu (host)
  speak("gathering cpu stats for #{host.name}")
  data = Hash.new
  data['Mhz']      = host.hardware.cpuInfo.hz / 1000 / 1000
  data['cores']    = host.hardware.cpuInfo.numCpuCores
  data['totalMhz'] = data['Mhz'] * data['cores']
  data['usageMhz'] = host.summary.quickStats.overallCpuUsage.to_f
  data['percent']  = (data['usageMhz'] / data['totalMhz']) * 100
  data
end

#host_memory(host) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/vspheremonitoring.rb', line 29

def host_memory (host)
  speak("gathering memory stats for #{host.name}")
  data            = Hash.new
  data['total']   = host.hardware.memorySize.bytes.to.megabytes.to_f
  data['usage']   = host.summary.quickStats.overallMemoryUsage.megabytes.to_f
  data['percent'] = (data['usage'] / data['total']) * 100
  data
end

#host_stats(host) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/vspheremonitoring.rb', line 49

def host_stats (host)
  speak("gathering host stats for #{host.name}")
  data = Hash.new
  data['memory']  = host_memory(host)
  data['cpu']     = host_cpu(host)
  data
end

#network_stats(network) ⇒ Object



101
102
103
104
105
# File 'lib/vspheremonitoring.rb', line 101

def network_stats (network)
  speak("gathering network stats for #{network.name}")
  data = Hash.new
  data
end

#process_all_datacentersObject



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/vspheremonitoring.rb', line 139

def process_all_datacenters ()

  rootFolder = @vim.serviceInstance.content.rootFolder
  dclist = rootFolder.children.select {|d| d.class == RbVmomi::VIM::Datacenter }

  data = Hash.new
  dclist.each do |datacenter|
    data[datacenter.name] = process_datacenter(datacenter)
  end
  data

end

#process_datacenter(dc) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/vspheremonitoring.rb', line 107

def process_datacenter (dc)
  speak("collecting metrics for datacenter #{dc.name}")

  data = Hash.new
  data['hypers'] = Hash.new
  data['clusters'] = Hash.new
  data['datastores'] = Hash.new
  # Get the information for host
  host_list    = dc.hostFolder.children.select {|h| h.class == RbVmomi::VIM::ComputeResource }
  host_list.map {|h|
    data['hypers'][h.name] = host_stats(h.host.first)
  }

  # Get the information for each host in a cluster
  cluster_list = dc.hostFolder.children.select {|h| h.class == RbVmomi::VIM::ClusterComputeResource }
  cluster_list.map {|c|
    data['clusters'][c.name] = cluster_stats(c)
    c.host.map {|h|
      data['hypers'][h.name] = host_stats(h)
    }
  }

  # Get informaton about datastore usage
  datastore_list = dc.datastore
  datastore_list.map {|d|
    data['datastores'][d.name] = datastore_stats(d)
  }

  data['vm_count'] = dc.vmFolder.children.select {|v| v.class == RbVmomi::VIM::VirtualMachine }.size
  data
end

#speak(what) ⇒ Object

module_function



25
26
27
# File 'lib/vspheremonitoring.rb', line 25

def speak(what)
  puts what if @verbose
end