Class: Fluent::Papertrail

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_papertrail.rb

Defined Under Namespace

Classes: SocketFailureError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



6
7
8
# File 'lib/fluent/plugin/out_papertrail.rb', line 6

def socket
  @socket
end

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
23
24
# File 'lib/fluent/plugin/out_papertrail.rb', line 20

def configure(conf)
  super
  @socket = create_socket(@papertrail_host, @papertrail_port)
  @default_hostname = ENV['FLUENT_HOSTNAME'] || @default_hostname
end

#create_packet(tag, time, record) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/plugin/out_papertrail.rb', line 54

def create_packet(tag,time,record)
  # construct syslog packet from fluent record
  packet = SyslogProtocol::Packet.new
  packet.hostname = record['hostname'] || @default_hostname
  packet.facility = record['facility'] || 'local0'
  packet.severity = record['severity'] || 'info'
  packet.tag      = record['program'] || tag
  packet.content  = record['message']
  packet.time     = time ? Time.at(time) : Time.now
  packet
end

#create_socket(host, port) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fluent/plugin/out_papertrail.rb', line 37

def create_socket(host, port)
  log.info "initializing tcp socket for #{host}:#{port}"
  begin
    socket = TCPSocket.new(host, port)
    log.debug "enabling ssl for socket #{host}:#{port}"
    ssl = OpenSSL::SSL::SSLSocket.new(socket)
    # close tcp and ssl socket when either fails
    ssl.sync_close = true
    # initiate SSL/TLS handshake with server
    ssl.connect
  rescue => e
    log.warn "failed to create tcp socket #{host}:#{port}: #{e}"
    ssl = nil
  end
  ssl
end

#format(tag, time, record) ⇒ Object



26
27
28
# File 'lib/fluent/plugin/out_papertrail.rb', line 26

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

#send_to_papertrail(packet) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fluent/plugin/out_papertrail.rb', line 66

def send_to_papertrail(packet)
  # recreate the socket if it's nil -- see below
  @socket ||= create_socket(@papertrail_host, @papertrail_port)

  papertrail_addr = "#{@papertrail_host}:#{@papertrail_port}"

  if @socket.nil?
    err_msg = "Unable to create socket with #{papertrail_addr}"
    log.error err_msg
    raise SocketFailureError, err_msg
  else
    begin
      # send it
      @socket.puts packet.assemble
    rescue => e
      err_msg = "Error writing to #{papertrail_addr}: #{e}"
      log.error err_msg
      # socket failed, reset to nil to recreate for the next write
      @socket = nil
      raise SocketFailureError, err_msg, e.backtrace
    end
  end
end

#write(chunk) ⇒ Object



30
31
32
33
34
35
# File 'lib/fluent/plugin/out_papertrail.rb', line 30

def write(chunk)
  chunk.msgpack_each {|(tag, time, record)|
    packet = create_packet(tag, time, record)
    send_to_papertrail(packet)
  }
end