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



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



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wavefront/events.rb', line 70

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wavefront/events.rb', line 51

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



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

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



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

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

#hash_to_qs(payload) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/wavefront/events.rb', line 87

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



83
84
85
# File 'lib/wavefront/events.rb', line 83

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