Class: Wavefront::Events

Inherits:
Object
  • Object
show all
Includes:
Constants, Mixins
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::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

Constructor Details

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

Returns a new instance of Events.



28
29
30
31
32
33
34
35
36
37
# File 'lib/wavefront/events.rb', line 28

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
  debug(debug)
  @noop = options[:noop]
  @verbose = options[:verbose]
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#noopObject (readonly)

Returns the value of attribute noop.



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

def noop
  @noop
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

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



43
44
45
# File 'lib/wavefront/events.rb', line 43

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

#close_uri(options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wavefront/events.rb', line 97

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

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

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



39
40
41
# File 'lib/wavefront/events.rb', line 39

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

#create_qs(payload = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wavefront/events.rb', line 78

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wavefront/events.rb', line 65

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

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

#debug(enabled) ⇒ Object



122
123
124
# File 'lib/wavefront/events.rb', line 122

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

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



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

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('/'))

  if (verbose || noop)
    puts "DELETE #{uri.to_s}"
    puts "HEADERS #{headers}"
  end

  return if noop

  RestClient.delete(uri.to_s, headers)
end

#make_call(uri, query) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/wavefront/events.rb', line 110

def make_call(uri, query)
  if (verbose || noop)
    puts "PUT #{uri.to_s}"
    puts "QUERY #{query}"
    puts "HEADERS #{headers}"
  end

  return if noop

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