LogAn/lib/logan/inc/main.rb

131 lines
3.4 KiB
Ruby
Raw Normal View History

2010-03-28 14:33:49 +02:00
require 'sbdb'
require 'safebox'
require 'robustserver'
require 'socket'
2010-03-29 11:50:59 +02:00
require 'logan/inc'
require 'logan/loglines'
2010-03-29 18:48:16 +02:00
require 'logan/cache'
2010-03-28 14:33:49 +02:00
2010-03-31 00:51:39 +02:00
module LogAn::Logging
2010-03-31 00:59:22 +02:00
class << self
def log lvl, *txt
$stderr.puts( ([Time.now, lvl]+txt).inspect)
end
alias method_missing log
end
def method_missing *paras
self.class.log *paras
2010-03-31 00:51:39 +02:00
end
end
2010-03-28 14:33:49 +02:00
module LogAn::Inc
class Main < RobustServer
2010-03-30 01:15:03 +02:00
def cache ret, type, &e
2010-03-28 14:33:49 +02:00
type ||= 1+4
2010-03-29 22:36:57 +02:00
ret = LogAn::AutoValueConvertHash.new ret, &e if type&4 > 0 or e
2010-03-29 18:51:36 +02:00
ret = LogAn::Cache.new ret, type&3 if type&3 > 0
2010-03-28 14:33:49 +02:00
ret
end
2010-03-30 00:32:50 +02:00
# Open Store.
def store env, db, type = nil, flags = nil, &e
2010-03-31 00:51:39 +02:00
LogAn::Logging.info :store, :open, "sids.cnf", db, type
2010-03-30 00:32:50 +02:00
cache env[ 'sids.cnf', db, :flags => flags || SBDB::CREATE | SBDB::AUTO_COMMIT], type, &e
end
# Open Config.
def config env, db, type = nil, flags = nil, &e
2010-03-31 00:51:39 +02:00
LogAn::Logging.info :config, :open, "sids.cnf", db, type
2010-03-30 00:32:50 +02:00
cache env[ 'sids.cnf', db, :flags => flags || SBDB::RDONLY], type, &e
end
# Prepare Server.
#
# * conf:
# logs
# : Where to store log-databases? default: ./logs
# etc
# : Where to find config-databases? default: ./etc
# server
# : Server-Configuration. default { port: 1087 }
2010-03-28 14:33:49 +02:00
def initialize conf
super
@conf = {}
2010-03-29 22:36:57 +02:00
# Copy config - changes possible
2010-03-29 11:50:59 +02:00
conf.each {|key, val| @conf[key]= val }
2010-03-29 22:36:57 +02:00
# Default directories
%w[logs etc].each {|key| @conf[key.to_sym] = key }
2010-03-29 22:36:57 +02:00
# Open Loglines-databases
@logs = LogAn::Loglines.new @conf[:logs]
2010-03-30 00:32:50 +02:00
LogAn::Inc::FileParser::Base.logdb = @logs
2010-03-29 22:36:57 +02:00
# Open config-databases
Dir.mkdir @conf[:etc] rescue Errno::EEXIST
@etc = SBDB::Env.new( @conf[:etc],
2010-03-28 14:33:49 +02:00
log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE,
flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL)
2010-03-29 22:36:57 +02:00
2010-03-30 00:32:50 +02:00
# Open configs
begin
configs = @conf[:configs] = {}
2010-04-01 13:51:25 +02:00
%w[hosts files].each {|key| configs[key.to_sym] = config( @etc, key) {|l| l } }
configs[:fileparser] = config @etc, 'fileparser', &Safebox.method( :eval)
2010-03-30 01:18:09 +02:00
LogAn::Inc::SID0.config = configs
2010-03-30 00:32:50 +02:00
end
# Open seeks-database
2010-03-29 22:36:57 +02:00
begin
2010-03-30 00:32:50 +02:00
stores = @conf[:stores] = {}
2010-03-30 00:37:02 +02:00
db = @etc[ 'sids.store', 'seeks', SBDB::Recno, SBDB::CREATE | SBDB::AUTO_COMMIT]
db = LogAn::AutoValueConvertHash.new( db, lambda {|val| val.pack( 'NN') }) {|val| (val||0.chr*8).unpack( 'NN') }
stores[:seeks] = LogAn::Cache.new db
2010-03-30 00:32:50 +02:00
LogAn::Inc::FileParser::Base.store = LogAn::Inc::SID0.store = stores
2010-03-29 22:36:57 +02:00
end
# Select-framework
@select = LogAn::Inc::Select.new
status = lambda do
@select.at Time.now+5, &status
2010-03-31 00:51:39 +02:00
LogAn::Logging.info @select
@conf[:stores].each{|key, db| db.flush!}
end
status.call
# Prepare Inc-server - create server
@serv = LogAn::Inc::Server.new :sock => TCPServer.new( *@conf[:server]), :config => @conf[:configs], :select => @select
2010-03-31 00:51:39 +02:00
LogAn::Logging.debug @serv
2010-03-29 22:36:57 +02:00
# Shutdown on signals
@sigs[:INT] = @sigs[:TERM] = method( :shutdown)
2010-03-29 22:36:57 +02:00
2010-03-29 19:16:27 +02:00
rescue Object
# It's better to close everything, because BDB doesn't like unexpected exits
self.at_exit
2010-03-29 19:19:11 +02:00
raise $!
2010-03-28 14:33:49 +02:00
end
# Will be called at exit. Will close all opened BDB::Env
2010-03-28 14:33:49 +02:00
def at_exit
2010-03-31 00:51:39 +02:00
LogAn::Logging.info :at_exit
2010-03-28 14:33:49 +02:00
@logs and @logs.close
@etc and @etc.close
end
2010-03-29 19:19:11 +02:00
# Shutdown Server cleanly. First shutdown TCPServer.
def shutdown signal = nil
2010-03-31 00:52:34 +02:00
LogAn::Logging.info :signal, signal, Signal[signal] if signal
2010-03-28 14:33:49 +02:00
@serv.close
exit 0
end
# Runs server. Don't use it! Use #main.
2010-03-28 14:33:49 +02:00
def run
@serv.run
end
end
end