still buggy

This commit is contained in:
Denis Knauf 2010-03-29 11:50:59 +02:00
parent 313be57c1d
commit a5aaab685e
11 changed files with 160 additions and 132 deletions

View file

@ -11,8 +11,8 @@ begin
gem.homepage = "http://github.com/DenisKnauf/logan" gem.homepage = "http://github.com/DenisKnauf/logan"
gem.authors = ["Denis Knauf"] gem.authors = ["Denis Knauf"]
gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb] gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb]
gem.require_paths = %w[lib] gem.require_paths = %w[bin lib]
gem.add_dependency 'sbdb' gem.add_dependency %w[robustserver sbdb]
end end
Jeweler::GemcutterTasks.new Jeweler::GemcutterTasks.new
rescue LoadError rescue LoadError

31
bin/conf.rb Executable file
View file

@ -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

View file

@ -1,34 +1,15 @@
#!/usr/bin/ruby #!/usr/bin/ruby
require 'sbdb' require 'logan'
require 'safebox' require 'logan/inc'
require 'robustserver'
class LogAn::Main < RobustServer opts = {}
def initialize conf opts[:server] = if ARGV[1]
super ARGV
@conf = conf elsif ARGV[0]
@logs = LogAn::Loglines.new 'logs' ['localhost', ARGV[1]]
Dir.mkdir 'etc' rescue Errno::EEXIST else %w[localhost 1087]
@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)
end end
opts[:server][1] = opts[:server][1].to_i
def at_exit LogAn::Inc::Main.main *opts
@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

View file

@ -2,8 +2,9 @@
require 'logan/inc/server' require 'logan/inc/server'
require 'logan/inc/fileparser' require 'logan/inc/fileparser'
require 'logan/inc/command' require 'logan/inc/command'
require 'logan/inc/main'
module Logan module LogAn
module Inc module Inc
end end
end end

View file

@ -1,5 +1,5 @@
module Logan module LogAn
module Inc module Inc
class Command < ::Array class Command < ::Array
attr_reader :sid attr_reader :sid

View file

@ -1,5 +1,5 @@
module Logan module LogAn
module Inc module Inc
module FileParser module FileParser
module Base module Base

View file

@ -2,13 +2,16 @@
require 'sbdb' require 'sbdb'
require 'safebox' require 'safebox'
require 'robustserver' require 'robustserver'
require 'logan/inc'
require 'logan/loglines'
module LogAn::Inc module LogAn::Inc
class Main < RobustServer class Main < RobustServer
# Open Config. # Open Config.
def config env, db, type = nil def config env, db, type = nil
$stderr.puts "Open Database "sids.cnf" #{db.inspect} (#{type.inspect})"
type ||= 1+4 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 = AutoValueConvertHash.new ret if type&4 > 0
ret = Cache.new ret, type&3 if type&3 > 0 ret = Cache.new ret, type&3 if type&3 > 0
ret ret
@ -27,7 +30,7 @@ module LogAn::Inc
super super
@conf = {} @conf = {}
# Copy config - changes possible # Copy config - changes possible
conf.each &@conf.method(:[]=) conf.each {|key, val| @conf[key]= val }
# Default directories # Default directories
%w[logs etc].each {|key| @conf[key.to_sym] = key } %w[logs etc].each {|key| @conf[key.to_sym] = key }
# Open Loglines-databases # Open Loglines-databases

View file

