Module: Morpheus::Cli::MonitoringHelper

Included in:
MonitorContactsCommand, MonitoringAppsCommand, MonitoringChecksCommand, MonitoringGroupsCommand, MonitoringIncidentsCommand
Defined in:
lib/morpheus/cli/mixins/monitoring_helper.rb

Overview

Provides common methods for the monitoring domain, incidents, checks, ‘n such

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 8

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#check_type_for_id(id) ⇒ Object



100
101
102
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 100

def check_type_for_id(id)
  return get_available_check_types().find { |z| z['id'].to_i == id.to_i}
end

#check_type_for_name(name) ⇒ Object



104
105
106
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 104

def check_type_for_name(name)
  return get_available_check_types().find { |z| z['name'].downcase == name.downcase || z['code'].downcase == name.downcase}
end

#check_type_for_name_or_id(val) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 92

def check_type_for_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return check_type_for_id(val)
  else
    return check_type_for_name(val)
  end
end

#find_app_by_id(id) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 401

def find_app_by_id(id)
  begin
    json_response = monitoring_interface.apps.get(id.to_i)
    return json_response['app']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Monitor App not found by id #{id}"
      exit 1 # return nil
    else
      raise e
    end
  end
end

#find_app_by_name(name) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 415

def find_app_by_name(name)
  json_results = monitoring_interface.apps.list({name: name})
  apps = json_results["apps"]
  if apps.empty?
    print_red_alert "Monitor App not found by name #{name}"
    exit 1 # return nil
  elsif apps.size > 1
    print_red_alert "#{apps.size} apps found by name #{name}"
    print "\n"
    puts as_pretty_table(apps, [{"ID" => "id" }, {"NAME" => "name"}], {color: red})
    print_red_alert "Try passing ID instead"
    print reset,"\n"
    exit 1 # return nil
  else
    return apps[0]
  end
end

#find_app_by_name_or_id(val) ⇒ Object

Monitoring apps



393
394
395
396
397
398
399
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 393

def find_app_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_app_by_id(val)
  else
    return find_app_by_name(val)
  end
end

#find_check_by_id(id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 26

def find_check_by_id(id)
  begin
    json_response = monitoring_interface.checks.get(id.to_i)
    return json_response['check']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Check not found by id #{id}"
      exit 1
    else
      raise e
    end
  end
end

#find_check_by_name(name) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 40

def find_check_by_name(name)
  json_results = monitoring_interface.checks.list({name: name})
  if json_results['checks'].empty?
    print_red_alert "Check not found by name #{name}"
    exit 1
  end
  check = json_results['checks'][0]
  return check
end

#find_check_by_name_or_id(val) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 18

def find_check_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_check_by_id(val)
  else
    return find_check_by_name(val)
  end
end

#find_check_group_by_id(id) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 359

def find_check_group_by_id(id)
  begin
    json_response = monitoring_interface.groups.get(id.to_i)
    return json_response['checkGroup']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Check Group not found by id #{id}"
      exit 1 # return nil
    else
      raise e
    end
  end
end

#find_check_group_by_name(name) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 373

def find_check_group_by_name(name)
  json_results = monitoring_interface.groups.list({name: name})
  groups = json_results["groups"]
  if groups.empty?
    print_red_alert "Check Group not found by name #{name}"
    exit 1 # return nil
  elsif groups.size > 1
    print_red_alert "#{groups.size} Check Groups found by name #{name}"
    print "\n"
    puts as_pretty_table(groups, [{"ID" => "id" }, {"NAME" => "name"}], {color: red})
    print_red_alert "Try passing ID instead"
    print reset,"\n"
    exit 1 # return nil
  else
    return groups[0]
  end
end

#find_check_group_by_name_or_id(val) ⇒ Object

Monitoring Check Groups



351
352
353
354
355
356
357
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 351

def find_check_group_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_check_group_by_id(val)
  else
    return find_check_group_by_name(val)
  end
end

#find_contact_by_id(id) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 316

def find_contact_by_id(id)
  begin
    json_response = monitoring_interface.contacts.get(id.to_i)
    return json_response['contact']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Contact not found by id #{id}"
      exit 1 # return nil
    else
      raise e
    end
  end
end

#find_contact_by_name(name) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 330

def find_contact_by_name(name)
  json_results = monitoring_interface.contacts.list({name: name})
  contacts = json_results["contacts"]
  if contacts.empty?
    print_red_alert "Contact not found by name #{name}"
    exit 1 # return nil
  elsif contacts.size > 1
    print_red_alert "#{contacts.size} Contacts found by name #{name}"
    print "\n"
    puts as_pretty_table(contacts, [{"ID" => "id" }, {"NAME" => "name"}], {color: red})
    print_red_alert "Try passing ID instead"
    print reset,"\n"
    exit 1 # return nil
  else
    return contacts[0]
  end
