Class: Fluent::SyslogBufferedOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluentd/plugin/out_syslog_buffered.rb

Instance Method Summary collapse

Constructor Details

#initializeSyslogBufferedOutput

Returns a new instance of SyslogBufferedOutput.



21
22
23
24
25
26
# File 'lib/fluentd/plugin/out_syslog_buffered.rb', line 21

def initialize
  super
  require 'socket'
  require 'syslog_protocol'
  require 'timeout'
end

Instance Method Details

#configure(conf) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



47
48
49
# 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) ⇒ Object



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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

#shutdownObject

This method is called when shutting down.



78
79
80
# File 'lib/fluentd/plugin/out_syslog_buffered.rb', line 78

def shutdown
  super
end

#startObject

This method is called when starting.



73
74
75
# File 'lib/fluentd/plugin/out_syslog_buffered.rb', line 73

def start
  super
end

#write(chunk) ⇒ Object



83
84
85
86
87
# 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