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,39 +16,34 @@
16
16
  # limitations under the License.
17
17
  #
18
18
  module Fluent
19
-
20
-
21
-
22
- class StatusClass
23
- def initialize
24
- @entries = {}
25
- @mutex = Mutex.new
26
- end
27
-
28
- def register(instance, name, &block)
29
- @mutex.synchronize {
30
- (@entries[instance.object_id] ||= {})[name] = block
31
- }
32
- nil
33
- end
34
-
35
- def each(&block)
36
- @mutex.synchronize {
37
- @entries.each {|obj_id,hash|
38
- record = {}
39
- hash.each_pair {|name,block|
40
- record[name] = block.call
19
+ class StatusClass
20
+ def initialize
21
+ @entries = {}
22
+ @mutex = Mutex.new
23
+ end
24
+
25
+ def register(instance, name, &block)
26
+ @mutex.synchronize {
27
+ (@entries[instance.object_id] ||= {})[name] = block
28
+ }
29
+ nil
30
+ end
31
+
32
+ def each(&block)
33
+ @mutex.synchronize {
34
+ @entries.each {|obj_id,hash|
35
+ record = {}
36
+ hash.each_pair {|name,block|
37
+ record[name] = block.call
38
+ }
39
+ block.call(record)
41
40
  }
42
- block.call(record)
43
41
  }
44
- }
42
+ end
45
43
  end
46
- end
47
-
48
- # Don't use this class from plugins.
49
- # The interface may be changed
50
- Status = StatusClass.new
51
-
52
44
 
45
+ # Don't use this class from plugins.
46
+ # The interface may be changed
47
+ Status = StatusClass.new
53
48
  end
54
49
 
@@ -16,364 +16,360 @@
16
16
  # limitations under the License.
17
17
  #
18
18
  module Fluent
19
+ class Supervisor
20
+ class LoggerInitializer
21
+ def initialize(path, level, chuser, chgroup)
22
+ @path = path
23
+ @level = level
24
+ @chuser = chuser
25
+ @chgroup = chgroup
26
+ end
19
27
 
20
-
21
- class Supervisor
22
- class LoggerInitializer
23
- def initialize(path, level, chuser, chgroup)
24
- @path = path
25
- @level = level
26
- @chuser = chuser
27
- @chgroup = chgroup
28
- end
29
-
30
- def init
31
- if @path && @path != "-"
32
- @io = File.open(@path, "a")
33
- if @chuser || @chgroup
34
- chuid = @chuser ? `id -u #{@chuser}`.to_i : nil
35
- chgid = @chgroup ? `id -g #{@chgroup}`.to_i : nil
36
- File.chown(chuid, chgid, @path)
28
+ def init
29
+ if @path && @path != "-"
30
+ @io = File.open(@path, "a")
31
+ if @chuser || @chgroup
32
+ chuid = @chuser ? `id -u #{@chuser}`.to_i : nil
33
+ chgid = @chgroup ? `id -g #{@chgroup}`.to_i : nil
34
+ File.chown(chuid, chgid, @path)
35
+ end
36
+ else
37
+ @io = STDOUT
37
38
  end
38
- else
39
- @io = STDOUT
40
- end
41
39
 
42
- $log = Fluent::Log.new(@io, @level)
40
+ $log = Fluent::Log.new(@io, @level)
43
41
 
44
- $log.enable_color(false) if @path
45
- $log.enable_debug if @level <= Fluent::Log::LEVEL_DEBUG
46
- end
42
+ $log.enable_color(false) if @path
43
+ $log.enable_debug if @level <= Fluent::Log::LEVEL_DEBUG
44
+ end
47
45
 
48
- def stdout?
49
- @io == STDOUT
50
- end
46
+ def stdout?
47
+ @io == STDOUT
48
+ end
51
49
 
52
- def reopen!
53
- if @path && @path != "-"
54
- @io.reopen(@path, "a")
50
+ def reopen!
51
+ if @path && @path != "-"
52
+ @io.reopen(@path, "a")
53
+ end
54
+ self
55
55
  end
56
- self
57
56
  end
58
- end
59
57
 
60
- def initialize(opt)
61
- @config_path = opt[:config_path]
62
- @log_path = opt[:log_path]
63
- @log_level = opt[:log_level]
64
- @daemonize = opt[:daemonize]
65
- @chgroup = opt[:chgroup]
66
- @chuser = opt[:chuser]
67
- @libs = opt[:libs]
68
- @plugin_dirs = opt[:plugin_dirs]
69
- @inline_config = opt[:inline_config]
70
- @suppress_interval = opt[:suppress_interval]
71
- @dry_run = opt[:dry_run]
72
-
73
- @log = LoggerInitializer.new(@log_path, @log_level, @chuser, @chgroup)
74
- @finished = false
75
- @main_pid = nil
76
- end
58
+ def initialize(opt)
59
+ @config_path = opt[:config_path]
60
+ @log_path = opt[:log_path]
61
+ @log_level = opt[:log_level]
62
+ @daemonize = opt[:daemonize]
63
+ @chgroup = opt[:chgroup]
64
+ @chuser = opt[:chuser]
65
+ @libs = opt[:libs]
66
+ @plugin_dirs = opt[:plugin_dirs]
67
+ @inline_config = opt[:inline_config]
68
+ @suppress_interval = opt[:suppress_interval]
69
+ @dry_run = opt[:dry_run]
70
+
71
+ @log = LoggerInitializer.new(@log_path, @log_level, @chuser, @chgroup)
72
+ @finished = false
73
+ @main_pid = nil
74
+ end
77
75
 
78
- def start
79
- require 'fluent/load'
80
- @log.init
81
-
82
- dry_run if @dry_run
83
- start_daemonize if @daemonize
84
- install_supervisor_signal_handlers
85
- until @finished
86
- supervise do
87
- read_config
88
- change_privilege
89
- init_engine
90
- install_main_process_signal_handlers
91
- run_configure
92
- finish_daemonize if @daemonize
93
- run_engine
94
- exit 0
76
+ def start
77
+ require 'fluent/load'
78
+ @log.init
79
+
80
+ dry_run if @dry_run
81
+ start_daemonize if @daemonize
82
+ install_supervisor_signal_handlers
83
+ until @finished
84
+ supervise do
85
+ read_config
86
+ change_privilege
87
+ init_engine
88
+ install_main_process_signal_handlers
89
+ run_configure
90
+ finish_daemonize if @daemonize
91
+ run_engine
92
+ exit 0
93
+ end
94
+ $log.error "fluentd main process died unexpectedly. restarting." unless @finished
95
95
  end
96
- $log.error "fluentd main process died unexpectedly. restarting." unless @finished
97
96
  end
98
- end
99
-
100
- private
101
-
102
- def dry_run
103
- read_config
104
- change_privilege
105
- init_engine
106
- install_main_process_signal_handlers
107
- run_configure
108
- exit 0
109
- rescue => e
110
- $log.error "Dry run failed: #{e}"
111
- exit 1
112
- end
113
97
 
114
- def start_daemonize
115
- @wait_daemonize_pipe_r, @wait_daemonize_pipe_w = IO.pipe
98
+ private
116
99
 
117
- if fork
118
- # console process
119
- @wait_daemonize_pipe_w.close
120
- @wait_daemonize_pipe_w = nil
121
- wait_daemonize
100
+ def dry_run
101
+ read_config
102
+ change_privilege
103
+ init_engine
104
+ install_main_process_signal_handlers
105
+ run_configure
122
106
  exit 0
107
+ rescue => e
108
+ $log.error "Dry run failed: #{e}"
109
+ exit 1
123
110
  end
124
111
 
125
- # daemonize intermediate process
126
- @wait_daemonize_pipe_r.close
127
- @wait_daemonize_pipe_r = nil
112
+ def start_daemonize
113
+ @wait_daemonize_pipe_r, @wait_daemonize_pipe_w = IO.pipe
128
114
 
129
- # in case the child process forked during run_configure
130
- @wait_daemonize_pipe_w.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
115
+ if fork
116
+ # console process
117
+ @wait_daemonize_pipe_w.close
118
+ @wait_daemonize_pipe_w = nil
119
+ wait_daemonize
120
+ exit 0
121
+ end
131
122
 
132
- Process.setsid
133
- exit!(0) if fork
134
- File.umask(0)
123
+ # daemonize intermediate process
124
+ @wait_daemonize_pipe_r.close
125
+ @wait_daemonize_pipe_r = nil
135
126
 
136
- # supervisor process
137
- @supervisor_pid = Process.pid
138
- end
127
+ # in case the child process forked during run_configure
128
+ @wait_daemonize_pipe_w.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
139
129
 
140
- def wait_daemonize
141
- supervisor_pid = @wait_daemonize_pipe_r.read
142
- if supervisor_pid.empty?
143
- # initialization failed
144
- exit! 1
130
+ Process.setsid
131
+ exit!(0) if fork
132
+ File.umask(0)
133
+
134
+ # supervisor process
135
+ @supervisor_pid = Process.pid
145
136
  end
146
137
 
147
- @wait_daemonize_pipe_r.close
148
- @wait_daemonize_pipe_r = nil
138
+ def wait_daemonize
139
+ supervisor_pid = @wait_daemonize_pipe_r.read
140
+ if supervisor_pid.empty?
141
+ # initialization failed
142
+ exit! 1
143
+ end
149
144
 
150
- # write pid file
151
- File.open(@daemonize, "w") {|f|
152
- f.write supervisor_pid
153
- }
154
- end
145
+ @wait_daemonize_pipe_r.close
146
+ @wait_daemonize_pipe_r = nil
155
147
 
156
- def finish_daemonize
157
- if @wait_daemonize_pipe_w
158
- STDIN.reopen("/dev/null")
159
- STDOUT.reopen("/dev/null", "w")
160
- STDERR.reopen("/dev/null", "w")
161
- @wait_daemonize_pipe_w.write @supervisor_pid.to_s
162
- @wait_daemonize_pipe_w.close
163
- @wait_daemonize_pipe_w = nil
148
+ # write pid file
149
+ File.open(@daemonize, "w") {|f|
150
+ f.write supervisor_pid
151
+ }
164
152
  end
165
- end
166
-
167
- def supervise(&block)
168
- start_time = Time.now
169
153
 
170
- $log.info "starting fluentd-#{Fluent::VERSION}"
171
- @main_pid = fork do
172
- main_process(&block)
154
+ def finish_daemonize
155
+ if @wait_daemonize_pipe_w
156
+ STDIN.reopen("/dev/null")
157
+ STDOUT.reopen("/dev/null", "w")
158
+ STDERR.reopen("/dev/null", "w")
159
+ @wait_daemonize_pipe_w.write @supervisor_pid.to_s
160
+ @wait_daemonize_pipe_w.close
161
+ @wait_daemonize_pipe_w = nil
162
+ end
173
163
  end
174
164
 
175
- if @daemonize && @wait_daemonize_pipe_w
176
- STDIN.reopen("/dev/null")
177
- STDOUT.reopen("/dev/null", "w")
178
- STDERR.reopen("/dev/null", "w")
179
- @wait_daemonize_pipe_w.close
180
- @wait_daemonize_pipe_w = nil
181
- end
165
+ def supervise(&block)
166
+ start_time = Time.now
182
167
 
183
- Process.waitpid(@main_pid)
184
- @main_pid = nil
185
- ecode = $?.to_i
168
+ $log.info "starting fluentd-#{Fluent::VERSION}"
169
+ @main_pid = fork do
170
+ main_process(&block)
171
+ end
186
172
 
187
- $log.info "process finished", :code=>ecode
173
+ if @daemonize && @wait_daemonize_pipe_w
174
+ STDIN.reopen("/dev/null")
175
+ STDOUT.reopen("/dev/null", "w")
176
+ STDERR.reopen("/dev/null", "w")
177
+ @wait_daemonize_pipe_w.close
178
+ @wait_daemonize_pipe_w = nil
179
+ end
188
180
 
189
- if !@finished && Time.now - start_time < 1
190
- $log.warn "process died within 1 second. exit."
191
- exit ecode
192
- end
193
- end
181
+ Process.waitpid(@main_pid)
182
+ @main_pid = nil
183
+ ecode = $?.to_i
194
184
 
195
- def main_process(&block)
196
- begin
197
- block.call
198
-
199
- rescue Fluent::ConfigError
200
- $log.error "config error", :file=>@config_path, :error=>$!.to_s
201
- $log.debug_backtrace
202
- unless @log.stdout?
203
- console = Fluent::Log.new(STDOUT, @log_level).enable_debug
204
- console.error "config error", :file=>@config_path, :error=>$!.to_s
205
- console.debug_backtrace
206
- end
185
+ $log.info "process finished", :code=>ecode
207
186
 
208
- rescue
209
- $log.error "unexpected error", :error=>$!.to_s
210
- $log.error_backtrace
211
- unless @log.stdout?
212
- console = Fluent::Log.new(STDOUT, @log_level).enable_debug
213
- console.error "unexpected error", :error=>$!.to_s
214
- console.error_backtrace
187
+ if !@finished && Time.now - start_time < 1
188
+ $log.warn "process died within 1 second. exit."
189
+ exit ecode
215
190
  end
216
191
  end
217
192
 
218
- exit! 1
219
- end
193
+ def main_process(&block)
194
+ begin
195
+ block.call
196
+
197
+ rescue Fluent::ConfigError
198
+ $log.error "config error", :file=>@config_path, :error=>$!.to_s
199
+ $log.debug_backtrace
200
+ unless @log.stdout?
201
+ console = Fluent::Log.new(STDOUT, @log_level).enable_debug
202
+ console.error "config error", :file=>@config_path, :error=>$!.to_s
203
+ console.debug_backtrace
204
+ end
220
205
 
221
- def install_supervisor_signal_handlers
222
- trap :INT do
223
- $log.debug "fluentd supervisor process get SIGINT"
224
- @finished = true
225
- if pid = @main_pid
226
- # kill processes only still exists
227
- unless Process.waitpid(pid, Process::WNOHANG)
228
- begin
229
- Process.kill(:INT, pid)
230
- rescue Errno::ESRCH
231
- # ignore processes already died
232
- end
206
+ rescue
207
+ $log.error "unexpected error", :error=>$!.to_s
208
+ $log.error_backtrace
209
+ unless @log.stdout?
210
+ console = Fluent::Log.new(STDOUT, @log_level).enable_debug
211
+ console.error "unexpected error", :error=>$!.to_s
212
+ console.error_backtrace
233
213
  end
234
214
  end
215
+
216
+ exit! 1
235
217
  end
236
218
 
237
- trap :TERM do
238
- $log.debug "fluentd supervisor process get SIGTERM"
239
- @finished = true
240
- if pid = @main_pid
241
- # kill processes only still exists
242
- unless Process.waitpid(pid, Process::WNOHANG)
243
- begin
244
- Process.kill(:TERM, pid)
245
- rescue Errno::ESRCH
246
- # ignore processes already died
219
+ def install_supervisor_signal_handlers
220
+ trap :INT do
221
+ $log.debug "fluentd supervisor process get SIGINT"
222
+ @finished = true
223
+ if pid = @main_pid
224
+ # kill processes only still exists
225
+ unless Process.waitpid(pid, Process::WNOHANG)
226
+ begin
227
+ Process.kill(:INT, pid)
228
+ rescue Errno::ESRCH
229
+ # ignore processes already died
230
+ end
247
231
  end
248
232
  end
249
233
  end
250
- end
251
234
 
252
- trap :HUP do
253
- $log.debug "fluentd supervisor process get SIGHUP"
254
- $log.info "restarting"
255
- if pid = @main_pid
256
- Process.kill(:TERM, pid)
257
- # don't resuce Erro::ESRSH here (invalid status)
235
+ trap :TERM do
236
+ $log.debug "fluentd supervisor process get SIGTERM"
237
+ @finished = true
238
+ if pid = @main_pid
239
+ # kill processes only still exists
240
+ unless Process.waitpid(pid, Process::WNOHANG)
241
+ begin
242
+ Process.kill(:TERM, pid)
243
+ rescue Errno::ESRCH
244
+ # ignore processes already died
245
+ end
246
+ end
247
+ end
258
248
  end
259
- end
260
249
 
261
- trap :USR1 do
262
- $log.debug "fluentd supervisor process get SIGUSR1"
263
- @log.reopen!
264
- if pid = @main_pid
265
- Process.kill(:USR1, pid)
266
- # don't resuce Erro::ESRSH here (invalid status)
250
+ trap :HUP do
251
+ $log.debug "fluentd supervisor process get SIGHUP"
252
+ $log.info "restarting"
253
+ if pid = @main_pid
254
+ Process.kill(:TERM, pid)
255
+ # don't resuce Erro::ESRSH here (invalid status)
256
+ end
257
+ end
258
+
259
+ trap :USR1 do
260
+ $log.debug "fluentd supervisor process get SIGUSR1"
261
+ @log.reopen!
262
+ if pid = @main_pid
263
+ Process.kill(:USR1, pid)
264
+ # don't resuce Erro::ESRSH here (invalid status)
265
+ end
267
266
  end
268
267
  end
269
- end
270
268
 
271
- def read_config
272
- $log.info "reading config file", :path=>@config_path
273
- @config_fname = File.basename(@config_path)
274
- @config_basedir = File.dirname(@config_path)
275
- @config_data = File.read(@config_path)
276
- if @inline_config == '-'
277
- @config_data << "\n" << STDIN.read
278
- elsif @inline_config
279
- @config_data << "\n" << @inline_config.gsub("\\n","\n")
269
+ def read_config
270
+ $log.info "reading config file", :path=>@config_path
271
+ @config_fname = File.basename(@config_path)
272
+ @config_basedir = File.dirname(@config_path)
273
+ @config_data = File.read(@config_path)
274
+ if @inline_config == '-'
275
+ @config_data << "\n" << STDIN.read
276
+ elsif @inline_config
277
+ @config_data << "\n" << @inline_config.gsub("\\n","\n")
278
+ end
280
279
  end
281
- end
282
280
 
283
- def run_configure
284
- Fluent::Engine.parse_config(@config_data, @config_fname, @config_basedir)
285
- end
281
+ def run_configure
282
+ Fluent::Engine.parse_config(@config_data, @config_fname, @config_basedir)
283
+ end
286
284
 
287
- def change_privilege
288
- if @chgroup
289
- chgid = @chgroup.to_i
290
- if chgid.to_s != @chgroup
291
- chgid = `id -g #{@chgroup}`.to_i
292
- if $?.to_i != 0
293
- exit 1
285
+ def change_privilege
286
+ if @chgroup
287
+ chgid = @chgroup.to_i
288
+ if chgid.to_s != @chgroup
289
+ chgid = `id -g #{@chgroup}`.to_i
290
+ if $?.to_i != 0
291
+ exit 1
292
+ end
294
293
  end
294
+ Process::GID.change_privilege(chgid)
295
295
  end
296
- Process::GID.change_privilege(chgid)
297
- end
298
296
 
299
- if @chuser
300
- chuid = @chuser.to_i
301
- if chuid.to_s != @chuser
302
- chuid = `id -u #{@chuser}`.to_i
297
+ if @chuser
298
+ chuid = @chuser.to_i
299
+ if chuid.to_s != @chuser
300
+ chuid = `id -u #{@chuser}`.to_i
301
+ if $?.to_i != 0
302
+ exit 1
303
+ end
304
+ end
305
+
306
+ user_groups = `id -G #{@chuser}`.split.map(&:to_i)
303
307
  if $?.to_i != 0
304
308
  exit 1
305
309
  end
310
+
311
+ Process.groups = Process.groups | user_groups
312
+ Process::UID.change_privilege(chuid)
306
313
  end
314
+ end
307
315
 
308
- user_groups = `id -G #{@chuser}`.split.map(&:to_i)
309
- if $?.to_i != 0
310
- exit 1
316
+ def init_engine
317
+ require 'fluent/load'
318
+ Fluent::Engine.init
319
+ if @suppress_interval
320
+ Fluent::Engine.suppress_interval(@suppress_interval)
311
321
  end
312
322
 
313
- Process.groups = Process.groups | user_groups
314
- Process::UID.change_privilege(chuid)
315
- end
316
- end
323
+ @libs.each {|lib|
324
+ require lib
325
+ }
317
326
 
318
- def init_engine
319
- require 'fluent/load'
320
- Fluent::Engine.init
321
- if @suppress_interval
322
- Fluent::Engine.suppress_interval(@suppress_interval)
327
+ @plugin_dirs.each {|dir|
328
+ if Dir.exist?(dir)
329
+ dir = File.expand_path(dir)
330
+ Fluent::Engine.load_plugin_dir(dir)
331
+ end
332
+ }
323
333
  end
324
334
 
325
- @libs.each {|lib|
326
- require lib
327
- }
335
+ def install_main_process_signal_handlers
336
+ # Strictly speaking, these signal handling is not thread safe.
337
+ # But enough safe to limit twice call of Fluent::Engine.stop.
328
338
 
329
- @plugin_dirs.each {|dir|
330
- if Dir.exist?(dir)
331
- dir = File.expand_path(dir)
332
- Fluent::Engine.load_plugin_dir(dir)
339
+ trap :INT do
340
+ $log.debug "fluentd main process get SIGINT"
341
+ unless @finished
342
+ @finished = true
343
+ $log.debug "getting start to shutdown main process"
344
+ Fluent::Engine.stop
345
+ end
333
346
  end
334
- }
335
- end
336
347
 
337
- def install_main_process_signal_handlers
338
- # Strictly speaking, these signal handling is not thread safe.
339
- # But enough safe to limit twice call of Fluent::Engine.stop.
340
-
341
- trap :INT do
342
- $log.debug "fluentd main process get SIGINT"
343
- unless @finished
344
- @finished = true
345
- $log.debug "getting start to shutdown main process"
346
- Fluent::Engine.stop
348
+ trap :TERM do
349
+ $log.debug "fluentd main process get SIGTERM"
350
+ unless @finished
351
+ @finished = true
352
+ $log.debug "getting start to shutdown main process"
353
+ Fluent::Engine.stop
354
+ end
347
355
  end
348
- end
349
356
 
350
- trap :TERM do
351
- $log.debug "fluentd main process get SIGTERM"
352
- unless @finished
353
- @finished = true
354
- $log.debug "getting start to shutdown main process"
355
- Fluent::Engine.stop
357
+ trap :HUP do
358
+ # TODO
359
+ $log.debug "fluentd main process get SIGHUP"
356
360
  end
357
- end
358
361
 
359
- trap :HUP do
360
- # TODO
361
- $log.debug "fluentd main process get SIGHUP"
362
+ trap :USR1 do
363
+ $log.debug "fluentd main process get SIGUSR1"
364
+ $log.info "force flushing buffered events"
365
+ @log.reopen!
366
+ Fluent::Engine.flush!
367
+ end
362
368
  end
363
369
 
364
- trap :USR1 do
365
- $log.debug "fluentd main process get SIGUSR1"
366
- $log.info "force flushing buffered events"
367
- @log.reopen!
368
- Fluent::Engine.flush!
370
+ def run_engine
371
+ Fluent::Engine.run
369
372
  end
370
373
  end
371
-
372
- def run_engine
373
- Fluent::Engine.run
374
- end
375
- end
376
-
377
-
378
374
  end
379
375