logstash/lib/logstash/inputs/pipe.rb
Jordan Sissel 541b673414 Set the 'ruby code' encoding for all files to UTF-8. This has the
effect that all string literals are UTF-8 now.

This was achieved with:
sed -i -e '1 { /^# encoding: utf-8/! { i # encoding: utf-8
} }' lib/logstash/**/*.rb
2013-11-19 09:14:03 -08:00

54 lines
1.5 KiB
Ruby

# encoding: utf-8
require "logstash/inputs/base"
require "logstash/namespace"
require "socket" # for Socket.gethostname
# Stream events from a long running command pipe.
#
# By default, each event is assumed to be one line. If you
# want to join lines, you'll want to use the multiline filter.
#
class LogStash::Inputs::Pipe < LogStash::Inputs::Base
config_name "pipe"
milestone 1
# TODO(sissel): This should switch to use the 'line' codec by default
# once we switch away from doing 'readline'
default :codec, "plain"
# Command to run and read events from, one line at a time.
#
# Example:
#
# command => "echo hello world"
config :command, :validate => :string, :required => true
public
def register
@logger.info("Registering pipe input", :command => @command)
end # def register
public
def run(queue)
begin
@pipe = IO.popen(@command, mode="r")
hostname = Socket.gethostname
@pipe.each do |line|
line = line.chomp
source = "pipe://#{hostname}/#{@command}"
@logger.debug? && @logger.debug("Received line", :command => @command, :line => line)
@codec.decode(line) do |event|
event["host"] = hostname
event["command"] = @command
decorate(event)
queue << event
end
end
rescue Exception => e
@logger.error("Exception while running command", :e => e, :backtrace => e.backtrace)
sleep(10)
retry
end
end # def run
end # class LogStash::Inputs::Pipe