Bez popisu

views.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from django.shortcuts import render
  2. from backend.mongodb import db
  3. from exfo.lib import Exfo, Mikrotik
  4. from pprint import pprint
  5. from ttp import ttp
  6. from django.http import JsonResponse
  7. exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  8. exfo.login()
  9. mkt = Mikrotik()
  10. def index(request):
  11. collection = db['mascot']
  12. mascot_details = collection.find({})
  13. rapi = exfo.list_api()
  14. sla = exfo.call_api("sla")
  15. data_to_parse = """
  16. interface Loopback0
  17. description Router-id-loopback
  18. ip address 192.168.0.113/24
  19. !
  20. interface Vlan778
  21. description CPE_Acces_Vlan
  22. ip address 2002::fd37/124
  23. ip vrf CPE1
  24. !
  25. """
  26. ttp_template = """
  27. interface {{ interface }}
  28. ip address {{ ip }}/{{ mask }}
  29. description {{ description }}
  30. ip vrf {{ vrf }}
  31. """
  32. template = """
  33. <input load="text">
  34. interface Loopback0
  35. description Router-id-loopback
  36. ip address 192.168.0.113/24
  37. !
  38. interface Loopback1
  39. description Router-id-loopback
  40. ip address 192.168.0.1/24
  41. !
  42. interface Vlan778
  43. ip address 2002::fd37/124
  44. ip vrf CPE1
  45. !
  46. interface Vlan779
  47. ip address 2002::bbcd/124
  48. ip vrf CPE2
  49. !
  50. </input>
  51. <group name="loopbacks_new**.{{ interface }}">
  52. interface {{ interface | contains("Loop") }}
  53. ip address {{ ip }}/{{ mask }}
  54. description {{ description }}
  55. ip vrf {{ vrf }}
  56. </group>
  57. <group name="vlans*">
  58. interface {{ interface | contains("Vlan") }}
  59. ip address {{ ip }}/{{ mask }}
  60. description {{ description }}
  61. ip vrf {{ vrf }}
  62. </group>
  63. """
  64. parser = ttp(template=template)
  65. parser.parse()
  66. # form excel table and save in file
  67. parser.result(
  68. format="excel",
  69. filename="excel_out_test_excel_formatter_update.xlsx",
  70. returner="file",
  71. update=True,
  72. url="./Output/",
  73. table=[
  74. {
  75. "headers": ["interface", "ip", "mask", "vrf", "description"],
  76. "path": "loopbacks_new",
  77. "key": "interface",
  78. "tab_name": "loopbacks_new",
  79. },
  80. {"path": "vlans"},
  81. ],
  82. )
  83. # create parser object and parse data using template:
  84. # parser = ttp(data=data_to_parse, template=ttp_template)
  85. # parser.parse()
  86. # # print result in JSON format
  87. # results = parser.result(format='xlsx')[0]
  88. # pprint(results)
  89. try:
  90. mk_ips = mkt.call_remote("ip/route")
  91. mk_address = mkt.call_remote("ip/address")
  92. except:
  93. mk_ips = []
  94. mk_address = []
  95. return render(request, 'backend/index.html', {'objs': mascot_details, 'output': rapi.json(),\
  96. 'sla': sla.json(), 'mk_ips': mk_ips, 'mk_address': mk_address})
  97. #Define Collection
  98. def remote(request):
  99. cmd = request.GET.get('cmd', None)
  100. # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  101. # exfo.login()
  102. r = exfo.call_remote_api(cmd)
  103. pprint(r.json())
  104. # return JsonResponse(r.json())
  105. return render(request, 'backend/remote_render.html', {'res': r})
  106. def service_status(request):
  107. cmd = request.GET.get('cmd', None)
  108. import urllib.parse
  109. section = request.GET.get('section', 'all')
  110. # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  111. # exfo.login()
  112. cmd = urllib.parse.unquote(cmd)
  113. pprint(f" cmd = {cmd}")
  114. r = exfo.call_remote_api(cmd)
  115. pprint(r.json())
  116. # return JsonResponse(r.json())
  117. return render(request, 'backend/service_status.html', {'res': r, 'section': section})
  118. def reports(request):
  119. days = range(1,31)
  120. sla = exfo.call_api("sla")
  121. sla_json = sla.json()
  122. sla_results = {}
  123. test_status = {}
  124. service_results = {}
  125. for r in sla_json['result']:
  126. sla_uri = r['ids']['sla_uri']
  127. t = exfo.call_remote_api(sla_uri).json()
  128. sla_results[r['ids']['sla_name']] = t
  129. t = exfo.call_remote_api(sla_uri).json()
  130. service_results[r['ids']['sla_name']] = []
  131. for si in t['result']['service_instances']:
  132. service_results[r['ids']['sla_name']].append(exfo.call_remote_api(si['service_uri']).json()['result'])
  133. try:
  134. t2 = exfo.call_remote_api(t['result']['tests'][-1]['test_status_uri']).json()
  135. test_status[r['ids']['sla_name']] = t2
  136. except:
  137. test_status[r['ids']['sla_name']] = {}
  138. return render(request, 'backend/reports2.html', {'days': days, 'sla': sla.json(), 'sla_results': sla_results, 'test_status': test_status, 'service_results': service_results })