Created
March 22, 2013 15:18
-
-
Save scr34m/5222068 to your computer and use it in GitHub Desktop.
logstash udp output plugin based on tcp output plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "logstash/outputs/base" | |
require "logstash/namespace" | |
require "thread" | |
# Write events over a UDP socket. | |
# | |
# Each event json is separated by a newline. | |
class LogStash::Outputs::Udp < LogStash::Outputs::Base | |
config_name "udp" | |
plugin_status "beta" | |
config :host, :validate => :string, :required => true | |
config :port, :validate => :number, :required => true | |
config :message_format, :validate => :string | |
class Client | |
public | |
def initialize(socket, logger) | |
@socket = socket | |
@logger = logger | |
@queue = Queue.new | |
end | |
public | |
def run | |
loop do | |
begin | |
@socket.write(@queue.pop) | |
rescue => e | |
@logger.warn("udo output exception", :socket => @socket, | |
:exception => e, :backtrace => e.backtrace) | |
break | |
end | |
end | |
end # def run | |
public | |
def write(msg) | |
@queue.push(msg) | |
end # def write | |
end # class Client | |
public | |
def register | |
@client_socket = nil | |
end # def register | |
private | |
def connect | |
@client_socket = UDPSocket.new | |
end # def connect | |
public | |
def receive(event) | |
return unless output?(event) | |
if @message_format | |
output = event.sprintf(@message_format) + "\n" | |
else | |
output = event.to_hash.to_json + "\n" | |
end | |
begin | |
connect unless @client_socket | |
@client_socket.send(output, 0, @host, @port) | |
rescue => e | |
@logger.warn("udp output exception", :host => @host, :port => @port, | |
:exception => e, :backtrace => e.backtrace) | |
@client_socket = nil | |
end | |
end # def receive | |
end # class LogStash::Outputs::Udp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment