Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.20 KB | None | 0 0
  1. cat ./get_interfaces.py
  2. # -*- coding: utf-8 -*-
  3. ##----------------------------------------------------------------------
  4. ## Qtech.QSW.get_interfaces
  5. ##----------------------------------------------------------------------
  6. ## Copyright (C) 2007-2012 The NOC Project
  7. ## See LICENSE for details
  8. ##----------------------------------------------------------------------
  9.  
  10. # Python modules
  11. from __future__ import with_statement
  12. import re
  13. from collections import defaultdict
  14. # NOC modules
  15. from noc.sa.script import Script as NOCScript
  16. from noc.sa.interfaces import IGetInterfaces
  17. from noc.lib.ip import IPv4
  18. from noc.lib.ip import IPv6
  19.  
  20.  
  21. class Script(NOCScript):
  22. """
  23. Qtech.QSW.get_interfaces
  24. @todo: VRF support
  25. @todo: IPv6
  26. @todo: ISIS
  27. @todo: isis, bgp, rip
  28. @todo: subinterfaces
  29. @todo: Q-in-Q
  30. """
  31. name = "Qtech.QSW.get_interfaces"
  32. implements = [IGetInterfaces]
  33.  
  34. TIMEOUT = 600
  35.  
  36. rx_mac = re.compile(r"^The mac-address of interface is\s+(?P<mac>\S+)$",
  37. re.MULTILINE)
  38.  
  39. rx_sh_svi = re.compile(
  40. r"^(Interface description\s*:\s*(?P<description>(\S+ \S+ \S+|\S+ \S+|\S+)).|)Interface name\s*:\s*(?P<interface>\S+?).Primary ipaddress\s*:\s*(?P<ip1>\d+\.\d+\.\d+\.\d+)/(?P<mask1>\d+\.\d+\.\d+\.\d+).Secondary ipaddress\s*:\s*((?P<ip2>\d+\.\d+\.\d+\.\d+)/(?P<mask2>\d+\.\d+\.\d+\.\d+)|None).VLAN\s*:\s*(?P<vlan>\d+).Address-range\s*:\s*\S+.Interface status\s*:\s*(?P<admin_status>(Up|Down))",
  41. re.DOTALL|re.MULTILINE)
  42.  
  43. rx_sh_mng = re.compile(
  44. r"^ip address\s*:\s*(?P<ip>\d+\.\d+\.\d+\.\d+).netmask\s*:\s*(?P<mask>\d+\.\d+\.\d+\.\d+).gateway\s*:\s*\S+.ManageVLAN\s*:\s*(?P<vlan>\S+).MAC address\s*:\s*(?P<mac>\S+)",
  45. re.DOTALL|re.MULTILINE)
  46.  
  47. rx_status = re.compile(
  48. r"^\s*Ethernet\s+(?P<interface>\S+)\s+is\s+(?P<admin_status>(enabled|disabled)),\s+port+\s+link+\s+is\s+(?P<oper_status>(up|down))",
  49. re.MULTILINE)
  50.  
  51. types = {
  52. "e": "physical", # FastEthernet
  53. "g": "physical", # GigabitEthernet
  54. "t": "physical", # TenGigabitEthernet
  55. }
  56.  
  57. def get_ospfint(self):
  58. ospfs = []
  59. return ospfs
  60.  
  61. def get_ripint(self):
  62. rip = []
  63. return rip
  64.  
  65. def get_bgpint(self):
  66. bgp = []
  67. return bgp
  68.  
  69. def execute(self):
  70.  
  71. # TODO
  72. # Get portchannes
  73. portchannel_members = {} # member -> (portchannel, type)
  74. # with self.cached():
  75. # for pc in self.scripts.get_portchannel():
  76. # i = pc["interface"]
  77. # t = pc["type"] == "L"
  78. # for m in pc["members"]:
  79. # portchannel_members[m] = (i, t)
  80.  
  81. interfaces = []
  82.  
  83. # Try SNMP first
  84.  
  85. """
  86. # SNMP working but without IP
  87.  
  88. if self.snmp and self.access_profile.snmp_ro:
  89. try:
  90. # Get mac
  91. mac = self.scripts.get_chassis_id()
  92. # Get switchports
  93. for swp in self.scripts.get_switchport():
  94. iface = swp["interface"]
  95. if iface[0] == "T":
  96. return 1
  97. # IF-MIB::ifAdminStatus
  98. if len(iface.split('/')) < 3:
  99. if_OID = iface.split('/')[1]
  100. else:
  101. if_OID = iface.split('/')[2]
  102. s = self.snmp.get("1.3.6.1.2.1.2.2.1.7.%d" % int(if_OID))
  103. admin = int(s) == 1
  104.  
  105. name = swp["interface"]
  106. iface = {
  107. "name": name,
  108. "type": "aggregated" if len(swp["members"]) > 0 else "physical",
  109. "admin_status": admin,
  110. "oper_status": swp["status"],
  111. "mac": mac,
  112. "subinterfaces": [{
  113. "name": name,
  114. "admin_status": admin,
  115. "oper_status": swp["status"],
  116. "is_bridge": True,
  117. "mac": mac,
  118. #"snmp_ifindex": self.scripts.get_ifindex(interface=name)
  119. }]
  120. }
  121. if swp["tagged"]:
  122. iface["subinterfaces"][0]["tagged_vlans"] = swp["tagged"]
  123. try:
  124. iface["subinterfaces"][0]["untagged_vlan"] = swp["untagged"]
  125. except KeyError:
  126. pass
  127. if swp["description"]:
  128. iface["description"] = swp["description"]
  129. if name in portchannel_members:
  130. iface["aggregated_interface"] = portchannel_members[name][0]
  131. iface["is_lacp"] = portchannel_members[name][1]
  132. interfaces += [iface]
  133.  
  134. return [{"interfaces": interfaces}]
  135.  
  136. except self.snmp.TimeOutError:
  137. pass # Fallback to CLI
  138. """
  139.  
  140. # Fallback to CLI
  141. # Get port-to-vlan mappings
  142. pvm = {}
  143. switchports = {} # interface -> (untagged, tagged)
  144. for swp in self.scripts.get_switchport():
  145. switchports[swp["interface"]] = (
  146. swp["untagged"] if "untagged" in swp else None,
  147. swp["tagged"],
  148. swp["description"]
  149. )
  150.  
  151. interfaces = []
  152.  
  153. # Get L3 interfaces
  154. try:
  155. enabled_afi = []
  156. ip_int = self.cli("show ip interface") # QWS-3xxx
  157. match = self.rx_mac.search(ip_int)
  158. mac = match.group("mac")
  159.  
  160. # TODO Get router interfaces
  161. ospfs = self.get_ospfint()
  162. rips = self.get_ripint()
  163. bgps = self.get_bgpint()
  164.  
  165. for match in self.rx_sh_svi.finditer(ip_int):
  166. description = match.group("description")
  167. if not description:
  168. description = 'Outband managment'
  169. ifname = match.group("interface")
  170. ip1 = match.group("ip1")
  171. ip2 = match.group("ip2")
  172. if ":" in ip1:
  173. ip_interfaces = "ipv6_addresses"
  174. enabled_afi += ["IPv6"]
  175. ip1 = IPv6(ip1, netmask=match.group("mask1")).prefix
  176. if ip2:
  177. ip2 = IPv6(ip2, netmask=match.group("mask2")).prefix
  178. ip_list = [ip1, ip2]
  179. else:
  180. ip_list = [ip1]
  181. else:
  182. ip_interfaces = "ipv4_addresses"
  183. enabled_afi += ["IPv4"]
  184. ip1 = IPv4(ip1, netmask=match.group("mask1")).prefix
  185. if ip2:
  186. ip2 = IPv4(ip2, netmask=match.group("mask2")).prefix
  187. ip_list = [ip1, ip2]
  188. else:
  189. ip_list = [ip1]
  190. vlan = match.group("vlan")
  191. a_stat = match.group("admin_status").lower() == "up"
  192. iface = {
  193. "name": ifname,
  194. "type": "SVI",
  195. "admin_status": a_stat,
  196. "oper_status": a_stat,
  197. "mac": mac,
  198. "description": description,
  199. "subinterfaces": [{
  200. "name": ifname,
  201. "description": description,
  202. "admin_status": a_stat,
  203. "oper_status": a_stat,
  204. "enabled_afi": enabled_afi,
  205. ip_interfaces: ip_list,
  206. "mac": mac,
  207. "vlan_ids": self.expand_rangelist(vlan),
  208. }]
  209. }
  210. interfaces += [iface]
  211.  
  212. except self.CLISyntaxError:
  213. enabled_afi = []
  214. ip_int = self.cli("show ip") # QWS-2xxx
  215. match = self.rx_sh_mng.search(ip_int)
  216. ip = match.group("ip")
  217. if ":" in ip:
  218. ip_interfaces = "ipv6_addresses"
  219. enabled_afi += ["IPv6"]
  220. ip = IPv6(ip, netmask=match.group("mask")).prefix
  221. else:
  222. ip_interfaces = "ipv4_addresses"
  223. enabled_afi += ["IPv4"]
  224. ip = IPv4(ip, netmask=match.group("mask")).prefix
  225. ip_list = [ip]
  226. vlan = match.group("vlan")
  227. mac = match.group("mac")
  228.  
  229. iface = {
  230. "name": "VLAN-" + vlan,
  231. "type": "management",
  232. "admin_status": True,
  233. "oper_status": True,
  234. "mac": mac,
  235. "description": 'Managment',
  236. "subinterfaces": [{
  237. "name": "VLAN-" + vlan,
  238. "description": 'Managment',
  239. "admin_status": True,
  240. "oper_status": True,
  241. "enabled_afi": enabled_afi,
  242. ip_interfaces: ip_list,
  243. "mac": mac,
  244. "vlan_ids": self.expand_rangelist(vlan),
  245. }]
  246. }
  247. interfaces += [iface]
  248. #
  249.  
  250. # Get L2 interfaces
  251. mac = self.scripts.get_chassis_id()[0]["first_chassis_mac"]
  252. status = self.cli("show interface")
  253. for match in self.rx_status.finditer(status):
  254. ifname = match.group("interface")
  255. a_stat = match.group("admin_status").lower() == "enabled"
  256. o_stat = match.group("oper_status").lower() == "up"
  257.  
  258. iface = {
  259. "name": ifname,
  260. "type": self.types[ifname[:1]],
  261. "admin_status": a_stat,
  262. "oper_status": o_stat,
  263. "mac": mac,
  264. "description": switchports[ifname][2],
  265. "subinterfaces": [{
  266. "name": ifname,
  267. "description": switchports[ifname][2],
  268. "admin_status": a_stat,
  269. "oper_status": o_stat,
  270. "enabled_afi": ["BRIDGE"],
  271. "mac": mac,
  272. #"snmp_ifindex": self.scripts.get_ifindex(interface=name)
  273. }]
  274. }
  275.  
  276. if switchports[ifname][1]:
  277. iface["subinterfaces"][0]["tagged_vlans"] = switchports[ifname][1]
  278. if switchports[ifname][0]:
  279. iface["subinterfaces"][0]["untagged_vlan"] = switchports[ifname][0]
  280.  
  281. # iface["description"] = switchports[ifname][2]
  282.  
  283. # Portchannel member
  284. if ifname in portchannel_members:
  285. ai, is_lacp = portchannel_members[ifname]
  286. iface["aggregated_interface"] = ai
  287. if is_lacp:
  288. iface["enabled_protocols"] = ["LACP"]
  289. interfaces += [iface]
  290.  
  291. return [{"interfaces": interfaces}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement