Class: LogStash::Inputs::Gelf

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/gelf.rb

Overview

This input will read GELF messages as events over the network, making it a good choice if you already use Graylog2 today.

The main use case for this input is to leverage existing GELF logging libraries such as the GELF log4j appender.

Constant Summary collapse

RECONNECT_BACKOFF_SLEEP =
5
TIMESTAMP_GELF_FIELD =
"timestamp".freeze
SOURCE_HOST_FIELD =
"source_host".freeze
MESSAGE_FIELD =
"message"
TAGS_FIELD =
"tags"
PARSE_FAILURE_TAG =
"_jsonparsefailure"
PARSE_FAILURE_LOG_MESSAGE =
"JSON parse failure. Falling back to plain-text"

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Gelf

Returns a new instance of Gelf.



62
63
64
65
# File 'lib/logstash/inputs/gelf.rb', line 62

def initialize(params)
  super
  BasicSocket.do_not_reverse_lookup = true
end

Instance Method Details

#registerObject



67
68
69
70
71
# File 'lib/logstash/inputs/gelf.rb', line 67

def register
  require 'gelfd2'
  @port_tcp ||= @port
  @port_udp ||= @port
end

#run(output_queue) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/logstash/inputs/gelf.rb', line 73

def run(output_queue)
  begin
    if @use_tcp
      @tcp_thr = Thread.new(output_queue) { |output_queue| tcp_listener(output_queue) }
    end
    if @use_udp
      @udp_thr = Thread.new(output_queue) { |output_queue| udp_listener(output_queue) }
    end
  rescue => e
    unless stop?
      @logger.warn("gelf listener died", :exception => e, :backtrace => e.backtrace)
      Stud.stoppable_sleep(RECONNECT_BACKOFF_SLEEP) { stop? }
      if !stop?
        # before retrying make sure we close all sockets
        stop
        wait_server_thread
        retry
      end
    end
  end

  wait_server_thread
end

#stopObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/logstash/inputs/gelf.rb', line 97

def stop
  begin
    @udp.close if @use_udp
    @udp = nil
  rescue => e
    @logger.debug("Caught exception while closing udp socket", :exception => e)
  end
  begin
    @tcp.close if @use_tcp
    @tcp = nil
  rescue => e
    @logger.debug("Caught exception while closing tcp socket", :exception => e)
  end
end