Guest User

Untitled

a guest
Jul 24th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## EdgeCore.ES.get_interfaces
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2012 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8. """
  9. """
  10. # Python modules
  11. import re
  12. from collections import defaultdict
  13. # NOC modules
  14. from noc.sa.script import Script as NOCScript
  15. from noc.sa.interfaces import IGetInterfaces, InterfaceTypeError, MACAddressParameter
  16.  
  17.  
  18. class Script(NOCScript):
  19. name = "EdgeCore.ES.get_interfaces"
  20. implements = [IGetInterfaces]
  21.  
  22. TIMEOUT = 240
  23.  
  24. types = {
  25. "Eth": "physical",
  26. "Trunk": "aggregated",
  27. }
  28.  
  29.  
  30. rx_if_admstat_3526 = re.compile(r"Port Admin:\s+(?P<admstatus>up|down)\n",
  31. re.MULTILINE | re.IGNORECASE | re.DOTALL)
  32. rx_if_intstat_3526 = re.compile(r".*?Name:[^\n]* (?P<descr>[^\n]*?)\n.*?Link Status:\s+(?P<intstatus>up|down)\n",
  33. re.MULTILINE | re.IGNORECASE | re.DOTALL)
  34. rx_if_3526 = re.compile(r"Information of (?P<name>[^\n]+?)\n.*?Mac Address:\s+(?P<mac>[^\n]+?)\n(?P<block>.*)",
  35. re.MULTILINE | re.IGNORECASE | re.DOTALL)
  36.  
  37.  
  38.  
  39. def execute(self):
  40. ifaces = {}
  41. current = None
  42. is_bundle = False
  43. print "bla"
  44. raise
  45. ae_map = {} # member -> bundle
  46. v = self.cli("show interface status")
  47. for l in v.splitlines():
  48. match = self.rx_if_3526.match(l)
  49. if match:
  50. current = match.group("name")
  51. mac = MACAddressParameter().clean(match.group("mac"))
  52. ifaces[current] = {
  53. "name": current,
  54. "mac": mac
  55. }
  56. is_bundle = current.startswith("Trunk")
  57. if is_bundle:
  58. members = []
  59. type = "aggregated"
  60. ifaces[current] = {
  61. "members": members,
  62. "type": type
  63. }
  64. else:
  65. type = "physical"
  66. ifaces[current]["type"] = type
  67. elif not current:
  68. continue
  69. print "blablabla"
  70. raise
  71. l = l.strip()
  72. # Process description and oper_status
  73. match = self.rx_if_intstat_3526.match(l)
  74. if match:
  75. description = match.group("descr")
  76. intstatus = match.group("intstatus") == "Up"
  77. ifaces[current] = {
  78. "description": description,
  79. "oper_status": intstatus
  80. }
  81. continue
  82.  
  83.  
  84. # Process admin status
  85. match = self.rx_if_admstat_3526.match(l)
  86. if match:
  87. admstatus = match.group("admstatus") == "Up"
  88. ifaces[current] = {
  89. "admin_status": admstatus
  90. }
  91. continue
  92.  
  93. # Process IP addresses
  94. # match = self.rx_ip.match(l)
  95. # if match:
  96. # ip = match.group("ip")
  97. # if ip.lower() != "unknown":
  98. # ifaces[current]["addresses"] = (
  99. # ifaces[current].get("addresses", []) +
  100. # [ip]
  101. # )
  102. # continue
  103. # Process hardware type and
  104. # match = self.rx_hw.match(l)
  105. # if match:
  106. # hw = match.group("hw").lower()
  107. # t = self.types.get(hw, "unknown")
  108. # if t == "unknown":
  109. # self.error("Unknown hardware type: %s" % hw)
  110. # ifaces[current]["type"] = t
  111. # mac = match.group("mac")
  112. # if mac:
  113. # ifaces[current]["mac"] = mac
  114. # Process VLAN id
  115. # match = self.rx_vlan_id.match(l)
  116. # if match:
  117. # ifaces[current]["vlan_ids"] = [int(match.group("vlan"))]
  118. # Process ethernet bundles
  119. if is_bundle:
  120. portchannel_members = {} # member)
  121. for s in self.scripts.get_portchannel():
  122. portchannel_members[s["interface"]] = s["members"]
  123. # match = self.rx_bundle_member.match(l)
  124. # if match:
  125. m = [portchannel_members.get(current, "")]
  126. ifaces[current]["members"] += [m]
  127. if "." in current:
  128. ae_map[m] = current.split(".", 1)[0]
  129. else:
  130. ae_map[m] = current
  131. # Get VRFs and "default" VRF interfaces
  132. r = []
  133. seen = set()
  134. # vpns = self.scripts.get_mpls_vpn()
  135. # for v in vpns:
  136. # seen.update(v["interfaces"])
  137. vpns = [{
  138. "name": "default",
  139. "type": "ip",
  140. "interfaces": set(ifaces) - seen
  141. }] + vpns
  142. # Bring result together
  143. for fi in vpns:
  144. # Forwarding instance
  145. rr = {
  146. "forwarding_instance": fi["name"],
  147. "type": fi["type"],
  148. "interfaces": []
  149. }
  150. rd = fi.get("rd")
  151. if rd:
  152. rr["rd"] = rd
  153. # Get interface -> subinterfaces mapping
  154. p_ifaces = dict((x, []) for x in
  155. set(i.split(".", 1)[0]
  156. for i in fi["interfaces"]))
  157. for i in fi["interfaces"]:
  158. p = i.split(".", 1)[0]
  159. p_ifaces[p] += [i]
  160. # Create interfaces
  161. for iface in p_ifaces:
  162. if iface not in ifaces:
  163. continue
  164. i = ifaces[iface]
  165. p = {
  166. "name": iface,
  167. "type": i["type"],
  168. "admin_status": i["status"],
  169. "oper_status": i["status"],
  170. "subinterfaces": [],
  171. }
  172. if i.get("mac"):
  173. p["mac"] = i["mac"]
  174. if i.get("description"):
  175. p["description"] = i["description"]
  176. if iface in ae_map:
  177. # Bundle member
  178. p["aggregated_interface"] = ae_map[iface]
  179. else:
  180. # Create subinterfaces
  181. for siface in p_ifaces[iface]:
  182. if siface not in ifaces:
  183. continue
  184. ii = ifaces[siface]
  185. sp = {
  186. "name": siface,
  187. "admin_status": ii["status"],
  188. "oper_status": ii["status"],
  189. }
  190. if ii.get("mac"):
  191. sp["mac"] = ii["mac"]
  192. if ii.get("description"):
  193. sp["description"] = ii["description"]
  194. # if ii.get("vlan_ids"):
  195. # sp["vlan_ids"] = ii["vlan_ids"]
  196. # Process addresses
  197. if "addresses" in ii:
  198. ipv4_addresses = [a for a in ii["addresses"] if ":" not in a]
  199. ipv6_addresses = [a for a in ii["addresses"] if ":" in a]
  200. if ipv4_addresses:
  201. sp["is_ipv4"] = True
  202. sp["ipv4_addresses"] = ipv4_addresses
  203. if ipv6_addresses:
  204. sp["is_ipv6"] = True
  205. sp["ipv6_addresses"] = ipv6_addresses
  206. p["subinterfaces"] += [sp]
  207. rr["interfaces"] += [p]
  208. r += [rr]
  209. # Return result
  210. return r
Advertisement
Add Comment
Please, Sign In to add comment