fluent-plugin-sensu 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+
8
+ script: bundle exec rake test
data/README.md CHANGED
@@ -47,19 +47,19 @@ which contains attributes as follows.
47
47
  Attributes are indicated
48
48
  by [JSONPath](http://goessner.net/articles/JsonPath/) expressions.
49
49
 
50
- * `$.name`
50
+ * [`$.name`](#name-attribute)
51
51
  * The check name to identify the check.
52
- * `$.output`
52
+ * [`$.output`](#output-attribute)
53
53
  * An arbitrary string to describe the check result.
54
54
  * This attribute is often used to contain metric values.
55
- * `$.status`
55
+ * [`$.status`](#status-attribute)
56
56
  * The severity of the check result.
57
57
  * 0 (OK), 1 (WARNING), 2 (CRITICAL) or 3 (UNKNOWN or CUSTOM).
58
58
 
59
59
  The check result can also contain other attributes.
60
60
  This plugin supports the attributes below.
61
61
 
62
- * `$.type`
62
+ * [`$.type`](#type-attribute)
63
63
  * Either "standard" or "metric".
64
64
  * If the attribute is set to "standard", the sensu-server creates an event
65
65
  only when the status is not OK or when the status is changed to OK.
@@ -67,27 +67,28 @@ This plugin supports the attributes below.
67
67
  even if the status is OK.
68
68
  It is useful when Sensu sends check results to metrics collectors
69
69
  such as [Graphite](http://graphite.wikidot.com/).
70
- * `$.ttl`
70
+ * [`$.ttl`](#ttl-attribute)
71
71
  * The time to live (TTL) in seconds,
72
72
  until the check result is considered stale.
73
73
  If TTL expires, sensu-server creates an event.
74
74
  * This attribute is useful when you want to be notified
75
75
  when logs are not output for a certain period.
76
76
  * Same as `freshness_threshold` in Nagios.
77
- * `$.handlers`
77
+ * [`$.handlers`](#handlers-attributes)
78
78
  * The names of handlers which process events created for the check.
79
- * `$.low_flap_threshold` and `$.high_flap_threshold`
79
+ * [`$.low_flap_threshold` and `$.high_flap_threshold`](
80
+ #low_flap_threshold-and-high_flap_threshold-attributes)
80
81
  * Threshold percentages to determine the status is considered "flapping,"
81
82
  or the state is changed too frequently.
82
83
  * Same as the options in Nagios.
83
84
  See the description of [Flap Detection](
84
85
  https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/flapping.html)
85
86
  in Nagios.
86
- * `$.source`
87
+ * [`$.source`](#source-attribute)
87
88
  * The source of the check, such as servers or network switches.
88
89
  * If this attribute is not specified,
89
90
  the host of sensu-client is considered as the source.
90
- * `$.executed`
91
+ * [`$.executed`](#executed-attribute)
91
92
  * The timestamp on which the check is executed.
92
93
  * Note that there is also another timestamp attribute named `issued`,
93
94
  which is automatically measured by sensu-client process.
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = "fluent-plugin-sensu"
9
- spec.version = '0.0.1'
9
+ spec.version = '0.0.2'
10
10
  spec.authors = ["MIYAKAWA Taku"]
11
11
  spec.email = ["miyakawa.taku@gmail.com"]
12
12
  spec.description = 'Fluentd output plugin to send checks' +
@@ -1,5 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # vim: et sw=2 sts=2
3
2
 
4
3
  module Fluent
5
4
 
@@ -88,6 +87,10 @@ module Fluent
88
87
  # Options for "executed" attribute.
89
88
  config_param :check_executed_field, :string, :default => nil
90
89
 
90
+ # Buffering option.
91
+ # Set to 1 second by default so that checks are not delayed so much.
92
+ config_param :flush_interval, :time, :default => 1
93
+
91
94
  # Load modules.
92
95
  private
93
96
  def initialize
@@ -116,11 +119,16 @@ module Fluent
116
119
  # Reject invalid check_low_flap_threshold and check_high_flap_threshold
117
120
  private
118
121
  def reject_invalid_flapping_thresholds
119
- if @check_low_flap_threshold.nil? ^ @check_high_flap_threshold.nil?
122
+ # Thresholds must be specified together or not specified at all.
123
+ only_one_threshold_is_specified =
124
+ @check_low_flap_threshold.nil? ^ @check_high_flap_threshold.nil?
125
+ if only_one_threshold_is_specified
120
126
  raise ConfigError,
121
127
  "'check_low_flap_threshold' and 'check_high_flap_threshold'" +
122
- " specified togher, or not specified at all."
128
+ " must be specified togher, or not specified at all."
123
129
  end
130
+
131
+ # Check 0 <= check_low_flap_threshold <= check_high_flap_threshold <= 100
124
132
  if @check_low_flap_threshold
125
133
  in_order = 0 <= @check_low_flap_threshold &&
126
134
  @check_low_flap_threshold <= @check_high_flap_threshold &&
@@ -139,40 +147,46 @@ module Fluent
139
147
  [tag, time, record].to_msgpack
140
148
  end
141
149
 
142
- # Send a check
150
+ # Send a check result for each data.
143
151
  public
144
152
  def write(chunk)
145
- results = []
146
153
  chunk.msgpack_each { |(tag, time, record)|
147
- payload = {
148
- 'name' => determine_check_name(tag, record),
149
- 'output' => determine_output(tag, record),
150
- 'status' => determine_status(tag, record),
151
- 'type' => @check_type,
152
- 'handlers' => @check_handlers,
153
- 'executed' => determine_executed_time(tag, time, record),
154
- 'fluentd' => {
155
- 'tag' => tag,
156
- 'time' => time.to_i,
157
- 'record' => record,
158
- },
159
- }
160
- add_attribute_if_present(
161
- payload, 'ttl', @check_ttl)
162
- add_attribute_if_present(
163
- payload, 'low_flap_threshold', @check_low_flap_threshold)
164
- add_attribute_if_present(
165
- payload, 'high_flap_threshold', @check_high_flap_threshold)
166
- add_attribute_if_present(
167
- payload, 'source', determine_source(tag, record))
154
+ payload = make_check_result(tag, time, record)
168
155
  send_check(@server, @port, payload)
169
156
  }
170
157
  end
171
158
 
172
- # Send a check to sensu-client.
159
+ # Make a check result for the Fluentd data.
160
+ private
161
+ def make_check_result(tag, time, record)
162
+ check_result = {
163
+ 'name' => determine_check_name(tag, record),
164
+ 'output' => determine_output(tag, record),
165
+ 'status' => determine_status(tag, record),
166
+ 'type' => @check_type,
167
+ 'handlers' => @check_handlers,
168
+ 'executed' => determine_executed_time(tag, time, record),
169
+ 'fluentd' => {
170
+ 'tag' => tag,
171
+ 'time' => time.to_i,
172
+ 'record' => record,
173
+ },
174
+ }
175
+ add_attribute_if_present(
176
+ check_result, 'ttl', @check_ttl)
177
+ add_attribute_if_present(
178
+ check_result, 'low_flap_threshold', @check_low_flap_threshold)
179
+ add_attribute_if_present(
180
+ check_result, 'high_flap_threshold', @check_high_flap_threshold)
181
+ add_attribute_if_present(
182
+ check_result, 'source', determine_source(tag, record))
183
+ return check_result
184
+ end
185
+
186
+ # Send a check result to sensu-client.
173
187
  private
174
- def send_check(server, port, payload)
175
- json = payload.to_json
188
+ def send_check(server, port, check_result)
189
+ json = check_result.to_json
176
190
  sensu_client = TCPSocket.open(@server, @port)
177
191
  begin
178
192
  sensu_client.puts(json)
@@ -181,10 +195,10 @@ module Fluent
181
195
  end
182
196
  end
183
197
 
184
- # Adds an attribute to the payload if present.
198
+ # Adds an attribute to the check result if present.
185
199
  private
186
- def add_attribute_if_present(payload, name, value)
187
- payload[name] = value if value
200
+ def add_attribute_if_present(check_result, name, value)
201
+ check_result[name] = value if value
188
202
  end
189
203
 
190
204
  # Determines "name" attribute of a check.
@@ -202,14 +216,17 @@ module Fluent
202
216
  :value => check_name)
203
217
  # Fall through
204
218
  end
219
+
205
220
  # check_name option
206
221
  return @check_name if @check_name
222
+
207
223
  # Tag
208
224
  return tag if tag =~ CHECK_NAME_PATTERN
209
- # Default value
210
225
  log.warn('Invalid check name in the tag.' +
211
226
  'Fallback to the constant "fluent-plugin-sensu".',
212
227
  :tag => tag)
228
+
229
+ # Default value
213
230
  return 'fluent-plugin-sensu'
214
231
  end
215
232
 
@@ -224,8 +241,10 @@ module Fluent
224
241
  :tag => tag, :check_output_field => @check_output_field)
225
242
  # Fall through
226
243
  end
244
+
227
245
  # Returns the option value
228
246
  return @check_output if @check_output
247
+
229
248
  # Default to JSON notation of the record
230
249
  return record.to_json
231
250
  end
@@ -244,6 +263,7 @@ module Fluent
244
263
  :tag => tag, :check_status_field => @check_status_field,
245
264
  :value => status_field_val)
246
265
  end
266
+
247
267
  # Returns the default
248
268
  return @check_status
249
269
  end
@@ -262,27 +282,35 @@ module Fluent
262
282
  # Determines "source" attribute of a check.
263
283
  private
264
284
  def determine_source(tag, record)
285
+ # Read the field
265
286
  if @check_source_field
266
287
  source = record[@check_source_field]
267
288
  return source if source
268
289
  log.warn('the field for "source" attribute is absent',
269
290
  :tag => tag, :check_source_field => @check_source_field)
270
291
  end
292
+
293
+ # Default to "check_source" option or N/A
271
294
  return @check_source
272
295
  end
273
296
 
274
297
  # Determines "executed" attribute of a check.
275
298
  private
276
299
  def determine_executed_time(tag, time, record)
300
+ # Read the field
277
301
  if @check_executed_field
278
302
  executed = record[@check_executed_field]
279
303
  return executed if executed.is_a?(Integer)
280
304
  log.warn('the field for "executed" attribute is absent',
281
305
  :tag => tag, :check_executed_field => @check_executed_field)
282
306
  end
307
+
308
+ # Default to the time of Fluentd data
283
309
  return time
284
310
  end
285
311
 
286
312
  end
287
313
 
288
314
  end
315
+
316
+ # vim: et sw=2 sts=2
@@ -30,6 +30,13 @@ class SensuOutputTest < Test::Unit::TestCase
30
30
  # }}}1
31
31
  # Default value {{{1
32
32
 
33
+ # flush_interval is set to 1 second so that checks are not delayed so much
34
+ def test_default_buffer_setting
35
+ driver = create_driver('', 'ddos')
36
+ flush_interval = driver.instance.instance_eval{ @flush_interval }
37
+ assert_equal 1, flush_interval
38
+ end
39
+
33
40
  # Send default values for empty data and empty setting
34
41
  def test_write_empty_data_with_empty_setting
35
42
  driver = create_driver('', 'ddos')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-sensu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-08-02 00:00:00.000000000 Z
12
+ date: 2015-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -99,6 +99,7 @@ extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
101
  - .gitignore
102
+ - .travis.yml
102
103
  - Gemfile
103
104
  - LICENSE.txt
104
105
  - README.md
@@ -122,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
123
  version: '0'
123
124
  segments:
124
125
  - 0
125
- hash: -3040551287980706948
126
+ hash: 494592010274468206
126
127
  required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  none: false
128
129
  requirements:
@@ -131,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
132
  version: '0'
132
133
  segments:
133
134
  - 0
134
- hash: -3040551287980706948
135
+ hash: 494592010274468206
135
136
  requirements: []
136
137
  rubyforge_project:
137
138
  rubygems_version: 1.8.23