def run(): log.startLogging(sys.stdout) parser = argparse.ArgumentParser() parser.add_argument('-admin', '--admin', action='store_true', default=False, dest='admin', help='run admin') parser.add_argument('-initdb', '--initdb', action='store_true', default=False, dest='initdb', help='run initdb') parser.add_argument('-debug', '--debug', action='store_true', default=False, dest='debug', help='debug option') parser.add_argument('-c', '--conf', type=str, default="/etc/toughtester.json", dest='conf', help='config file') args = parser.parse_args(sys.argv[1:]) config = iconfig.find_config(args.conf) update_timezone(config) check_env(config) if args.debug: config.system.debug = True syslog = logger.Logger(config) dbengine = get_engine(config) dispatch.register(syslog) if args.admin: start_admin(config,dbengine=dbengine) start_authorized(config, dbengine) reactor.run() elif args.initdb: start_initdb(config) else: parser.print_help()
def run(): log.startLogging(sys.stdout) parser = argparse.ArgumentParser() parser.add_argument('-manage', '--manage', action='store_true', default=False, dest='manage', help='run manage') parser.add_argument('-initdb', '--initdb', action='store_true', default=False, dest='initdb', help='run initdb') parser.add_argument('-port', '--port', type=int, default=0, dest='port', help='admin port') parser.add_argument('-debug', '--debug', action='store_true', default=False, dest='debug', help='debug option') parser.add_argument('-x', '--xdebug', action='store_true', default=False, dest='xdebug', help='xdebug option') parser.add_argument('-c', '--conf', type=str, default="/etc/toughnms.json", dest='conf', help='config file') args = parser.parse_args(sys.argv[1:]) config = iconfig.find_config(args.conf) syslog = logger.Logger(config) dbengine = get_engine(config) dispatch.register(syslog) with open("/var/toughnms/token","wb") as tf: tf.write(md5(config.system.secret.encode('utf-8')).hexdigest()) update_timezone(config) check_env(config) if args.debug or args.xdebug: config.defaults.debug = True if args.port > 0: config.server.port = int(args.port) if args.manage: from toughnms.console import admin_app admin_app.run(config,dbengine) reactor.run() if args.initdb: init_db.update(get_engine(config))
def load_events(event_path=None, pkg_prefix=None, excludes=[], event_params={}): _excludes = ["__init__", "settings"] + excludes evs = set(os.path.splitext(it)[0] for it in os.listdir(event_path)) evs = [it for it in evs if it not in _excludes] for ev in evs: try: sub_module = os.path.join(event_path, ev) if os.path.isdir(sub_module): # logger.info('load sub event %s' % ev) load_events( event_path=sub_module, pkg_prefix="{0}.{1}".format(pkg_prefix, ev), excludes=excludes, event_params=event_params, ) _ev = "{0}.{1}".format(pkg_prefix, ev) # logger.info('load_event %s with params:%s' % (_ev,repr(event_params))) robj = importlib.import_module(_ev) if hasattr(robj, "evobj"): dispatch.register(robj.evobj) if hasattr(robj, "__call__"): dispatch.register(robj.__call__(**event_params)) except Exception as err: logger.error("%s, skip module %s.%s" % (str(err), pkg_prefix, ev)) import traceback traceback.print_exc() continue
def load_events(event_path=None, pkg_prefix=None, excludes=[], event_params={}): _excludes = ['__init__', 'settings'] + excludes evs = set(os.path.splitext(it)[0] for it in os.listdir(event_path)) evs = [it for it in evs if it not in _excludes] for ev in evs: try: sub_module = os.path.join(event_path, ev) if os.path.isdir(sub_module): # logger.info('load sub event %s' % ev) load_events( event_path=sub_module, pkg_prefix="{0}.{1}".format(pkg_prefix, ev), excludes=excludes, event_params=event_params, ) _ev = "{0}.{1}".format(pkg_prefix, ev) # logger.info('load_event %s with params:%s' % (_ev,repr(event_params))) robj = importlib.import_module(_ev) if hasattr(robj, 'evobj'): dispatch.register(robj.evobj) if hasattr(robj, '__call__'): dispatch.register(robj.__call__(**event_params)) except Exception as err: logger.error("%s, skip module %s.%s" % (str(err), pkg_prefix, ev)) import traceback traceback.print_exc() continue
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config self.db_engine = dbengine or get_engine(config, pool_size=20) self.aes = kwargs.pop("aes", None) self.cache = kwargs.pop( "cache", CacheManager(redis_conf(config), cache_name='RadiusTaskCache-%s' % os.getpid())) self.cache.print_hit_stat(300) self.db = scoped_session( sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) self.taskclss = [] self.load_tasks() if not kwargs.get('standalone'): logger.info("start register taskd events") dispatch.register(log_trace.LogTrace(redis_conf(config)), check_exists=True) event_params = dict(dbengine=self.db_engine, mcache=self.cache, aes=self.aes) event_path = os.path.abspath( os.path.dirname(toughradius.manage.events.__file__)) dispatch.load_events(event_path, "toughradius.manage.events", event_params=event_params)
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config settings = dict( cookie_secret="12oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", login_url="/admin/login", template_path=os.path.join(os.path.dirname(toughradius.__file__), "views"), static_path=os.path.join(os.path.dirname(toughradius.__file__), "static"), xsrf_cookies=True, config=self.config, debug=self.config.system.debug, xheaders=True, ) self.tp_lookup = TemplateLookup( directories=[settings['template_path']], default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8', encoding_errors='ignore', module_directory="/tmp/toughradius_admin" ) self.db_engine = dbengine or get_engine(config) self.db = scoped_session(sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) self.session_manager = session.SessionManager(settings["cookie_secret"], self.db_engine, 600) redisconf = redis_conf(config) self.session_manager = redis_session.SessionManager(redisconf,settings["cookie_secret"], 600) self.mcache = redis_cache.CacheManager(redisconf,cache_name='RadiusManageCache-%s'%os.getpid()) self.mcache.print_hit_stat(60) self.db_backup = DBBackup(models.get_metadata(self.db_engine), excludes=[ 'tr_online','system_session','system_cache','tr_ticket','tr_billing','tr_online_stat', 'tr_flow_stat' ]) self.aes = utils.AESCipher(key=self.config.system.secret) # cache event init dispatch.register(self.mcache) # app init_route load_handlers(handler_path=os.path.join(os.path.abspath(os.path.dirname(__file__))), pkg_prefix="toughradius.manage", excludes=['views','webserver','radius']) # app event init event_params= dict(dbengine=self.db_engine, mcache=self.mcache, aes=self.aes) dispatch.load_events(os.path.join(os.path.abspath(os.path.dirname(toughradius.manage.events.__file__))), "toughradius.manage.events",event_params=event_params) permit.add_route(cyclone.web.StaticFileHandler, r"/admin/backup/download/(.*)", u"下载数据",MenuSys, handle_params={"path": self.config.database.backup_path}, order=5.0005) cyclone.web.Application.__init__(self, permit.all_handlers, **settings)
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config settings = dict( cookie_secret="12oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", login_url="/login", template_path=os.path.join(os.path.dirname(toughnms.__file__), "views"), static_path=os.path.join(os.path.dirname(toughnms.__file__), "static"), xsrf_cookies=True, config=self.config, debug=self.config.system.debug, xheaders=True, ) self.tp_lookup = TemplateLookup( directories=[settings['template_path']], default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8', encoding_errors='ignore', module_directory="/tmp/toughnms" ) self.db_engine = dbengine or get_engine(config) self.db = scoped_session(sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) self.session_manager = session.SessionManager(settings["cookie_secret"], self.db_engine, 600) self.mcache = cache.CacheManager(self.db_engine) self.nagapi = NagiosApi(self.config) self.mongodb = MongoDB(self.config.database.mongodb_url,self.config.database.mongodb_port) self.aes = utils.AESCipher(key=self.config.system.secret) # cache event init dispatch.register(self.mcache) # app init_route load_handlers(handler_path=os.path.join(os.path.abspath(os.path.dirname(__file__))), pkg_prefix="toughnms.console", excludes=['base','webserver','radius']) self.init_route_permit() # app event init # event_params= dict(dbengine=self.db_engine, mcache=self.mcache, aes=self.aes) # load_events(os.path.join(os.path.abspath(os.path.dirname(toughnms.manage.events.__file__))), # "toughnms.manage.events", excludes=[],event_params=event_params) # for g in self.nagapi.list_hostgroup(): # for host in g.get_effective_hosts(): # permit.add_route(host_perf.HostPerfDataHandler, # r"/gperfdata?group_name=%s&host_name=%s"%(g.hostgroup_name,host.host_name), # utils.safeunicode(host.alias), # MenuRes, # is_menu=True, # order=7.0005) cyclone.web.Application.__init__(self, permit.all_handlers, **settings)
def run(): log.startLogging(sys.stdout) parser = argparse.ArgumentParser() parser.add_argument('-manage', '--manage', action='store_true', default=False, dest='manage', help='run manage') parser.add_argument('-task', '--task', action='store_true', default=False, dest='task', help='run task') parser.add_argument('-auth', '--auth', action='store_true', default=False, dest='auth', help='run auth') parser.add_argument('-acct', '--acct', action='store_true', default=False, dest='acct', help='run acct') parser.add_argument('-worker', '--worker', action='store_true', default=False, dest='worker', help='run worker') parser.add_argument('-standalone', '--standalone', action='store_true', default=False, dest='standalone', help='run standalone') parser.add_argument('-initdb', '--initdb', action='store_true', default=False, dest='initdb', help='run initdb') parser.add_argument('-debug', '--debug', action='store_true', default=False, dest='debug', help='debug option') parser.add_argument('-c', '--conf', type=str, default="/etc/toughradius.json", dest='conf', help='config file') args = parser.parse_args(sys.argv[1:]) config = iconfig.find_config(args.conf) syslog = logger.Logger(config) dbengine = get_engine(config) dispatch.register(syslog) update_timezone(config) check_env(config) if args.debug: config.defaults.debug = True if args.manage: httpd.run(config,dbengine) reactor.run() elif args.auth: radiusd.run_auth(config) reactor.run() elif args.acct: radiusd.run_acct(config) reactor.run() elif args.worker: radiusd.run_worker(config,dbengine) reactor.run() elif args.task: taskd.run(config,dbengine) reactor.run() elif args.standalone: httpd.run(config,dbengine) radiusd.run_auth(config) radiusd.run_acct(config) radiusd.run_worker(config,dbengine) taskd.run(config,dbengine) reactor.run() elif args.initdb: run_initdb(config) else: parser.print_help()
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config settings = dict( cookie_secret="12oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", login_url="/admin/login", template_path=os.path.join(os.path.dirname(toughradius.__file__), "views"), static_path=os.path.join(os.path.dirname(toughradius.__file__), "static"), xsrf_cookies=True, config=self.config, debug=self.config.system.debug, xheaders=True, ) self.tp_lookup = TemplateLookup( directories=[settings['template_path']], default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8', encoding_errors='ignore', module_directory="/tmp/toughradius_admin" ) self.db_engine = dbengine or get_engine(config) self.db = scoped_session(sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) self.session_manager = session.SessionManager(settings["cookie_secret"], self.db_engine, 600) redisconf = redis_conf(config) self.session_manager = redis_session.SessionManager(redisconf,settings["cookie_secret"], 600) self.mcache = redis_cache.CacheManager(redisconf,cache_name='RadiusManageCache-%s'%os.getpid()) self.mcache.print_hit_stat(60) self.db_backup = DBBackup(models.get_metadata(self.db_engine), excludes=[ 'tr_online','system_session','system_cache','tr_ticket']) self.aes = utils.AESCipher(key=self.config.system.secret) # cache event init dispatch.register(self.mcache) # app init_route load_handlers(handler_path=os.path.join(os.path.abspath(os.path.dirname(__file__))), pkg_prefix="toughradius.manage", excludes=['views','webserver','radius']) # app event init event_params= dict(dbengine=self.db_engine, mcache=self.mcache, aes=self.aes) dispatch.load_events(os.path.join(os.path.abspath(os.path.dirname(toughradius.manage.events.__file__))), "toughradius.manage.events",event_params=event_params) permit.add_route(cyclone.web.StaticFileHandler, r"/admin/backup/download/(.*)", u"下载数据",MenuSys, handle_params={"path": self.config.database.backup_path}, order=5.0005) cyclone.web.Application.__init__(self, permit.all_handlers, **settings)
def run_worker(config,dbengine,**kwargs): _cache = kwargs.pop("cache",CacheManager(redis_conf(config),cache_name='RadiusWorkerCache-%s'%os.getpid())) _cache.print_hit_stat(120) # app event init if not kwargs.get('standalone'): logger.info("start register radiusd events") dispatch.register(log_trace.LogTrace(redis_conf(config)),check_exists=True) event_params= dict(dbengine=dbengine, mcache=_cache, aes=kwargs.pop('aes',None)) event_path = os.path.abspath(os.path.dirname(toughradius.manage.events.__file__)) dispatch.load_events(event_path,"toughradius.manage.events",event_params=event_params) logger.info('start radius worker: %s' % RADIUSAuthWorker(config,dbengine,radcache=_cache)) logger.info('start radius worker: %s' % RADIUSAcctWorker(config,dbengine,radcache=_cache))
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config self.db_engine = dbengine or get_engine(config) self.cache = cache.CacheManager(self.db_engine) self.db = scoped_session(sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) # init task self.expire_notify_task = expire_notify.ExpireNotifyTask(self) self.ddns_update_task = ddns_update.DdnsUpdateTask(self) self.radius_stat_task = radius_stat.RadiusStatTask(self) dispatch.register(radius_events.__call__(self.db_engine,self.cache))
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config self.db_engine = dbengine or get_engine(config,pool_size=20) self.cache = cache.CacheManager(self.db_engine,cache_name='RadiusTaskCache-%s'%os.getpid()) self.db = scoped_session(sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) # init task self.expire_notify_task = expire_notify.ExpireNotifyTask(self) self.ddns_update_task = ddns_update.DdnsUpdateTask(self) self.radius_stat_task = radius_stat.RadiusStatTask(self) self.online_stat_task = online_stat.OnlineStatTask(self) self.flow_stat_task = flow_stat.FlowStatTask(self) dispatch.register(radius_events.__call__(self.db_engine,self.cache))
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config self.db_engine = dbengine or get_engine(config,pool_size=20) self.aes = kwargs.pop("aes",None) self.cache = kwargs.pop("cache",CacheManager(redis_conf(config),cache_name='RadiusTaskCache-%s'%os.getpid())) self.cache.print_hit_stat(300) self.db = scoped_session(sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) self.taskclss = [] self.load_tasks() if not kwargs.get('standalone'): logger.info("start register taskd events") dispatch.register(log_trace.LogTrace(redis_conf(config)),check_exists=True) event_params= dict(dbengine=self.db_engine, mcache=self.cache,aes=self.aes) event_path = os.path.abspath(os.path.dirname(toughradius.manage.events.__file__)) dispatch.load_events(event_path,"toughradius.manage.events",event_params=event_params)
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config self.db_engine = dbengine or get_engine(config) self.cache = cache.CacheManager(self.db_engine) self.db = scoped_session( sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) # init task self.expire_notify_task = expire_notify.ExpireNotifyTask(self) self.ddns_update_task = ddns_update.DdnsUpdateTask(self) self.radius_stat_task = radius_stat.RadiusStatTask(self) self.online_stat_task = online_stat.OnlineStatTask(self) self.flow_stat_task = flow_stat.FlowStatTask(self) dispatch.register(radius_events.__call__(self.db_engine, self.cache))
def __init__(self, config=None, dbengine=None, **kwargs): self.config = config self.db_engine = dbengine or get_engine(config, pool_size=20) redisconf = settings.redis_conf(config) self.cache = redis_cache.CacheManager(redisconf, cache_name='RadiusTaskCache-%s' % os.getpid()) self.cache.print_hit_stat(60) self.db = scoped_session( sessionmaker(bind=self.db_engine, autocommit=False, autoflush=False)) # init task self.expire_notify_task = expire_notify.ExpireNotifyTask(self) self.ddns_update_task = ddns_update.DdnsUpdateTask(self) self.radius_stat_task = radius_stat.RadiusStatTask(self) self.online_stat_task = online_stat.OnlineStatTask(self) self.flow_stat_task = flow_stat.FlowStatTask(self) dispatch.register(radius_events.__call__(self.db_engine, self.cache))
def load_events(self,event_path=None,pkg_prefix=None): _excludes = ['__init__','settings'] evs = set(os.path.splitext(it)[0] for it in os.listdir(event_path)) evs = [it for it in evs if it not in _excludes] for ev in evs: try: sub_module = os.path.join(event_path, ev) if os.path.isdir(sub_module): dispatch.pub(logger.EVENT_INFO,'load sub event %s' % ev) self.load_events( event_path=sub_module, pkg_prefix="{0}.{1}".format(pkg_prefix, ev) ) _ev = "{0}.{1}".format(pkg_prefix, ev) dispatch.pub(logger.EVENT_INFO,'load_event %s' % _ev) dispatch.register(importlib.import_module(_ev).__call__( dbengine=self.db_engine, mcache=self.mcache)) except Exception as err: dispatch.pub(logger.EVENT_EXCEPTION,err) dispatch.pub(logger.EVENT_ERROR,"%s, skip event %s.%s" % (str(err),pkg_prefix,ev)) continue
def load_events(self, event_path=None, pkg_prefix=None): _excludes = ['__init__', 'settings'] evs = set(os.path.splitext(it)[0] for it in os.listdir(event_path)) evs = [it for it in evs if it not in _excludes] for ev in evs: try: sub_module = os.path.join(event_path, ev) if os.path.isdir(sub_module): dispatch.pub(logger.EVENT_INFO, 'load sub event %s' % ev) self.load_events(event_path=sub_module, pkg_prefix="{0}.{1}".format( pkg_prefix, ev)) _ev = "{0}.{1}".format(pkg_prefix, ev) dispatch.pub(logger.EVENT_INFO, 'load_event %s' % _ev) dispatch.register( importlib.import_module(_ev).__call__( dbengine=self.db_engine, mcache=self.mcache)) except Exception as err: dispatch.pub(logger.EVENT_EXCEPTION, err) dispatch.pub( logger.EVENT_ERROR, "%s, skip event %s.%s" % (str(err), pkg_prefix, ev)) continue
def run(): log.startLogging(sys.stdout) parser = argparse.ArgumentParser() parser.add_argument('-manage', '--manage', action='store_true', default=False, dest='manage', help='run manage') parser.add_argument('-task', '--task', action='store_true', default=False, dest='task', help='run task') parser.add_argument('-auth', '--auth', action='store_true', default=False, dest='auth', help='run auth') parser.add_argument('-acct', '--acct', action='store_true', default=False, dest='acct', help='run acct') parser.add_argument('-standalone', '--standalone', action='store_true', default=False, dest='standalone', help='run standalone') parser.add_argument('-initdb', '--initdb', action='store_true', default=False, dest='initdb', help='run initdb') parser.add_argument('-debug', '--debug', action='store_true', default=False, dest='debug', help='debug option') parser.add_argument('-c', '--conf', type=str, default="/etc/toughradius.json", dest='conf', help='config file') args = parser.parse_args(sys.argv[1:]) config = iconfig.find_config(args.conf) syslog = logger.Logger(config) dbengine = get_engine(config) dispatch.register(syslog) update_timezone(config) check_env(config) if args.debug: config.defaults.debug = True if args.manage: webserver.run(config, dbengine) reactor.run() elif args.auth: radiusd.run_auth(config, dbengine) reactor.run() elif args.acct: radiusd.run_acct(config, dbengine) reactor.run() elif args.task: taskd.run(config, dbengine) reactor.run() elif args.standalone: radiusd.run_auth(config, dbengine) radiusd.run_acct(config, dbengine) webserver.run(config, dbengine) taskd.run(config, dbengine) reactor.run() elif args.initdb: run_initdb(config) else: parser.print_help()
def run(): log.startLogging(sys.stdout) parser = argparse.ArgumentParser() parser.add_argument('-manage', '--manage', action='store_true', default=False, dest='manage', help='run manage') parser.add_argument('-task', '--task', action='store_true', default=False, dest='task', help='run task') parser.add_argument('-auth', '--auth', action='store_true', default=False, dest='auth', help='run auth') parser.add_argument('-acct', '--acct', action='store_true', default=False, dest='acct', help='run acct') parser.add_argument('-worker', '--worker', action='store_true', default=False, dest='worker', help='run worker') parser.add_argument('-standalone', '--standalone', action='store_true', default=False, dest='standalone', help='run standalone') parser.add_argument('-initdb', '--initdb', action='store_true', default=False, dest='initdb', help='run initdb') parser.add_argument('-debug', '--debug', action='store_true', default=False, dest='debug', help='debug option') parser.add_argument('-exitwith', '--exitwith', type=float, default=0, dest='exitwith', help='exitwith option') parser.add_argument('-c', '--conf', type=str, default="/etc/toughradius.json", dest='conf', help='config file') args = parser.parse_args(sys.argv[1:]) config = iconfig.find_config(args.conf) syslog = logger.Logger(config) dbengine = get_engine(config) dispatch.register(syslog) update_timezone(config) check_env(config) if args.debug: config.defaults.debug = True if args.manage: httpd.run(config, dbengine) reactor.run() elif args.auth: radiusd.run_auth(config) reactor.run() elif args.acct: radiusd.run_acct(config) reactor.run() elif args.worker: radiusd.run_worker(config, dbengine) reactor.run() elif args.task: taskd.run(config, dbengine) reactor.run() elif args.standalone: httpd.run(config, dbengine) radiusd.run_auth(config) radiusd.run_acct(config) radiusd.run_worker(config, dbengine) taskd.run(config, dbengine) if args.exitwith > 0: log.msg("testing application running and exit after %s seconds" % args.exitwith) reactor.callLater(args.exitwith, reactor.stop) reactor.run() elif args.initdb: run_initdb(config) else: parser.print_help()