Class: Wavefront::Events

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/wavefront/events.rb

Overview

These methods expect to be called with a hash whose keys are as defined in the Wavefront API Console. That is, ‘n’ as ‘name for the event’, ‘s’ as ‘start time for the event’ and so-on.

Constant Summary collapse

DEFAULT_PATH =
'/api/events/'

Constants included from Constants

Constants::ALERT_FORMATS, Constants::DEFAULT_ALERT_FORMAT, Constants::DEFAULT_FORMAT, Constants::DEFAULT_HOST, Constants::DEFAULT_INFILE_FORMAT, Constants::DEFAULT_OBSOLETE_METRICS, Constants::DEFAULT_PERIOD_SECONDS, Constants::DEFAULT_PREFIX_LENGTH, Constants::DEFAULT_PROXY, Constants::DEFAULT_PROXY_PORT, Constants::DEFAULT_STRICT, Constants::EVENT_LEVELS, Constants::EVENT_STATE_DIR, Constants::FORMATS, Constants::GRANULARITIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Events

Returns a new instance of Events.



26
27
28
# File 'lib/wavefront/events.rb', line 26

def initialize(token)
  @headers = { :'X-AUTH-TOKEN' => token }
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



24
25
26
# File 'lib/wavefront/events.rb', line 24

def headers
  @headers
end

Instance Method Details

#close(payload = {}, options = {}) ⇒ Object



34
35
36
# File 'lib/wavefront/events.rb', line 34

def close(payload = {}, options = {})
  make_call(close_uri(options), hash_to_qs(payload))
end

#close_uri(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wavefront/events.rb', line 81

def close_uri(options = {})
  #
  # Build the URI we use to send a 'close' request
  #
  options[:host] ||= DEFAULT_HOST
  options[:path] ||= DEFAULT_PATH

  URI::HTTPS.build(
    host:  options[:host],
    path:  options[:path] + 'close',
  )
end

#create(payload = {}, options = {}) ⇒ Object



30
31
32
# File 'lib/wavefront/events.rb', line 30

def create(payload = {}, options = {})
  make_call(create_uri(options), create_qs(payload))
end

#create_qs(payload = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wavefront/events.rb', line 62

def create_qs(payload = {})
  #
  # It seems that posting the hash means the 'host' data is
  # lost. Making a query string works though, so let's do that.
  #
  if payload[:h].is_a?(Array)
    hosts = payload[:h]
  elsif payload[:h].is_a?(String)
    hosts = [payload[:h]]
  else
    hosts = []
  end

  payload.delete(:h)
  query = hash_to_qs(payload)
  hosts.each { |host| query.<< "&h=#{host}" }
  query
end

#create_uri(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wavefront/events.rb', line 49

def create_uri(options = {})
  #
  # Build the URI we use to send a 'create' request.
  #
  options[:host] ||= DEFAULT_HOST
  options[:path] ||= DEFAULT_PATH

  URI::HTTPS.build(
    host:  options[:host],
    path:  options[:path],
  )
end

#debug(enabled) ⇒ Object



106
107
108
# File 'lib/wavefront/events.rb', line 106

def debug(enabled)
  RestClient.log = 'stdout' if enabled
end

#delete(payload = {}, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/wavefront/events.rb', line 38

def delete(payload = {}, options = {})
  unless payload.has_key?(:startTime)  && payload.has_key?(:name)
    raise 'invalid payload'
  end

  uri = create_uri(path: [DEFAULT_PATH, payload[:startTime],
                   payload[:name]].join('/').squeeze('/'))

  RestClient.delete(uri.to_s, headers)
end

#hash_to_qs(payload) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/wavefront/events.rb', line 98

def hash_to_qs(payload)
  #
  # Make a properly escaped query string out of a key: value
  # hash.
  #
  URI.escape(payload.map { |k, v| [k, v].join('=') }.join('&'))
end

#make_call(uri, query) ⇒ Object



94
95
96
# File 'lib/wavefront/events.rb', line 94

def make_call(uri, query)
  RestClient.post(uri.to_s, query, headers)
end