2010-03-28 14:33:49 +02:00
|
|
|
|
|
|
|
require 'sbdb'
|
|
|
|
require 'safebox'
|
|
|
|
require 'robustserver'
|
2010-03-29 19:54:29 +02:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
$stderr.puts [:store, :open, "sids.cnf", db, type].inspect
|
|
|
|
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
|
|
|
|
$stderr.puts [:config, :open, "sids.cnf", db, type].inspect
|
|
|
|
cache env[ 'sids.cnf', db, :flags => flags || SBDB::RDONLY], type, &e
|
|
|
|
end
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# 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
|
2010-03-29 01:12:22 +02:00
|
|
|
@conf = {}
|
2010-03-29 22:36:57 +02:00
|
|
|
|
2010-03-29 01:12:22 +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
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# Default directories
|
|
|
|
%w[logs etc].each {|key| @conf[key.to_sym] = key }
|
2010-03-29 22:36:57 +02:00
|
|
|
|
2010-03-29 01:12:22 +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
|
|
|
|
2010-03-29 01:12:22 +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] = {}
|
|
|
|
%w[hosts files].each {|key| configs[key.to_sym] = config( @etc, key) }
|
|
|
|
configs[:fileparser] = config( @etc, 'fileparser') {|val| Safebox.run "[#{val}]"}
|
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
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# Prepare Inc-server - create server
|
2010-03-30 00:32:50 +02:00
|
|
|
@serv = LogAn::Inc::Server.new :sock => TCPServer.new( *@conf[:server]), :config => @conf[:configs]
|
2010-03-29 22:36:57 +02:00
|
|
|
|
2010-03-29 01:12:22 +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
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# Will be called at exit. Will close all opened BDB::Env
|
2010-03-28 14:33:49 +02:00
|
|
|
def at_exit
|
2010-03-29 19:30:25 +02:00
|
|
|
$stderr.puts :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.
|
2010-03-29 01:12:22 +02:00
|
|
|
def shutdown signal = nil
|
|
|
|
$stderr.puts [:signal, signal, Signal[signal]].inspect if signal
|
2010-03-28 14:33:49 +02:00
|
|
|
@serv.close
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# Runs server. Don't use it! Use #main.
|
2010-03-28 14:33:49 +02:00
|
|
|
def run
|
|
|
|
@serv.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|