- Move agent flags and settings management into logstash/agent.rb

This commit is contained in:
Jordan Sissel 2011-03-19 00:24:56 -07:00
parent 2e160365a0
commit 90b72b4c5a
2 changed files with 83 additions and 78 deletions

View file

@ -6,79 +6,6 @@ require "rubygems"
require "logstash/agent"
require "optparse"
Settings = Struct.new(:config_file, :daemonize, :logfile, :verbose)
settings = Settings.new
settings.daemonize = false
settings.config_file = nil
settings.verbose = 0
progname = File.basename($0)
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{progname} [options]"
opts.on("-f CONFIGFILE", "--config CONFIGFILE",
"Load the logstash config from a specific file") do |arg|
settings.config_file = arg
end
opts.on("-d", "--daemonize", "Daemonize (default is run in foreground)") do
settings.daemonize = true
end
opts.on("-l", "--log FILE", "Log to a given path. Default is stdout.") do |path|
settings.logfile = path
end
opts.on("-v", "Increase verbosity") do
settings.verbose += 1
end
end
begin
opts.parse!
if settings.config_file == "" or settings.config_file == nil
raise "No config file given. (missing -f or --config flag?)"
end
rescue
$stderr.puts "#{progname}: #{$!}"
$stderr.puts opts
exit(1)
end
# TODO(sissel): put the config management stuff in LogStash::Agent
if !File.exist?(settings.config_file)
$stderr.puts "Config file '#{settings.config_file}' does not exist."
end
#begin
#config = YAML::load_file(settings.config_file)
#rescue => e
#$stderr.puts "Loading config file '#{settings.config_file}' failed: #{e}"
##exit 1
#end
if settings.daemonize
if Process.fork == nil
Process.setsid
else
exit(0)
end
end
if settings.logfile
logfile = File.open(settings.logfile, "w")
STDOUT.reopen(logfile)
STDERR.reopen(logfile)
elsif settings.daemonize
# Write to /dev/null if
devnull = File.open("/dev/null", "w")
STDOUT.reopen(devnull)
STDERR.reopen(devnull)
end
agent = LogStash::Agent.new(settings)
if settings.verbose > 0
agent.logger.level = Logger::DEBUG
end
agent = LogStash::Agent.new
agent.argv = ARGV
agent.run

View file

@ -21,11 +21,20 @@ class LogStash::Agent
attr_reader :filters
attr_accessor :logger
# flags
attr_reader :config_file
attr_reader :daemonize
attr_reader :logfile
attr_reader :verbose
public
def initialize(settings)
def initialize
log_to(STDERR)
@settings = settings
# flag/config defaults
@verbose = 0
@daemonize = false
@threads = {}
@outputs = []
@inputs = []
@ -39,12 +48,81 @@ class LogStash::Agent
@logger = LogStash::Logger.new(target)
end # def log_to
public
def argv=(argv)
@argv = argv
end
private
def options(opts)
opts.on("-f CONFIGFILE", "--config CONFIGFILE",
"Load the logstash config from a specific file") do |arg|
@config_file = arg
end
opts.on("-d", "--daemonize", "Daemonize (default is run in foreground)") do
@daemonize = true
end
opts.on("-l", "--log FILE", "Log to a given path. Default is stdout.") do |path|
@logfile = path
end
opts.on("-v", "Increase verbosity") do
@verbose += 1
end
end
# Parse options.
private
def parse_options
@opts = OptionParser.new
options(@opts)
# TODO(sissel): Go through all inputs, filters, and outputs to get the flags.
@opts.parse!(@argv)
end # def parse_options
private
def configure
if @config_file.nil? || @config_file.empty?
@logger.fatal "No config file given. (missing -f or --config flag?)"
@logger.fatal @opts.help
raise "Configuration problem"
end
if !File.exist?(@config_file)
@logger.fatal "Config file '#{@config_file}' does not exist."
raise "Configuration problem"
end
if @daemonize
@logger.fatal "Can't daemonize, no support yet in JRuby."
raise "Can't daemonize, no fork in JRuby."
end
if @logfile
logfile = File.open(settings.logfile, "w")
STDOUT.reopen(logfile)
STDERR.reopen(logfile)
elsif @daemonize
devnull = File.open("/dev/null", "w")
STDOUT.reopen(devnull)
STDERR.reopen(devnull)
end
if @verbose > 0
@logger.level = Logger::DEBUG
end
end # def configure
public
def run
JThread.currentThread().setName("agent")
parse_options
configure
# Load the config file
config = LogStash::Config::File.new(@settings.config_file)
config = LogStash::Config::File.new(@config_file)
config.parse do |plugin|
# 'plugin' is a has containing:
# :type => the base class of the plugin (LogStash::Inputs::Base, etc)