Skip to content

Commit

Permalink
- Add '-d' (daemonize)
Browse files Browse the repository at this point in the history
- Add '-l LOGFILE' (log to a file instead of STDOUT)
- Give the agent's logger to each input/output/filter.
  • Loading branch information
jordansissel committed Dec 7, 2010
1 parent d0625bc commit 1aa5365
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
31 changes: 25 additions & 6 deletions bin/logstash
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require "logstash/agent"
require "optparse"
require "yaml"

Settings = Struct.new(:config_file, :daemonize)
Settings = Struct.new(:config_file, :daemonize, :logfile)

settings = Settings.new
settings.daemonize = false
Expand All @@ -23,17 +23,26 @@ opts = OptionParser.new do |opts|
settings.config_file = arg
end

#opts.on("-d", "--daemonize", "Daemonize (default is run in foreground)") do
#settings.daemonize = true
#end
opts.on("-d", "--daemonize", "Daemonize (default is run in foreground)") do
settings.daemonize = true
begin
require "daemons"
rescue LoadError => e
$stderr.puts "Could not load library 'daemons'. Is the 'daemons' rubygem installed?"
exit 1
end
end

opts.on("-l", "--log FILE", "Log to a given path. Default is stdout.") do |path|
settings.logfile = path
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
Expand All @@ -52,5 +61,15 @@ rescue => e
exit 1
end

if settings.daemonize
if Process.fork == nil
Process.setsid
else
exit(0)
end
end
agent = LogStash::Agent.new(config)
agent.run
if settings.logfile
agent.log_to(settings.logfile)
end
agent.run
2 changes: 1 addition & 1 deletion etc/logstash-standalone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ filters:
timestamp: "%d/%b/%Y:%H:%M:%S %Z"
outputs:
- stdout:///
- elasticsearch://localhost:9200/logs/all
#- elasticsearch://localhost:9200/logs/all
# But we could write to mongodb, too.
# - mongodb://localhost/parsedlogs
# And also write to an AMQP topic
Expand Down
25 changes: 21 additions & 4 deletions lib/logstash/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LogStash::Agent
attr_reader :filters

def initialize(config)
@logger = LogStash::Logger.new(STDERR)
log_to(STDERR)

@config = config
@outputs = []
Expand All @@ -26,6 +26,11 @@ def initialize(config)
# - where to ship to
end # def initialize

public
def log_to(target)
@logger = LogStash::Logger.new(target)
end # def log_to

# Register any event handlers with EventMachine
# Technically, this agent could listen for anything (files, sockets, amqp,
# stomp, etc).
Expand Down Expand Up @@ -55,6 +60,7 @@ def register
urls.each do |url|
@logger.debug("Using input #{url} of type #{type}")
input = LogStash::Inputs.from_url(url, type) { |event| receive(event) }
input.logger = @logger
input.register
@inputs << input
end
Expand All @@ -67,6 +73,7 @@ def register
name, value = filter
@logger.debug("Using filter #{name} => #{value.inspect}")
filter = LogStash::Filters.from_name(name, value)
filter.logger = @logger
filter.register
@filters << filter
end # each filter
Expand All @@ -76,6 +83,7 @@ def register
@config["outputs"].each do |url|
@logger.debug("Using output #{url}")
output = LogStash::Outputs.from_url(url)
output.logger = @logger
output.register
@outputs << output
end # each output
Expand Down Expand Up @@ -135,6 +143,10 @@ def sighandler
@sigchannel.push(:USR1)
end

Signal.trap("INT") do
@sigchannel.push(:INT)
end

@sigchannel.subscribe do |msg|
case msg
when :USR1
Expand All @@ -153,7 +165,12 @@ def sighandler
counts.sort { |a,b| a[1] <=> b[1] or a[0] <=> b[0] }.each do |key, value|
@logger.info("Class: [#{value}] #{key}")
end
end
end
end
when :INT
@logger.warn("SIGINT received. Shutting down.")
EventMachine::stop_event_loop
# TODO(sissel): Should have input/output/filter register shutdown
# hooks.
end # case msg
end # @sigchannel.subscribe
end # def sighandler
end # class LogStash::Components::Agent
1 change: 1 addition & 0 deletions lib/logstash/filters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "logstash/logging"

class LogStash::Filters::Base
attr_accessor :logger
def initialize(config = {})
@logger = LogStash::Logger.new(STDERR)
@config = config
Expand Down
1 change: 1 addition & 0 deletions lib/logstash/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "uri"

class LogStash::Inputs::Base
attr_accessor :logger
def initialize(url, type, config={}, &block)
@logger = LogStash::Logger.new(STDERR)
@url = url
Expand Down
1 change: 1 addition & 0 deletions lib/logstash/outputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "uri"

class LogStash::Outputs::Base
attr_accessor :logger
def initialize(url, config={}, &block)
@url = url
@url = URI.parse(url) if url.is_a? String
Expand Down

0 comments on commit 1aa5365

Please sign in to comment.