diff --git a/agent/mod_mikrotik.py b/agent/mod_mikrotik.py index d6ad1cc..89bea50 100644 --- a/agent/mod_mikrotik.py +++ b/agent/mod_mikrotik.py @@ -274,8 +274,7 @@ class QueueManager(TransmitterManager, metaclass=ABCMeta): # читаем шейпер, возващаем записи о шейпере def read_queue_iter(self): - queues = self._exec_cmd_iter(['/queue/simple/print', '=detail']) - for queue in queues: + for queue in self._exec_cmd_iter(['/queue/simple/print', '=detail']): if queue[0] == '!done': return sobj = self._build_shape_obj(queue[1]) if sobj is not None: @@ -348,7 +347,7 @@ class IpAddressListManager(TransmitterManager, metaclass=ABCMeta): if len(ids) > 0: return self._exec_cmd([ '/ip/firewall/address-list/remove', - 'numbers=' + ','.join(ids) + '=numbers=*%s' % ',*'.join(ids) ]) def find(self, ip, list_name): @@ -361,10 +360,14 @@ class IpAddressListManager(TransmitterManager, metaclass=ABCMeta): ]) def read_ips_iter(self, list_name): - return self._exec_cmd([ + ips = self._exec_cmd_iter([ '/ip/firewall/address-list/print', 'where', - '?list=%s' % list_name + '?list=%s' % list_name, + '?dynamic=no' ]) + for code, dat in ips: + if dat != {}: + yield IpAddressListObj(dat['=address'], dat['=.id']) def disable(self, user): r = IpAddressListManager.find(self, user.ip, LIST_USERS_ALLOWED) @@ -500,6 +503,15 @@ class MikrotikTransmitter(QueueManager, IpAddressListManager): def read_users(self): # shapes is ShapeItem - # allowed_ips = IpAddressListManager.read_ips_iter(self, LIST_USERS_ALLOWED) - queues = QueueManager.read_queue_iter(self) + allowed_ips = set(IpAddressListManager.read_ips_iter(self, LIST_USERS_ALLOWED)) + queues = set(q for q in QueueManager.read_queue_iter(self) if q.ip in allowed_ips) + + ips_from_queues = set([q.ip for q in queues]) + allowed_ips = set(allowed_ips) + + # удаляем ip адреса которые есть в firewall/address-list и нет соответствующих в queues + diff = list(allowed_ips - ips_from_queues) + if len(diff) > 0: + IpAddressListManager.remove_range(self, diff) + return queues diff --git a/cron.py b/cron.py index 872c4f1..0f8d6b6 100755 --- a/cron.py +++ b/cron.py @@ -16,32 +16,12 @@ def main(): # бдим за услугами абонента user.bill_service(user) - # если нет ip то и нет смысла лезть в NAS - if user.ip_address is None: - continue - - # а есть-ли у абонента доступ к услуге - if not user.is_access(): - continue - - # строим структуру агента - ab = user.build_agent_struct() - if ab is None: - # если не построилась структура агента, значит нет ip - # а если нет ip то и синхронизировать абонента без ip нельзя - continue - - # обновляем абонента если он статический. Иначе его обновит dhcp - #if tm is None: - # tm = Transmitter() - #tm.update_user(ab) - except (NasNetworkError, NasFailedResult) as er: print("Error:", er) except LogicError as er: print("Notice:", er) tm = Transmitter() - users = Abon.objects.filter(is_dynamic_ip=False) + users = Abon.objects.filter(is_dynamic_ip=False, is_active=True).exclude(current_tariff=None) tm.sync_nas(users) @@ -49,4 +29,6 @@ if __name__ == "__main__": try: main() except (NasNetworkError, NasFailedResult) as e: - print('NAS:', e) + print("Error while sync nas:", e) + except LogicError as e: + print("Notice while sync nas:", e)