class Fluent::SyslogBufferedOutput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 21
def initialize
  super
  require 'socket'
  require 'syslog_protocol'
  require 'timeout'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 28
def configure(conf)
  super
  if not conf['remote_syslog']
    raise Fluent::ConfigError.new("remote syslog required")
  end
    @socket = create_tcp_socket(conf['remote_syslog'], conf['port'])
  @packet = SyslogProtocol::Packet.new
  if remove_tag_prefix = conf['remove_tag_prefix']
    @remove_tag_prefix = Regexp.new('^' + Regexp.escape(remove_tag_prefix))
  end
  @facilty = conf['facility']
  @severity = conf['severity']
  @use_record = conf['use_record']
  @payload_key = conf['payload_key']
  if not @payload_key
    @payload_key = "message"
  end
end
create_tcp_socket(host, port) click to toggle source
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 51
def create_tcp_socket(host, port)
  begin
    Timeout.timeout(10) do
      begin
        socket = TCPSocket.new(host, port)
      rescue Errno::ENETUNREACH
        retry
      end
    end
    socket = TCPSocket.new(host, port)
    secs = Integer(1)
    usecs = Integer((1 - secs) * 1_000_000)
    optval = [secs, usecs].pack("l_2")
    socket.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
  rescue SocketError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EPIPE, Timeout::Error, OpenSSL::SSL::SSLError, Timeout::Error => e
    log.warn "out:syslog: failed to open tcp socket  #{@remote_syslog}:#{@port} :#{e}"
    socket = nil
  end
  socket
end
format(tag, time, record) click to toggle source
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 47
def format(tag, time, record)
  [tag, time, record].to_msgpack
end
send_to_syslog(tag, time, record) click to toggle source
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 89
def send_to_syslog(tag, time, record)
  tag = tag.sub(@remove_tag_prefix, '') if @remove_tag_prefix
  @packet.hostname = hostname
  if @use_record
    @packet.facility = record['facility'] || @facilty
    @packet.severity = record['severity'] || @severity
  else
    @packet.facility = @facilty
    @packet.severity = @severity
  end
  if record['time']
    time = Time.parse(record['time'])
  else
    time = Time.now
  end
  @packet.time = time
  @packet.tag      = if tag_key
                       record[tag_key][0..31].gsub(/[\[\]]/,'') # tag is trimmed to 32 chars for syslog_protocol gem compatibility
                     else
                       tag[0..31] # tag is trimmed to 32 chars for syslog_protocol gem compatibility
                     end
  packet = @packet.dup
  packet.content = record[@payload_key]
  begin
    if not @socket
      @socket = create_tcp_socket(@remote_syslog, @port)
    end
    if @socket
      begin
        @socket.write packet.assemble + "\n"
        @socket.flush
      rescue SocketError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EPIPE, Timeout::Error, OpenSSL::SSL::SSLError => e
        log.warn "out:syslog: connection error by #{@remote_syslog}:#{@port} :#{e}"
        @socket = nil
        raise #{e}
      end
    else
      log.warn "out:syslog: Socket connection couldn't be reestablished"
      raise #{e}
    end
  end
end
shutdown() click to toggle source

This method is called when shutting down.

Calls superclass method
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 78
def shutdown
  super
end
start() click to toggle source

This method is called when starting.

Calls superclass method
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 73
def start
  super
end
write(chunk) click to toggle source
# File lib/fluentd/plugin/out_syslog_buffered.rb, line 83
def write(chunk)
  chunk.msgpack_each {|(tag,time,record)|
        send_to_syslog(tag, time, record)
      }
end