Нет описания

views.py 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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, HttpResponse
  7. from datetime import datetime
  8. from celery import shared_task
  9. exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  10. exfo.login()
  11. mkt = Mikrotik()
  12. def index(request):
  13. collection = db['mascot']
  14. mascot_details = collection.find({})
  15. rapi = exfo.list_api()
  16. sla = exfo.call_api("sla")
  17. data_to_parse = """
  18. interface Loopback0
  19. description Router-id-loopback
  20. ip address 192.168.0.113/24
  21. !
  22. interface Vlan778
  23. description CPE_Acces_Vlan
  24. ip address 2002::fd37/124
  25. ip vrf CPE1
  26. !
  27. """
  28. ttp_template = """
  29. interface {{ interface }}
  30. ip address {{ ip }}/{{ mask }}
  31. description {{ description }}
  32. ip vrf {{ vrf }}
  33. """
  34. template = """
  35. <input load="text">
  36. interface Loopback0
  37. description Router-id-loopback
  38. ip address 192.168.0.113/24
  39. !
  40. interface Loopback1
  41. description Router-id-loopback
  42. ip address 192.168.0.1/24
  43. !
  44. interface Vlan778
  45. ip address 2002::fd37/124
  46. ip vrf CPE1
  47. !
  48. interface Vlan779
  49. ip address 2002::bbcd/124
  50. ip vrf CPE2
  51. !
  52. </input>
  53. <group name="loopbacks_new**.{{ interface }}">
  54. interface {{ interface | contains("Loop") }}
  55. ip address {{ ip }}/{{ mask }}
  56. description {{ description }}
  57. ip vrf {{ vrf }}
  58. </group>
  59. <group name="vlans*">
  60. interface {{ interface | contains("Vlan") }}
  61. ip address {{ ip }}/{{ mask }}
  62. description {{ description }}
  63. ip vrf {{ vrf }}
  64. </group>
  65. """
  66. parser = ttp(template=template)
  67. parser.parse()
  68. # form excel table and save in file
  69. parser.result(
  70. format="excel",
  71. filename="excel_out_test_excel_formatter_update.xlsx",
  72. returner="file",
  73. update=True,
  74. url="./Output/",
  75. table=[
  76. {
  77. "headers": ["interface", "ip", "mask", "vrf", "description"],
  78. "path": "loopbacks_new",
  79. "key": "interface",
  80. "tab_name": "loopbacks_new",
  81. },
  82. {"path": "vlans"},
  83. ],
  84. )
  85. # create parser object and parse data using template:
  86. # parser = ttp(data=data_to_parse, template=ttp_template)
  87. # parser.parse()
  88. # # print result in JSON format
  89. # results = parser.result(format='xlsx')[0]
  90. # pprint(results)
  91. try:
  92. mk_ips = mkt.call_remote("ip/route")
  93. mk_address = mkt.call_remote("ip/address")
  94. except:
  95. mk_ips = []
  96. mk_address = []
  97. return render(request, 'backend/index.html', {'objs': mascot_details, 'output': rapi.json(),\
  98. 'sla': sla.json(), 'mk_ips': mk_ips, 'mk_address': mk_address})
  99. #Define Collection
  100. def remote(request):
  101. cmd = request.GET.get('cmd', None)
  102. # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  103. # exfo.login()
  104. r = exfo.call_remote_api(cmd)
  105. pprint(r.json())
  106. # return JsonResponse(r.json())
  107. return render(request, 'backend/remote_render.html', {'res': r})
  108. def service_status(request):
  109. cmd = request.GET.get('cmd', None)
  110. import urllib.parse
  111. section = request.GET.get('section', 'all')
  112. # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  113. # exfo.login()
  114. cmd = urllib.parse.unquote(cmd)
  115. pprint(f" cmd = {cmd}")
  116. r = exfo.call_remote_api(cmd)
  117. pprint(r.json())
  118. # return JsonResponse(r.json())
  119. return render(request, 'backend/service_status.html', {'res': r, 'section': section})
  120. def reports(request):
  121. days = range(1,31)
  122. sla = exfo.call_api("sla")
  123. sla_json = sla.json()
  124. sla_results = {}
  125. test_status = {}
  126. service_results = {}
  127. for r in sla_json['result']:
  128. sla_uri = r['ids']['sla_uri']
  129. t = exfo.call_remote_api(sla_uri).json()
  130. sla_results[r['ids']['sla_name']] = t
  131. t = exfo.call_remote_api(sla_uri).json()
  132. service_results[r['ids']['sla_name']] = []
  133. for si in t['result']['service_instances']:
  134. service_results[r['ids']['sla_name']].append(exfo.call_remote_api(si['service_uri']).json()['result'])
  135. try:
  136. t2 = exfo.call_remote_api(t['result']['tests'][-1]['test_status_uri']).json()
  137. test_status[r['ids']['sla_name']] = t2
  138. except:
  139. test_status[r['ids']['sla_name']] = {}
  140. return render(request, 'backend/reports2.html', {'days': days, 'sla': sla.json(), 'sla_results': sla_results, 'test_status': test_status, 'service_results': service_results })
  141. def dump_api(request):
  142. from bson.json_util import dumps
  143. from bson.json_util import loads
  144. col = db['exfo_api']
  145. # col.delete_many({})
  146. sla = exfo.call_api("sla")
  147. sla_json = sla.json()
  148. temp = sla_json['result']
  149. for t in temp:
  150. sla_uri = t['ids']['sla_uri']
  151. sla_name = t['ids']['sla_name']
  152. r = exfo.call_remote_api(sla_uri).json()
  153. t['sla_uri_result'] = r['result']
  154. service_result = []
  155. for si in r['result']['service_instances']:
  156. c = exfo.call_remote_api(si['service_uri']).json()['result']
  157. service_result.append(c)
  158. test_instance_class_result = []
  159. test_status_result = []
  160. for si in r['result']['tests']:
  161. c = exfo.call_remote_api(si['test_instance_class_uri']).json()['result']
  162. service = c['service']
  163. target = None
  164. if 'target' in c:
  165. target = c['target']
  166. test_instance_class_result.append(c)
  167. c = exfo.call_remote_api(si['test_status_uri']).json()['result']
  168. test_status_result.append({'sla_name':sla_name, 'test_instance_id': si['test_instance_id'],\
  169. 'service': service, 'target': target, \
  170. 'type_type_name': si['test_type_name'],\
  171. 'test_instance_class_id': si['test_instance_class_id'] , 'status': c, })
  172. t['test_instance_class_result'] = test_instance_class_result
  173. t['test_status_result'] = test_status_result
  174. t['created'] = datetime.utcnow()
  175. col.insert_one(t)
  176. # pprint(temp)
  177. #col.insert_many(temp)
  178. results = col.find({})
  179. data = dumps(list(results), indent=4)
  180. return HttpResponse(data, content_type='application/json')
  181. @shared_task
  182. def dump_api_task():
  183. from bson.json_util import dumps
  184. from bson.json_util import loads
  185. col = db['exfo_api']
  186. # col.delete_many({})
  187. sla = exfo.call_api("sla")
  188. sla_json = sla.json()
  189. temp = sla_json['result']
  190. for t in temp:
  191. sla_uri = t['ids']['sla_uri']
  192. sla_name = t['ids']['sla_name']
  193. r = exfo.call_remote_api(sla_uri).json()
  194. t['sla_uri_result'] = r['result']
  195. service_result = []
  196. for si in r['result']['service_instances']:
  197. c = exfo.call_remote_api(si['service_uri']).json()['result']
  198. service_result.append(c)
  199. test_instance_class_result = []
  200. test_status_result = []
  201. for si in r['result']['tests']:
  202. c = exfo.call_remote_api(si['test_instance_class_uri']).json()['result']
  203. service = c['service']
  204. target = None
  205. if 'target' in c:
  206. target = c['target']
  207. test_instance_class_result.append(c)
  208. c = exfo.call_remote_api(si['test_status_uri']).json()['result']
  209. test_status_result.append({'sla_name':sla_name, 'test_instance_id': si['test_instance_id'],\
  210. 'service': service, 'target': target, \
  211. 'type_type_name': si['test_type_name'],\
  212. 'test_instance_class_id': si['test_instance_class_id'] , 'status': c, })
  213. t['test_instance_class_result'] = test_instance_class_result
  214. t['test_status_result'] = test_status_result
  215. t['created'] = datetime.utcnow()
  216. col.insert_one(t)
  217. pprint("Dump API ... Finished")
  218. def print_table(request):
  219. return render(request, 'backend/print_table.html')