Class: Fluent::SyslogBufferedOutputCG

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

Instance Method Summary collapse

Constructor Details

#initializeSyslogBufferedOutputCG

Returns a new instance of SyslogBufferedOutputCG.



25
26
27
28
29
30
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 25

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

Instance Method Details

#configure(conf) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 32

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
  if conf['max_size']
    @max_size = conf['max_size'].to_i
  end
end

#create_tcp_socket(host, port) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 58

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



54
55
56
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 54

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#send_to_syslog(tag, time, record) ⇒ Object



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
131
132
133
134
135
136
137
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 96

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(@max_size) + "\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.



85
86
87
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 85

def shutdown
  super
end

#startObject

This method is called when starting.



80
81
82
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 80

def start
  super
end

#write(chunk) ⇒ Object



90
91
92
93
94
# File 'lib/fluentd/plugin/out_syslog_buffered-cg.rb', line 90

def write(chunk)
  chunk.msgpack_each {|(tag,time,record)|
        send_to_syslog(tag, time, record)
      }
end