Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_trafico_actual_servicio(request):
- empresa = request.user.perfilusuario.empresa
- usuario = request.GET['usuario']
- id_servicio = request.GET['id_servicio']
- interface = request.GET['interface']
- gibibyte = 1073741824
- mebibyte = 1048576
- #cliente = ServiciosCliente.objects.prefetch_related('user__perfilusuario').filter(cliente_rb=servicio, empresa = empresa)
- usuario_cliente = get_object_or_404(User, username = usuario)
- servicio_cliente = get_object_or_404(ServiciosCliente.objects.prefetch_related('user__perfilusuario', 'router_cliente'), user = usuario_cliente, id_servicio = id_servicio, empresa = empresa)
- router = servicio_cliente.router_cliente
- try:
- connection = routeros_api.RouterOsApiPool(router.ip, username=router.usuario_router, password=router.password,
- port=router.puerto)
- api = connection.get_api()
- except:
- pass
- torch_ip_result = "0/0"
- tx=0
- rx=0
- if connection.connected:
- torch_ip = api.get_resource('/tool')
- try:
- torch_ip_promise = torch_ip.call_async('torch', {'interface': interface, 'src_address': servicio_cliente.ip, 'port': 'any'})
- iterator = iter(torch_ip_promise)
- result = next(iterator) # waits for first answer
- if first_result['.section'] == '0': #sometimes mikrotik returns no data in first answer.
- result = next(iterator) # waits for second answer
- api.get_resource('/').call('cancel') # cancels last command
- try:
- torch_ip_promise.get() # without this there may be some garbage left in memory (This cleanup should be implemented in library but is not yet)
- except RouterOsApiCommunicationError: #command was canceled so exception was raised - it is expected
- pass
- get_torch_success = True
- except RouterOsApiCommunicationError:
- get_torch_success = False
- if get_torch_success and len(torch_ip_result) > 0:
- rx= int(result['rx'])/mebibyte
- rx= round(rx, 2)
- tx = int(result['tx']) / mebibyte
- tx = round(tx, 2)
- data_torch_ip = [1,tx, rx ]
- return JsonResponse({"tx": tx, "rx": rx})
Add Comment
Please, Sign In to add comment