diff --git a/Rakefile b/Rakefile index b87f34e..7d84be5 100644 --- a/Rakefile +++ b/Rakefile @@ -11,8 +11,8 @@ begin gem.homepage = "http://github.com/DenisKnauf/logan" gem.authors = ["Denis Knauf"] gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb] - gem.require_paths = %w[lib] - gem.add_dependency 'sbdb' + gem.require_paths = %w[bin lib] + gem.add_dependency %w[robustserver sbdb] end Jeweler::GemcutterTasks.new rescue LoadError diff --git a/bin/conf.rb b/bin/conf.rb new file mode 100755 index 0000000..289d61a --- /dev/null +++ b/bin/conf.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby + +require 'sbdb' + +conf = {} +%w[etc sids].each {|key| conf[key.to_sym] = key } +conf[:sids] = File.basename( conf[:sids], ".cnf")+".cnf" + +$stdout.puts ARGV.inspect + +SBDB::Env.open( conf[:etc], SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL, + log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE) do |etc| + etc.recno( conf[:sids], ARGV[0], flags: SBDB::CREATE | SBDB::AUTO_COMMIT) do |db| + $stderr.puts db.inspect + if ARGV[2] + db[ ARGV[1].to_i] = ARGV[2].empty? ? nil : ARGV[2] + end + if ARGV[1] + $stdout.puts "#{ARGV[0].inspect} #{ARGV[1].to_i} #{db[ ARGV[1].to_i].inspect}" + else + db.each do |k, v| + begin + $stdout.puts "#{ARGV[0].inspect} #{k} #{v.inspect}" + rescue Bdb::DbError + next if 22 == $!.code + raise $! + end + end + end + end +end diff --git a/bin/loganinc b/bin/loganinc index 25117d7..97c0930 100755 --- a/bin/loganinc +++ b/bin/loganinc @@ -1,34 +1,15 @@ #!/usr/bin/ruby -require 'sbdb' -require 'safebox' -require 'robustserver' +require 'logan' +require 'logan/inc' -class LogAn::Main < RobustServer - def initialize conf - super - @conf = conf - @logs = LogAn::Loglines.new 'logs' - Dir.mkdir 'etc' rescue Errno::EEXIST - @etc = SBDB::Env.new( 'etc', - log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE, - flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL) - @serv = LogAn::Inc.new :sock => TCPServer.new( *@conf[:server]) - @sigs[:INT] = @sigs[:TERM] = method(:shutdown) +opts = {} +opts[:server] = if ARGV[1] + ARGV + elsif ARGV[0] + ['localhost', ARGV[1]] + else %w[localhost 1087] end +opts[:server][1] = opts[:server][1].to_i - def at_exit - @logs and @logs.close - @etc and @etc.close - end - - def shutdown s = nil - $stderr.puts [:signal, s, Signal[s]].inspect - @serv.close - exit 0 - end - - def run - @serv.run - end -end +LogAn::Inc::Main.main *opts diff --git a/lib/logan/inc.rb b/lib/logan/inc.rb index 5d36ce8..297ce24 100644 --- a/lib/logan/inc.rb +++ b/lib/logan/inc.rb @@ -2,8 +2,9 @@ require 'logan/inc/server' require 'logan/inc/fileparser' require 'logan/inc/command' +require 'logan/inc/main' -module Logan +module LogAn module Inc end end diff --git a/lib/logan/inc/command.rb b/lib/logan/inc/command.rb index 90e8794..29aab03 100644 --- a/lib/logan/inc/command.rb +++ b/lib/logan/inc/command.rb @@ -1,5 +1,5 @@ -module Logan +module LogAn module Inc class Command < ::Array attr_reader :sid diff --git a/lib/logan/inc/fileparser.rb b/lib/logan/inc/fileparser.rb index 8d830be..8a470c5 100644 --- a/lib/logan/inc/fileparser.rb +++ b/lib/logan/inc/fileparser.rb @@ -1,5 +1,5 @@ -module Logan +module LogAn module Inc module FileParser module Base diff --git a/lib/logan/inc/main.rb b/lib/logan/inc/main.rb index f031512..560292f 100644 --- a/lib/logan/inc/main.rb +++ b/lib/logan/inc/main.rb @@ -2,13 +2,16 @@ require 'sbdb' require 'safebox' require 'robustserver' +require 'logan/inc' +require 'logan/loglines' module LogAn::Inc class Main < RobustServer # Open Config. def config env, db, type = nil + $stderr.puts "Open Database "sids.cnf" #{db.inspect} (#{type.inspect})" type ||= 1+4 - ret = env[ 'inc.cnf', db, SBDB::RDONLY] + ret = env[ 'sids.cnf', db, SBDB::RDONLY] ret = AutoValueConvertHash.new ret if type&4 > 0 ret = Cache.new ret, type&3 if type&3 > 0 ret @@ -27,7 +30,7 @@ module LogAn::Inc super @conf = {} # Copy config - changes possible - conf.each &@conf.method(:[]=) + conf.each {|key, val| @conf[key]= val } # Default directories %w[logs etc].each {|key| @conf[key.to_sym] = key } # Open Loglines-databases diff --git a/lib/logan/inc/server.rb b/lib/logan/inc/server.rb index 884a99a..cacc9d8 100644 --- a/lib/logan/inc/server.rb +++ b/lib/logan/inc/server.rb @@ -1,106 +1,104 @@ require 'select' -module Logan +module LogAn module Inc - class Select <::Select - attr_reader :entries - def initialize *p - super *p - @entries=[] - end + end +end - def run - until @exit || (@exit_on_empty && self.empty?) - cron - run_once 1 - end - end +class LogAn::Inc::Select <::Select + attr_reader :entries + def initialize *p + super *p + @entries=[] + end - def cron - @entries.each do |e| - return if e > Time.now - e.call - @entries.shift - end - end + def run + until @exit || (@exit_on_empty && self.empty?) + cron + run_once 1 + end + end - class Entry < Time - attr_reader :do - def do &e - @do = e - end + def cron + @entries.each do |e| + return if e > Time.now + e.call + @entries.shift + end + end - def call *p - @do.call *p - end - - def self.new *a, &e - x = self.at *a - x.do &e - x - end - end - - def at a, &e - a = Entry.new a, &e if e - @entries << a - @entries.sort! - end + class Entry < Time + attr_reader :do + def do &e + @do = e end - class Socket <::Select::Socket - def event_read sock = @sock, event = :read - begin - @linebuf += sock.readpartial( @bufsize) - rescue EOFError - self.event_eof sock - rescue Errno::EPIPE => e - self.event_errno e, sock, event - rescue IOError - self.event_ioerror sock, event - rescue Errno::ECONNRESET => e - self.event_errno e, sock, event - end - loop do - return if @linebuf.size < 4 - l = @linebuf.unpack( 'N')[0] - return if l > @linebuf.length - @linebuf.remove 4 - event_cmd @linebuf.remove( l) - end - end + def call *p + @do.call *p end - class Server < ::Select::Server - attr_reader :config + def self.new *a, &e + x = self.at *a + x.do &e + x + end + end - def init opts - super opts - @config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!") - end + def at a, &e + a = Entry.new a, &e if e + @entries << a + @entries.sort! + end +end - def event_new_client sock - { :clientclass => LogAn::Inc::Server::Socket, :config => @config } - end - - class Socket < LogAn::Inc::Server::Socket - attr_reader :config - - def init opts - super opts - @config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!") - end - - def event_cmd cmd - sid, line = cmd.unpack 'Na*' - begin - @config[ :fileparser][sid].event_line line, self - rescue Didi::Config::NoSIDFound, Didi::Config::SIDLoadingError - $stderr.puts( {:sid => sid, :exception => $!, :backtrace => $!.backtrace}.inspect) - end - end - end +class LogAn::Inc::Socket <::Select::Socket + def event_read sock = @sock, event = :read + begin + @linebuf += sock.readpartial( @bufsize) + rescue EOFError + self.event_eof sock + rescue Errno::EPIPE => e + self.event_errno e, sock, event + rescue IOError + self.event_ioerror sock, event + rescue Errno::ECONNRESET => e + self.event_errno e, sock, event + end + loop do + return if @linebuf.size < 4 + l = @linebuf.unpack( 'N')[0] + return if l > @linebuf.length + @linebuf.remove 4 + event_cmd @linebuf.remove( l) + end + end +end + +class LogAn::Inc::Server < ::Select::Server + attr_reader :config + + def init opts + super opts + @config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!") + end + + def event_new_client sock + { :clientclass => LogAn::Inc::Server::Socket, :config => @config } + end + + class Socket < LogAn::Inc::Socket + attr_reader :config + + def init opts + super opts + @config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!") + end + + def event_cmd cmd + sid, line = cmd.unpack 'Na*' + fileparser = @config[:fileparser][sid] + fileparser.event_line line, self if fileparser end end end diff --git a/lib/rotates.rb b/lib/logan/loglines.rb similarity index 98% rename from lib/rotates.rb rename to lib/logan/loglines.rb index 86cdfa4..0712203 100644 --- a/lib/rotates.rb +++ b/lib/logan/loglines.rb @@ -29,7 +29,7 @@ module LogAn @rdb = @env[ 'rotates.db', :type => SBDB::Btree, :flags => SBDB::CREATE | SBDB::AUTO_COMMIT] @queue = @env[ "newids.queue", :type => SBDB::Queue, :flags => SBDB::CREATE | SBDB::AUTO_COMMIT, :re_len => 16] @dbs = {} - self.hash = lambda {|k| + self.hash_func = lambda {|k| [k.timestamp.to_i/60/60].pack 'N' # Hour-based rotation } end diff --git a/lib/logan/types/syslog.rb b/lib/logan/types/syslog.rb index b172515..bfca220 100644 --- a/lib/logan/types/syslog.rb +++ b/lib/logan/types/syslog.rb @@ -1,3 +1,3 @@ require 'logan' -Logan.add CStruct.new( :line) +LogAn.add CStruct.new( :line) diff --git a/logan.gemspec b/logan.gemspec index e8d9c39..f4e75e9 100644 --- a/logan.gemspec +++ b/logan.gemspec @@ -9,22 +9,33 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Denis Knauf"] - s.date = %q{2010-02-24} + s.date = %q{2010-03-29} s.description = %q{} s.email = %q{Denis.Knauf@gmail.com} + s.executables = ["box3.rb", "box.rb", "box2.rb", "loganinc"] s.extra_rdoc_files = [ - "LICENSE" + "LICENSE", + "README.md" ] s.files = [ - "VERSION", + "AUTHORS", + "README.md", + "VERSION", "lib/cstruct.rb", "lib/logan.rb", + "lib/logan/cache.rb", + "lib/logan/inc.rb", + "lib/logan/inc/command.rb", + "lib/logan/inc/fileparser.rb", + "lib/logan/inc/main.rb", + "lib/logan/inc/server.rb", + "lib/logan/loglines.rb", "lib/logan/types/syslog.rb" ] s.homepage = %q{http://github.com/DenisKnauf/logan} s.rdoc_options = ["--charset=UTF-8"] - s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.5} + s.require_paths = ["bin", "lib"] + s.rubygems_version = %q{1.3.6} s.summary = %q{Logdata analysing database} if s.respond_to? :specification_version then @@ -32,9 +43,12 @@ Gem::Specification.new do |s| s.specification_version = 3 if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + s.add_runtime_dependency(%q<["robustserver", "sbdb"]>, [">= 0"]) else + s.add_dependency(%q<["robustserver", "sbdb"]>, [">= 0"]) end else + s.add_dependency(%q<["robustserver", "sbdb"]>, [">= 0"]) end end