Class: WavefrontCli::Alert

Inherits:
Base
  • Object
show all
Includes:
Mixin::Acl, Mixin::Tag
Defined in:
lib/wavefront-cli/alert.rb

Overview

CLI coverage for the v2 ‘alert’ API.

Constant Summary

Constants included from Constants

Constants::ALL_PAGE_SIZE, Constants::DEFAULT_CONFIG, Constants::DEFAULT_OPTS, Constants::EVENT_STATE_DIR, Constants::HUMAN_TIME_FORMAT, Constants::HUMAN_TIME_FORMAT_MS, Constants::SEARCH_SPLIT

Instance Attribute Summary

Attributes inherited from Base

#klass, #klass_word, #options, #wf

Instance Method Summary collapse

Methods included from Mixin::Acl

#_acl_action, #do_acl_clear, #do_acl_grant, #do_acl_revoke, #do_acls, #everyone_id, #grant_modify, #grant_view, #print_status, #revoke_modify, #revoke_view

Methods included from Mixin::Tag

#do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_pathsearch, #do_tag_set, #do_tags

Methods inherited from Base

#_sdk_class, #cannot_noop!, #check_response_blocks, #check_status, #cli_output_class, #conds_to_query, #descriptive_name, #dispatch, #display, #display_api_error, #display_class, #display_no_api_response, #do_dump, #do_import, #do_list, #do_search, #do_set, #do_undelete, #dump_json, #dump_yaml, #extract_values, #failed_validation_message, #format_var, #handle_error, #handle_response, #hcl_fields, #initialize, #item_dump_call, #load_display_class, #matching_method, #method_word_list, #mk_creds, #mk_opts, #name_of_do_method, #no_api_response, #ok_exit, #one_or_all, #options_and_exit, #parseable_output, #range_hash, #require_sdk_class, #run, #search_key, #smart_delete, #smart_delete_message, #status_error_handler, #unsupported_format_message, #validate_id, #validate_input, #validate_opts, #validate_tags, #validator_exception, #validator_method, #warning_message

Constructor Details

This class inherits a constructor from WavefrontCli::Base

Instance Method Details

#affected_hosts_for_id(id) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/wavefront-cli/alert.rb', line 167

def affected_hosts_for_id(id)
  resp = wf.describe(id)

  return if options[:noop]

  return resp unless resp.ok? && resp.response.key?(:failingHostLabelPairs)

  resp.tap do |r|
    r.response = r.response[:failingHostLabelPairs].map { |h| h[:host] }
  end
end

#all_affected_hostsObject



158
159
160
161
162
163
164
165
# File 'lib/wavefront-cli/alert.rb', line 158

def all_affected_hosts
  cannot_noop!
  in_state(:firing).tap do |r|
    r.response = r.response.items.each_with_object({}) do |alert, aggr|
      aggr[alert[:id]] = affected_hosts_for_id(alert[:id]).response
    end
  end
end

#do_affected_hostsObject



53
54
55
56
57
58
59
# File 'lib/wavefront-cli/alert.rb', line 53

def do_affected_hosts
  if options[:'<id>']
    affected_hosts_for_id(options[:'<id>'])
  else
    all_affected_hosts
  end
end

#do_cloneObject



37
38
39
# File 'lib/wavefront-cli/alert.rb', line 37

def do_clone
  wf.clone(options[:'<id>'], options[:version]&.to_i)
end

#do_currentlyObject



61
62
63
64
65
66
67
68
69
# File 'lib/wavefront-cli/alert.rb', line 61

def do_currently
  state = options[:'<state>'].to_s

  if wf.respond_to?(state)
    in_state(state)
  else
    abort format("'%<state>s' is not a valid alert state.", state: state)
  end
end

#do_deleteObject



33
34
35
# File 'lib/wavefront-cli/alert.rb', line 33

def do_delete
  smart_delete
end

#do_describeObject



21
22
23
# File 'lib/wavefront-cli/alert.rb', line 21

def do_describe
  wf.describe(options[:'<id>'], options[:version])
end

#do_firingObject



71
72
73
# File 'lib/wavefront-cli/alert.rb', line 71

