Class: Fluent::HerokuSyslogInput::TcpHandler

Inherits:
Coolio::Socket
  • Object
show all
Defined in:
lib/fluent/plugin/in_heroku_syslog.rb

Instance Method Summary collapse

Constructor Details

#initialize(io, on_message) ⇒ TcpHandler

Returns a new instance of TcpHandler.



172
173
174
175
176
177
178
179
180
181
# File 'lib/fluent/plugin/in_heroku_syslog.rb', line 172

def initialize(io, on_message)
  super(io)
  if io.is_a?(TCPSocket)
    opt = [1, @timeout.to_i].pack('I!I!')  # { int l_onoff; int l_linger; }
    io.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, opt)
  end
  $log.trace { "accepted fluent socket object_id=#{self.object_id}" }
  @on_message = on_message
  @buffer = "".force_encoding('ASCII-8BIT')
end

Instance Method Details

#on_closeObject



216
217
218
# File 'lib/fluent/plugin/in_heroku_syslog.rb', line 216

def on_close
  $log.trace { "closed fluent socket object_id=#{self.object_id}" }
end

#on_connectObject



183
184
# File 'lib/fluent/plugin/in_heroku_syslog.rb', line 183

def on_connect
end

#on_read(data) ⇒ Object



186
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
# File 'lib/fluent/plugin/in_heroku_syslog.rb', line 186

def on_read(data)
  @buffer << data
  pos = 0

  # syslog family add "\n" to each message and this seems only way to split messages in tcp stream
  while i = @buffer.index("\n", pos)
    msg = @buffer[pos..i]

    # Support Octet Counting 
    # https://tools.ietf.org/html/rfc6587#section-3.4.1
    m = OCTET_COUNTING_REGEXP.match(msg)
    valid = true
    if m
      msg_len = m[1].to_i - 1
      msg = m[2]

      if msg_len != msg.length
        $log.debug "invalid syslog message length", :expected => msg_len, :actual => msg.length, :data => msg
        valid = false
      end
    end
    @on_message.call(msg) if valid
    pos = i + 1
  end
  @buffer.slice!(0, pos) if pos > 0
rescue => e
  $log.error "syslog error", :error => e, :error_class => e.class
  close
end