Exemplo n.º 1
0
    def start_accounting(self,req,nas):
        service.incr_stat(service.STAT_ACCT_START)
        _key = '%s_%s'%(nas.ip_addr,req.get_acctsessionid())
        if service.is_online(_key):
            radiuslog.error('[Acct] accounting of start is repeated')
            return

        user = service.get_user(req.get_username())

        if not user:
            radiuslog.error("[Acct] Received an accounting request but user[%s] not exists"%req.get_username())
            return

        online = dict(user = user,
            nas = nas,
            sessionid = req.get_acctsessionid(),
            acctstarttime = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S"),
            framedipaddr = req.get_framedipaddr(),
            macaddr = req.get_macaddr(),
            nasportid = req.get_nasportid(),
            startsource = STATUS_TYPE_START)

        service.add_online(_key,online)

        if radiuslog.isEnabledFor(DEBUG):
            radiuslog.info('[Acct] User[%s],Nas[%s] billing starting'%(user.user_name,nas.ip_addr))
Exemplo n.º 2
0
    def process(self,req):
        if radiuslog.isEnabledFor(DEBUG):
            radiuslog.debug("Received an accounting request")
            radiuslog.debug( "Attributes: ")
            for attr in req.keys():
                radiuslog.debug( "%s: %s" % (attr, req[attr]))

        reply = req.CreateReply()
        reply.source = req.source
        req.sock.sendto(reply.ReplyPacket(), reply.source)

        nasaddr = req.get_nasaddr()
        nas = service.get_nas(nasaddr)         
        
        acct_status_type  = req.get_acctstatustype()

        if acct_status_type == STATUS_TYPE_START:
            return self.start_accounting(req,nas)
        elif acct_status_type == STATUS_TYPE_STOP:
            return self.stop_accounting(req,nas)
        elif acct_status_type == STATUS_TYPE_UPDATE:
            return self.update_accounting(req,nas)
        elif acct_status_type == STATUS_TYPE_NAS_ON or \
             acct_status_type == STATUS_TYPE_NAS_OFF  :
            return self.nasonoff_accounting(nasaddr,acct_status_type)     
        else:
            return                                        
Exemplo n.º 3
0
    def stop_accounting(self,req,nas):
        _key = '%s_%s'%(nas.ip_addr,req.get_acctsessionid())
        service.incr_stat(service.STAT_ACCT_STOP)
        online = service.rmv_online(_key)

        ticket = req.get_ticket()

        if online is None:
            sessiontime = req.get_acctsessiontime()
            _stoptime = datetime.datetime.now()
            _starttime = _stoptime + datetime.timedelta(seconds=-sessiontime)
            ticket['acctstarttime'] = _starttime.strftime( "%Y-%m-%d %H:%M:%S")
            ticket['acctstoptime'] = _stoptime.strftime( "%Y-%m-%d %H:%M:%S")
            ticket['startsource'] = STATUS_TYPE_STOP
            ticket['stopsource'] = STATUS_TYPE_STOP
            ticket['nodeid'] = nas.id
        else:
            ticket['nodeid'] = online['user'].node_id
            ticket['acctstarttime'] = online['acctstarttime']
            ticket['acctstoptime'] = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
            ticket['startsource'] = online['startsource']
            ticket['stopsource'] = STATUS_TYPE_STOP
            ticket['framedipaddr'] = online['framedipaddr']

        if radiuslog.isEnabledFor(DEBUG):
            radiuslog.info('[Acct] User[%s],Nas[%s] billing stop'%(req.get_username(),nas.ip_addr))

        user = service.get_user(req.get_username())
        if not user:
            ticketlog.info(json.dumps(ticket))
            return

        product  = service.get_product(user.product_id)

        if product.policy == 0:
            sessiontime = round(req.get_acctsessiontime()/60,0)
            usedfee = round(sessiontime/60*product.fee_price,0)
            remaind = round(sessiontime%60,0)
            if remaind > 0 :
                usedfee = usedfee + round(remaind*product.fee_price/60,0);
            balance = user.balance - usedfee

            balance = balance < 0 and 0 or balance
            service.update_user_balance(user.user_name,balance)

            ticket['actualfee'] = usedfee
            ticket['acctfee'] = usedfee
            ticket['isdeduct'] = 1
            ticketlog.info("ticket:%s"%json.dumps(ticket))
        else :
            ticket['actualfee'] = 0
            ticket['acctfee'] = 0
            ticket['isdeduct'] = 0
            ticketlog.info("ticket:%s"%json.dumps(ticket))