Class: Wavefront::Alerting

Inherits:
Object
  • Object
show all
Includes:
Constants, Mixins, Validators
Defined in:
lib/wavefront/alerting.rb

Constant Summary collapse

DEFAULT_PATH =
'/api/alert/'

Constants included from Constants

Constants::ALERT_FORMATS, Constants::DASH_FORMATS, Constants::DEFAULT_ALERT_FORMAT, Constants::DEFAULT_DASH_FORMAT, Constants::DEFAULT_FORMAT, Constants::DEFAULT_HOST, Constants::DEFAULT_INFILE_FORMAT, Constants::DEFAULT_OBSOLETE_METRICS, Constants::DEFAULT_OPTS, Constants::DEFAULT_PERIOD_SECONDS, Constants::DEFAULT_PREFIX_LENGTH, Constants::DEFAULT_PROXY, Constants::DEFAULT_PROXY_PORT, Constants::DEFAULT_SOURCE_FORMAT, Constants::DEFAULT_STRICT, Constants::EVENT_LEVELS, Constants::EVENT_STATE_DIR, Constants::FORMATS, Constants::GRANULARITIES, Constants::SOURCE_FORMATS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins

#call_delete, #call_get, #call_post, #hash_to_qs, #interpolate_schema, #load_file, #parse_time, #time_to_ms, #uri_concat

Methods included from Validators

#valid_path?, #valid_source?, #valid_string?, #valid_tags?, #valid_ts?, #valid_value?

Constructor Details

#initialize(token, host = DEFAULT_HOST, debug = false, options = {}) ⇒ Alerting

Returns a new instance of Alerting.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wavefront/alerting.rb', line 34

def initialize(token, host = DEFAULT_HOST, debug=false, options = {})
  #
  # Following existing convention, 'host' is the Wavefront API endpoint.
  #
  @headers = { :'X-AUTH-TOKEN' => token }
  @endpoint = host
  @token = token
  debug(debug)
  @noop = options[:noop]
  @verbose = options[:verbose]
  @options = options
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



32
33
34
# File 'lib/wavefront/alerting.rb', line 32

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



32
33
34
# File 'lib/wavefront/alerting.rb', line 32

def headers
  @headers
end

#noopObject (readonly)

Returns the value of attribute noop.



32
33
34
# File 'lib/wavefront/alerting.rb', line 32

def noop
  @noop
end

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/wavefront/alerting.rb', line 32

def options
  @options
end

#tokenObject (readonly)

Returns the value of attribute token.



32
33
34
# File 'lib/wavefront/alerting.rb', line 32

def token
  @token
end

#verboseObject (readonly)

Returns the value of attribute verbose.



32
33
34
# File 'lib/wavefront/alerting.rb', line 32

def verbose
  @verbose
end

Instance Method Details

#active(options = {}) ⇒ Object



127
128
129
130
# File 'lib/wavefront/alerting.rb', line 127

def active(options={})
  call_get(create_uri(options.merge(path: 'active',
                                    qs: mk_qs(options))))
end

#affected_by_maintenance(options = {}) ⇒ Object



146
147
148
149
# File 'lib/wavefront/alerting.rb', line 146

def affected_by_maintenance(options={})
  call_get(create_uri(options.merge(path: 'affected_by_maintenance',
                                    qs: mk_qs(options))))
end

#all(options = {}) ⇒ Object



132
133
134
# File 'lib/wavefront/alerting.rb', line 132

def all(options={})
  call_get(create_uri(options.merge(path: 'all', qs: mk_qs(options))))
end

#create_alert(alert = {}) ⇒ Object



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
# File 'lib/wavefront/alerting.rb', line 79

def create_alert(alert={})
  #
  # Create an alert. Expects you to provide it with a hash of
  # the form:
  #
  # {
  #   name:                 string
  #   condition:            string
  #   displayExpression:    string     (optional)
  #   minutes:              int
  #   resolveMinutes:       int        (optional)
  #   notifications:        array
  #   severity:             INFO | SMOKE | WARN | SEVERE
  #   privateTags:          array      (optional)
  #   sharedTags:           array      (optional)
  #   additionalInformation string     (optional)
  #   id                    string     (optional - will trigger update behaviour)
  # }
  #
  %w(name condition minutes notifications severity).each do |f|
    raise "missing field: #{f}" unless alert.key?(f.to_sym)
  end

  unless %w(INFO SMOKE WARN SEVERE).include?(alert[:severity])
    raise 'invalid severity'
  end

  %w(notifications privateTags sharedTags).each do |f|
    f = f.to_sym
    alert[f] = alert[f].join(',') if alert[f] && alert[f].is_a?(Array)
  end

  path = alert.has_key?(:id) ? alert[:id] : 'create'

  call_post(create_uri(path: path),
            hash_to_qs(alert), 'application/x-www-form-urlencoded')
end

#get_alert(id, options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/wavefront/alerting.rb', line 117

def get_alert(id, options = {})
  #
  # Alerts are identified by the timestamp at which they were
  # created. Returns a hash. Exceptions are just passed on
  # through. You get a 500 if the alert doesn't exist.
  #
  resp = call_get(create_uri(path: id)) || '{}'
  return JSON.parse(resp)
end

#import_to_create(raw) ⇒ Object



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
# File 'lib/wavefront/alerting.rb', line 47

def import_to_create(raw)
  #
  # Take a previously exported alert, and construct a hash which
  # create_alert() can use to re-create it.
  #
  ret = {
    name:          raw['name'],
    condition:     raw['condition'],
    minutes:       raw['minutes'],
    notifications: raw['target'].split(','),
    severity:      raw['severity'],
  }

  if raw.key?('displayExpression')
    ret[:displayExpression] = raw['displayExpression']
  end

  if raw.key?('resolveAfterMinutes')
    ret[:resolveMinutes] = raw['resolveAfterMinutes']
  end

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

  if raw.key?('additionalInformation')
    ret[:additionalInformation] = raw['additionalInformation']
  end

  ret
end

#invalid(options = {}) ⇒ Object



136
137
138
139
# File 'lib/wavefront/alerting.rb', line 136

def invalid(options={})
  call_get(create_uri(options.merge(path: 'invalid',
                                    qs: mk_qs(options))))
end

#snoozed(options = {}) ⇒ Object



141
142
143
144
# File 'lib/wavefront/alerting.rb', line 141

def snoozed(options={})
  call_get(create_uri(options.merge(path: 'snoozed',
                                    qs: mk_qs(options))))
end