Class: Fluent::Plugin::DstatInput::DstatCSVWatcher

Inherits:
Cool.io::StatWatcher
  • Object
show all
Defined in:
lib/fluent/plugin/in_dstat.rb

Constant Summary collapse

INTERVAL =
0.500

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, &receive_lines) ⇒ DstatCSVWatcher

Returns a new instance of DstatCSVWatcher.



152
153
154
155
156
157
158
# File 'lib/fluent/plugin/in_dstat.rb', line 152

def initialize(path, &receive_lines)
  super path, INTERVAL
  @path = path
  @io = File.open(path, File::NONBLOCK | File::TRUNC)
  @receive_lines = receive_lines
  @partial = ""
end

Instance Attribute Details

#curObject

Returns the value of attribute cur.



150
151
152
# File 'lib/fluent/plugin/in_dstat.rb', line 150

def cur
  @cur
end

#previousObject

Returns the value of attribute previous.



150
151
152
# File 'lib/fluent/plugin/in_dstat.rb', line 150

def previous
  @previous
end

Instance Method Details

#on_change(prev, cur) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/fluent/plugin/in_dstat.rb', line 160

def on_change(prev, cur)
  buffer = @io.read_nonblock(65536)
  lines = buffer.split("\n").map(&:chomp)
  return if lines.empty?
  lines[0] = @partial + lines.first unless @partial.empty?
  @partial = buffer.end_with?("\n") ? "" : lines.pop
  @receive_lines.call(lines)
rescue IO::WaitReadable
  # will be readable on next event
end