- Add support for agent '-e' flag - lets you specify a configuration

in a simple string.

  If no inputs are specified, it assumes: stdin { type => stdin }
  If no outputs are specified, it assumes: stdout { debug => true }
  Any given filters default with 'type => stdin' unless otherwise specified.

  https://logstash.jira.com/browse/LOGSTASH-105
This commit is contained in:
Jordan Sissel 2011-06-24 00:13:16 -07:00
parent e16f12b7b5
commit 583ed2b90b
5 changed files with 48 additions and 7 deletions

View file

@ -10,5 +10,4 @@ if ARGV[0] =~ /^-/
ARGV.unshift("agent")
end
p :bin => ARGV
LogStash::Runner.new.main(ARGV)

View file

@ -67,7 +67,15 @@ class LogStash::Agent
opts.on("-f CONFIGFILE", "--config CONFIGFILE",
"Load the logstash config from a specific file") do |arg|
@config_file = arg
end
end # -f / --config
opts.on("-e CONFIGSTRING",
"Use the given string as the configuration data. Same syntax as " \
"the config file. If not input is specified, " \
"'stdin { type => stdin }' is default. If no output is " \
"specified, 'stdout { debug => true }}' is default.") do |arg|
@config_string = arg
end # -e
opts.on("-d", "--daemonize", "Daemonize (default is run in foreground)") do
@daemonize = true
@ -173,13 +181,16 @@ class LogStash::Agent
private
def configure
if @config_file.nil? || @config_file.empty?
if @config_file && @config_string
@logger.fatal "Can't use -f and -e at the same time"
raise "Configuration problem"
elsif (@config_file.nil? || @config_file.empty?) && @config_string.nil?
@logger.fatal "No config file given. (missing -f or --config flag?)"
@logger.fatal @opts.help
raise "Configuration problem"
end
if !File.exist?(@config_file)
if @config_file and !File.exist?(@config_file)
@logger.fatal "Config file '#{@config_file}' does not exist."
raise "Configuration problem"
end
@ -224,7 +235,11 @@ class LogStash::Agent
configure
# Load the config file
config = LogStash::Config::File.new(@config_file)
if @config_file
config = LogStash::Config::File.new(@config_file, nil)
elsif @config_string
config = LogStash::Config::File.new(nil, @config_string)
end
@thread = Thread.new do
run_with_config(config, &block)
@ -266,6 +281,26 @@ class LogStash::Agent
end # case type
end # config.parse
# If we are given a config string (run usually with 'agent -e "some config string"')
# then set up some defaults.
if @config_string
require "logstash/inputs/stdin"
require "logstash/outputs/stdout"
# set defaults if necessary
# All filters default to 'stdin' type
@filters.each do |filter|
filter.type = "stdin" if filter.type.nil?
end
# If no inputs are specified, use stdin by default.
@inputs = [LogStash::Inputs::Stdin.new("type" => [ "stdin" ])] if @inputs.length == 0
# If no outputs are specified, use stdout in debug mode.
@outputs = [LogStash::Outputs::Stdout.new("debug" => [ "true" ])] if @outputs.length == 0
end
if @inputs.length == 0 or @outputs.length == 0
raise "Must have both inputs and outputs configured."
end

View file

@ -27,7 +27,7 @@ class LogStash::Config::File
@config = grammar.config
registry = LogStash::Config::Registry::registry
self.each do |o|
each do |o|
# Load the base class for the type given (like inputs/base, or filters/base)
# TODO(sissel): Error handling
tryload o[:type], :base

View file

@ -77,6 +77,8 @@ module LogStash::Config::Mixin
# set @foo
#ivar = "@#{key}"
instance_variable_set("@#{key}", value)
#define_method(key.to_sym) { value }
#define_method("#{key}=".to_sym) { |v| instance_variable_set("@#{key}", v) }
end
@config = params
@ -100,6 +102,11 @@ module LogStash::Config::Mixin
name = name.to_s if name.is_a?(Symbol)
@config[name] = opts # ok if this is empty
if name.is_a?(String)
define_method(name) { instance_variable_get("@#{name}") }
define_method("#{name}=") { |v| instance_variable_set("@#{name}", v) }
end
end # def config
def get_config

View file

@ -7,7 +7,7 @@ class LogStash::Runner
@runners = []
while !args.empty?
p :args => args
#p :args => args
args = run(args)
end