Advertisement
Guest User

Untitled

a guest
Dec 30th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. diff -r 710121307405 sa/profiles/Cisco/IOS/get_inventory.py
  2. --- a/sa/profiles/Cisco/IOS/get_inventory.py Wed Dec 18 11:20:26 2013 +0400
  3. +++ b/sa/profiles/Cisco/IOS/get_inventory.py Mon Dec 30 11:18:30 2013 +0400
  4. @@ -20,42 +20,165 @@
  5.  
  6. rx_item = re.compile(
  7. r"^NAME: \"(?P<name>[^\"]+)\", DESCR: \"(?P<descr>[^\"]+)\"\n"
  8. - r"PID:\s+(?P<pid>\S+)?\s*,\s+VID:\s+(?P<vid>\S+)?\s*, SN: (?P<serial>\S+)",
  9. + r"PID:\s+(?P<pid>\S+)?\s*,\s+VID:\s+(?P<vid>\S+[^\,]+)?\s*, SN: (?P<serial>\S+)",
  10. re.MULTILINE | re.DOTALL
  11. )
  12. + rx_csn = re.compile("[A-Z]{3}\w{4}\w{4}")
  13. rx_trans = re.compile("((?:100|1000|10G)BASE\S+)")
  14. + rx_int = re.compile("([a-z,A-Z]+(\d+\/)+\d+)")
  15. + rx_cvend = re.compile("(CISCO.(?P<ven>\S{3,15}))")
  16. + rx_trans_6k = re.compile(
  17. + r"\s*Vendor Name\s+(:|=)(\s+)?(?P<t_vendor>\S+[\S ]*)\n"
  18. + r"(\s*Vendor OUI\s+(:|=)(\s+)?[\S+ ]*(\s+)?\n)?"
  19. + r"\s*Vendor (PN|Part No.|Part Number)\s+(:|=)(\s+)?(?P<t_part_no>\S+[\S ]*)\n"
  20. + r"\s*Vendor (rev|Revision|Part Rev.)\s+(:|=)(\s+)?(?P<t_rev>\S+[\S ]*)?\n"
  21. + r"\s*Vendor (SN|Serial No.|Serial Number)\s+(:|=)(\s+)?(?P<t_sn>\S+)(\s+)?\n",
  22. + re.IGNORECASE | re.MULTILINE | re.DOTALL
  23. + )
  24.  
  25. IGNORED_NAMES = set([
  26. "c7201"
  27. ])
  28.  
  29. + IGNORED_SERIAL = set([
  30. + "H22L714"
  31. + ])
  32. +
  33. +
  34. def execute(self):
  35. objects = []
  36. v = self.cli("show inventory")
  37. for match in self.rx_item.finditer(v):
  38. + vendor = None
  39. if match.group("name") in self.IGNORED_NAMES:
  40. continue
  41. type, number, part_no = self.get_type(
  42. match.group("name"), match.group("pid"),
  43. match.group("descr"), len(objects)
  44. )
  45. + serial = match.group("serial") if match.group("serial") not in self.IGNORED_SERIAL else None
  46. + descr = match.group("descr")
  47. + vid = match.group("vid")
  48. + pid = match.group("pid")
  49. + # If not part_no for transceiver
  50. + if type == "XCVR":
  51. + if part_no == "N/A" or "GBIC" in pid:
  52. + # try idprom
  53. + try:
  54. + i = self.rx_int.search(match.group("name"))
  55. + if i:
  56. + vendor, serial, vid, pid = self.get_idprom(i.group(1), descr.upper())
  57. + except:
  58. + print "Unable to get idprom"
  59. + # fix gbic_pid noname xcvr
  60. + if pid == "GBIC_LX" and "Gigabit" in match.group("name"):
  61. + part_no = self.get_transceiver_pid("1000BASELX")
  62. + elif pid == "GBIC_ZX" and "Gigabit" in match.group("name"):
  63. + part_no = self.get_transceiver_pid("1000BASEZX")
  64. + elif pid == "GBIC_LH" and "Gigabit" in match.group("name"):
  65. + part_no = self.get_transceiver_pid("1000BASELH")
  66. + elif pid == "GBIC_T" and "Gigabit" in match.group("name"):
  67. + part_no = self.get_transceiver_pid("1000BASET")
  68. + else:
  69. + if self.rx_trans.search(pid):
  70. + part_no = self.get_transceiver_pid(pid)
  71. + else:
  72. + part_no = pid
  73. +
  74. if not part_no:
  75. print "!!! UNKNOWN: ", match.groupdict()
  76. continue
  77. else:
  78. - vendor = "CISCO" if "NoName" not in part_no else "NONAME"
  79. + if not vendor:
  80. + if "NoName" in part_no or "Unknown" in part_no:
  81. + vendor = "NONAME"
  82. + else:
  83. + vendor = "CISCO"
  84. objects += [{
  85. "type": type,
  86. "number": number,
  87. "vendor": vendor,
  88. - "serial": match.group("serial"),
  89. - "description": match.group("descr"),
  90. + "serial": serial,
  91. + "description": descr,
  92. "part_no": [part_no],
  93. - "revision": match.group("vid"),
  94. + "revision": vid,
  95. "builtin": False
  96. }]
  97. +
  98. + #if not transceiver in "sh inv" output - 6500 series gbic cards and sup
  99. + if ((type == "LINECARD" or type == "SUP") and
  100. + "RJ45" not in match.group("descr") and
  101. + ("WS-X64" in match.group("descr") or
  102. + "WS-X6K" in match.group("descr"))):
  103. + i = self.cli("show interface status module " + str(number))
  104. + for s in i.split("\n"):
  105. + if not s or "No Transceiver" in s or s.startswith("Port"):
  106. + continue
  107. + else:
  108. + t_num = s.split()[0].split("/")[-1]
  109. + t_vendor, t_sn, t_rev, pid = self.get_idprom(s.split()[0], s.split()[-1].upper())
  110. + objects += [{
  111. + "type": "XCVR",
  112. + "number": t_num,
  113. + "vendor": t_vendor,
  114. + "serial": t_sn,
  115. + "description": s.split()[-1] + " Transceiver",
  116. + "part_no": [pid],
  117. + "revision": t_rev,
  118. + "builtin": False
  119. + }]
  120. +
  121. return objects
  122.  
  123. + def get_idprom(self, int, descr):
  124. + try:
  125. + t = self.cli("show idprom int " + int + " | i Vendor")
  126. + match = self.rx_trans_6k.search(t)
  127. + if match:
  128. + # Check Cisco-standart format SN
  129. + if self.rx_csn.search(match.group("t_sn")):
  130. + t_vendor = "CISCO"
  131. + v = self.rx_cvend.search(match.group("t_vendor").upper())
  132. + if v and "SYSTEMS" not in v.group("ven"):
  133. + t_vendor = v.group("ven")
  134. + else:
  135. + # China noname products with "CISCO-" or "OEM" vendor
  136. + if ("CISCO" in match.group("t_vendor").upper() or
  137. + "OEM" in match.group("t_vendor").upper()):
  138. + t_vendor = "NONAME"
  139. + else:
  140. + # Others vendors
  141. + t_vendor = match.group("t_vendor").upper().strip()
  142. +
  143. + # Ignored serial
  144. + t_sn = match.group("t_sn") if match.group("t_sn") not in self.IGNORED_SERIAL else None
  145. +
  146. + # Decode hex revision (need rewrite)
  147. + if match.group("t_rev"):
  148. +# if "0x" in match.group("t_rev"):
  149. +# t_rev = None
  150. +# else:
  151. + t_rev = match.group("t_rev").strip()
  152. + else:
  153. + t_rev = None
  154. + if self.rx_trans.search(match.group("t_part_no").upper().replace("-", "")):
  155. + pid = self.get_transceiver_pid(match.group("t_part_no"))
  156. + else:
  157. + if ("GBIC-LX" in match.group("t_part_no") and
  158. + "Gi" in int):
  159. + pid = self.get_transceiver_pid("1000BASELX")
  160. + else:
  161. + if "NONAME" in t_vendor and self.rx_trans.search(descr):
  162. + pid = self.get_transceiver_pid(descr)
  163. + else:
  164. + pid = match.group("t_part_no").strip()
  165. + return t_vendor, t_sn, t_rev, pid
  166. + else:
  167. + return None, None, None, None
  168. + except self.CLISyntaxError:
  169. + print "sh idprom command not supported"
  170. +
  171. +
  172. def get_type(self, name, pid, descr, lo):
  173. """
  174. Get type, number and part_no
  175. @@ -81,26 +204,34 @@
  176. else:
  177. number = None
  178. if pid in ("", "N/A", "Unspecified") or self.rx_trans.search(pid):
  179. - # Non-Cisco transceivers
  180. - pid = self.get_transceiver_pid(descr)
  181. - if not pid:
  182. - return None, None, None
  183. - else:
  184. - return "XCVR", number, pid
  185. + return "XCVR", number, "N/A"
  186. else:
  187. return "XCVR", number, pid
  188. - elif lo == 0 or pid.startswith("CISCO") or pid.startswith("WS-C"):
  189. + elif "FRU" in descr:
  190. + return None, None, None
  191. + elif (lo == 0 or pid.startswith("CISCO") or
  192. + (pid.startswith("WS-C") and "PS" not in name)):
  193. try:
  194. number = int(name)
  195. except ValueError:
  196. number = None
  197. return "CHASSIS", number, pid
  198. - elif name.startswith("module "):
  199. + elif name.startswith("module ") or pid.startswith("WS-X"):
  200. # Linecards or supervisors
  201. - if pid.startswith("RSP"):
  202. - return "SUP", name[7:], pid
  203. + try:
  204. + number = int(name)
  205. + except ValueError:
  206. + number = None
  207. + if pid.startswith("RSP") or "SUP" in pid:
  208. + if not number:
  209. + return "SUP", name[7:], pid
  210. + else:
  211. + return "SUP", str(number), pid
  212. else:
  213. - return "LINECARD", name[7:], pid
  214. + if not number:
  215. + return "LINECARD", name[7:], pid
  216. + else:
  217. + return "LINECARD", number, pid
  218. elif "-DFC" in pid or "-CFC" in pid:
  219. # DFC subcard
  220. return "DFC", None, pid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement