# encoding: utf-8 require "logstash/inputs/base" require "logstash/namespace" require "logstash/json" require "logstash/timestamp" require "stud/interval" require "date" require "socket" # This input will read GELF messages as events over the network, # making it a good choice if you already use Graylog2 today. # # class LogStash::Inputs::Gelf < LogStash::Inputs::Base config_name "gelfx" default :codec, "plain" # The IP address or hostname to listen on. config :host, :validate => :string, :default => "0.0.0.0" # The port to listen on. Remember that ports less than 1024 (privileged # ports) may require root to use. config :port, :validate => :number, :default => 9397 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" public def initialize(params) super BasicSocket.do_not_reverse_lookup = true end # def initialize public def register require 'gelfd' end # def register public def run(output_queue) begin # udp server udp_listener(output_queue) rescue => e unless stop? @logger.warn("gelf listener died", :exception => e, :backtrace => e.backtrace) Stud.stoppable_sleep(RECONNECT_BACKOFF_SLEEP) { stop? } retry unless stop? end end # begin end # def run public def stop @udp.close rescue IOError # the plugin is currently shutting down, so its safe to ignore theses errors end private def udp_listener(output_queue) @logger.info("Starting gelf listener", :address => "#{@host}:#{@port}") @udp = UDPSocket.new(Socket::AF_INET) @udp.bind(@host, @port) while !stop? line, client = @udp.recvfrom(8192) begin data = Gelfd::Parser.parse(line) rescue => ex @logger.warn("Gelfd failed to parse a message skipping", :exception => ex, :backtrace => ex.backtrace) next end # Gelfd parser outputs null if it received and cached a non-final chunk next if data.nil? event = self.class.new_event(data, client[3]) next if event.nil? output_queue << event end end # def udp_listener def self.new_event(json_gelf, host) event = parse(json_gelf) return if event.nil? event.set(SOURCE_HOST_FIELD, host) event end # from_json_parse uses the Event#from_json method to deserialize and directly produce events def self.from_json_parse(json) # from_json will always return an array of item. # in the context of gelf, the payload should be an array of 1 LogStash::Event.from_json(json).first rescue LogStash::Json::ParserError => e logger.error(PARSE_FAILURE_LOG_MESSAGE, :error => e, :data => json) LogStash::Event.new(MESSAGE_FIELD => json, TAGS_FIELD => [PARSE_FAILURE_TAG, '_fromjsonparser']) end # def self.from_json_parse end # class LogStash::Inputs::Gelf