def save(self, *args, **kwargs): if self.id: new_record = True else: new_record = False result = super(CoaQueue, self).save(*args, **kwargs) if new_record: log(self).info(u"Pending CoA #{0} ({1}->{2} created.".format(self.id, self.coa.name, self.uid)) return result
def run(self, uid, log_level="info"): logger = log(self) log_level = getattr(logging, log_level.upper()) logger.log(log_level, u"Run CoA '{0}' to user ID '{1}'".format(self.name, uid)) bid, sid = get_session_detail(uid) if not (bid and sid): logger.log(log_level, u"No cached session info for user '{0}'".format(uid)) return None bras = Bras.objects.get(ip_address=bid) message = self.message.format(user_id=uid, aaa_session_id=sid) cmd = "radclient -x -t 3 -r 1 {bid}:{port} coa {secret}".format( bid=bid, port=bras.coa_port, secret=bras.coa_secret ) if get_last_coa_sid(self.id, uid) == sid: logger.log( log_level, u"User '{0}' session ID {1} hasn't changed since last CoA. Avoid CoA".format(uid, sid) ) return None logger.log(log_level, u"Send CoA to '{0}'".format(bras.ip_address)) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) (output, error) = p.communicate(input=message) increase_coa_counter() if p.returncode == 0: logger.debug(u"CoA Response:\n{0}".format(output)) set_coa_sid(self.id, uid, sid) else: logger.log(log_level, u"CoA request failed:\n{0}".format(output)) return p.returncode
def handle(self, *args, **options): logger = log(self) logger.info('Run session cache update job.') for bras in Bras.objects.filter(is_active=True): logger.info(u'Run session list update procedure from bras \'{0}\''.format(bras.name)) bras.aaa_list_update() logger.info(u'Sessions update job complete.')
def __init__(self): super(AnswerView, self).__init__() self.logger = log(self) try: self.notification = UserNotification.objects.get_active() except UserNotification.DoesNotExist as ex: self.logger.critical(u'Active notification doesn\'t exist.') raise_exception(ex)
def complete(self, result): logger = log(self) if not self.is_acknowledged: logger.info(u'Notification #{0} UID{1} is completed.'.format(self.id, self.uid)) self.json_result = result self.is_completed = True self.completed = datetime.now() self.save() CoaQueue.objects.create(coa=self.notification.successful_coa, uid=self.uid)
def handle(self, *args, **options): logger = log(self) logger.info(u'Pending CoA job started') logger.info('Waiting for CoA is unlocked...') while coa_is_locked(): sleep(0.2) lock_coa() try: for coa in CoaQueue.objects.all(): coa.run() sleep(0.05) except: logger.critical(u'PCJ catched exception: [{0}] {1}'.format(*sys.exc_info())) logger.info(u'Pending CoA job finished') unlock_coa()
def aaa_list_update(self): logger = log(self) if self.method == "tln": logger.info(u"Get sessiong list from BRAS '{0}' by command-line interface".format(self.name)) output = self._telnet_output("show aaa sessions") else: logger.info(u"Get session list from BRAS '{0}' by RShell".format(self.name)) output = self._rsh_output("show aaa sessions") sessions = [] counter = 0 session_id = None uid = None ip_address = None logger.info(u"Parse {} output".format("telnet" if self.method == "tln" else "RShell")) for raw_line in output.split("\n"): try: param, value = raw_line.strip().split(": ") except ValueError: continue if param == "Session Id": session_id = value uid = None ip_address = None elif param == "User Name" and value != "*not available*": uid = value elif param == "IP Address": ip_address = value if not uid: uid = value else: continue if uid and session_id and ip_address and ip_address != "0.0.0.0": counter += 1 sessions.append(dict(ip=ip_address, uid=uid, bid=self.ip_address, sid="{:X}".format(int(session_id)))) logger.info(u"Parsed {0} sessions(s).".format(counter)) logger.info(u"Caching query result") cache_session_data(sessions) set_bras_last_update(self.id, counter)
def handle(self, *args, **options): logger = log(self) logger.info('Start services refresh') counter = 0 skipped = 0 failed = 0 qs = UserNotificationRecord.objects.get_active().filter(is_completed=False) if options['limit'] > 0: qs = qs[:options['limit']] logger.info('Waiting for CoA is unlocked...') while coa_is_locked(): sleep(0.2) lock_coa() for record in qs: if counter > 0 and counter % settings.COA_BLOCK_SIZE == 0: unlock_coa() logger.info('Records are refreshed: {}. Waiting for the next block...'.format(counter)) sleep(settings.COA_BLOCK_DELAY) logger.info('Waiting for CoA is unlocked...') while coa_is_locked(): sleep(0.2) lock_coa() return_code = record.notification.coa.run(record.uid) if return_code is None: skipped += 1 elif return_code == 0: counter += 1 failed += 1 else: counter += 1 record.refreshed = datetime.now() record.save() sleep(settings.COA_MESSAGE_INTERVAL) unlock_coa() logger.info('Services refresh finished')