Module: Morpheus::Cli::MonitoringHelper

Included in:
MonitoringAppsCommand, MonitoringChecksCommand, MonitoringContactsCommand, 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_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



370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 370

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



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 384

def find_check_group_by_name(name)
  json_results = monitoring_interface.groups.list({name: name})
  groups = json_results["checkGroups"]
  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



362
363
364
365
366
367
368
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 362

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



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 327

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



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 341

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



319
320
321
322
323
324
325
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 319

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

#find_monitoring_app_by_id(id) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 429

def find_monitoring_app_by_id(id)
  begin
    json_response = monitoring_interface.apps.get(id.to_i)
    return json_response["monitorApp"] || 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_monitoring_app_by_name(name) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 443

def find_monitoring_app_by_name(name)
  json_results = monitoring_interface.apps.list({name: name})
  apps = json_results["monitorApps"] || 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_monitoring_app_by_name_or_id(val) ⇒ Object

Monitoring apps



421
422
423
424
425
426
427
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 421

def find_monitoring_app_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_monitoring_app_by_id(val)
  else
    return find_monitoring_app_by_name(val)
  end
end

#format_monitoring_check_last_metric(check) ⇒ Object



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

def format_monitoring_check_last_metric(check)
  if check['lastMetric']
    metric_name = check['checkType'] ? check['checkType']['metricName'] : nil
    if metric_name
      "#{check['lastMetric']} #{metric_name}"
    else
      "#{check['lastMetric']}"
    end
  else
    "N/A" 
  end
end

#format_monitoring_check_status(check, include_msg = false, return_color = cyan) ⇒ Object

Checks



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
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 209

def format_monitoring_check_status(check, include_msg=false, return_color=cyan)
  out = ""
  unknown = check['lastRunDate'].nil?
  failure = check['lastCheckStatus'] == 'error'
  health = check['health']
  muted = check['createIncident'] == false
  status_string = check['lastCheckStatus'].to_s # null for groups, ignore?

  if unknown
    out << "#{white}UNKNOWN#{return_color}"
  elsif failure || health == 0
    if include_msg && check['lastError']
      out << "#{red}ERROR - #{check['lastError']}#{return_color}"
    else
      out << "#{red}ERROR#{return_color}"
    end
  elsif health.to_i >= 10
    out << "#{green}HEALTHY#{return_color}"
  else
    out << "#{yellow}WARNING#{return_color}"
  end
  if muted
    out << " (MUTED)"
  end
  out
end

#format_monitoring_check_type(check) ⇒ Object



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

def format_monitoring_check_type(check)
  out = ""
  if check['checkType']
    if check['checkType']['code'] == 'mixedCheck' || check['checkType']['code'] == 'mixed'
      out = check['checkType']["name"] || "Mixed"
    else
      out = check['checkType']["name"] || ""
    end
  end
  out
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


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

def print_check_groups_table(check_groups, opts={})
  columns = [
    {"ID" => lambda {|check| check['id'] } },
    {"STATUS" => lambda {|check| format_monitoring_check_status(check) } },
    {"NAME" => lambda {|check| check['name'] } },
    {"TIME" => lambda {|check| check['lastRunDate'] ? format_local_dt(check['lastRunDate']) : "N/A" } },
    {"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| format_monitoring_check_last_metric(check) } }, justify: "center" },
    {"TYPE" => lambda {|check| format_monitoring_check_type(check) } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(check_groups, columns, opts)
end


278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 278

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


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 293

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


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 261

def print_checks_table(checks, opts={})
  columns = [
    {"ID" => lambda {|check| check['id'] } },
    {"STATUS" => lambda {|check| format_monitoring_check_status(check) } },
    {"NAME" => lambda {|check| check['name'] } },
    {"TIME" => lambda {|check| check['lastRunDate'] ? format_local_dt(check['lastRunDate']) : "N/A" } },
    {"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| format_monitoring_check_last_metric(check) } }, justify: "center" },
    {"TYPE" => lambda {|check| format_monitoring_check_type(check) } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(checks, 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


461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 461

def print_monitoring_apps_table(apps, opts={})
  columns = [
    {"ID" => lambda {|app| app['id'] } },
    {"STATUS" => lambda {|app| format_monitoring_check_status(app) } },
    {"NAME" => lambda {|app| app['name'] } },
    # {"DESCRIPTION" => lambda {|app| app['description'] } },
    {"TIME" => lambda {|app| app['lastRunDate'] ? format_local_dt(app['lastRunDate']) : "N/A" } },
    {"AVAILABILITY" => {display_method: lambda {|app| app['availability'] ? "#{app['availability'].to_f.round(3).to_s}%" : "N/A"} }, justify: "center" },
    {"RESPONSE TIME" => {display_method: lambda {|app| app['lastTimer'] ? "#{app['lastTimer']}ms" : "N/A" } }, justify: "center" },
    #{"LAST METRIC" => {display_method: lambda {|app| app['lastMetric'] ? "#{app['lastMetric']}" : "N/A" } }, justify: "center" },
    {"CHECKS" => lambda {|app| 
      checks = app['checks']
      checks_str = ""
      if checks && checks.size > 0
        checks_str = "#{checks.size} #{checks.size == 1 ? 'check' : 'checks'}"
        # checks_str << " [#{checks.join(', ')}]"
      end
      check_groups = app['checkGroups']
      check_groups_str = ""
      if check_groups && check_groups.size > 0
        check_groups_str = "#{check_groups.size} #{check_groups.size == 1 ? 'group' : 'groups'}"
        # check_groups_str << " [#{check_groups.join(', ')}]"
      end
      [checks_str, check_groups_str].reject {|s| s.empty? }.join(", ")
    } },
    
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(apps, columns, opts)
end