fluentd 0.10.36 → 0.10.37

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fluentd might be problematic. Click here for more details.

@@ -16,282 +16,278 @@
16
16
  # limitations under the License.
17
17
  #
18
18
  module Fluent
19
+ class TextParser
20
+ class RegexpParser
21
+ include Configurable
19
22
 
23
+ config_param :time_format, :string, :default => nil
20
24
 
21
- class TextParser
22
- class RegexpParser
23
- include Configurable
24
-
25
- config_param :time_format, :string, :default => nil
26
-
27
- def initialize(regexp, conf={})
28
- super()
29
- @regexp = regexp
30
- unless conf.empty?
31
- configure(conf)
32
- end
33
- end
34
-
35
- def call(text)
36
- m = @regexp.match(text)
37
- unless m
38
- $log.warn "pattern not match: #{text.inspect}"
39
- return nil, nil
25
+ def initialize(regexp, conf={})
26
+ super()
27
+ @regexp = regexp
28
+ unless conf.empty?
29
+ configure(conf)
30
+ end
40
31
  end
41
32
 
42
- time = nil
43
- record = {}
33
+ def call(text)
34
+ m = @regexp.match(text)
35
+ unless m
36
+ $log.warn "pattern not match: #{text.inspect}"
37
+ return nil, nil
38
+ end
44
39
 
45
- m.names.each {|name|
46
- if value = m[name]
47
- case name
48
- when "time"
49
- if @time_format
50
- time = Time.strptime(value, @time_format).to_i
40
+ time = nil
41
+ record = {}
42
+
43
+ m.names.each {|name|
44
+ if value = m[name]
45
+ case name
46
+ when "time"
47
+ if @time_format
48
+ time = Time.strptime(value, @time_format).to_i
49
+ else
50
+ time = Time.parse(value).to_i
51
+ end
51
52
  else
52
- time = Time.parse(value).to_i
53
+ record[name] = value
53
54
  end
54
- else
55
- record[name] = value
56
55
  end
57
- end
58
- }
56
+ }
59
57
 
60
- time ||= Engine.now
58
+ time ||= Engine.now
61
59
 
62
- return time, record
60
+ return time, record
61
+ end
63
62
  end
64
- end
65
63
 
66
- class JSONParser
67
- include Configurable
64
+ class JSONParser
65
+ include Configurable
68
66
 
69
- config_param :time_key, :string, :default => 'time'
70
- config_param :time_format, :string, :default => nil
67
+ config_param :time_key, :string, :default => 'time'
68
+ config_param :time_format, :string, :default => nil
71
69
 
72
- def call(text)
73
- record = Yajl.load(text)
70
+ def call(text)
71
+ record = Yajl.load(text)
74
72
 
75
- if value = record.delete(@time_key)
76
- if @time_format
77
- time = Time.strptime(value, @time_format).to_i
73
+ if value = record.delete(@time_key)
74
+ if @time_format
75
+ time = Time.strptime(value, @time_format).to_i
76
+ else
77
+ time = value.to_i
78
+ end
78
79
  else
79
- time = value.to_i
80
+ time = Engine.now
80
81
  end
81
- else
82
- time = Engine.now
83
- end
84
82
 
85
- return time, record
86
- rescue Yajl::ParseError
87
- $log.warn "pattern not match: #{text.inspect}: #{$!}"
88
- return nil, nil
83
+ return time, record
84
+ rescue Yajl::ParseError
85
+ $log.warn "pattern not match: #{text.inspect}: #{$!}"
86
+ return nil, nil
87
+ end
89
88
  end
90
- end
91
89
 
92
- class ValuesParser
93
- include Configurable
90
+ class ValuesParser
91
+ include Configurable
94
92
 
95
- config_param :keys, :string
96
- config_param :time_key, :string, :default => nil
97
- config_param :time_format, :string, :default => nil
93
+ config_param :keys, :string
94
+ config_param :time_key, :string, :default => nil
95
+ config_param :time_format, :string, :default => nil
98
96
 
