Skip to content

Commit

Permalink
- Add support for agent '-e' flag - lets you specify a configuration
Browse files Browse the repository at this point in the history
  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
  • Loading branch information
jordansissel committed Jun 24, 2011
1 parent e16f12b commit 583ed2b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
1 change: 0 additions & 1 deletion bin/logstash
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ if ARGV[0] =~ /^-/
ARGV.unshift("agent")
end

p :bin => ARGV
LogStash::Runner.new.main(ARGV)
43 changes: 39 additions & 4 deletions lib/logstash/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ def options(opts)
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
Expand Down Expand Up @@ -173,13 +181,16 @@ def parse_options(args)

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
Expand Down Expand Up @@ -224,7 +235,11 @@ def run(args, &block)
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)
Expand Down Expand Up @@ -266,6 +281,26 @@ def run_with_config(config)
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
Expand Down
2 changes: 1 addition & 1 deletion lib/logstash/config/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def parse
@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
Expand Down
7 changes: 7 additions & 0 deletions lib/logstash/config/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def config_init(params)
# 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
Expand All @@ -100,6 +102,11 @@ def config(name, opts={})

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
Expand Down
2 changes: 1 addition & 1 deletion lib/logstash/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def main(args)

@runners = []
while !args.empty?
p :args => args
#p :args => args
args = run(args)
end

Expand Down

0 comments on commit 583ed2b

Please sign in to comment.