end

#find_contact_by_name_or_id(val) ⇒ Object

Monitoring Contacts



308
309
310
311
312
313
314
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 308

def find_contact_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_contact_by_id(val)
  else
    return find_contact_by_name(val)
  end
end

#find_incident_by_id(id) ⇒ Object

def find_incident_by_name_or_id(val)

if val.to_s =~ /\A\d{1,}\Z/
  return find_incident_by_id(val)
else
  return find_incident_by_name(val)
end

end



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 58

def find_incident_by_id(id)
  begin
    json_response = monitoring_interface.incidents.get(id.to_i)
    return json_response['incident']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Incident not found by id #{id}"
      exit 1
    else
      raise e
    end
  end
end

#format_monitoring_check_last_metric(check) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 227

def format_monitoring_check_last_metric(check)
  #<td class="last-metric-col">${check.lastMetric} ${check.lastMetric ? checkTypes.find{ type -> type.id == check.checkTypeId}?.metricName : ''}</td>
  out = ""
  out << "#{check['lastMetric']} "
  # todo:
  out.strip
end

#format_monitoring_check_status(check, return_color = cyan) ⇒ Object

Checks



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 209

def format_monitoring_check_status(check, return_color=cyan)
  #<morph:statusIcon unknown="${!check.lastRunDate}" muted="${!check.createIncident}" failure="${check.lastCheckStatus == 'error'}" health="${check.health}" class="pull-left"/>
  out = ""
  muted = !check['createIncident']
  status_string = check['lastCheckStatus'].to_s
  failure = check['lastCheckStatus'] == 'error'
  health = check['health'] # todo: examine at this too?
  if failure
    out << "#{red}#{status_string.capitalize}#{return_color}"
  else
    out << "#{cyan}#{status_string.capitalize}#{return_color}"
  end
  if muted
    out << "(muted)"
  end
  out
end

#format_monitoring_check_type(check) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 235

def format_monitoring_check_type(check)
  #<td class="check-type-col"><div class="check-type-icon ${morph.checkTypeCode(id:check.checkTypeId, checkTypes:checkTypes)}"></div></td>
  # return get_object_value(check, 'checkType.name') # this works too
  out = ""
  if check && check['checkType'] && check['checkType']['name']
    out << check['checkType']['name']
  elsif check['checkTypeId']
    out << check['checkTypeId'].to_s
  elsif !check.empty?
    out << check.to_s
  end
  out.strip! + "WEEEEEE"
end

#format_monitoring_incident_status(incident) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 135

def format_monitoring_incident_status(incident)
  status_string = incident['status']
  if status_string == 'closed'
    "closed ✓"
  else
    status_string
  end
end

#format_monitoring_issue_attachment_type(issue) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 123

def format_monitoring_issue_attachment_type(issue)
  if issue["app"]
    "App"
  elsif issue["check"]
    "Check"
  elsif issue["checkGroup"]
    "Group"
  else
    "Severity Change"
  end
end

#format_monitoring_issue_status(issue) ⇒ Object



144
145
146
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 144

def format_monitoring_issue_status(issue)
  format_monitoring_incident_status(issue)
end

#format_severity(severity, return_color = cyan) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 108

def format_severity(severity, return_color=cyan)
  out = ""
  status_string = severity
  if status_string == 'critical'
    out << "#{red}#{status_string.capitalize}#{return_color}"
  elsif status_string == 'warning'
    out << "#{yellow}#{status_string.capitalize}#{return_color}"
  elsif status_string == 'info'
    out << "#{cyan}#{status_string.capitalize}#{return_color}"
  else
    out << "#{cyan}#{status_string}#{return_color}"
  end
  out
end

#get_available_check_types(refresh = false) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 83

def get_available_check_types(refresh=false)
  if !@available_check_types || refresh
    # @available_check_types = [{name: 'A Fake Check Type', code: 'achecktype'}]
    # todo: use options api instead probably...
    @available_check_types = check_types_interface.list_check_types['checkTypes']
  end
  return @available_check_types
end

#monitoring_interfaceObject



12
13
14
15
16
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 12

def monitoring_interface
  # @api_client.monitoring
  raise "#{self.class} has not defined @monitoring_interface" if @monitoring_interface.nil?
  @monitoring_interface
end


267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 267