99
- def configure(conf)
100
- super
97
+ def configure(conf)
98
+ super
101
99
 
102
- @keys = @keys.split(",")
100
+ @keys = @keys.split(",")
103
101
 
104
- if @time_key && !@keys.include?(@time_key)
105
- raise ConfigError, "time_key (#{@time_key.inspect}) is not included in keys (#{@keys.inspect})"
106
- end
102
+ if @time_key && !@keys.include?(@time_key)
103
+ raise ConfigError, "time_key (#{@time_key.inspect}) is not included in keys (#{@keys.inspect})"
104
+ end
107
105
 
108
- if @time_format && !@time_key
109
- raise ConfigError, "time_format parameter is ignored because time_key parameter is not set. at #{conf.inspect}"
106
+ if @time_format && !@time_key
107
+ raise ConfigError, "time_format parameter is ignored because time_key parameter is not set. at #{conf.inspect}"
108
+ end
110
109
  end
111
- end
112
110
 
113
- def values_map(values)
114
- record = Hash[keys.zip(values)]
111
+ def values_map(values)
112
+ record = Hash[keys.zip(values)]
115
113
 
116
- if @time_key
117
- value = record.delete(@time_key)
118
- if @time_format
119
- time = Time.strptime(value, @time_format).to_i
114
+ if @time_key
115
+ value = record.delete(@time_key)
116
+ if @time_format
117
+ time = Time.strptime(value, @time_format).to_i
118
+ else
119
+ time = Time.parse(value).to_i
120
+ end
120
121
  else
121
- time = Time.parse(value).to_i
122
+ time = Engine.now
122
123
  end
123
- else
124
- time = Engine.now
125
- end
126
124
 
127
- return time, record
125
+ return time, record
126
+ end
128
127
  end
129
- end
130
128
 
131
- class TSVParser < ValuesParser
132
- config_param :delimiter, :string, :default => "\t"
129
+ class TSVParser < ValuesParser
130
+ config_param :delimiter, :string, :default => "\t"
133
131
 
134
- def call(text)
135
- return values_map(text.split(@delimiter))
132
+ def call(text)
133
+ return values_map(text.split(@delimiter))
134
+ end
136
135
  end
137
- end
138
136
 
139
- class LabeledTSVParser < ValuesParser
140
- config_param :delimiter, :string, :default => "\t"
141
- config_param :label_delimiter, :string, :default => ":"
142
- config_param :time_key, :string, :default => "time"
137
+ class LabeledTSVParser < ValuesParser
138
+ config_param :delimiter, :string, :default => "\t"
139
+ config_param :label_delimiter, :string, :default => ":"
140
+ config_param :time_key, :string, :default => "time"
143
141
 
144
- def configure(conf)
145
- conf['keys'] = conf['time_key'] || 'time'
146
- super(conf)
147
- end
142
+ def configure(conf)
143
+ conf['keys'] = conf['time_key'] || 'time'
144
+ super(conf)
145
+ end
148
146
 
149
- def call(text)
150
- @keys = []
151
- values = []
147
+ def call(text)
148
+ @keys = []
149
+ values = []
152
150
 
153
- text.split(delimiter).each do |pair|
154
- key, value = pair.split(label_delimiter, 2)
155
- @keys.push(key)
156
- values.push(value)
157
- end
151
+ text.split(delimiter).each do |pair|
152
+ key, value = pair.split(label_delimiter, 2)
153
+ @keys.push(key)
154
+ values.push(value)
155
+ end
158
156
 
159
- return values_map(values)
157
+ return values_map(values)
158
+ end
160
159
  end
161
- end
162
160
 
163
- class CSVParser < ValuesParser
164
- def initialize
165
- super
166
- require 'csv'
167
- end
161
+ class CSVParser < ValuesParser
162
+ def initialize
163
+ super
164
+ require 'csv'
165
+ end
168
166
 
169
- def call(text)
170
- return values_map(CSV.parse_line(text))
167
+ def call(text)
168
+ return values_map(CSV.parse_line(text))
169
+ end
171
170
  end
