| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- from django.shortcuts import render
- from backend.mongodb import db
- from exfo.lib import Exfo, Mikrotik
- from pprint import pprint
- from ttp import ttp
- from django.http import JsonResponse
- exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
- exfo.login()
- mkt = Mikrotik()
- def index(request):
- collection = db['mascot']
- mascot_details = collection.find({})
- rapi = exfo.list_api()
- sla = exfo.call_api("sla")
-
- data_to_parse = """
- interface Loopback0
- description Router-id-loopback
- ip address 192.168.0.113/24
- !
- interface Vlan778
- description CPE_Acces_Vlan
- ip address 2002::fd37/124
- ip vrf CPE1
- !
- """
- ttp_template = """
- interface {{ interface }}
- ip address {{ ip }}/{{ mask }}
- description {{ description }}
- ip vrf {{ vrf }}
- """
- template = """
- <input load="text">
- interface Loopback0
- description Router-id-loopback
- ip address 192.168.0.113/24
- !
- interface Loopback1
- description Router-id-loopback
- ip address 192.168.0.1/24
- !
- interface Vlan778
- ip address 2002::fd37/124
- ip vrf CPE1
- !
- interface Vlan779
- ip address 2002::bbcd/124
- ip vrf CPE2
- !
- </input>
- <group name="loopbacks_new**.{{ interface }}">
- interface {{ interface | contains("Loop") }}
- ip address {{ ip }}/{{ mask }}
- description {{ description }}
- ip vrf {{ vrf }}
- </group>
- <group name="vlans*">
- interface {{ interface | contains("Vlan") }}
- ip address {{ ip }}/{{ mask }}
- description {{ description }}
- ip vrf {{ vrf }}
- </group>
- """
- parser = ttp(template=template)
- parser.parse()
- # form excel table and save in file
- parser.result(
- format="excel",
- filename="excel_out_test_excel_formatter_update.xlsx",
- returner="file",
- update=True,
- url="./Output/",
- table=[
- {
- "headers": ["interface", "ip", "mask", "vrf", "description"],
- "path": "loopbacks_new",
- "key": "interface",
- "tab_name": "loopbacks_new",
- },
- {"path": "vlans"},
- ],
- )
- # create parser object and parse data using template:
- # parser = ttp(data=data_to_parse, template=ttp_template)
- # parser.parse()
- # # print result in JSON format
- # results = parser.result(format='xlsx')[0]
- # pprint(results)
- try:
- mk_ips = mkt.call_remote("ip/route")
- mk_address = mkt.call_remote("ip/address")
- except:
- mk_ips = []
- mk_address = []
- return render(request, 'backend/index.html', {'objs': mascot_details, 'output': rapi.json(),\
- 'sla': sla.json(), 'mk_ips': mk_ips, 'mk_address': mk_address})
- #Define Collection
- def remote(request):
- cmd = request.GET.get('cmd', None)
- # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
- # exfo.login()
- r = exfo.call_remote_api(cmd)
- pprint(r.json())
- # return JsonResponse(r.json())
- return render(request, 'backend/remote_render.html', {'res': r})
- def service_status(request):
- cmd = request.GET.get('cmd', None)
- import urllib.parse
- section = request.GET.get('section', 'all')
- # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
- # exfo.login()
- cmd = urllib.parse.unquote(cmd)
- pprint(f" cmd = {cmd}")
- r = exfo.call_remote_api(cmd)
- pprint(r.json())
- # return JsonResponse(r.json())
- return render(request, 'backend/service_status.html', {'res': r, 'section': section})
- def reports(request):
- days = range(1,31)
- sla = exfo.call_api("sla")
- sla_json = sla.json()
- sla_results = {}
- test_status = {}
- service_results = {}
- for r in sla_json['result']:
- sla_uri = r['ids']['sla_uri']
- t = exfo.call_remote_api(sla_uri).json()
- sla_results[r['ids']['sla_name']] = t
- t = exfo.call_remote_api(sla_uri).json()
- service_results[r['ids']['sla_name']] = []
- for si in t['result']['service_instances']:
- service_results[r['ids']['sla_name']].append(exfo.call_remote_api(si['service_uri']).json()['result'])
- try:
- t2 = exfo.call_remote_api(t['result']['tests'][-1]['test_status_uri']).json()
- test_status[r['ids']['sla_name']] = t2
- except:
- test_status[r['ids']['sla_name']] = {}
- return render(request, 'backend/reports2.html', {'days': days, 'sla': sla.json(), 'sla_results': sla_results, 'test_status': test_status, 'service_results': service_results })
-
|