Guest User

Untitled

a guest
Dec 24th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.55 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 Tue Dec 24 17:25:36 2013 +0400
  4. @@ -20,42 +20,159 @@
  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").upper()
  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)
  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. + else:
  63. + if self.rx_trans.search(pid):
  64. + part_no = self.get_transceiver_pid(pid)
  65. + else:
  66. + part_no = pid
  67. +
  68. if not part_no:
  69. print "!!! UNKNOWN: ", match.groupdict()
  70. continue
  71. else:
  72. - vendor = "CISCO" if "NoName" not in part_no else "NONAME"
  73. + if not vendor:
  74. + if "NoName" in part_no or "Unknown" in part_no:
  75. + vendor = "NONAME"
  76. + else:
  77. + vendor = "CISCO"
  78. objects += [{
  79. "type": type,
  80. "number": number,
  81. "vendor": vendor,
  82. - "serial": match.group("serial"),
  83. - "description": match.group("descr"),
  84. + "serial": serial,
  85. + "description": descr,
  86. "part_no": [part_no],
  87. - "revision": match.group("vid"),
  88. + "revision": vid,
  89. "builtin": False
  90. }]
  91. +
  92. + #if not transceiver in "sh inv" output - 6500 series gbic cards and sup
  93. + if ((type == "LINECARD" or type == "SUP") and
  94. + "RJ45" not in match.group("descr") and
  95. + ("WS-X64" in match.group("descr") or
  96. + "WS-X6K" in match.group("descr"))):
  97. + i = self.cli("show interface status module " + str(number))
  98. + for s in i.split("\n"):
  99. + if not s or "No Transceiver" in s or s.startswith("Port"):
  100. + continue
  101. + else:
  102. + t_num = s.split()[0].split("/")[-1]
  103. + t_vendor, t_sn, t_rev, pid = self.get_idprom(s.split()[0], s.split()[-1].upper())
  104. + objects += [{
  105. + "type": "XCVR",
  106. + "number": t_num,
  107. + "vendor": t_vendor,
  108. + "serial": t_sn,
  109. + "description": s.split()[-1] + " Transceiver",
  110. + "part_no": [pid],
  111. + "revision": t_rev,
  112. + "builtin": False
  113. + }]
  114. +
  115. return objects
  116.  
  117. + def get_idprom(self, int, descr):
  118. + try:
  119. + t = self.cli("show idprom int " + int + " | i Vendor")
  120. + match = self.rx_trans_6k.search(t)
  121. + if match:
  122. + # Check Cisco-standart format SN
  123. + if self.rx_csn.search(match.group("t_sn")):
  124. + t_vendor = "CISCO"
  125. + v = self.rx_cvend.search(match.group("t_vendor").upper())
  126. + if v and "SYSTEMS" not in v.group("ven"):
  127. + t_vendor = v.group("ven")
  128. + else:
  129. + # China noname products with "CISCO-" or "OEM" vendor
  130. + if ("CISCO" in match.group("t_vendor").upper() or
  131. + "OEM" in match.group("t_vendor").upper()):
  132. + t_vendor = "NONAME"
  133. + else:
  134. + # Others vendors
  135. + t_vendor = match.group("t_vendor").upper().strip()
  136. +
  137. + # Ignored serial
  138. + t_sn = match.group("t_sn") if match.group("t_sn") not in self.IGNORED_SERIAL else None
  139. +
  140. + # Decode hex revision (need rewrite)
  141. + if match.group("t_rev"):
  142. +# if "0x" in match.group("t_rev"):
  143. +# t_rev = None
  144. +# else:
  145. + t_rev = match.group("t_rev").strip()
  146. + else:
  147. + t_rev = None
  148. + if self.rx_trans.search(match.group("t_part_no").upper().replace("-", "")):
  149. + pid = self.get_transceiver_pid(match.group("t_part_no"))
  150. + else:
  151. + if ("GBIC-LX" in match.group("t_part_no") and
  152. + "Gi" in int):
  153. + pid = self.get_transceiver_pid("1000BASELX")
  154. + else:
  155. + if "NONAME" in t_vendor and self.rx_trans.search(descr):
  156. + pid = self.get_transceiver_pid(descr)
  157. + else:
  158. + pid = match.group("t_part_no").strip()
  159. + return t_vendor, t_sn, t_rev, pid
  160. + else:
  161. + return None, None, None, None
  162. + except self.CLISyntaxError:
  163. + print "sh idprom command not supported"
  164. +
  165. +
  166. def get_type(self, name, pid, descr, lo):
  167. """
  168. Get type, number and part_no
  169. @@ -81,26 +198,34 @@
  170. else:
  171. number = None
  172. if pid in ("", "N/A", "Unspecified") or self.rx_trans.search(pid):
  173. - # Non-Cisco transceivers
  174. - pid = self.get_transceiver_pid(descr)
  175. - if not pid:
  176. - return None, None, None
  177. - else:
  178. - return "XCVR", number, pid
  179. + return "XCVR", number, "N/A"
  180. else:
  181. return "XCVR", number, pid
  182. - elif lo == 0 or pid.startswith("CISCO") or pid.startswith("WS-C"):
  183. + elif "FRU" in descr:
  184. + return None, None, None
  185. + elif (lo == 0 or pid.startswith("CISCO") or
  186. + (pid.startswith("WS-C") and "PS" not in name)):
  187. try:
  188. number = int(name)
  189. except ValueError:
  190. number = None
  191. return "CHASSIS", number, pid
  192. - elif name.startswith("module "):
  193. + elif name.startswith("module ") or pid.startswith("WS-X"):
  194. # Linecards or supervisors
  195. - if pid.startswith("RSP"):
  196. - return "SUP", name[7:], pid
  197. + try:
  198. + number = int(name)
  199. + except ValueError:
  200. + number = None
  201. + if pid.startswith("RSP") or "SUP" in pid:
  202. + if not number:
  203. + return "SUP", name[7:], pid
  204. + else:
  205. + return "SUP", str(number), pid
  206. else:
  207. - return "LINECARD", name[7:], pid
  208. + if not number:
  209. + return "LINECARD", name[7:], pid
  210. + else:
  211. + return "LINECARD", number, pid
  212. elif "-DFC" in pid or "-CFC" in pid:
  213. # DFC subcard
  214. return "DFC", None, pid
Advertisement
Add Comment
Please, Sign In to add comment