172
- end
173
171
 
174
- class ApacheParser
175
- include Configurable
172
+ class ApacheParser
173
+ include Configurable
176
174
 
177
- REGEXP = /^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/
175
+ REGEXP = /^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/
178
176
 
179
- def call(text)
180
- m = REGEXP.match(text)
181
- unless m
182
- $log.warn "pattern not match: #{text.inspect}"
183
- return nil, nil
184
- end
177
+ def call(text)
178
+ m = REGEXP.match(text)
179
+ unless m
180
+ $log.warn "pattern not match: #{text.inspect}"
181
+ return nil, nil
182
+ end
185
183
 
186
- host = m['host']
187
- host = (host == '-') ? nil : host
184
+ host = m['host']
185
+ host = (host == '-') ? nil : host
188
186
 
189
- user = m['user']
190
- user = (user == '-') ? nil : user
187
+ user = m['user']
188
+ user = (user == '-') ? nil : user
191
189
 
192
- time = m['time']
193
- time = Time.strptime(time, "%d/%b/%Y:%H:%M:%S %z").to_i
190
+ time = m['time']
191
+ time = Time.strptime(time, "%d/%b/%Y:%H:%M:%S %z").to_i
194
192
 
195
- method = m['method']
196
- path = m['path']
193
+ method = m['method']
194
+ path = m['path']
197
195
 
198
- code = m['code'].to_i
199
- code = nil if code == 0
196
+ code = m['code'].to_i
197
+ code = nil if code == 0
200
198
 
201
- size = m['size']
202
- size = (size == '-') ? nil : size.to_i
199
+ size = m['size']
200
+ size = (size == '-') ? nil : size.to_i
203
201
 
204
- referer = m['referer']
205
- referer = (referer == '-') ? nil : referer
202
+ referer = m['referer']
203
+ referer = (referer == '-') ? nil : referer
206
204
 
207
- agent = m['agent']
208
- agent = (agent == '-') ? nil : agent
205
+ agent = m['agent']
206
+ agent = (agent == '-') ? nil : agent
209
207
 
210
- record = {
211
- "host" => host,
212
- "user" => user,
213
- "method" => method,
214
- "path" => path,
215
- "code" => code,
216
- "size" => size,
217
- "referer" => referer,
218
- "agent" => agent,
219
- }
208
+ record = {
209
+ "host" => host,
210
+ "user" => user,
211
+ "method" => method,
212
+ "path" => path,
213
+ "code" => code,
214
+ "size" => size,
215
+ "referer" => referer,
216
+ "agent" => agent,
217
+ }
220
218
 
221
- return time, record
219
+ return time, record
220
+ end
222
221
  end
223
- end
224
222
 
