Class: Wavefront::Cli::Alerts
Instance Attribute Summary collapse
#noop
Instance Method Summary
collapse
-
#format_result(result, format) ⇒ Object
-
#human_line(k, v) ⇒ Object
-
#human_line_additionalInformation(k, v) ⇒ Object
-
#human_line_alertStates(k, v) ⇒ Object
-
#human_line_created(k, v) ⇒ Object
-
#human_line_hostsUsed(k, v) ⇒ Object
-
#human_line_metricsUsed(k, v) ⇒ Object
-
#human_line_updated(k, v) ⇒ Object
-
#humanize(alerts) ⇒ Object
-
#indent_wrap(line, cols = 78, offset = 22) ⇒ Object
-
#run ⇒ Object
-
#valid_format?(fmt) ⇒ Boolean
-
#valid_state?(wfa, state) ⇒ Boolean
#initialize, #validate_opts
Constructor Details
This class inherits a constructor from Wavefront::Cli
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
24
25
26
|
# File 'lib/wavefront/cli/alerts.rb', line 24
def arguments
@arguments
end
|
#options ⇒ Object
Returns the value of attribute options.
24
25
26
|
# File 'lib/wavefront/cli/alerts.rb', line 24
def options
@options
end
|
Instance Method Details
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/wavefront/cli/alerts.rb', line 57
def format_result(result, format)
return if noop
case format
when :ruby
pp result
when :json
puts JSON.pretty_generate(JSON.parse(result))
when :human
puts humanize(JSON.parse(result))
else
raise "Invalid output format '#{format}'. See --help for more detail."
end
end
|
#human_line(k, v) ⇒ Object
131
132
133
|
# File 'lib/wavefront/cli/alerts.rb', line 131
def human_line(k, v)
('%-22s%s' % [k, v]).rstrip
end
|
165
166
167
|
# File 'lib/wavefront/cli/alerts.rb', line 165
def human_line_additionalInformation(k, v)
human_line(k, indent_wrap(v))
end
|
#human_line_alertStates(k, v) ⇒ Object
161
162
163
|
# File 'lib/wavefront/cli/alerts.rb', line 161
def human_line_alertStates(k, v)
human_line(k, v.join(','))
end
|
#human_line_created(k, v) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/wavefront/cli/alerts.rb', line 135
def human_line_created(k, v)
human_line(k, Time.at(v / 1000))
end
|
#human_line_hostsUsed(k, v) ⇒ Object
147
148
149
150
151
152
153
154
155
|
# File 'lib/wavefront/cli/alerts.rb', line 147
def human_line_hostsUsed(k, v)
return k unless v && v.is_a?(Array) && ! v.empty?
v.sort!
[human_line(k, v.shift)] + v.map {|el| human_line('', el)}
end
|
#human_line_metricsUsed(k, v) ⇒ Object
157
158
159
|
# File 'lib/wavefront/cli/alerts.rb', line 157
def human_line_metricsUsed(k, v)
human_line_hostsUsed(k, v)
end
|
#human_line_updated(k, v) ⇒ Object
143
144
145
|
# File 'lib/wavefront/cli/alerts.rb', line 143
def human_line_updated(k, v)
human_line_created(k, v)
end
|
#humanize(alerts) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/wavefront/cli/alerts.rb', line 99
def humanize(alerts)
row_order = %w(name created severity condition displayExpression
minutes resolveAfterMinutes updated alertStates
metricsUsed hostsUsed additionalInformation)
x = alerts.map do |alert|
row_order.map do |key|
lm = "human_line_#{key}"
if self.respond_to?(lm)
self.method(lm.to_sym).call(key, alert[key])
else
human_line(key, alert[key])
end
end.<< ''
end
end
|
#indent_wrap(line, cols = 78, offset = 22) ⇒ Object
169
170
171
172
173
174
175
176
|
# File 'lib/wavefront/cli/alerts.rb', line 169
def indent_wrap(line, cols=78, offset=22)
return unless line
line.gsub(/(.{1,#{cols - offset}})(\s+|\Z)/, "\\1\n#{' ' *
offset}").rstrip
end
|
#run ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/wavefront/cli/alerts.rb', line 26
def run
raise 'Missing token.' if ! @options[:token] || @options[:token].empty?
raise 'Missing query.' if arguments.empty?
query = arguments[0].to_sym
wfa = Wavefront::Alerting.new(@options[:token], @options[:endpoint],
@options[:debug], {
noop: @options[:noop], verbose: @options[:verbose]})
valid_state?(wfa, query)
valid_format?(@options[:alertformat].to_sym)
options = { host: @options[:endpoint] }
if @options[:shared]
options[:shared_tags] = @options[:shared].delete(' ').split(',')
end
if @options[:private]
options[:private_tags] = @options[:private].delete(' ').split(',')
end
begin
result = wfa.send(query, options)
rescue => e
puts e if @options[:debug]
raise 'Unable to execute query.'
end
format_result(result, @options[:alertformat].to_sym)
exit
end
|
76
77
78
79
80
81
82
83
84
|
# File 'lib/wavefront/cli/alerts.rb', line 76
def valid_format?(fmt)
fmt = fmt.to_sym if fmt.is_a?(String)
unless Wavefront::Client::ALERT_FORMATS.include?(fmt)
raise 'Output format must be one of: ' +
Wavefront::Client::ALERT_FORMATS.join(', ') + '.'
end
true
end
|
#valid_state?(wfa, state) ⇒ Boolean
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/wavefront/cli/alerts.rb', line 86
def valid_state?(wfa, state)
states = %w(active affected_by_maintenance all invalid snoozed)
unless states.include?(state.to_s)
raise "State must be one of: #{states.join(', ')}."
end
true
end
|