Class: Wavefront::Cli::Events
Overview
in a timeseries API call.
Constant Summary
Wavefront::Constants::ALERT_FORMATS, Wavefront::Constants::DEFAULT_ALERT_FORMAT, Wavefront::Constants::DEFAULT_FORMAT, Wavefront::Constants::DEFAULT_HOST, Wavefront::Constants::DEFAULT_INFILE_FORMAT, Wavefront::Constants::DEFAULT_OBSOLETE_METRICS, Wavefront::Constants::DEFAULT_PERIOD_SECONDS, Wavefront::Constants::DEFAULT_PREFIX_LENGTH, Wavefront::Constants::DEFAULT_PROXY, Wavefront::Constants::DEFAULT_PROXY_PORT, Wavefront::Constants::DEFAULT_STRICT, Wavefront::Constants::EVENT_LEVELS, Wavefront::Constants::EVENT_STATE_DIR, Wavefront::Constants::FORMATS, Wavefront::Constants::GRANULARITIES
Instance Attribute Summary collapse
#arguments, #options
Instance Method Summary
collapse
Methods included from Mixins
#interpolate_schema, #parse_time, #time_to_ms
#initialize, #load_profile
Constructor Details
This class inherits a constructor from Wavefront::Cli
Instance Attribute Details
#hostname ⇒ Object
Returns the value of attribute hostname.
30
31
32
|
# File 'lib/wavefront/cli/events.rb', line 30
def hostname
@hostname
end
|
#hosts ⇒ Object
Returns the value of attribute hosts.
30
31
32
|
# File 'lib/wavefront/cli/events.rb', line 30
def hosts
@hosts
end
|
#state_dir ⇒ Object
Returns the value of attribute state_dir.
30
31
32
|
# File 'lib/wavefront/cli/events.rb', line 30
def state_dir
@state_dir
end
|
#t_end ⇒ Object
Returns the value of attribute t_end.
30
31
32
|
# File 'lib/wavefront/cli/events.rb', line 30
def t_end
@t_end
end
|
#t_start ⇒ Object
Returns the value of attribute t_start.
30
31
32
|
# File 'lib/wavefront/cli/events.rb', line 30
def t_start
@t_start
end
|
#wf_event ⇒ Object
Returns the value of attribute wf_event.
30
31
32
|
# File 'lib/wavefront/cli/events.rb', line 30
def wf_event
@wf_event
end
|
Instance Method Details
#close_event(ev_name, ts) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/wavefront/cli/events.rb', line 134
def close_event(ev_name, ts)
puts "Closing event '#{ev_name}'. [#{Time.at(ts.to_i / 1000)}]"
begin
wf_event.close(
s: ts,
n: ev_name
)
rescue RestClient::Unauthorized
raise 'Not authorized to connect to Wavefront.'
rescue => e
puts e
raise
end
state_file = state_filename(ev_name, ts)
return unless state_file.exist?
puts "Removing state file #{state_file}."
File.unlink state_file
end
|
#close_event_handler ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/wavefront/cli/events.rb', line 114
def close_event_handler
if options[:'<timestamp>'] && options[:'<event>']
close_event(options[:'<event>'], options[:'<timestamp>'])
else
ev_file = pop_event(options[:'<event>'])
if ev_file
close_event(ev_file[1], ev_file[0])
else
fail "No event '#{options[:'<event>']}' to close."
end
end
end
|
#create_event ⇒ Object
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
|
# File 'lib/wavefront/cli/events.rb', line 87
def create_event
req_obj = {
n: options[:'<event>'],
d: options[:desc],
h: hosts,
c: options[:instant],
l: options[:level]
}
req_obj[:s] = t_start if t_start
req_obj[:e] = t_end if t_end
begin
response = wf_event.create(req_obj)
rescue RestClient::Unauthorized
raise 'Cannot connect to Wavefront API'
rescue => e
puts e
raise 'Cannot create event'
end
response
end
|
#create_event_handler ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/wavefront/cli/events.rb', line 73
def create_event_handler
output = create_event
unless options[:end] || options[:instant]
create_state_dir
create_state_file(output) unless options[:nostate]
end
puts JSON.pretty_generate(JSON.parse(output)) if options[:verbose]
end
|
#create_state_dir ⇒ Object
188
189
190
191
192
193
|
# File 'lib/wavefront/cli/events.rb', line 188
def create_state_dir
FileUtils.mkdir_p(state_dir)
unless state_dir.exist? && state_dir.directory? && state_dir.writable?
fail 'Cannot create state directory.'
end
end
|
#create_state_file(response) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/wavefront/cli/events.rb', line 202
def create_state_file(response)
ts = JSON.parse(response)['startTime']
fname = state_filename(options[:'<event>'], ts)
begin
File.open(fname, 'w') { hosts.to_s }
rescue
JSON.pretty_generate(JSON.parse(response))
raise 'Event was created but state file was not.'
end
puts "Event state recorded at #{fname}."
end
|
#pop_event(name = false) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/wavefront/cli/events.rb', line 173
def pop_event(name = false)
list = state_dir.children
list.select! { |f| f.basename.to_s.split('::').last == name } if name
return false if list.length == 0
list.sort.last.basename.to_s.split('::')
end
|
#prep_hosts(hosts = false) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/wavefront/cli/events.rb', line 63
def prep_hosts(hosts = false)
hosts = hostname unless hosts
hosts.split(',')
end
|
#prep_time(t) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/wavefront/cli/events.rb', line 55
def prep_time(t)
options[t] ? time_to_ms(parse_time(options[t])) : false
end
|
#run ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/wavefront/cli/events.rb', line 35
def run
@state_dir = EVENT_STATE_DIR + Etc.getlogin
@hostname = Socket.gethostname
@hosts = prep_hosts(options[:host])
@t_start = prep_time(:start)
@t_end = prep_time(:end)
@wf_event = Wavefront::Events.new(options[:token])
if options[:create]
create_event_handler
elsif options[:close]
close_event_handler
elsif options[:show]
show_open_events
else
fail 'undefined event error.'
end
end
|
#show_open_events ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/wavefront/cli/events.rb', line 159
def show_open_events
events = state_dir.children
if events.length == 0
puts 'No open events.'
else
events.each { |e| puts e.basename }
end
end
|
#state_filename(ev_name, ts) ⇒ Object
195
196
197
198
199
200
|
# File 'lib/wavefront/cli/events.rb', line 195
def state_filename(ev_name, ts)
state_dir + [ts, ev_name].join('::')
end
|