def do_firing
  in_state(:firing)
end

#do_historyObject



49
50
51
# File 'lib/wavefront-cli/alert.rb', line 49

def do_history
  wf.history(options[:'<id>'], options[:offset], options[:limit])
end

#do_installObject



89
90
91
# File 'lib/wavefront-cli/alert.rb', line 89

def do_install
  wf.install(options[:'<id>'])
end

#do_latestObject



45
46
47
# File 'lib/wavefront-cli/alert.rb', line 45

def do_latest
  wf.versions(options[:'<id>'])
end

#do_queriesObject



79
80
81
82
83
84
85
86
87
# File 'lib/wavefront-cli/alert.rb', line 79

def do_queries
  resp, data = one_or_all

  resp.tap do |r|
    r.response.items = data.map do |a|
      { id: a.id, condition: a.condition }
    end
  end
end

#do_snoozeObject



25
26
27
# File 'lib/wavefront-cli/alert.rb', line 25

def do_snooze
  wf.snooze(options[:'<id>'], options[:time])
end

#do_snoozedObject



75
76
77
# File 'lib/wavefront-cli/alert.rb', line 75

def do_snoozed
  in_state(:snoozed)
end

#do_summaryObject



41
42
43
# File 'lib/wavefront-cli/alert.rb', line 41

def do_summary
  wf.summary
end

#do_uninstallObject



93
94
95
# File 'lib/wavefront-cli/alert.rb', line 93

def do_uninstall
  wf.uninstall(options[:'<id>'])
end

#do_unsnoozeObject



29
30
31
# File 'lib/wavefront-cli/alert.rb', line 29

def do_unsnooze
  wf.unsnooze(options[:'<id>'])
end

#find_in_state(status) ⇒ Object

Does the work for #in_state

Parameters:

  • status (Symbol, String)

    the alert status you wish to find

Returns:

  • Wavefront::Response



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wavefront-cli/alert.rb', line 116

def find_in_state(status)
  search = do_search([format('status=%<status>s', status: status)])

  return if options[:noop]

  items = search.response.items.map do |i|
    { name: i.name, id: i.id, time: state_time(i) }
  end

  search.tap { |s| s.response[:items] = items }
end

#import_fieldsObject



15
16
17
18
19
# File 'lib/wavefront-cli/alert.rb', line 15

def import_fields
  %i[name condition minutes target severity displayExpression tags
     additionalInformation resolveAfterMinutes alertType severityList
     conditions acl]
end

#import_to_create(raw) ⇒ Object

Take a previously exported alert, and construct a hash which create() can use to re-create it.

Parameters:

  • raw (Hash)

    Ruby hash of imported data



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/wavefront-cli/alert.rb', line 145

def import_to_create(raw)
  import_fields.each_with_object({}) { |k, a| a[k.to_sym] = raw[k] }
               .tap do |ret|
    if raw.key?(:resolveAfterMinutes)
      ret[:resolveMinutes] = raw[:resolveAfterMinutes]
    end

    if raw.key?('customerTagsWithCounts')
      ret[:sharedTags] = raw['customerTagsWithCounts'].keys
    end
  end.compact
end

#in_state(status) ⇒ Object

How many alerts are in the given state? If none, say so, rather than just printing nothing.



100
101
102
103
104
105
106
107
108
109
# File 'lib/wavefront-cli/alert.rb', line 100

def in_state(status)
  options[:all] = true
  ret = find_in_state(status)

  exit if options[:noop]

  return ret unless ret.is_a?(Wavefront::Response) && ret.empty?

  ok_exit(format('No alerts are currently %<status>s.', status: status))
end

#state_time(item) ⇒ Integer

Snoozed alerts don’t have a start time, they have a “snoozed” time. This is -1 if they are snoozed forever: the formatting methods know what to do with that.

Returns:

  • (Integer)


133
134
135
136
137
138
# File 'lib/wavefront-cli/alert.rb', line 133

def state_time(item)
  return item[:event][:startTime] if item.key?(:event)
  return item[:snoozed] if item.key?(:snoozed)

  nil
end