Class: Fluent::TailMultilineInput_EX

Inherits:
TailInput
  • Object
show all
Includes:
Mixin::ConfigPlaceholders
Defined in:
lib/fluent/plugin/in_tail_multiline_ex.rb

Defined Under Namespace

Classes: MultilineTextParser_EX, TailExWatcher_EX

Constant Summary collapse

FORMAT_MAX_NUMS =
20

Instance Method Summary collapse

Constructor Details

#initializeTailMultilineInput_EX

Returns a new instance of TailMultilineInput_EX.



64
65
66
67
68
69
70
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 64

def initialize
  super
  @locker = Monitor.new
  @logbuf = nil
  @logbuf_flusher = CallLater_EX::new
  @ready = false
end

Instance Method Details

#configure(conf) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 72

def configure(conf)
  if conf['format'].nil?
    invalids = conf.keys.select{|k| k =~ /^format(\d+)$/ and not (1..FORMAT_MAX_NUMS).include?($1.to_i)}
    if invalids.size > 0
      raise ConfigError, "invalid number formats (valid format number:1-#{FORMAT_MAX_NUMS}):" + invalids.join(",")
    end
    format_index_list = conf.keys.select{|s| s =~ /^format\d+$/}.map{|v| (/^format(\d+)$/.match(v))[1].to_i}
    if (1..format_index_list.max).map{|i| conf["format#{i}"]}.include?(nil)
      raise Fluent::ConfigError, "jump of format index found"
    end
    formats = (1..FORMAT_MAX_NUMS).map {|i|
      conf["format#{i}"]
    }.delete_if {|format|
      format.nil?
    }.map {|format|
      format[1..-2]
    }.join
    conf['format'] = '/' + formats + '/'
  end
  super
  if @tag.index('*')
    @tag_prefix, @tag_suffix = @tag.split('*')
    @tag_suffix ||= ''
  else
    @tag_prefix = nil
    @tag_suffix = nil
  end
  @watchers = {}
  @refresh_trigger = TailWatcher::TimerWatcher.new(@refresh_interval, true, &method(:refresh_watchers))
  if read_newfile_from_head and @pf
    # Tread new file as rotated file
    # Use temp file inode number as previos logfile
    @paths.map {|path|
      pe = @pf[path]
      if pe.read_inode == 0
        require 'tempfile'
        tmpfile = Tempfile.new('gettempinode')
        pe.update(File.stat(tmpfile).ino, 0)
        tmpfile.unlink
      end
    }
  end
end

#configure_parser(conf) ⇒ Object



182
183
184
185
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 182

def configure_parser(conf)
  @parser = MultilineTextParser_EX.new
  @parser.configure(conf)
end

#expand_pathsObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 116

def expand_paths
  date = Time.now
  paths = []
  for path in @paths
    if @expand_date
      path = date.strftime(path)
    end
    paths += Dir.glob(path)
  end
  paths
end

#flush_logbufObject



228
229
230
231
232
233
234
235
236
237
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 228

def flush_logbuf
  time, record = nil,nil
  @locker.synchronize do
    time, record = parse_logbuf(@logbuf)
    @logbuf = nil
  end
  if time && record
    Engine.emit(@tag, time, record)
  end
end

#parse_logbuf(buf) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 239

def parse_logbuf(buf)
  return nil,nil unless buf
  buf.chomp!
  begin
    time, record = @parser.parse(buf)
  rescue
    $log.warn buf.dump, :error=>$!.to_s
    $log.debug_backtrace
  end
  return nil,nil unless time && record
  record[@rawdata_key] = buf if @rawdata_key
  return time, record
end

#receive_lines(lines, tag) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 187

def receive_lines(lines,tag)
  if @tag_prefix || @tag_suffix
    @tag = @tag_prefix + tag + @tag_suffix
  end
  @logbuf_flusher.cancel()
  es = MultiEventStream.new
  @locker.synchronize do
    lines.each {|line|
        if @parser.match_firstline(line)
          time, record = parse_logbuf(@logbuf)
          if time && record
            es.add(time, record)
          end
          @logbuf = line
        else
          @logbuf += line if(@logbuf)
        end
    }
  end
  unless es.empty?
    begin
      Engine.emit_stream(@tag, es)
    rescue
      # ignore errors. Engine shows logs and backtraces.
    end
  end
  @logbuf_flusher.call_later(@auto_flush_sec) do
    flush_logbuf()
  end
end

#refresh_watchersObject



128
129
130
131
132
133
134
135
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 128

def refresh_watchers
  paths = expand_paths
  missing = @watchers.keys - paths
  added = paths - @watchers.keys

  stop_watch(missing) unless missing.empty?
  start_watch(added) unless added.empty?
end

#runObject



175
176
177
178
179
180
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 175

def run
  # don't run unless ready to avoid coolio error
  if @ready
    super
  end
end

#shutdownObject



218
219
220
221
222
223
224
225
226
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 218

def shutdown
  @refresh_trigger.detach
  stop_watch(@watchers.keys, true)
  @loop.stop
  @thread.join
  @pf_file.close if @pf_file
  flush_logbuf()
  @logbuf_flusher.shutdown()
end

#startObject



164
165
166
167
168
169
170
171
172
173
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 164

def start
  paths, @paths = @paths, []
  super
  @thread.join
  @paths = paths
  refresh_watchers
  @refresh_trigger.attach(@loop)
  @ready = true
  @thread = Thread.new(&method(:run))
end

#start_watch(paths) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 137

def start_watch(paths)
  paths.each do |path|
    if @pf
      pe = @pf[path]
      if @read_all && pe.read_inode == 0
        inode = File::Stat.new(path).ino
        pe.update(inode, 0)
      end
    else
      pe = nil
    end

    watcher = TailExWatcher_EX.new(path, @rotate_wait, pe, &method(:receive_lines))
    watcher.attach(@loop)
    @watchers[path] = watcher
  end
end

#stop_watch(paths, immediate = false) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/fluent/plugin/in_tail_multiline_ex.rb', line 155

def stop_watch(paths, immediate=false)
  paths.each do |path|
    watcher = @watchers.delete(path)
    if watcher
      watcher.close(immediate ? nil : @loop)
    end
  end
end