225
- TEMPLATE_FACTORIES = {
226
- 'apache' => Proc.new { RegexpParser.new(/^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/, {'time_format'=>"%d/%b/%Y:%H:%M:%S %z"}) },
227
- 'apache2' => Proc.new { ApacheParser.new },
228
- 'syslog' => Proc.new { RegexpParser.new(/^(?<time>[^ ]*\s*[^ ]* [^ ]*) (?<host>[^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?[^\:]*\: *(?<message>.*)$/, {'time_format'=>"%b %d %H:%M:%S"}) },
229
- 'json' => Proc.new { JSONParser.new },
230
- 'tsv' => Proc.new { TSVParser.new },
231
- 'ltsv' => Proc.new { LabeledTSVParser.new },
232
- 'csv' => Proc.new { CSVParser.new },
233
- 'nginx' => Proc.new { RegexpParser.new(/^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/, {'time_format'=>"%d/%b/%Y:%H:%M:%S %z"}) },
234
- }
235
-
236
- def self.register_template(name, regexp_or_proc, time_format=nil)
237
- if regexp_or_proc.is_a?(Regexp)
238
- regexp = regexp_or_proc
239
- factory = Proc.new { RegexpParser.new(regexp, {'time_format'=>time_format}) }
240
- else
241
- factory = regexp_or_proc
242
- end
223
+ TEMPLATE_FACTORIES = {
224
+ 'apache' => Proc.new { RegexpParser.new(/^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/, {'time_format'=>"%d/%b/%Y:%H:%M:%S %z"}) },
225
+ 'apache2' => Proc.new { ApacheParser.new },
226
+ 'syslog' => Proc.new { RegexpParser.new(/^(?<time>[^ ]*\s*[^ ]* [^ ]*) (?<host>[^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?[^\:]*\: *(?<message>.*)$/, {'time_format'=>"%b %d %H:%M:%S"}) },
227
+ 'json' => Proc.new { JSONParser.new },
228
+ 'tsv' => Proc.new { TSVParser.new },
229
+ 'ltsv' => Proc.new { LabeledTSVParser.new },
230
+ 'csv' => Proc.new { CSVParser.new },
231
+ 'nginx' => Proc.new { RegexpParser.new(/^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/, {'time_format'=>"%d/%b/%Y:%H:%M:%S %z"}) },
232
+ }
233
+
234
+ def self.register_template(name, regexp_or_proc, time_format=nil)
235
+ if regexp_or_proc.is_a?(Regexp)
236
+ regexp = regexp_or_proc
237
+ factory = Proc.new { RegexpParser.new(regexp, {'time_format'=>time_format}) }
238
+ else
239
+ factory = regexp_or_proc
240
+ end
243
241
 
244
- TEMPLATE_FACTORIES[name] = factory
245
- end
242
+ TEMPLATE_FACTORIES[name] = factory
243
+ end
246
244
 
247
- def initialize
248
- @parser = nil
249
- end
245
+ def initialize
246
+ @parser = nil
247
+ end
250
248
 
251
- def configure(conf, required=true)
252
- format = conf['format']
249
+ def configure(conf, required=true)
250
+ format = conf['format']
253
251
 
254
- if format == nil
255
- if required
256
- raise ConfigError, "'format' parameter is required"
257
- else
258
- return nil
252
+ if format == nil
253
+ if required
254
+ raise ConfigError, "'format' parameter is required"
255
+ else
256
+ return nil
257
+ end
259
258
  end
260
- end
261
259
 
262
- if format[0] == ?/ && format[format.length-1] == ?/
263
- # regexp
264
- begin
265
- regexp = Regexp.new(format[1..-2])
266
- if regexp.named_captures.empty?
267
- raise "No named captures"
260
+ if format[0] == ?/ && format[format.length-1] == ?/
261
+ # regexp
262
+ begin
263
+ regexp = Regexp.new(format[1..-2])
264
+ if regexp.named_captures.empty?
265
+ raise "No named captures"
266
+ end
267
+ rescue
268
+ raise ConfigError, "Invalid regexp '#{format[1..-2]}': #{$!}"
268
269
  end
269
- rescue
270
- raise ConfigError, "Invalid regexp '#{format[1..-2]}': #{$!}"
271
- end
272
270
 
273
- @parser = RegexpParser.new(regexp)
271
+ @parser = RegexpParser.new(regexp)
272
+
273
+ else
274
+ # built-in template
275
+ factory = TEMPLATE_FACTORIES[format]
276
+ unless factory
277
+ raise ConfigError, "Unknown format template '#{format}'"
278
+ end
279
+ @parser = factory.call
280
+ end
274
281
 
275
- else
276
- # built-in template
277
- factory = TEMPLATE_FACTORIES[format]
278
- unless factory
279
- raise ConfigError, "Unknown format template '#{format}'"
282
+ if @parser.respond_to?(:configure)
283
+ @parser.configure(conf)
280
284
  end
281
- @parser = factory.call
282
- end
283
285
 
284
- if @parser.respond_to?(:configure)
285
- @parser.configure(conf)
286
+ return true
286
287
  end
287
288
 
288
- return true
289
- end
290
-
291
- def parse(text)
292
- return @parser.call(text)
289
+ def parse(text)
290
+ return @parser.call(text)
291
+ end
293
292
  end
294
293
  end
295
-
296
-
297
- end