class Wavefront::Alerting

Constants

DEFAULT_PATH

Attributes

endpoint[R]
headers[R]
noop[R]
options[R]
token[R]
verbose[R]

Public Class Methods

new(token, host = DEFAULT_HOST, debug=false, options = {}) click to toggle source
# 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

Public Instance Methods

active(options={}) click to toggle source
# 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={}) click to toggle source
# 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={}) click to toggle source
# 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={}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# 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={}) click to toggle source
# File lib/wavefront/alerting.rb, line 141
def snoozed(options={})
  call_get(create_uri(options.merge(path: 'snoozed',
                                    qs: mk_qs(options))))
end

Private Instance Methods

create_uri(options = {}) click to toggle source
# File lib/wavefront/alerting.rb, line 175
def create_uri(options = {})
  #
  # Build the URI we use to send a 'create' request.
  #
  options[:host] ||= endpoint
  options[:path] ||= ''
  options[:qs]   ||= nil

  options[:qs] = nil if options[:qs] && options[:qs].empty?

  URI::HTTPS.build(
    host:  options[:host],
    path:  uri_concat(DEFAULT_PATH, options[:path]),
    query: options[:qs],
  )
end
debug(enabled) click to toggle source
# File lib/wavefront/alerting.rb, line 192
def debug(enabled)
  RestClient.log = 'stdout' if enabled
end
list_of_tags(t) click to toggle source
# File lib/wavefront/alerting.rb, line 153
def list_of_tags(t)
  t.is_a?(Array) ? t : [t]
end
mk_qs(options) click to toggle source
# File lib/wavefront/alerting.rb, line 157
def mk_qs(options)
  query = []

  if options[:shared_tags]
    query.push(list_of_tags(options[:shared_tags]).map do |t|
      "customerTag=#{t}"
    end.join('&'))
  end

  if options[:private_tags]
    query.push(list_of_tags(options[:private_tags]).map do |t|
      "userTag=#{t}"
    end.join('&'))
  end

  query.join('&')
end