diff --git a/.gitignore b/.gitignore index 13b62b2..fb78931 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ spec/fixtures test/tmp test/version_tmp tmp +.rbenv* diff --git a/lib/woodchuck/agent.rb b/lib/woodchuck/agent.rb index 950c5d0..85ecbcf 100644 --- a/lib/woodchuck/agent.rb +++ b/lib/woodchuck/agent.rb @@ -6,12 +6,13 @@ class Woodchuck::Agent - attr_accessor :logger, :watcher, :watcher_thread, :paths, :output + attr_accessor :logger, :watcher, :watcher_thread, :paths, :output, :type def initialize(options={}) @paths = options[:paths] @logger = Woodchuck::Logger.new(::STDOUT) @mutex = Mutex.new + @type = options[:type] || 'file' @output = case options[:output] when :stdout Woodchuck::Output::STDOUT.new @@ -22,7 +23,7 @@ def initialize(options={}) else Woodchuck::Output::STDOUT.new end - @watcher = Woodchuck::Watcher.new(self, @paths) + @watcher = Woodchuck::Watcher.new(self, @paths, @type) end def start(blocking=false) diff --git a/lib/woodchuck/event.rb b/lib/woodchuck/event.rb index 85bf836..9d8c8fc 100644 --- a/lib/woodchuck/event.rb +++ b/lib/woodchuck/event.rb @@ -2,9 +2,9 @@ class Woodchuck::Event - attr_accessor :path, :line, :host, :timestamp, :source, :message, :fields, :tags + attr_accessor :path, :line, :host, :timestamp, :source, :message, :fields, :tags, :type - def initialize(path, line) + def initialize(path, line, type) @path = path @line = line @host = Socket.gethostname @@ -13,6 +13,7 @@ def initialize(path, line) @message = line.strip @fields = {} @tags = [] + @type = type end def method_missing(symbol, *args, &block) @@ -26,7 +27,7 @@ def method_missing(symbol, *args, &block) def to_hash { '@source' => source.to_s, - '@type' => source.scheme, + '@type' => type, '@tags' => tags, '@fields' => fields, '@timestamp' => timestamp, diff --git a/lib/woodchuck/output/redis.rb b/lib/woodchuck/output/redis.rb index c9c59c6..e620ba6 100644 --- a/lib/woodchuck/output/redis.rb +++ b/lib/woodchuck/output/redis.rb @@ -2,13 +2,14 @@ require 'redis/namespace' class Woodchuck::Output::Redis < Woodchuck::Output - attr_accessor :url, :host, :port, :db, :namespace + attr_accessor :url, :host, :port, :db, :namespace, :key def initialize super @type = :redis @url = Addressable::URI.parse(ENV['REDIS_URL'] || 'redis://localhost:6379/9') @namespace = ENV['REDIS_NAMESPACE'] || 'logstash:woodchuck' + @key = ENV['REDIS_KEY'] || 'events' end def redis @@ -17,7 +18,7 @@ def redis end def handle(event) - redis.lpush("events", event.to_json) + redis.lpush(key, event.to_json) @logger.info "Logging event to Redis", event.to_hash end end \ No newline at end of file diff --git a/lib/woodchuck/runner.rb b/lib/woodchuck/runner.rb index 2a98c7d..f509af9 100644 --- a/lib/woodchuck/runner.rb +++ b/lib/woodchuck/runner.rb @@ -26,6 +26,9 @@ def read(args=ARGV) opts.on('-p', '--paths [PATHS]', Array, 'A list of file paths to watch') do |paths| options[:paths] = paths end + opts.on('-t', '--type [TYPE]', 'set logstash type') do |type| + options[:type] = type + end end optparse.parse!(args) options diff --git a/lib/woodchuck/version.rb b/lib/woodchuck/version.rb index b3b4156..71fe6e4 100644 --- a/lib/woodchuck/version.rb +++ b/lib/woodchuck/version.rb @@ -1,3 +1,3 @@ module Woodchuck - VERSION = "0.0.1" + VERSION = "0.0.2" end diff --git a/lib/woodchuck/watcher.rb b/lib/woodchuck/watcher.rb index d2a6320..2665a41 100644 --- a/lib/woodchuck/watcher.rb +++ b/lib/woodchuck/watcher.rb @@ -9,14 +9,15 @@ class Woodchuck::Watcher extend Forwardable - attr_accessor :tailer, :paths, :logger, :events, :agent + attr_accessor :tailer, :paths, :logger, :events, :agent, :type def_delegator :@agent, :output, :output - def initialize(agent, *paths) + def initialize(agent, *paths, type) @agent = agent @logger = Woodchuck::Logger.new(::STDOUT) @tailer = FileWatch::Tail.new @paths = paths.flatten + @type = type end def start @@ -24,7 +25,7 @@ def start tailer.tail(path) end tailer.subscribe do |path, line| - event = Woodchuck::Event.new(path, line) + event = Woodchuck::Event.new(path, line, type) output.handle(event) end end