Module: Morpheus::Cli::MonitoringHelper

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

#available_severitiesObject



605
606
607
608
609
610
611
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 605

def available_severities
  [
    [name:'Critical', code:'critical', value:'critical'],
    [name:'Warning', code:'warning', value:'warning'],
    [name:'Info', code:'info', value:'info']
  ]
end

#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_alert_by_id(id) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 439

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

#find_alert_by_name(name) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 453

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

#find_alert_by_name_or_id(val) ⇒ Object

Monitoring Alerts



431
432
433
434
435
436
437
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 431

def find_alert_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_alert_by_id(val)
  else
    return find_alert_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



481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 481

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



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 495

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



473
474
475
476
477
478
479
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 473

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



397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 397

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



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 411

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"}, {"EMAIL" => "emailAddress"}], {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



389
390
391
392
393
394
395
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 389

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



540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 540

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



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 554

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



532
533
534
535
536
537
538
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 532

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_health_status(item, return_color = cyan) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 123

def format_health_status(item, return_color=cyan)
  out = ""
  if item
    attrs = {}
    attrs[:unknown] = item['lastRunDate'] ? false : true
    attrs[:muted] = item['createIncident'] == false
    attrs[:failure] = item['lastCheckStatus'] == 'error'
    attrs[:health] = item['health'] ? item['health'].to_i : 0
    
    if attrs[:unknown]
      out << "#{cyan}UNKNOWN#{return_color}"
    elsif attrs[:health] >= 10
      out << "#{green}HEALTHY#{return_color}"
    elsif attrs[:failure]
      out << "#{red}ERROR#{return_color}"
    else
      out << "#{yellow}CAUTION#{return_color}"
    end
    if attrs[:muted]
      out << "#{cyan} (Muted)#{return_color}"
    end
  end
  out
end

#format_monitoring_check_last_metric(check) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 271

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



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

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



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 284

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 160

def format_monitoring_incident_status(incident)
  out = ""
  muted = incident['inUptime'] == false
  status_string = incident['status']
  if status_string == 'closed'
    out << "CLOSED ✓"
  else
    out << status_string.to_s.upcase
    if muted
      out << " (MUTED)"
    end
  end
  out
end

#format_monitoring_issue_attachment_type(issue) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 148

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



175
176
177
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 175

def format_monitoring_issue_status(issue)
  format_monitoring_incident_status(issue)
end

#format_recipient_method(address_types) ⇒ Object



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 613

def format_recipient_method(address_types)
  address_types = address_types.to_s
  alert_method_names = []
  if address_types =~ /email/i
    alert_method_names << "Email"
  end
  if address_types =~ /sms/i
    alert_method_names << "SMS"
  end
  if address_types =~ /apn/i
    alert_method_names << "APN"
  end
  # if alert_method_names.empty?
  #   alert_method_names << "None"
  # end
  anded_list(alert_method_names)
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.upcase}#{return_color}"
  elsif status_string == 'warning'
    out << "#{yellow}#{status_string.upcase}#{return_color}"
  elsif status_string == 'info'
    out << "#{cyan}#{status_string.upcase}#{return_color}"
  else
    out << "#{cyan}#{status_string.to_s.upcase}#{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

#parse_recipient_method(address_types) ⇒ Object

server expects “emailAddress” or “smsAddress” or “emailAddress,smsAddress” todo: just use array, expect server to parse it.



633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 633

def parse_recipient_method(address_types)
  requested_methods = address_types.to_s
  alert_methods = []
  if address_types =~ /email/i
    alert_methods << "emailAddress"
  end
  if address_types =~ /sms/i || address_types =~ /phone/i || address_types =~ /mobile/i
    alert_methods << "smsAddress"
  end
  if address_types =~ /apn/i
    alert_methods << "apns"
  end
  alert_methods.join(',')
end


336
337
338
339
340
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 336

