-
Notifications
You must be signed in to change notification settings - Fork 898
/
miq_cim_instance.rb
394 lines (333 loc) · 10.3 KB
/
miq_cim_instance.rb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
require 'miq_storage_defs'
# Required for loading serialized objects in 'obj' column
require 'wbem'
require 'net_app_manageability/types'
class MiqCimInstance < ActiveRecord::Base
has_many :miq_cim_associations,
:dependent => :destroy
has_many :associations_we_are_result_of,
:class_name => "MiqCimAssociation",
:foreign_key => "result_instance_id",
:dependent => :destroy
has_many :elements_with_metrics,
:class_name => "MiqCimInstance",
:foreign_key => "metric_top_id"
belongs_to :metrics,
:class_name => "MiqStorageMetric",
:foreign_key => "metric_id",
:dependent => :destroy
belongs_to :agent,
:class_name => "StorageManager",
:foreign_key => "agent_id"
belongs_to :top_managed_element,
:class_name => "MiqCimInstance",
:foreign_key => "top_managed_element_id"
belongs_to :vmdb_obj,
:polymorphic => true
belongs_to :zone
serialize :obj
serialize :obj_name
serialize :type_spec_obj
virtual_column :evm_display_name, :type => :string
virtual_column :last_update_status_str, :type => :string
include MiqStorageDefs
#
# The order of the entries in the following array is significant;
# relevant subclasses must appear before their superclasses.
#
SUPER_CLASSES = [
'CIM_StorageExtent',
'CIM_CompositeExtent',
'CIM_LogicalDisk',
'CIM_StorageVolume',
'CIM_ComputerSystem',
'SNIA_FileShare',
'SNIA_LocalFileSystem',
'MIQ_CimVirtualDisk',
'MIQ_CimVirtualMachine',
'MIQ_CimDatastore',
'MIQ_CimHostSystem'
]
def self.topManagedElements
where(:is_top_managed_element => true).to_a
end
def self.find_kinda(cimClass, zoneId)
where("class_hier LIKE ? AND zone_id = ?", "%/#{cimClass}/%", zoneId).to_a
end
def evm_display_name
property('ElementName') || property('Name') || property('DeviceID')
end
def last_update_status_str
return "OK" if last_update_status == STORAGE_UPDATE_OK
#
# If any agent in this zone is STORAGE_UPDATE_IN_PROGRESS or STORAGE_UPDATE_PENDING, then "In Progress"
# should be returned, because the true status of the instance can only be determined when the full
# scan is complete.
#
aa = MiqSmisAgent.find(:all, :conditions => [
"zone_id = ? and (last_update_status = ? or last_update_status = ?)",
zone_id, STORAGE_UPDATE_IN_PROGRESS, STORAGE_UPDATE_PENDING
])
return "In Progress" unless aa.empty?
return "Agent Inaccessible" if last_update_status == STORAGE_UPDATE_AGENT_INACCESSIBLE
if last_update_status == STORAGE_UPDATE_NO_AGENT
return "No Agent" unless agent
return "Failed" if agent.last_update_status == STORAGE_UPDATE_FAILED
elsif last_update_status == STORAGE_UPDATE_AGENT_OK_NO_INSTANCE
return "Failed" if agent.last_update_status == STORAGE_UPDATE_FAILED
return "No Instance"
end
"Unknown"
end
#
# Are metrics for the given interval available?
# interval_name: "hourly" || "daily" || "realtime"
#
def has_perf_data?(interval_name = "hourly")
@has_perf_data ||= {}
unless (rv = @has_perf_data[interval_name]).nil?
return rv
end
return @has_perf_data[interval_name] = false if metrics.nil?
if interval_name == "realtime"
return @has_perf_data[interval_name] = metrics.miq_derived_metrics.exists?
end
@has_perf_data[interval_name] = metrics.miq_metrics_rollups.exists?(:rollup_type => interval_name)
end
def last_capture(interval_name = "hourly")
return nil unless has_metrics?
if interval_name == "realtime"
perf = metrics.miq_derived_metrics.first(
:select => "statistic_time",
:order => "statistic_time DESC"
)
else
perf = metrics.miq_metrics_rollups.first(
:select => "statistic_time",
:conditions => {:rollup_type => interval_name},
:order => "statistic_time DESC"
)
end
perf.nil? ? nil : perf.statistic_time
end
def first_capture(interval_name = "hourly")
return nil unless has_metrics?
if interval_name == "realtime"
perf = metrics.miq_derived_metrics.first(
:select => "statistic_time",
:order => "statistic_time ASC"
)
else
perf = metrics.miq_metrics_rollups.first(
:select => "statistic_time",
:conditions => {:rollup_type => interval_name},
:order => "statistic_time ASC"
)
end
perf.nil? ? nil : perf.statistic_time
end
def first_and_last_capture(interval_name = "hourly")
[first_capture(interval_name), last_capture(interval_name)].compact
end
#
# Does this object support metrics collection.
#
def has_metrics?
!metrics.nil?
end
def derived_metrics
return [] unless has_metrics?
metrics.miq_derived_metrics
end
def derived_metrics_in_range(start_time, end_time)
return [] unless has_metrics?
metrics.derived_metrics_in_range(start_time, end_time)
end
# XXX
def latest_derived_metrics
derived_metrics.last
end
# XXX
def earliest_derived_metrics
derived_metrics.first
end
def metrics_rollups
return [] unless has_metrics?
metrics.miq_metrics_rollups
end
def metrics_rollups_in_range(rollupType, startTime, endTime)
return [] unless has_metrics?
metrics.metrics_rollups_in_range(rollupType, startTime, endTime)
end
def metrics_rollups_by_rollup_type(rollupType)
return [] unless has_metrics?
metrics.metrics_rollups_by_rollup_type(rollupType)
end
def vendor
return "NetApp" if class_name =~ /^ONTAP_/
"Unknown"
end
def class_hier
chs = read_attribute(:class_hier)
chs = chs[1..-2] if chs
return chs.split('/') if chs
[]
end
def class_hier=(val)
if val.kind_of?(Array)
val = '/' + val.join('/') + '/'
end
write_attribute(:class_hier, val)
end
def addAssociation(result_instance, assoc)
MiqCimAssociation.add_association(assoc, self, result_instance)
end
#
# Get the nodes associated with this node through the given association.
#
def getAssociators(association)
results = []
query = miq_cim_associations.scoped.includes(:result_instance).select(:result_instance_id, :id)
query = query.where_association(association)
query.find_each { |a| results << a.result_instance }
results.uniq
end
def getAssociatedVmdbObjs(association)
results = []
query = miq_cim_associations.scoped.includes(:result_instance => :vmdb_obj).select(:result_instance_id, :id)
query = query.where_association(association)
query.find_each { |a| results << a.result_instance.vmdb_obj }
results.uniq
end
#
# Get the associations from this node that match the given association.
#
def getAssociations(association)
miq_cim_associations.scoped.where_association(association)
end
#
# Return the number of associations from this node that match the given association.
#
def getAssociationSize(association)
miq_cim_associations.scoped.where_association(association).size
end
def mark_associations_stale
miq_cim_associations.update_all(:status => MiqCimAssociation::STATUS_STALE)
end
def addNewMetric(metric)
metric.miq_cim_instance = self
self.metrics = metric
metric.save
end
def updateStats(metricObj)
metrics.metric_obj = metricObj
metrics.save
end
def kinda?(className)
class_hier.include? className
end
def typeFromClassHier
class_hier.each { |c| return typeFromClassName(c) if SUPER_CLASSES.include?(c) }
nil
end
def typeFromClassName(className)
className.underscore.camelize
end
def operational_status_to_str(val)
return "Not Available" if val.nil?
case val[0]
when 2
return "OK"
when 3
return "Degraded"
when 6
return "Error"
when 8
return "Starting"
when 9
return "Stopping"
when 10
return "Stopped"
when 12
return "No contact"
when 13
return "Lost communication"
when 15
return "Dormant"
else
return "Unknown (#{val[0]})"
end
end
def health_state_to_str(val)
return "Not Available" if val.nil?
return "OK" if val == 5
return "Issue Detected" if val > 5 && val <= 10
return "Attention Required" if val > 10 && val < 30
return "Major Failure" if val >= 30
"Unknown (#{val})"
end
def getLeafNodes(prof, node, retHash, level = 0, visited = {})
objName = node.obj_name_str
return if visited[objName]
unless prof
unless retHash.key?(objName)
retHash[objName] = node
end
visited[objName] = true
return
end
prof = [prof] unless prof.kind_of?(Array)
children = false
prof.each do |p|
associations = p[:association]
associations = [associations] unless associations.kind_of?(Array)
associations.each do |a|
node.getAssociators(a).each do |an|
children = true
if p[:flags][:recurse]
getLeafNodes(p, an, retHash, level + 1, visited)
end
getLeafNodes(p[:next], an, retHash, level + 1, visited)
end
end
end
visited[objName] = true
if children
retHash.delete(objName) if retHash.key?(objName)
elsif !retHash.key?(objName)
retHash[objName] = node
end
end
def property(key)
v = obj.properties[key]
return nil if v.nil?
if v.value.kind_of?(Array)
v.value.collect { |val| decode(val) }
else
decode(v.value)
end
end
def dumpInstance(globalIndent = "", level = 0, io = $stdout)
obj.properties.each do |k, v|
unless v.value.kind_of?(Array)
indentedPrint(" #{k} => #{v.value} (#{v.value.class})", globalIndent, level, io)
else
indentedPrint(" #{k} =>", globalIndent, level, io)
v.value.each { |val| indentedPrint(" #{val}", globalIndent, level, io) }
end
end
end
def indentedPrint(s, globalIndent, i, io = $stdout)
io.print globalIndent + " " * i
io.puts s
end
private
def decode(val)
return val.value if val.kind_of?(WBEM::Uint8)
return val.value if val.kind_of?(WBEM::Uint16)
return val.value if val.kind_of?(WBEM::Uint32)
return val.value if val.kind_of?(WBEM::Uint64)
return val.value if val.kind_of?(WBEM::Boolean)
val
end
end