@ -1,106 +1,104 @@
require 'select' require 'select'
module Logan module LogAn
module Inc module Inc
class Select <::Select end
attr_reader :entries end
def initialize *p
super *p
@entries=[]
end
def run class LogAn::Inc::Select <::Select
until @exit || (@exit_on_empty && self.empty?) attr_reader :entries
cron def initialize *p
run_once 1 super *p
end @entries=[]
end end
def cron def run
@entries.each do |e| until @exit || (@exit_on_empty && self.empty?)
return if e > Time.now cron
e.call run_once 1
@entries.shift end
end end
end
class Entry < Time def cron
attr_reader :do @entries.each do |e|
def do &e return if e > Time.now
@do = e e.call
end @entries.shift
end
end
def call *p class Entry < Time
@do.call *p attr_reader :do
end def do &e
@do = e
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
end end
class Socket <::Select::Socket def call *p
def event_read sock = @sock, event = :read @do.call *p
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 end
class Server < ::Select::Server def self.new *a, &e
attr_reader :config x = self.at *a
x.do &e
x
end
end
def init opts def at a, &e
super opts a = Entry.new a, &e if e
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!") @entries << a
end @entries.sort!
end
end
def event_new_client sock class LogAn::Inc::Socket <::Select::Socket
{ :clientclass => LogAn::Inc::Server::Socket, :config => @config } def event_read sock = @sock, event = :read
end begin
@linebuf += sock.readpartial( @bufsize)
class Socket < LogAn::Inc::Server::Socket rescue EOFError
attr_reader :config self.event_eof sock
rescue Errno::EPIPE => e
def init opts self.event_errno e, sock, event
super opts rescue IOError
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!") self.event_ioerror sock, event
end rescue Errno::ECONNRESET => e
self.event_errno e, sock, event
def event_cmd cmd end
sid, line = cmd.unpack 'Na*' loop do
begin return if @linebuf.size < 4
@config[ :fileparser][sid].event_line line, self l = @linebuf.unpack( 'N')[0]
rescue Didi::Config::NoSIDFound, Didi::Config::SIDLoadingError return if l > @linebuf.length
$stderr.puts( {:sid => sid, :exception => $!, :backtrace => $!.backtrace}.inspect) @linebuf.remove 4
end event_cmd @linebuf.remove( l)
end end
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 end
end end

View file

@ -29,7 +29,7 @@ module LogAn
@rdb = @env[ 'rotates.db', :type => SBDB::Btree, :flags => SBDB::CREATE | SBDB::AUTO_COMMIT] @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] @queue = @env[ "newids.queue", :type => SBDB::Queue, :flags => SBDB::CREATE | SBDB::AUTO_COMMIT, :re_len => 16]
@dbs = {} @dbs = {}
self.hash = lambda {|k| self.hash_func = lambda {|k|
[k.timestamp.to_i/60/60].pack 'N' # Hour-based rotation [k.timestamp.to_i/60/60].pack 'N' # Hour-based rotation
} }
end end

View file

@ -1,3 +1,3 @@
require 'logan' require 'logan'
Logan.add CStruct.new( :line) LogAn.add CStruct.new( :line)

View file

@ -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.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Denis Knauf"] s.authors = ["Denis Knauf"]
s.date = %q{2010-02-24} s.date = %q{2010-03-29}
s.description = %q{} s.description = %q{}
s.email = %q{Denis.Knauf@gmail.com} s.email = %q{Denis.Knauf@gmail.com}
s.executables = ["box3.rb", "box.rb", "box2.rb", "loganinc"]
s.extra_rdoc_files = [ s.extra_rdoc_files = [
"LICENSE" "LICENSE",
"README.md"
] ]
s.files = [ s.files = [
"VERSION", "AUTHORS",
"README.md",
"VERSION",
"lib/cstruct.rb", "lib/cstruct.rb",
"lib/logan.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" "lib/logan/types/syslog.rb"
] ]
s.homepage = %q{http://github.com/DenisKnauf/logan} s.homepage = %q{http://github.com/DenisKnauf/logan}
s.rdoc_options = ["--charset=UTF-8"] s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"] s.require_paths = ["bin", "lib"]
s.rubygems_version = %q{1.3.5} s.rubygems_version = %q{1.3.6}
s.summary = %q{Logdata analysing database} s.summary = %q{Logdata analysing database}
if s.respond_to? :specification_version then if s.respond_to? :specification_version then
@ -32,9 +43,12 @@ Gem::Specification.new do |s|
s.specification_version = 3 s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<["robustserver", "sbdb"]>, [">= 0"])
else else
s.add_dependency(%q<["robustserver", "sbdb"]>, [">= 0"])
end end
else else
s.add_dependency(%q<["robustserver", "sbdb"]>, [">= 0"])
end end
end end