def print_check_history_table(history_items, opts={})
  columns = [
    # {"ID" => lambda {|issue| issue['id'] } },
    {"SEVERITY" => lambda {|issue| format_severity(issue['severity']) } },
    {"AVAILABLE" => lambda {|issue| format_boolean issue['available'] } },
    {"TYPE" => lambda {|issue| issue["attachmentType"] } },
    {"NAME" => lambda {|issue| issue['name'] } },
    {"DATE CREATED" => lambda {|issue| format_local_dt(issue['startDate']) } }
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(history_items, columns, opts)
end


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 282

def print_check_notifications_table(notifications, opts={})
  columns = [
    {"NAME" => lambda {|notification| notification['recipient'] ? notification['recipient']['name'] : '' } },
    {"DELIVERY TYPE" => lambda {|notification| notification['addressTypes'].to_s } },
    {"NOTIFIED ON" => lambda {|notification| format_local_dt(notification['dateCreated']) } },
    # {"AVAILABLE" => lambda {|notification| format_boolean notification['available'] } },
    # {"TYPE" => lambda {|notification| notification["attachmentType"] } },
    # {"NAME" => lambda {|notification| notification['name'] } },
    {"DATE CREATED" => lambda {|notification| 
      date_str = format_local_dt(notification['startDate']).to_s
      if notification['pendingUtil']
        "(pending) #{date_str}"
      else
        date_str
      end
    } }
  ]
  #event['pendingUntil']
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(notifications, columns, opts)
end


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 249

def print_checks_table(incidents, opts={})
  columns = [
    {"ID" => lambda {|check| check['id'] } },
    {"STATUS" => lambda {|check| format_monitoring_check_status(check) } },
    {"NAME" => lambda {|check| check['name'] } },
    {"TIME" => lambda {|check| format_local_dt(check['lastRunDate']) } },
    {"AVAILABILITY" => {display_method: lambda {|check| check['availability'] ? "#{check['availability'].to_f.round(3).to_s}%" : "N/A"} }, justify: "center" },
    {"RESPONSE TIME" => {display_method: lambda {|check| check['lastTimer'] ? "#{check['lastTimer']}ms" : "N/A" } }, justify: "center" },
    {"LAST METRIC" => {display_method: lambda {|check| check['lastMetric'] ? "#{check['lastMetric']}" : "N/A" } }, justify: "center" },
    {"TYPE" => 'checkType.name'},
    
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(incidents, columns, opts)
end


167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 167

def print_incident_history_table(history_items, opts={})
  columns = [
    # {"ID" => lambda {|issue| issue['id'] } },
    {"SEVERITY" => lambda {|issue| format_severity(issue['severity']) } },
    {"AVAILABLE" => lambda {|issue| format_boolean issue['available'] } },
    {"TYPE" => lambda {|issue| issue["attachmentType"] } },
    {"NAME" => lambda {|issue| issue['name'] } },
    {"DATE CREATED" => lambda {|issue| format_local_dt(issue['startDate']) } }
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(history_items, columns, opts)
end


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 182

def print_incident_notifications_table(notifications, opts={})
  columns = [
    {"NAME" => lambda {|notification| notification['recipient'] ? notification['recipient']['name'] : '' } },
    {"DELIVERY TYPE" => lambda {|notification| notification['addressTypes'].to_s } },
    {"NOTIFIED ON" => lambda {|notification| format_local_dt(notification['dateCreated']) } },
    # {"AVAILABLE" => lambda {|notification| format_boolean notification['available'] } },
    # {"TYPE" => lambda {|notification| notification["attachmentType"] } },
    # {"NAME" => lambda {|notification| notification['name'] } },
    {"DATE CREATED" => lambda {|notification| 
      date_str = format_local_dt(notification['startDate']).to_s
      if notification['pendingUtil']
        "(pending) #{date_str}"
      else
        date_str
      end
    } }
  ]
  #event['pendingUntil']
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(notifications, columns, opts)
end

Incidents



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 152

def print_incidents_table(incidents, opts={})
  columns = [
    {"ID" => lambda {|incident| incident['id'] } },
    {"SEVERITY" => lambda {|incident| format_severity(incident['severity']) } },
    {"NAME" => lambda {|incident| incident['name'] || 'No Subject' } },
    {"TIME" => lambda {|incident| format_local_dt(incident['startDate']) } },
    {"STATUS" => lambda {|incident| format_monitoring_incident_status(incident) } },
    {"DURATION" => lambda {|incident| format_duration(incident['startDate'], incident['endDate']) } }
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(incidents, columns, opts)
end