def print_check_group_history_table(history_items, opts={})
  columns = [
    {"STATUS" => lambda {|issue| format_health_status(issue) } },
    {"DATE CHECKED" => lambda {|issue| format_local_dt(issue['lastRunDate']) } },
    {"CHECK" => lambda {|issue| issue['name'] } },
    # {"AVAILABLE" => lambda {|issue| format_boolean issue['createIncident'] } },
    {"RESPONSE TIME" => lambda {|issue| issue["lastTimer"] ? "#{issue['lastTimer']}ms" : "" } }, 
    {"LAST METRIC" => lambda {|issue| issue["lastMetric"] } }, 
    {"MESSAGE" => lambda {|issue| 
      # issue["lastError"].to_s.empty? ? issue["lastMessage"] : issue["lastError"]
      if issue['lastCheckStatus'] == 'error'
        issue["lastError"].to_s
      else
        issue["lastMessage"]
      end
    } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(history_items, columns, opts)
end


513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 513

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


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 313

def print_check_history_table(history_items, opts={})
  columns = [
    {"STATUS" => lambda {|issue| format_health_status(issue) } },
    {"DATE CHECKED" => lambda {|issue| format_local_dt(issue['lastRunDate']) } },
    # {"NAME" => lambda {|issue| issue['name'] } },
    # {"AVAILABLE" => lambda {|issue| format_boolean issue['createIncident'] } },
    {"RESPONSE TIME" => lambda {|issue| issue["lastTimer"] ? "#{issue['lastTimer']}ms" : "" } }, 
    {"LAST METRIC" => lambda {|issue| issue["lastMetric"] } }, 
    {"MESSAGE" => lambda {|issue| 
      # issue["lastError"].to_s.empty? ? issue["lastMessage"] : issue["lastError"]
      if issue['lastCheckStatus'] == 'error'
        issue["lastError"].to_s
      else
        issue["lastMessage"]
      end
    } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(history_items, columns, opts)
end


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 363

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


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

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


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

def print_incident_history_table(history_items, opts={})
  columns = [
    # {"ID" => lambda {|issue| issue['id'] } },
    # {"SEVERITY" => lambda {|issue| format_health_status(issue) } },
    {"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


198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 198

def print_incident_issues_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


229
230
231
232
233
234
235
236
237
238
239
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 229

def print_incident_notifications_table(notifications, opts={})
  columns = [
    {"NAME" => lambda {|notification| notification['recipient'] ? notification['recipient']['name'] : '' } },
    {"DELIVERY TYPE" => lambda {|recipient| format_recipient_method(recipient['method'] || recipient['addressTypes']) } },
    {"NOTIFIED ON" => lambda {|notification| format_local_dt(notification['dateCreated']) } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(notifications, columns, opts)
end

Incidents



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 183

def print_incidents_table(incidents, opts={})
  columns = [
    {"ID" => lambda {|incident| incident['id'] } },
    {"SEVERITY" => lambda {|incident| format_severity(incident['severity']) } },
    {"NAME" => lambda {|incident| incident['displayName'] || 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


359
360
361
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 359

def print_monitor_app_history_table(history_items, opts={})
  print_check_group_history_table(history_items, opts)
end


572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 572

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

#prompt_for_check_groups(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 652

def prompt_for_check_groups(params, options={}, api_client=nil, api_params={})
# def prompt_for_check_groups(params, options={})
  # Check Groups
  check_group_list = nil
  check_group_ids = []
  still_prompting = true
  
  if params['checkGroups'].nil?
    while still_prompting
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'checkGroups', 'type' => 'text', 'fieldLabel' => 'Check Groups', 'required' => false, 'description' => 'Check Groups to include in this alert rule, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['checkGroups'].to_s.empty?
        check_group_list = v_prompt['checkGroups'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      bad_ids = []
      if check_group_list && check_group_list.size > 0
        check_group_list.each do |it|
          found_check = nil
          begin
            found_check = find_check_group_by_name_or_id(it)
          rescue SystemExit => cmdexit
          end
          if found_check
            check_group_ids << found_check['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    check_group_list = params['checkGroups']
    still_prompting = false
    bad_ids = []
    if check_group_list && check_group_list.size > 0
      check_group_list.each do |it|
        found_check = nil
        begin
          found_check = find_check_group_by_name_or_id(it)
        rescue SystemExit => cmdexit
        end
        if found_check
          check_group_ids << found_check['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Check Groups not found: #{bad_ids}"}
    end
    # return check_group_ids
    # payload = {'checkGroups':check_group_ids}
    # return payload
    return {success:true, data: check_group_ids}
  end
end

#prompt_for_checks(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 710

def prompt_for_checks(params, options={}, api_client=nil, api_params={})
  # Checks
  check_list = nil
  check_ids = nil
  still_prompting = true
  if params['checks'].nil?
    still_prompting = true
    while still_prompting do
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'checks', 'type' => 'text', 'fieldLabel' => 'Checks', 'required' => false, 'description' => 'Checks to include, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['checks'].to_s.empty?
        check_list = v_prompt['checks'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      check_ids = []
      bad_ids = []
      if check_list && check_list.size > 0
        check_list.each do |it|
          found_check = nil
          begin
            found_check = find_check_by_name_or_id(it)
          rescue SystemExit => cmdexit
          end
          if found_check
            check_ids << found_check['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    check_list = params['checks']
    still_prompting = false
    check_ids = []
    bad_ids = []
    if check_list && check_list.size > 0
      check_list.each do |it|
        found_check = nil
        begin
          found_check = find_check_by_name_or_id(it)
        rescue SystemExit => cmdexit
        end
        if found_check
          check_ids << found_check['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Checks not found: #{bad_ids}"}
    end
  end
  return {success:true, data: check_ids}
end

#prompt_for_monitor_apps(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 822

def prompt_for_monitor_apps(params, options={}, api_client=nil, api_params={})
  # Apps
    
  monitor_app_list = nil
  monitor_app_ids = []
  if params['apps'].nil?
    still_prompting = true
    while still_prompting
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'apps', 'type' => 'text', 'fieldLabel' => 'Apps', 'required' => false, 'description' => 'Monitor Apps to include, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['apps'].to_s.empty?
        monitor_app_list = v_prompt['apps'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      check_group_ids = []
      bad_ids = []
      if monitor_app_list && monitor_app_list.size > 0
        monitor_app_list.each do |it|
          found_monitor_app = nil
          begin
            found_monitor_app = find_monitoring_app_by_name_or_id(it)
          rescue SystemExit => cmdexit
          end
          if found_monitor_app
            monitor_app_ids << found_monitor_app['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    monitor_app_list = params['apps']
    check_group_ids = []
    bad_ids = []
    if monitor_app_list && monitor_app_list.size > 0
      monitor_app_list.each do |it|
        found_monitor_app = nil
        begin
          found_monitor_app = find_monitoring_app_by_name_or_id(it)
        rescue SystemExit => cmdexit
        end
        if found_monitor_app
          monitor_app_ids << found_monitor_app['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Monitor Apps not found: #{bad_ids}"}
    end
  end
  return {success:true, data: monitor_app_ids}
end

#prompt_for_recipients(params, options = {}) ⇒ Object



648
649
650
# File 'lib/morpheus/cli/mixins/monitoring_helper.rb', line 648

def prompt_for_recipients(params, options={})
  #todo
end