fluent-plugin-sensu 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +13 -0
- data/README.md +344 -0
- data/Rakefile +13 -0
- data/fluent-plugin-sensu.gemspec +28 -0
- data/lib/fluent/plugin/out_sensu.rb +288 -0
- data/test/helper.rb +33 -0
- data/test/plugin/test_out_sensu.rb +671 -0
- metadata +143 -0
data/test/helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# vim: et sw=2 sts=2
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler'
|
6
|
+
|
7
|
+
begin
|
8
|
+
Bundler.setup(:default, :development)
|
9
|
+
rescue Bundler::BundlerError => e
|
10
|
+
$stderr.puts e.message
|
11
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
+
exit e.status_code
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'test/unit'
|
16
|
+
require 'rr'
|
17
|
+
|
18
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
19
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
20
|
+
|
21
|
+
require 'fluent/test'
|
22
|
+
|
23
|
+
unless ENV.has_key?('VERBOSE')
|
24
|
+
nulllogger = Object.new
|
25
|
+
nulllogger.instance_eval {|obj|
|
26
|
+
def method_missing(method, *args)
|
27
|
+
# pass
|
28
|
+
end
|
29
|
+
}
|
30
|
+
$log = nulllogger
|
31
|
+
end
|
32
|
+
|
33
|
+
require 'fluent/plugin/out_sensu'
|
@@ -0,0 +1,671 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# vim: et sw=2 sts=2 fdm=marker
|
3
|
+
|
4
|
+
require 'helper'
|
5
|
+
|
6
|
+
# Test class for Fluent::SensuOutput
|
7
|
+
class SensuOutputTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
# Setup {{{1
|
10
|
+
|
11
|
+
# Sets up the test env and the stub
|
12
|
+
def setup
|
13
|
+
Fluent::Test.setup
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns a driver of the plugin
|
17
|
+
def create_driver(conf, tag = 'test')
|
18
|
+
driver = Fluent::Test::BufferedOutputTestDriver.new(Fluent::SensuOutput, tag)
|
19
|
+
driver.configure(conf)
|
20
|
+
def (driver.instance).send_check(server, port, payload)
|
21
|
+
@sent_data ||= []
|
22
|
+
@sent_data.push([server, port, payload])
|
23
|
+
end
|
24
|
+
def (driver.instance).sent_data
|
25
|
+
return @sent_data
|
26
|
+
end
|
27
|
+
return driver
|
28
|
+
end
|
29
|
+
|
30
|
+
# }}}1
|
31
|
+
# Default value {{{1
|
32
|
+
|
33
|
+
# Send default values for empty data and empty setting
|
34
|
+
def test_write_empty_data_with_empty_setting
|
35
|
+
driver = create_driver('', 'ddos')
|
36
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
37
|
+
driver.emit({}, time)
|
38
|
+
driver.run
|
39
|
+
result = driver.instance.sent_data
|
40
|
+
expected = {
|
41
|
+
'name' => 'ddos',
|
42
|
+
'output' => '{}',
|
43
|
+
'status' => 3,
|
44
|
+
'type' => 'standard',
|
45
|
+
'handlers' => ['default'],
|
46
|
+
'executed' => time,
|
47
|
+
'fluentd' => {
|
48
|
+
'tag' => 'ddos',
|
49
|
+
'time' => time.to_i,
|
50
|
+
'record' => {},
|
51
|
+
},
|
52
|
+
}
|
53
|
+
assert_equal [['localhost', 3030, expected]], result
|
54
|
+
end
|
55
|
+
|
56
|
+
# }}}1
|
57
|
+
# Connection {{{1
|
58
|
+
|
59
|
+
# Connection settings are read
|
60
|
+
def test_connection_settings
|
61
|
+
driver = create_driver(%[
|
62
|
+
server sensu-client.local
|
63
|
+
port 3031
|
64
|
+
])
|
65
|
+
assert_equal 'sensu-client.local', driver.instance.instance_eval{ @server }
|
66
|
+
assert_equal 3031, driver.instance.instance_eval{ @port }
|
67
|
+
end
|
68
|
+
|
69
|
+
# Default values are set to connection settings
|
70
|
+
def test_default_connection_settings
|
71
|
+
driver = create_driver('')
|
72
|
+
assert_equal 'localhost', driver.instance.instance_eval{ @server }
|
73
|
+
assert_equal 3030, driver.instance.instance_eval{ @port }
|
74
|
+
end
|
75
|
+
|
76
|
+
# Send default values to aother node and another port
|
77
|
+
def test_send_to_another_connection
|
78
|
+
driver = create_driver(%[
|
79
|
+
server sensu-client.local
|
80
|
+
port 3031
|
81
|
+
], 'ddos')
|
82
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
83
|
+
driver.emit({}, time)
|
84
|
+
driver.run
|
85
|
+
result = driver.instance.sent_data
|
86
|
+
expected = {
|
87
|
+
'name' => 'ddos',
|
88
|
+
'output' => '{}',
|
89
|
+
'status' => 3,
|
90
|
+
'type' => 'standard',
|
91
|
+
'handlers' => ['default'],
|
92
|
+
'executed' => time,
|
93
|
+
'fluentd' => {
|
94
|
+
'tag' => 'ddos',
|
95
|
+
'time' => time.to_i,
|
96
|
+
'record' => {},
|
97
|
+
},
|
98
|
+
}
|
99
|
+
assert_equal [['sensu-client.local', 3031, expected]], result
|
100
|
+
end
|
101
|
+
|
102
|
+
# }}}1
|
103
|
+
# "name" attribute {{{1
|
104
|
+
|
105
|
+
# Rejects invalid check name config
|
106
|
+
def test_rejects_invalid_check_name_in_option
|
107
|
+
assert_raise(Fluent::ConfigError) {
|
108
|
+
create_driver(%[check_name invalid/check/name], 'ddos')
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
# Determine "name" attribute with options
|
113
|
+
def test_determine_name_attribute_with_options
|
114
|
+
driver = create_driver(%[
|
115
|
+
check_name_field service
|
116
|
+
check_name ddos_detection
|
117
|
+
], 'ddos')
|
118
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
119
|
+
driver.emit({ 'service' => 'flood' }, time)
|
120
|
+
driver.emit({ 'service' => 'invalid/check/name' }, time)
|
121
|
+
driver.emit({}, time)
|
122
|
+
driver.run
|
123
|
+
result = driver.instance.sent_data
|
124
|
+
expected1 = {
|
125
|
+
'name' => 'flood',
|
126
|
+
'output' => '{"service":"flood"}',
|
127
|
+
'status' => 3,
|
128
|
+
'type' => 'standard',
|
129
|
+
'handlers' => ['default'],
|
130
|
+
'executed' => time,
|
131
|
+
'fluentd' => {
|
132
|
+
'tag' => 'ddos',
|
133
|
+
'time' => time.to_i,
|
134
|
+
'record' => { 'service' => 'flood' },
|
135
|
+
},
|
136
|
+
}
|
137
|
+
expected2 = {
|
138
|
+
'name' => 'ddos_detection',
|
139
|
+
'output' => '{"service":"invalid/check/name"}',
|
140
|
+
'status' => 3,
|
141
|
+
'type' => 'standard',
|
142
|
+
'handlers' => ['default'],
|
143
|
+
'executed' => time,
|
144
|
+
'fluentd' => {
|
145
|
+
'tag' => 'ddos',
|
146
|
+
'time' => time.to_i,
|
147
|
+
'record' => { 'service' => 'invalid/check/name' },
|
148
|
+
},
|
149
|
+
}
|
150
|
+
expected3 = {
|
151
|
+
'name' => 'ddos_detection',
|
152
|
+
'output' => '{}',
|
153
|
+
'status' => 3,
|
154
|
+
'type' => 'standard',
|
155
|
+
'handlers' => ['default'],
|
156
|
+
'executed' => time,
|
157
|
+
'fluentd' => {
|
158
|
+
'tag' => 'ddos',
|
159
|
+
'time' => time.to_i,
|
160
|
+
'record' => {},
|
161
|
+
},
|
162
|
+
}
|
163
|
+
assert_equal [
|
164
|
+
['localhost', 3030, expected1],
|
165
|
+
['localhost', 3030, expected2],
|
166
|
+
['localhost', 3030, expected3],
|
167
|
+
], result
|
168
|
+
end
|
169
|
+
|
170
|
+
# Dertermine "name" attribute with tag
|
171
|
+
def test_determine_name_attribute_with_tag
|
172
|
+
driver = create_driver('', 'ddos')
|
173
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
174
|
+
driver.emit({}, time)
|
175
|
+
driver.run
|
176
|
+
result = driver.instance.sent_data
|
177
|
+
expected = {
|
178
|
+
'name' => 'ddos',
|
179
|
+
'output' => '{}',
|
180
|
+
'status' => 3,
|
181
|
+
'type' => 'standard',
|
182
|
+
'handlers' => ['default'],
|
183
|
+
'executed' => time,
|
184
|
+
'fluentd' => {
|
185
|
+
'tag' => 'ddos',
|
186
|
+
'time' => time.to_i,
|
187
|
+
'record' => {},
|
188
|
+
},
|
189
|
+
}
|
190
|
+
assert_equal([['localhost', 3030, expected]], result)
|
191
|
+
end
|
192
|
+
|
193
|
+
# Dertermine "name" attribute as default constant
|
194
|
+
def test_determine_name_attribute_as_default_constant
|
195
|
+
driver = create_driver('', 'invalid/check/name')
|
196
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
197
|
+
driver.emit({}, time)
|
198
|
+
driver.run
|
199
|
+
result = driver.instance.sent_data
|
200
|
+
expected = {
|
201
|
+
'name' => 'fluent-plugin-sensu',
|
202
|
+
'output' => '{}',
|
203
|
+
'status' => 3,
|
204
|
+
'type' => 'standard',
|
205
|
+
'handlers' => ['default'],
|
206
|
+
'executed' => time,
|
207
|
+
'fluentd' => {
|
208
|
+
'tag' => 'invalid/check/name',
|
209
|
+
'time' => time.to_i,
|
210
|
+
'record' => {},
|
211
|
+
},
|
212
|
+
}
|
213
|
+
assert_equal([['localhost', 3030, expected]], result)
|
214
|
+
end
|
215
|
+
|
216
|
+
# }}}1
|
217
|
+
# "output" attribute {{{1
|
218
|
+
|
219
|
+
# Check output is determined by check_output_field
|
220
|
+
# or check_output if specified
|
221
|
+
def test_determine_output_attribute_with_options
|
222
|
+
driver = create_driver(%[
|
223
|
+
check_output_field desc
|
224
|
+
check_output Something is going on
|
225
|
+
], 'ddos')
|
226
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
227
|
+
driver.emit({ 'desc' => 'Something is afield' }, time)
|
228
|
+
driver.emit({}, time)
|
229
|
+
driver.run
|
230
|
+
result = driver.instance.sent_data
|
231
|
+
expected_for_output_field = {
|
232
|
+
'name' => 'ddos',
|
233
|
+
'output' => 'Something is afield',
|
234
|
+
'status' => 3,
|
235
|
+
'type' => 'standard',
|
236
|
+
'handlers' => ['default'],
|
237
|
+
'executed' => time,
|
238
|
+
'fluentd' => {
|
239
|
+
'tag' => 'ddos',
|
240
|
+
'time' => time.to_i,
|
241
|
+
'record' => { 'desc' => 'Something is afield' },
|
242
|
+
},
|
243
|
+
}
|
244
|
+
expected_for_output_option = {
|
245
|
+
'name' => 'ddos',
|
246
|
+
'output' => 'Something is going on',
|
247
|
+
'status' => 3,
|
248
|
+
'type' => 'standard',
|
249
|
+
'handlers' => ['default'],
|
250
|
+
'executed' => time,
|
251
|
+
'fluentd' => {
|
252
|
+
'tag' => 'ddos',
|
253
|
+
'time' => time.to_i,
|
254
|
+
'record' => {},
|
255
|
+
},
|
256
|
+
}
|
257
|
+
assert_equal([
|
258
|
+
['localhost', 3030, expected_for_output_field],
|
259
|
+
['localhost', 3030, expected_for_output_option],
|
260
|
+
], result)
|
261
|
+
end
|
262
|
+
|
263
|
+
# }}}1
|
264
|
+
# "status" attribute {{{1
|
265
|
+
|
266
|
+
# Rejects an invalid value for check_status option.
|
267
|
+
def test_rejects_invalid_status
|
268
|
+
assert_raise(Fluent::ConfigError) {
|
269
|
+
create_driver(%[check_status 4], 'ddos')
|
270
|
+
}
|
271
|
+
end
|
272
|
+
|
273
|
+
# "status" attribute is determined by check_status
|
274
|
+
# and check_status_field option if specified.
|
275
|
+
def test_determine_status_attribute_with_options
|
276
|
+
driver = create_driver(%[
|
277
|
+
check_status_field severity
|
278
|
+
check_status 0
|
279
|
+
], 'ddos')
|
280
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
281
|
+
driver.emit({ 'severity' => 1 }, time)
|
282
|
+
driver.emit({ 'severity' => 'Critical' }, time)
|
283
|
+
driver.emit({}, time)
|
284
|
+
driver.run
|
285
|
+
result = driver.instance.sent_data
|
286
|
+
expected_for_numerical_severity = {
|
287
|
+
'name' => 'ddos',
|
288
|
+
'output' => '{"severity":1}',
|
289
|
+
'status' => 1,
|
290
|
+
'type' => 'standard',
|
291
|
+
'handlers' => ['default'],
|
292
|
+
'executed' => time,
|
293
|
+
'fluentd' => {
|
294
|
+
'tag' => 'ddos',
|
295
|
+
'time' => time.to_i,
|
296
|
+
'record' => { 'severity' => 1 },
|
297
|
+
},
|
298
|
+
}
|
299
|
+
expected_for_string_severity = {
|
300
|
+
'name' => 'ddos',
|
301
|
+
'output' => '{"severity":"Critical"}',
|
302
|
+
'status' => 2,
|
303
|
+
'type' => 'standard',
|
304
|
+
'handlers' => ['default'],
|
305
|
+
'executed' => time,
|
306
|
+
'fluentd' => {
|
307
|
+
'tag' => 'ddos',
|
308
|
+
'time' => time.to_i,
|
309
|
+
'record' => { 'severity' => 'Critical' },
|
310
|
+
},
|
311
|
+
}
|
312
|
+
expected_for_option = {
|
313
|
+
'name' => 'ddos',
|
314
|
+
'output' => '{}',
|
315
|
+
'status' => 0,
|
316
|
+
'type' => 'standard',
|
317
|
+
'handlers' => ['default'],
|
318
|
+
'executed' => time,
|
319
|
+
'fluentd' => {
|
320
|
+
'tag' => 'ddos',
|
321
|
+
'time' => time.to_i,
|
322
|
+
'record' => {},
|
323
|
+
},
|
324
|
+
}
|
325
|
+
assert_equal([
|
326
|
+
['localhost', 3030, expected_for_numerical_severity],
|
327
|
+
['localhost', 3030, expected_for_string_severity],
|
328
|
+
['localhost', 3030, expected_for_option],
|
329
|
+
], result)
|
330
|
+
end
|
331
|
+
|
332
|
+
# }}}1
|
333
|
+
# "type" attribute {{{1
|
334
|
+
|
335
|
+
# Rejects a check types if it is neither "standard" nor "metric".
|
336
|
+
def test_rejects_invalid_check_type
|
337
|
+
assert_raise(Fluent::ConfigError) {
|
338
|
+
create_driver(%[check_type nosuchtype], 'ddos')
|
339
|
+
}
|
340
|
+
end
|
341
|
+
|
342
|
+
# Specifies "standard" check type.
|
343
|
+
def test_specify_standard_check_type
|
344
|
+
driver = create_driver(%[
|
345
|
+
check_type standard
|
346
|
+
], 'ddos')
|
347
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
348
|
+
driver.emit({}, time)
|
349
|
+
driver.run
|
350
|
+
result = driver.instance.sent_data
|
351
|
+
expected = {
|
352
|
+
'name' => 'ddos',
|
353
|
+
'output' => '{}',
|
354
|
+
'status' => 3,
|
355
|
+
'type' => 'standard',
|
356
|
+
'handlers' => ['default'],
|
357
|
+
'executed' => time,
|
358
|
+
'fluentd' => {
|
359
|
+
'tag' => 'ddos',
|
360
|
+
'time' => time.to_i,
|
361
|
+
'record' => {},
|
362
|
+
},
|
363
|
+
}
|
364
|
+
assert_equal([
|
365
|
+
['localhost', 3030, expected],
|
366
|
+
], result)
|
367
|
+
end
|
368
|
+
|
369
|
+
# Specifies "metric" check type.
|
370
|
+
def test_specify_metric_check_type
|
371
|
+
driver = create_driver(%[
|
372
|
+
check_type metric
|
373
|
+
], 'ddos')
|
374
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
375
|
+
driver.emit({}, time)
|
376
|
+
driver.run
|
377
|
+
result = driver.instance.sent_data
|
378
|
+
expected = {
|
379
|
+
'name' => 'ddos',
|
380
|
+
'output' => '{}',
|
381
|
+
'status' => 3,
|
382
|
+
'type' => 'metric',
|
383
|
+
'handlers' => ['default'],
|
384
|
+
'executed' => time,
|
385
|
+
'fluentd' => {
|
386
|
+
'tag' => 'ddos',
|
387
|
+
'time' => time.to_i,
|
388
|
+
'record' => {},
|
389
|
+
},
|
390
|
+
}
|
391
|
+
assert_equal([
|
392
|
+
['localhost', 3030, expected],
|
393
|
+
], result)
|
394
|
+
end
|
395
|
+
|
396
|
+
# }}}1
|
397
|
+
# "ttl" attribute {{{1
|
398
|
+
|
399
|
+
# Specifies TTL at config.
|
400
|
+
def test_specify_ttl
|
401
|
+
driver = create_driver(%[
|
402
|
+
check_ttl 30
|
403
|
+
], 'ddos')
|
404
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
405
|
+
driver.emit({}, time)
|
406
|
+
driver.run
|
407
|
+
result = driver.instance.sent_data
|
408
|
+
expected = {
|
409
|
+
'name' => 'ddos',
|
410
|
+
'output' => '{}',
|
411
|
+
'status' => 3,
|
412
|
+
'type' => 'standard',
|
413
|
+
'handlers' => ['default'],
|
414
|
+
'executed' => time,
|
415
|
+
'fluentd' => {
|
416
|
+
'tag' => 'ddos',
|
417
|
+
'time' => time.to_i,
|
418
|
+
'record' => {},
|
419
|
+
},
|
420
|
+
'ttl' => 30,
|
421
|
+
}
|
422
|
+
assert_equal([
|
423
|
+
['localhost', 3030, expected],
|
424
|
+
], result)
|
425
|
+
end
|
426
|
+
|
427
|
+
# }}}1
|
428
|
+
# "handlers" attribute {{{1
|
429
|
+
|
430
|
+
# Rejects the option if it is malformed.
|
431
|
+
def test_rejects_malformed_handlers
|
432
|
+
assert_raise(JSON::ParserError) {
|
433
|
+
create_driver(%[check_handlers NotJsonString], 'ddos')
|
434
|
+
}
|
435
|
+
end
|
436
|
+
|
437
|
+
# Rejects the option if it is not an array.
|
438
|
+
def test_rejects_nonarray_handlers
|
439
|
+
assert_raise(Fluent::ConfigError) {
|
440
|
+
create_driver(%[check_handlers 30], 'ddos')
|
441
|
+
}
|
442
|
+
end
|
443
|
+
|
444
|
+
# Rejects the option if the array contains non-integer values.
|
445
|
+
def test_rejects_nonstring_element_in_handlers
|
446
|
+
assert_raise(Fluent::ConfigError) {
|
447
|
+
create_driver(%[check_handlers ["default", 32]], 'ddos')
|
448
|
+
}
|
449
|
+
end
|
450
|
+
|
451
|
+
# Specifies handlers at config.
|
452
|
+
def test_specifies_handlers
|
453
|
+
driver = create_driver(%[
|
454
|
+
check_handlers ["default", "graphite"]
|
455
|
+
], 'ddos')
|
456
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
457
|
+
driver.emit({}, time)
|
458
|
+
driver.run
|
459
|
+
result = driver.instance.sent_data
|
460
|
+
expected = {
|
461
|
+
'name' => 'ddos',
|
462
|
+
'output' => '{}',
|
463
|
+
'status' => 3,
|
464
|
+
'type' => 'standard',
|
465
|
+
'handlers' => ['default', 'graphite'],
|
466
|
+
'executed' => time,
|
467
|
+
'fluentd' => {
|
468
|
+
'tag' => 'ddos',
|
469
|
+
'time' => time.to_i,
|
470
|
+
'record' => {},
|
471
|
+
},
|
472
|
+
}
|
473
|
+
assert_equal([
|
474
|
+
['localhost', 3030, expected],
|
475
|
+
], result)
|
476
|
+
end
|
477
|
+
|
478
|
+
# }}}1
|
479
|
+
# "low_flap_threshold" and "high_flap_threshold" attributes {{{1
|
480
|
+
|
481
|
+
# The thresholds must be specified together if one is specified.
|
482
|
+
def test_flapping_thrsholds_must_be_specified_together
|
483
|
+
assert_raise(Fluent::ConfigError) {
|
484
|
+
create_driver(%[
|
485
|
+
check_low_flap_threshold 40
|
486
|
+
], 'ddos')
|
487
|
+
}
|
488
|
+
assert_raise(Fluent::ConfigError) {
|
489
|
+
create_driver(%[
|
490
|
+
check_high_flap_threshold 60
|
491
|
+
], 'ddos')
|
492
|
+
}
|
493
|
+
end
|
494
|
+
|
495
|
+
# The thresholds must be in order.
|
496
|
+
def test_flapping_thresholds_order
|
497
|
+
assert_nothing_raised {
|
498
|
+
create_driver(%[
|
499
|
+
check_low_flap_threshold 0
|
500
|
+
check_high_flap_threshold 100
|
501
|
+
], 'ddos')
|
502
|
+
}
|
503
|
+
assert_raise(Fluent::ConfigError) {
|
504
|
+
create_driver(%[
|
505
|
+
check_low_flap_threshold -1
|
506
|
+
check_high_flap_threshold 100
|
507
|
+
], 'ddos')
|
508
|
+
}
|
509
|
+
assert_raise(Fluent::ConfigError) {
|
510
|
+
create_driver(%[
|
511
|
+
check_low_flap_threshold 0
|
512
|
+
check_high_flap_threshold 101
|
513
|
+
], 'ddos')
|
514
|
+
}
|
515
|
+
assert_nothing_raised {
|
516
|
+
create_driver(%[
|
517
|
+
check_low_flap_threshold 50
|
518
|
+
check_high_flap_threshold 50
|
519
|
+
], 'ddos')
|
520
|
+
}
|
521
|
+
assert_raise(Fluent::ConfigError) {
|
522
|
+
create_driver(%[
|
523
|
+
check_low_flap_threshold 50
|
524
|
+
check_high_flap_threshold 49
|
525
|
+
], 'ddos')
|
526
|
+
}
|
527
|
+
end
|
528
|
+
|
529
|
+
# Specifies flapping thresholds.
|
530
|
+
def test_specify_flapping_thresholds
|
531
|
+
driver = create_driver(%[
|
532
|
+
check_low_flap_threshold 40
|
533
|
+
check_high_flap_threshold 60
|
534
|
+
], 'ddos')
|
535
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
536
|
+
driver.emit({}, time)
|
537
|
+
driver.run
|
538
|
+
result = driver.instance.sent_data
|
539
|
+
expected = {
|
540
|
+
'name' => 'ddos',
|
541
|
+
'output' => '{}',
|
542
|
+
'status' => 3,
|
543
|
+
'type' => 'standard',
|
544
|
+
'handlers' => ['default'],
|
545
|
+
'executed' => time,
|
546
|
+
'fluentd' => {
|
547
|
+
'tag' => 'ddos',
|
548
|
+
'time' => time.to_i,
|
549
|
+
'record' => {},
|
550
|
+
},
|
551
|
+
'low_flap_threshold' => 40,
|
552
|
+
'high_flap_threshold' => 60,
|
553
|
+
}
|
554
|
+
assert_equal([
|
555
|
+
['localhost', 3030, expected],
|
556
|
+
], result)
|
557
|
+
end
|
558
|
+
|
559
|
+
# }}}1
|
560
|
+
# "source" attribute {{{1
|
561
|
+
|
562
|
+
# "source" attribute is determined by check_source_field
|
563
|
+
# and check_source option if specified.
|
564
|
+
def test_determine_source_by_options
|
565
|
+
driver = create_driver(%[
|
566
|
+
check_source_field machine
|
567
|
+
check_source router.example.com
|
568
|
+
], 'ddos')
|
569
|
+
time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
570
|
+
driver.emit({ 'machine' => 'ups.example.com' }, time)
|
571
|
+
driver.emit({}, time)
|
572
|
+
driver.run
|
573
|
+
result = driver.instance.sent_data
|
574
|
+
expected_for_field = {
|
575
|
+
'name' => 'ddos',
|
576
|
+
'output' => '{"machine":"ups.example.com"}',
|
577
|
+
'status' => 3,
|
578
|
+
'type' => 'standard',
|
579
|
+
'handlers' => ['default'],
|
580
|
+
'executed' => time,
|
581
|
+
'fluentd' => {
|
582
|
+
'tag' => 'ddos',
|
583
|
+
'time' => time.to_i,
|
584
|
+
'record' => { 'machine' => 'ups.example.com' },
|
585
|
+
},
|
586
|
+
'source' => 'ups.example.com',
|
587
|
+
}
|
588
|
+
expected_for_option = {
|
589
|
+
'name' => 'ddos',
|
590
|
+
'output' => '{}',
|
591
|
+
'status' => 3,
|
592
|
+
'type' => 'standard',
|
593
|
+
'handlers' => ['default'],
|
594
|
+
'executed' => time,
|
595
|
+
'fluentd' => {
|
596
|
+
'tag' => 'ddos',
|
597
|
+
'time' => time.to_i,
|
598
|
+
'record' => {},
|
599
|
+
},
|
600
|
+
'source' => 'router.example.com',
|
601
|
+
}
|
602
|
+
assert_equal([
|
603
|
+
['localhost', 3030, expected_for_field],
|
604
|
+
['localhost', 3030, expected_for_option],
|
605
|
+
], result)
|
606
|
+
end
|
607
|
+
|
608
|
+
# }}}1
|
609
|
+
# "executed" attribute {{{1
|
610
|
+
|
611
|
+
# Determine "executed" time attribute by the options.
|
612
|
+
def test_determine_executed_by_options
|
613
|
+
driver = create_driver(%[
|
614
|
+
check_executed_field timestamp
|
615
|
+
], 'ddos')
|
616
|
+
data_time = Time.parse('2015-01-03 12:34:56 UTC').to_i
|
617
|
+
field_time = Time.parse('2015-01-03 23:45:12 UTC').to_i
|
618
|
+
driver.emit({ 'timestamp' => field_time.to_i }, data_time)
|
619
|
+
driver.emit({ 'timestamp' => 'not-integer' }, data_time)
|
620
|
+
driver.emit({}, data_time)
|
621
|
+
driver.run
|
622
|
+
result = driver.instance.sent_data
|
623
|
+
expected_for_field = {
|
624
|
+
'name' => 'ddos',
|
625
|
+
'output' => "{\"timestamp\":#{field_time.to_i}}",
|
626
|
+
'status' => 3,
|
627
|
+
'type' => 'standard',
|
628
|
+
'handlers' => ['default'],
|
629
|
+
'executed' => field_time.to_i,
|
630
|
+
'fluentd' => {
|
631
|
+
'tag' => 'ddos',
|
632
|
+
'time' => data_time.to_i,
|
633
|
+
'record' => { 'timestamp' => field_time.to_i },
|
634
|
+
},
|
635
|
+
}
|
636
|
+
expected_for_error_data = {
|
637
|
+
'name' => 'ddos',
|
638
|
+
'output' => '{"timestamp":"not-integer"}',
|
639
|
+
'status' => 3,
|
640
|
+
'type' => 'standard',
|
641
|
+
'handlers' => ['default'],
|
642
|
+
'executed' => data_time.to_i,
|
643
|
+
'fluentd' => {
|
644
|
+
'tag' => 'ddos',
|
645
|
+
'time' => data_time.to_i,
|
646
|
+
'record' => { 'timestamp' => 'not-integer' },
|
647
|
+
},
|
648
|
+
}
|
649
|
+
expected_for_data_time = {
|
650
|
+
'name' => 'ddos',
|
651
|
+
'output' => "{}",
|
652
|
+
'status' => 3,
|
653
|
+
'type' => 'standard',
|
654
|
+
'handlers' => ['default'],
|
655
|
+
'executed' => data_time.to_i,
|
656
|
+
'fluentd' => {
|
657
|
+
'tag' => 'ddos',
|
658
|
+
'time' => data_time.to_i,
|
659
|
+
'record' => {},
|
660
|
+
},
|
661
|
+
}
|
662
|
+
assert_equal([
|
663
|
+
['localhost', 3030, expected_for_field],
|
664
|
+
['localhost', 3030, expected_for_error_data],
|
665
|
+
['localhost', 3030, expected_for_data_time],
|
666
|
+
], result)
|
667
|
+
end
|
668
|
+
|
669
|
+
# }}}1
|
670
|
+
|
671
|
+
end
|