def process(self, *args, **kwargs): with make_db(self.db) as db: try: nodes = db.query(models.TrNode) for node in nodes: r = db.query( func.sum( models.TrOnline.input_total).label("input_total"), func.sum(models.TrOnline.output_total).label( "output_total")).filter( models.TrOnline.account_number == models.TrAccount.account_number, models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrCustomer.node_id == node.id).first() if r and all([r.input_total, r.output_total]): stat = models.TrFlowStat() stat.node_id = node.id stat.stat_time = int(time.time()) stat.input_total = r.input_total stat.output_total = r.output_total db.add(stat) db.commit() logger.info("flow stat task done") except Exception as err: db.rollback() logger.error('flow_stat_job err,%s' % (str(err))) return 120.0
def process(self, *args, **kwargs): with make_db(self.db) as db: try: nas_list = db.query(models.TrBas) for nas in nas_list: if not nas.dns_name: continue results, _, _ = yield client.lookupAddress(nas.dns_name) if not results: logger.info("domain {0} resolver empty".format( nas.dns_name)) if results[0].type == dns.A: ipaddr = ".".join( str(i) for i in struct.unpack( "BBBB", results[0].payload.address)) if ipaddr: nas.ip_addr = ipaddr db.commit() logger.info( "domain {0} resolver {1} success".format( nas.dns_name, ipaddr)) else: logger.info("domain {0} no ip address,{1}".format( nas.dns_name, repr(results))) except Exception as err: logger.error('ddns process error %s' % utils.safeunicode(err.message)) defer.returnValue(60)
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: nodes = db.query(models.TrNode) for node in nodes: online_count = db.query(models.TrOnline.id).filter( models.TrOnline.account_number == models.TrAccount.account_number, models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrCustomer.node_id == node.id).count() stat = models.TrOnlineStat() stat.node_id = node.id stat.stat_time = int(time.time()) stat.total = online_count db.add(stat) # clean expire data _time = int(time.time()) - (86400 * 2) db.query(models.TrOnlineStat).filter( models.TrOnlineStat.stat_time < _time).delete() db.commit() logger.info("online stat task done") except Exception as err: db.rollback() logger.exception(err) return self.get_notify_interval()
def process(self, *args, **kwargs): dispatch.pub(logger.EVENT_INFO, "process ddns update task..") with make_db(self.db) as db: try: nas_list = db.query(models.TrBas) for nas in nas_list: if not nas.dns_name: continue results, _, _ = yield client.lookupAddress(nas.dns_name) if not results: dispatch.pub(logger.EVENT_INFO, "domain {0} resolver empty".format(nas.dns_name)) if results[0].type == dns.A: ipaddr = ".".join(str(i) for i in struct.unpack("BBBB", results[0].payload.address)) if ipaddr: nas.ip_addr = ipaddr db.commit() dispatch.pub( logger.EVENT_INFO, "domain {0} resolver {1} success".format(nas.dns_name, ipaddr) ) else: dispatch.pub( logger.EVENT_INFO, "domain {0} no ip address,{1}".format(nas.dns_name, repr(results)) ) except Exception as err: dispatch.pub(logger.EVENT_ERROR, "ddns process error %s" % utils.safeunicode(err.message)) defer.returnValue(60)
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: onlines = db.query(models.TrOnline) for online in onlines: acct_start_time = datetime.datetime.strptime( online.acct_start_time, '%Y-%m-%d %H:%M:%S') acct_session_time = online.billing_times nowdate = datetime.datetime.now() dt = nowdate - acct_start_time online_times = dt.total_seconds() max_interim_intelval = int( self.get_param_value('radius_acct_interim_intelval', 240)) if (online_times - acct_session_time) > (max_interim_intelval + 30): logger.info( "online %s overtime, system auto clear this online" % online.account_number) dispatch.pub(CLEAR_ONLINE_EVENT, online.account_number, online.nas_addr, online.acct_session_id, async=True) except Exception as err: db.rollback() logger.exception(err) return self.get_notify_interval()
def process(self, *args, **kwargs): with make_db(self.db) as db: try: nodes = db.query(models.TrNode) for node in nodes: online_count = ( db.query(models.TrOnline.id) .filter( models.TrOnline.account_number == models.TrAccount.account_number, models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrCustomer.node_id == node.id, ) .count() ) stat = models.TrOnlineStat() stat.node_id = node.id stat.stat_time = int(time.time()) stat.total = online_count db.add(stat) db.commit() logger.info("online stat task done") except Exception as err: db.rollback() logger.error("online_stat_job err,%s" % (str(err))) return 120.0
def process(self, *args, **kwargs): next_interval = self.get_notify_interval() user_total = 0 online_total = 0 with make_db(self.db) as db: try: user_total = db.query(models.TrAccount).count() online_total = db.query(models.TrOnline).count() except Exception as err: pass try: api_url = "https://www.toughcloud.net/api/v1/ping" api_token = yield tools.get_sys_token() params = dict( token=api_token, app="toughradius", ver=__version__, release=self.config.system.get('release', "standard"), unum=user_total, onum=online_total, dist=' '.join(pf.linux_distribution()), ) param_str = urlencode(params) resp = yield httpclient.fetch(api_url + "?" + param_str, followRedirect=True) logger.info("toughcloud ping resp code: %s" % resp.code) except Exception as err: logger.error(err) defer.returnValue(next_interval)
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: nodes = db.query(models.TrNode) for node in nodes: online_count = db.query(models.TrOnline.id).filter( models.TrOnline.account_number == models.TrAccount.account_number, models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrCustomer.node_id == node.id ).count() stat = models.TrOnlineStat() stat.node_id = node.id stat.stat_time = int(time.time()) stat.total = online_count db.add(stat) # clean expire data _time = int(time.time()) - (86400 * 2) db.query(models.TrOnlineStat).filter(models.TrOnlineStat.stat_time < _time).delete() db.commit() logger.info("online stat task done") except Exception as err: db.rollback() logger.exception(err) return self.get_notify_interval()
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: onlines = db.query(models.TrOnline) for online in onlines: acct_start_time = datetime.datetime.strptime( online.acct_start_time, '%Y-%m-%d %H:%M:%S') nowdate = datetime.datetime.now() dt = nowdate - acct_start_time online_times = dt.total_seconds() max_session_time = int( self.get_param_value('radius_max_session_timeout', 86400)) if online_times > (max_session_time): logger.info( "online %s overtime, system auto disconnect this online" % online.account_number) dispatch.pub(UNLOCK_ONLINE_EVENT, online.account_number, online.nas_addr, online.acct_session_id, async=True) logger.info("online overtime check task done") except Exception as err: db.rollback() logger.error('online overtime check job err,%s' % (str(err))) return self.get_notify_interval()
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: nodes = db.query(models.TrNode) for node in nodes: r = db.query( func.sum(models.TrOnline.input_total).label("input_total"), func.sum(models.TrOnline.output_total).label("output_total") ).filter( models.TrOnline.account_number == models.TrAccount.account_number, models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrCustomer.node_id == node.id ).first() if r and all([r.input_total,r.output_total]): stat = models.TrFlowStat() stat.node_id = node.id stat.stat_time = int(time.time()) stat.input_total = r.input_total stat.output_total = r.output_total db.add(stat) # clean expire data _time = int(time.time()) - (86400 * 2) db.query(models.TrFlowStat).filter(models.TrFlowStat.stat_time < _time).delete() db.commit() logger.info("flow stat task done") except Exception as err: db.rollback() logger.exception(err) return self.get_notify_interval()
def process(self, *args, **kwargs): next_interval = self.get_notify_interval() user_total = 0 online_total = 0 with make_db(self.db) as db: try: user_total = db.query(models.TrAccount).count() online_total = db.query(models.TrOnline).count() except Exception as err: pass try: api_url = "https://www.toughcloud.net/api/v1/ping" api_token = yield tools.get_sys_token() params = dict( token=api_token, app="toughradius", ver=__version__, release=self.config.system.get('release',"standard"), unum=user_total, onum=online_total, dist=' '.join(pf.linux_distribution()), ) param_str = urlencode(params) resp = yield httpclient.fetch(api_url+"?"+param_str,followRedirect=True) logger.info("toughcloud ping resp code: %s"%resp.code) except Exception as err: logger.error(err) defer.returnValue(next_interval)
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: nodes = db.query(models.TrNode) for node in nodes: r = db.query( func.sum(models.TrOnline.input_total).label("input_total"), func.sum(models.TrOnline.output_total).label("output_total") ).filter( models.TrOnline.account_number == models.TrAccount.account_number, models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrCustomer.node_id == node.id ).first() if r and all([r.input_total,r.output_total]): stat = models.TrFlowStat() stat.node_id = node.id stat.stat_time = int(time.time()) stat.input_total = r.input_total stat.output_total = r.output_total db.add(stat) # clean expire data _time = int(time.time()) - (86400 * 2) db.query(models.TrFlowStat).filter(models.TrFlowStat.stat_time < _time).delete() db.commit() logger.info("flow stat task done") except Exception as err: db.rollback() logger.error('flow_stat_job err,%s'%(str(err))) return self.get_notify_interval()
def get_customer_info(self, account_number): with make_db(self.db) as db: return db.query( models.TrCustomer.mobile, models.TrCustomer.realname, models.TrProduct.product_name, models.TrAccount.account_number, models.TrAccount.install_address, models.TrAccount.expire_date, models.TrAccount.password).filter( models.TrCustomer.customer_id == models.TrAccount.customer_id, models.TrAccount.product_id == models.TrProduct.id, models.TrAccount.account_number == account_number).first()
def init_route_permit(self): with make_db(self.db) as conn: try: oprs = conn.query(models.TrOperator) for opr in oprs: if opr.operator_type > 0: for rule in self.db.query(models.TrOperatorRule).filter_by(operator_name=opr.operator_name): permit.bind_opr(rule.operator_name, rule.rule_path) elif opr.operator_type == 0: # 超级管理员授权所有 permit.bind_super(opr.operator_name) except Exception as err: dispatch.pub(logger.EVENT_ERROR,"init route error , %s" % str(err))
def init_route_permit(self): with make_db(self.db) as conn: try: oprs = conn.query(models.TlOperator) for opr in oprs: if opr.operator_type > 0: for rule in self.db.query(models.TlOperatorRule).filter_by(operator_name=opr.operator_name): permit.bind_opr(rule.operator_name, rule.rule_path) elif opr.operator_type == 0: # 超级管理员授权所有 permit.bind_super(opr.operator_name) except Exception as err: logger.error("init route error , %s" % str(err))
def get_customer_info(self, account_number): with make_db(self.db) as db: return db.query( models.TrCustomer.mobile, models.TrCustomer.realname, models.TrProduct.product_name, models.TrAccount.account_number, models.TrAccount.install_address, models.TrAccount.expire_date, models.TrAccount.password ).filter( models.TrCustomer.customer_id == models.TrAccount.customer_id, models.TrAccount.product_id == models.TrProduct.id, models.TrAccount.account_number == account_number ).first()
def process(self, *args, **kwargs): logger.info("process expire notify task..") with make_db(self.db) as db: _enable = int(self.get_param_value("expire_notify_enable",0)) if not _enable: return 120.0 _ndays = self.get_param_value("expire_notify_days") notify_tpl = self.get_param_value("expire_notify_tpl") notify_url = self.get_param_value("expire_notify_url") notify_interval = self.get_notify_interval() if notify_interval > 3: return notify_interval _now = datetime.datetime.now() _date = (datetime.datetime.now() + datetime.timedelta(days=int(_ndays))).strftime("%Y-%m-%d") expire_query = db.query( models.TrAccount.account_number, models.TrAccount.expire_date, models.TrCustomer.email, models.TrCustomer.mobile ).filter( models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrAccount.expire_date <= _date, models.TrAccount.expire_date >= _now.strftime("%Y-%m-%d"), models.TrAccount.status == 1 ) logger.info('expire_notify total: %s'%expire_query.count()) for account,expire,email,mobile in expire_query: dispatch.pub('account_expire',account, async=False) ctx = notify_tpl.replace('#account#',account) ctx = ctx.replace('#expire#',expire) topic = ctx[:ctx.find('\n')] if email: self.send_mail(email, topic, ctx).addCallbacks(self.syslog.info,self.syslog.error) url = notify_url.replace('{account}',account) url = url.replace('{expire}',expire) url = url.replace('{email}',email) url = url.replace('{mobile}',mobile) url = url.encode('utf-8') url = quote(url,":?=/&") httpclient.get(url).addCallbacks(logger.info,logger.error) return self.get_notify_interval() + 120
def process(self, *args, **kwargs): with make_db(self.db) as db: _enable = int(self.get_param_value("expire_notify_enable", 0)) if not _enable: return 120.0 _ndays = self.get_param_value("expire_notify_days") notify_tpl = self.get_param_value("expire_notify_tpl") notify_url = self.get_param_value("expire_notify_url") notify_interval = self.get_notify_interval() if notify_interval > 3: return notify_interval _now = datetime.datetime.now() _date = (datetime.datetime.now() + datetime.timedelta(days=int(_ndays))).strftime("%Y-%m-%d") expire_query = db.query( models.TrAccount.account_number, models.TrAccount.expire_date, models.TrCustomer.email, models.TrCustomer.mobile).filter( models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrAccount.expire_date <= _date, models.TrAccount.expire_date >= _now.strftime("%Y-%m-%d"), models.TrAccount.status == 1) logger.info('expire_notify total: %s' % expire_query.count()) for account, expire, email, mobile in expire_query: dispatch.pub('account_expire', account, async=False) ctx = notify_tpl.replace('#account#', account) ctx = ctx.replace('#expire#', expire) topic = ctx[:ctx.find('\n')] if email: self.send_mail(email, topic, ctx).addCallbacks(self.syslog.info, self.syslog.error) url = notify_url.replace('{account}', account) url = url.replace('{expire}', expire) url = url.replace('{email}', email) url = url.replace('{mobile}', mobile) url = url.encode('utf-8') url = quote(url, ":?=/&") httpclient.get(url).addCallbacks(logger.info, logger.error) return self.get_notify_interval() + 120
def event_clear_online(self, account_number,nas_addr, acct_session_id): try: with make_db(self.db) as db: logger.info("event clear expire online [username:{0}] {1} {2}".format(account_number, nas_addr, acct_session_id)) nas = db.query(models.TrBas).filter_by(ip_addr=nas_addr).first() if nas_addr and not nas: db.query(models.TrOnline).filter_by( nas_addr=nas_addr,acct_session_id=acct_session_id).delete() db.commit() return online = db.query(models.TrOnline).filter_by( nas_addr=nas_addr, acct_session_id=acct_session_id).first() clear_req = self.get_request(online) radius_acct_stop.RadiusAcctStop(self.dbengine,self.mcache,self.aes,clear_req).acctounting() logger.info(u"系统触发用户过期清理成功: [username:%s] OnlineInfo: %s "%(str(account_number), json.dumps(clear_req)),trace="event" ) except Exception as err: logger.exception(err)
def process(self, *args, **kwargs): self.logtimes() next_interval = self.get_notify_interval() with make_db(self.db) as db: try: _days = int(self.get_param_value("system_ticket_expire_days",30)) td = datetime.timedelta(days=_days) _now = datetime.datetime.now() edate = (_now - td).strftime("%Y-%m-%d 23:59:59") db.query(models.TrTicket).filter(models.TrTicket.acct_stop_time < edate).delete() db.commit() logger.info(u"上网数据清理完成,下次执行还需等待 %s"%(self.format_time(next_interval)),trace="task") except: logger.info(u"上网数据清理失败,%s, 下次执行还需等待 %s"%( repr(err), self.format_time(next_interval)) ,trace="task") logger.exception(err) return next_interval
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: dstr = "%s 00:00:00" % datetime.datetime.now().strftime("%Y-%m-%d") startstat = datetime.datetime.strptime(dstr, "%Y-%m-%d %H:%M:%S") online_count = db.query(models.TrOnline.id).count() olstat = self.cache.get(online_statcache_key) or [] for ol in olstat: stat_time = datetime.datetime.fromtimestamp(ol[0]/1000.0) if stat_time < startstat: olstat.remove(ol) olstat.append( (int(time.time()*1000),online_count) ) self.cache.update(online_statcache_key,olstat) logger.info("online stat task done") except Exception as err: logger.error('online_stat_job err,%s'%(str(err))) return 120.0
def process(self, *args, **kwargs): self.logtimes() next_interval = self.get_notify_interval() try: logger.info("start process expire notify task") _ndays = self.get_param_value("expire_notify_days") _now = datetime.datetime.now() _date = (datetime.datetime.now() + datetime.timedelta(days=int(_ndays))).strftime("%Y-%m-%d") with make_db(self.db) as db: expire_query = db.query( models.TrCustomer.mobile, models.TrCustomer.realname, models.TrCustomer.email, models.TrProduct.product_name, models.TrAccount.account_number, models.TrAccount.install_address, models.TrAccount.expire_date, models.TrAccount.password).filter( models.TrCustomer.customer_id == models.TrAccount.customer_id, models.TrAccount.product_id == models.TrProduct.id, models.TrAccount.expire_date <= _date, models.TrAccount.expire_date >= _now.strftime("%Y-%m-%d"), models.TrAccount.status == 1) for userinfo in expire_query: self.trigger_notify(userinfo) logger.info( u"到期通知任务已执行(%s个已通知)。下次执行还需等待 %s" % (expire_query.count(), self.format_time(next_interval)), trace="task") except Exception as err: logger.info(u"到期通知任务执行失败,%s。下次执行还需等待 %s" % (repr(err), self.format_time(next_interval)), trace="task") logger.exception(err) return next_interval
def process(self, *args, **kwargs): self.logtimes() next_interval = self.get_notify_interval() try: logger.info("start process expire notify task") _ndays = self.get_param_value("expire_notify_days") _now = datetime.datetime.now() _date = (datetime.datetime.now() + datetime.timedelta( days=int(_ndays))).strftime("%Y-%m-%d") with make_db(self.db) as db: expire_query = db.query( models.TrCustomer.mobile, models.TrCustomer.realname, models.TrCustomer.email, models.TrProduct.product_name, models.TrAccount.account_number, models.TrAccount.install_address, models.TrAccount.expire_date, models.TrAccount.password ).filter( models.TrCustomer.customer_id == models.TrAccount.customer_id, models.TrAccount.product_id == models.TrProduct.id, models.TrAccount.expire_date <= _date, models.TrAccount.expire_date >= _now.strftime("%Y-%m-%d"), models.TrAccount.status == 1 ) for userinfo in expire_query: self.trigger_notify(userinfo) logger.info(u"到期通知任务已执行(%s个已通知)。下次执行还需等待 %s"% ( expire_query.count(),self.format_time(next_interval)),trace="task") except Exception as err: logger.info(u"到期通知任务执行失败,%s。下次执行还需等待 %s"%( repr(err),self.format_time(next_interval)),trace="task") logger.exception(err) return next_interval
def process(self, *args, **kwargs): with make_db(self.db) as db: try: onlines = db.query(models.TrOnline) for online in onlines: acct_start_time = datetime.datetime.strptime(online.acct_start_time, '%Y-%m-%d %H:%M:%S') nowdate = datetime.datetime.now() dt = nowdate - acct_start_time online_times = dt.total_seconds() max_session_time = int(self.get_param_value('radius_max_session_timeout',86400)) if online_times > (max_session_time): logger.info("online %s overtime, system auto disconnect this online"%online.account_number) dispatch.pub(UNLOCK_ONLINE_EVENT, online.account_number, online.nas_addr, online.acct_session_id,async=True) logger.info("online overtime check task done") except Exception as err: db.rollback() logger.error('online overtime check job err,%s'%(str(err))) return 3600.0
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: onlines = db.query(models.TrOnline) for online in onlines: acct_start_time = datetime.datetime.strptime(online.acct_start_time, '%Y-%m-%d %H:%M:%S') nowdate = datetime.datetime.now() dt = nowdate - acct_start_time online_times = dt.total_seconds() max_session_time = int(self.get_param_value('radius_max_session_timeout',86400)) if online_times > (max_session_time): logger.info("online %s overtime, system auto disconnect this online"%online.account_number) dispatch.pub(UNLOCK_ONLINE_EVENT, online.account_number, online.nas_addr, online.acct_session_id,async=True) logger.info("在线用户过期清理任务完成,下次执行还需等待一小时",trace="task") except Exception as err: db.rollback() logger.exception(err) return self.get_notify_interval()
def process(self, *args, **kwargs): with make_db(self.db) as db: try: nodes = db.query(models.TrNode) for node in nodes: online_count = db.query(models.TrOnline.id).filter( models.TrOnline.account_number == models.TrAccount.account_number, models.TrAccount.customer_id == models.TrCustomer.customer_id, models.TrCustomer.node_id == node.id).count() stat = models.TrOnlineStat() stat.node_id = node.id stat.stat_time = int(time.time()) stat.total = online_count db.add(stat) db.commit() logger.info("online stat task done") except Exception as err: db.rollback() logger.error('online_stat_job err,%s' % (str(err))) return 120.0
def process(self, *args, **kwargs): self.logtimes() with make_db(self.db) as db: try: nas_list = db.query(models.TrBas) for nas in nas_list: if not nas.dns_name: continue results, _, _ = yield client.lookupAddress(nas.dns_name) if not results: logger.info("domain {0} resolver empty".format(nas.dns_name)) if results[0].type == dns.A: ipaddr = ".".join(str(i) for i in struct.unpack("BBBB", results[0].payload.address)) if ipaddr: nas.ip_addr = ipaddr db.commit() logger.info("domain {0} resolver {1} success".format(nas.dns_name,ipaddr)) else: logger.info("domain {0} no ip address,{1}".format(nas.dns_name, repr(results))) except Exception as err: logger.error('ddns process error %s' % utils.safeunicode(err.message)) defer.returnValue(self.get_notify_interval())
def get_param_value(self, name, defval=None): with make_db(self.db) as conn: val = self.db.query(models.TrParam.param_value).filter_by(param_name = name).scalar() return val or defval
def get_param_value(self, name, defval=None): with make_db(self.db) as conn: val = self.db.query(models.TrParam.param_value).filter_by( param_name=name).scalar() return val or defval