Guest User

Untitled

a guest
Jun 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. """
  2. spathStats.py: ExtremeXOS slow-path statistics
  3. """
  4.  
  5. __author__ = "Laurens Heeren"
  6. __email__ = "laheeren@extremenetworks.com"
  7. __copyright__ = "Copyright 2014, Extreme Networks"
  8. __version__ = "1.0"
  9.  
  10. import datetime
  11. import re
  12. from collections import defaultdict
  13. from time import sleep
  14.  
  15.  
  16. def l2stats():
  17. l2stats = defaultdict(dict)
  18. vlan = None
  19. for line in exsh.clicmd("show l2stats", True).splitlines():
  20. # VLAN name
  21. m = re.search(r'.*VLAN\s(.*):', line)
  22. if m:
  23. vlan = m.group(1)
  24. continue
  25. if vlan:
  26. # CPU
  27. m = re.search(r'Total\snumber\sof\spackets\sto\sCPU\s=\s(\d+)\.', line)
  28. if m:
  29. l2stats['cpu'][vlan] = int(m.group(1))
  30. continue
  31. # learned
  32. m = re.search(r'Total\snumber\sof\spackets\slearned\s=\s(\d+)\.', line)
  33. if m:
  34. l2stats['learned'][vlan] = int(m.group(1))
  35. continue
  36. # MPLS PW examined
  37. m = re.search(r'Total\snumber\sof\sMPLS\sPW\spackets\sexamined\s=\s(\d+)\.', line)
  38. if m:
  39. l2stats['mpls_examined'][vlan] = int(m.group(1))
  40. continue
  41. # MPLS PW learned
  42. m = re.search(r'Total\snumber\sof\sMPLS\sPW\spackets\slearned\s=\s(\d+)\.', line)
  43. if m:
  44. l2stats['mpls_learned'][vlan] = int(m.group(1))
  45. continue
  46. # IGMP snooped
  47. m = re.search(r'Total\snumber\sof\sIGMP\scontrol\spackets\ssnooped\s=\s(\d+)\.', line)
  48. if m:
  49. l2stats['igmp_snooped'][vlan] = int(m.group(1))
  50. continue
  51. # IGMP switched
  52. m = re.search(r'Total\snumber\sof\sIGMP\sdata\spackets\sswitched\s=\s(\d+)\.', line)
  53. if m:
  54. l2stats['igmp_switched'][vlan] = int(m.group(1))
  55. continue
  56. # MLD snooped
  57. m = re.search(r'Total\snumber\sof\sMLD\scontrol\spackets\ssnooped\s=\s(\d+)\.', line)
  58. if m:
  59. l2stats['mld_snooped'][vlan] = int(m.group(1))
  60. continue
  61. # MLD switched
  62. m = re.search(r'Total\snumber\sof\sMLD\sdata\spackets\sswitched\s=\s(\d+)\.', line)
  63. if m:
  64. l2stats['mld_switched'][vlan] = int(m.group(1))
  65. continue
  66. return l2stats
  67.  
  68.  
  69. def ipstats():
  70. ipstats = defaultdict(dict)
  71.  
  72. ip_stats = False
  73. icmp_stats = False
  74. igmp_stats = False
  75. int_stats = False
  76. int_name = None
  77.  
  78. re_counters = re.compile(r'(?P<counter>\w+.+?)=\s+(?P<val>\d+)')
  79. re_icmp = re.compile(r'(?P<counter>\w+.+?)\s+In\s=\s+(?P<in>\d+)\s+Out\s=\s+(?P<out>\d+)')
  80. re_int_name = re.compile(r'Router\sInterface\s(?P<int_name>.+)')
  81. re_int_counters = re.compile(r'\s+(?P<in>\d+)\s+(?P<out>\d+)\s(?P<counter>.+)')
  82. re_int_counters_in = re.compile(r'\s+(?P<in>\d+)\s+(?P<counter>.+)')
  83.  
  84. for line in exsh.clicmd("show ipstats", True).splitlines():
  85. if re.match('IP Global Statistics', line):
  86. ipstats['global']['ip'] = {}
  87. ip_stats = True
  88. continue
  89. if re.match('Global ICMP Statistics', line):
  90. ipstats['global']['icmp'] = {}
  91. ip_stats = False
  92. icmp_stats = True
  93. continue
  94. if re.match('Global IGMP Statistics', line):
  95. ipstats['global']['igmp'] = {}
  96. icmp_stats = False
  97. igmp_stats = True
  98. continue
  99. if re.match('Router Interface', line):
  100. igmp_stats = False
  101. int_stats = True
  102.  
  103. # Global IP Statistics
  104. if ip_stats:
  105. for m in re.finditer(re_counters, line):
  106. ipstats['global']['ip'][m.group('counter').strip().replace(' ', '_')] = int(m.group('val'))
  107. continue
  108. # Global ICMP Statistics
  109. if icmp_stats:
  110. if re.search(re_icmp, line):
  111. for m in re.finditer(re_icmp, line):
  112. ipstats['global']['icmp'][m.group('counter').strip().replace(' ', '_') + '_in'] = \
  113. int(m.group('in'))
  114. ipstats['global']['icmp'][m.group('counter').strip().replace(' ', '_') + '_out'] = \
  115. int(m.group('out'))
  116. else:
  117. for m in re.finditer(re_counters, line):
  118. ipstats['global']['icmp'][m.group('counter').strip().replace(' ', '_')] = int(m.group('val'))
  119. continue
  120. # Global IGMP Statistics
  121. if igmp_stats:
  122. for m in re.finditer(re_counters, line):
  123. ipstats['global']['igmp'][m.group('counter').strip().replace(' ', '_')] = int(m.group('val'))
  124. continue
  125. # Router Interface
  126. if int_stats:
  127. m = re.match(re_int_name, line)
  128. if m:
  129. int_name = m.group('int_name')
  130. ipstats['interfaces'][int_name] = {}
  131. continue
  132. if int_name:
  133. if re.search(re_int_counters, line):
  134. for m in re.finditer(re_int_counters, line):
  135. ipstats['interfaces'][int_name][m.group('counter').strip().replace(' ', '_')] = \
  136. (int(m.group('in')), int(m.group('out')))
  137. elif re.search(re_int_counters_in, line):
  138. for m in re.finditer(re_int_counters_in, line):
  139. ipstats['interfaces'][int_name][m.group('counter').strip().replace(' ', '_')] = \
  140. (int(m.group('in')), 0)
  141. continue
  142. return ipstats
  143.  
  144. def expkt_stats():
  145. re_expkt = re.compile(r'(?P<port>\d+:\d+)\s+'
  146. r'(?P<tx_count>\d+)\s+'
  147. r'(?P<rx_count>\d+)\s+'
  148. r'(?P<drop_count>\d+)\s+'
  149. r'(?P<sflow_samples>\d+)')
  150. expkt = defaultdict(dict)
  151. stats_file = open('/proc/net/expkt/stats', 'r')
  152. for line in stats_file:
  153. m = re.search(re_expkt, line)
  154. if m:
  155. expkt['tx_count'][m.group('port')] = int(m.group('tx_count'))
  156. expkt['rx_count'][m.group('port')] = int(m.group('rx_count'))
  157. expkt['drop_count'][m.group('port')] = int(m.group('drop_count'))
  158. expkt['sflow_samples'][m.group('port')] = int(m.group('sflow_samples'))
  159. stats_file.close()
  160. return expkt
  161.  
  162.  
  163. if __name__ == "__main__":
  164. interval = 2
  165. top = 5
  166.  
  167. print "[+] Timestamp:", datetime.datetime.now()
  168. print "[+] Interval :", interval, "seconds"
  169. print "[+] Top :", top
  170. print "[+] Collecting slow-path statistics..."
  171.  
  172. l2_stats_1 = l2stats()
  173. sleep(interval)
  174. l2_stats_2 = l2stats()
  175.  
  176. ip_stats_1 = ipstats()
  177. sleep(interval)
  178. ip_stats_2 = ipstats()
  179.  
  180. expkt_stats_1 = expkt_stats()
  181. sleep(interval)
  182. expkt_stats_2 = expkt_stats()
  183.  
  184. print "-" * 80
  185. print "Layer 2 slow-path statistics"
  186. print "-" * 80
  187. print "{0:<32} {1:<20} {2:<20}".format("VLAN", "PPS", "Total")
  188. l2_stats = defaultdict(dict)
  189. l2_pps = 0
  190. for cntr in l2_stats_2:
  191. for vlan in l2_stats_2[cntr]:
  192. pps = (l2_stats_2[cntr][vlan] - l2_stats_1[cntr][vlan]) / interval
  193. if pps > 0:
  194. l2_stats[cntr][vlan] = pps
  195. l2_pps += pps
  196.  
  197. for cntr in sorted(l2_stats):
  198. print "{0:^32}".format(cntr)
  199. for vlan in sorted(l2_stats[cntr], key=lambda vl: l2_stats[cntr][vl], reverse=True)[:top]:
  200. print "{0:<32} {1:<20d} {2:<20d}".format(vlan, l2_stats[cntr][vlan], l2_stats_2[cntr][vlan])
  201. print "{0:>32} {1:<20d}".format("Total", l2_pps)
  202. print "-" * 80
  203.  
  204. print "{0:<32} {1:<20} {2:<20}".format("VLAN", " ", "Total")
  205. for cntr in sorted(l2_stats_2):
  206. total = sum([l2_stats_2[cntr][x] for x in l2_stats_2[cntr]])
  207. if total > 0:
  208. print "{0:^32}".format(cntr)
  209. for vlan in sorted(l2_stats_2[cntr], key=lambda vl: l2_stats_2[cntr][vl], reverse=True)[:top]:
  210. if l2_stats_2[cntr][vlan] > 0:
  211. print "{0:<32} {1:<20} {2:<20d}".format(vlan, " ", l2_stats_2[cntr][vlan])
  212. print
  213.  
  214. print "-" * 80
  215. print "Layer 3 slow-path statistics (global)"
  216. print "-" * 80
  217. print "{0:<32} {1:<20} {2:<20}".format("Counter", "PPS", "Total")
  218. ip_stats = defaultdict(dict)
  219. for cntr in ip_stats_2['global']:
  220. ip_stats['global'][cntr] = {}
  221. for stat in ip_stats_2['global'][cntr]:
  222. pps = (ip_stats_2['global'][cntr][stat] - ip_stats_1['global'][cntr][stat]) / interval
  223. if pps > 0:
  224. ip_stats['global'][cntr][stat] = pps
  225.  
  226. for cntr in sorted(ip_stats['global']):
  227. if len(ip_stats['global'][cntr]) > 0:
  228. print "{0:^32}".format(cntr)
  229. for stat in sorted(ip_stats['global'][cntr], key=lambda st: ip_stats['global'][cntr][st],
  230. reverse=True)[:top]:
  231. print "{0:<32} {1:<20d} {2:<20d}".format(stat, ip_stats['global'][cntr][stat],
  232. ip_stats_2['global'][cntr][stat])
  233. print "-" * 80
  234.  
  235. print "{0:<32} {1:<20} {2:<20}".format("Counter", " ", "Total")
  236. for cntr in sorted(ip_stats_2['global']):
  237. total = sum([ip_stats_2['global'][cntr][x] for x in ip_stats_2['global'][cntr]])
  238. if total > 0:
  239. print "{0:^32}".format(cntr)
  240. for stat in sorted(ip_stats_2['global'][cntr], key=lambda st: ip_stats_2['global'][cntr][st],
  241. reverse=True)[:top]:
  242. if ip_stats_2['global'][cntr][stat] > 0:
  243. print "{0:<32} {1:<20} {2:<20d}".format(stat, " ", ip_stats_2['global'][cntr][stat])
  244. print
  245.  
  246. print "-" * 80
  247. print "ExtremeXOS packet interface statistics (ExPkt)"
  248. print "-" * 80
  249. print "{0:<32} {1:<20} {2:<20}".format("Port", "PPS", "Total")
  250. expkt_stats = defaultdict(dict)
  251. for cntr in expkt_stats_2:
  252. for port in expkt_stats_2.get(cntr):
  253. pps = (expkt_stats_2.get(cntr).get(port) - expkt_stats_1.get(cntr).get(port)) / interval
  254. if pps > 0:
  255. expkt_stats[cntr][port] = pps
  256.  
  257. for cntr in sorted(expkt_stats):
  258. print "{0:^32}".format(cntr)
  259. for port in sorted(expkt_stats[cntr], key=lambda p: expkt_stats[cntr][p], reverse=True)[:top]:
  260. print "{0:<32} {1:<20d} {2:<20d}".format(port, expkt_stats[cntr][port], expkt_stats_2[cntr][port])
  261. print "-" * 80
  262.  
  263. print "{0:<32} {1:<20} {2:<20}".format("Port", " ", "Total")
  264. for cntr in sorted(expkt_stats_2):
  265. total = sum([expkt_stats_2[cntr][x] for x in expkt_stats_2[cntr]])
  266. if total > 0:
  267. print "{0:^32}".format(cntr)
  268. for port in sorted(expkt_stats_2[cntr], key=lambda p: expkt_stats_2[cntr][p], reverse=True)[:top]:
  269. if expkt_stats_2[cntr][port] > 0:
  270. print "{0:<32} {1:<20} {2:<20d}".format(port, " ", expkt_stats_2[cntr][port])
  271. print
Add Comment
Please, Sign In to add comment