Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- spathStats.py: ExtremeXOS slow-path statistics
- """
- __author__ = "Laurens Heeren"
- __email__ = "laheeren@extremenetworks.com"
- __copyright__ = "Copyright 2014, Extreme Networks"
- __version__ = "1.0"
- import datetime
- import re
- from collections import defaultdict
- from time import sleep
- def l2stats():
- l2stats = defaultdict(dict)
- vlan = None
- for line in exsh.clicmd("show l2stats", True).splitlines():
- # VLAN name
- m = re.search(r'.*VLAN\s(.*):', line)
- if m:
- vlan = m.group(1)
- continue
- if vlan:
- # CPU
- m = re.search(r'Total\snumber\sof\spackets\sto\sCPU\s=\s(\d+)\.', line)
- if m:
- l2stats['cpu'][vlan] = int(m.group(1))
- continue
- # learned
- m = re.search(r'Total\snumber\sof\spackets\slearned\s=\s(\d+)\.', line)
- if m:
- l2stats['learned'][vlan] = int(m.group(1))
- continue
- # MPLS PW examined
- m = re.search(r'Total\snumber\sof\sMPLS\sPW\spackets\sexamined\s=\s(\d+)\.', line)
- if m:
- l2stats['mpls_examined'][vlan] = int(m.group(1))
- continue
- # MPLS PW learned
- m = re.search(r'Total\snumber\sof\sMPLS\sPW\spackets\slearned\s=\s(\d+)\.', line)
- if m:
- l2stats['mpls_learned'][vlan] = int(m.group(1))
- continue
- # IGMP snooped
- m = re.search(r'Total\snumber\sof\sIGMP\scontrol\spackets\ssnooped\s=\s(\d+)\.', line)
- if m:
- l2stats['igmp_snooped'][vlan] = int(m.group(1))
- continue
- # IGMP switched
- m = re.search(r'Total\snumber\sof\sIGMP\sdata\spackets\sswitched\s=\s(\d+)\.', line)
- if m:
- l2stats['igmp_switched'][vlan] = int(m.group(1))
- continue
- # MLD snooped
- m = re.search(r'Total\snumber\sof\sMLD\scontrol\spackets\ssnooped\s=\s(\d+)\.', line)
- if m:
- l2stats['mld_snooped'][vlan] = int(m.group(1))
- continue
- # MLD switched
- m = re.search(r'Total\snumber\sof\sMLD\sdata\spackets\sswitched\s=\s(\d+)\.', line)
- if m:
- l2stats['mld_switched'][vlan] = int(m.group(1))
- continue
- return l2stats
- def ipstats():
- ipstats = defaultdict(dict)
- ip_stats = False
- icmp_stats = False
- igmp_stats = False
- int_stats = False
- int_name = None
- re_counters = re.compile(r'(?P<counter>\w+.+?)=\s+(?P<val>\d+)')
- re_icmp = re.compile(r'(?P<counter>\w+.+?)\s+In\s=\s+(?P<in>\d+)\s+Out\s=\s+(?P<out>\d+)')
- re_int_name = re.compile(r'Router\sInterface\s(?P<int_name>.+)')
- re_int_counters = re.compile(r'\s+(?P<in>\d+)\s+(?P<out>\d+)\s(?P<counter>.+)')
- re_int_counters_in = re.compile(r'\s+(?P<in>\d+)\s+(?P<counter>.+)')
- for line in exsh.clicmd("show ipstats", True).splitlines():
- if re.match('IP Global Statistics', line):
- ipstats['global']['ip'] = {}
- ip_stats = True
- continue
- if re.match('Global ICMP Statistics', line):
- ipstats['global']['icmp'] = {}
- ip_stats = False
- icmp_stats = True
- continue
- if re.match('Global IGMP Statistics', line):
- ipstats['global']['igmp'] = {}
- icmp_stats = False
- igmp_stats = True
- continue
- if re.match('Router Interface', line):
- igmp_stats = False
- int_stats = True
- # Global IP Statistics
- if ip_stats:
- for m in re.finditer(re_counters, line):
- ipstats['global']['ip'][m.group('counter').strip().replace(' ', '_')] = int(m.group('val'))
- continue
- # Global ICMP Statistics
- if icmp_stats:
- if re.search(re_icmp, line):
- for m in re.finditer(re_icmp, line):
- ipstats['global']['icmp'][m.group('counter').strip().replace(' ', '_') + '_in'] = \
- int(m.group('in'))
- ipstats['global']['icmp'][m.group('counter').strip().replace(' ', '_') + '_out'] = \
- int(m.group('out'))
- else:
- for m in re.finditer(re_counters, line):
- ipstats['global']['icmp'][m.group('counter').strip().replace(' ', '_')] = int(m.group('val'))
- continue
- # Global IGMP Statistics
- if igmp_stats:
- for m in re.finditer(re_counters, line):
- ipstats['global']['igmp'][m.group('counter').strip().replace(' ', '_')] = int(m.group('val'))
- continue
- # Router Interface
- if int_stats:
- m = re.match(re_int_name, line)
- if m:
- int_name = m.group('int_name')
- ipstats['interfaces'][int_name] = {}
- continue
- if int_name:
- if re.search(re_int_counters, line):
- for m in re.finditer(re_int_counters, line):
- ipstats['interfaces'][int_name][m.group('counter').strip().replace(' ', '_')] = \
- (int(m.group('in')), int(m.group('out')))
- elif re.search(re_int_counters_in, line):
- for m in re.finditer(re_int_counters_in, line):
- ipstats['interfaces'][int_name][m.group('counter').strip().replace(' ', '_')] = \
- (int(m.group('in')), 0)
- continue
- return ipstats
- def expkt_stats():
- re_expkt = re.compile(r'(?P<port>\d+:\d+)\s+'
- r'(?P<tx_count>\d+)\s+'
- r'(?P<rx_count>\d+)\s+'
- r'(?P<drop_count>\d+)\s+'
- r'(?P<sflow_samples>\d+)')
- expkt = defaultdict(dict)
- stats_file = open('/proc/net/expkt/stats', 'r')
- for line in stats_file:
- m = re.search(re_expkt, line)
- if m:
- expkt['tx_count'][m.group('port')] = int(m.group('tx_count'))
- expkt['rx_count'][m.group('port')] = int(m.group('rx_count'))
- expkt['drop_count'][m.group('port')] = int(m.group('drop_count'))
- expkt['sflow_samples'][m.group('port')] = int(m.group('sflow_samples'))
- stats_file.close()
- return expkt
- if __name__ == "__main__":
- interval = 2
- top = 5
- print "[+] Timestamp:", datetime.datetime.now()
- print "[+] Interval :", interval, "seconds"
- print "[+] Top :", top
- print "[+] Collecting slow-path statistics..."
- l2_stats_1 = l2stats()
- sleep(interval)
- l2_stats_2 = l2stats()
- ip_stats_1 = ipstats()
- sleep(interval)
- ip_stats_2 = ipstats()
- expkt_stats_1 = expkt_stats()
- sleep(interval)
- expkt_stats_2 = expkt_stats()
- print "-" * 80
- print "Layer 2 slow-path statistics"
- print "-" * 80
- print "{0:<32} {1:<20} {2:<20}".format("VLAN", "PPS", "Total")
- l2_stats = defaultdict(dict)
- l2_pps = 0
- for cntr in l2_stats_2:
- for vlan in l2_stats_2[cntr]:
- pps = (l2_stats_2[cntr][vlan] - l2_stats_1[cntr][vlan]) / interval
- if pps > 0:
- l2_stats[cntr][vlan] = pps
- l2_pps += pps
- for cntr in sorted(l2_stats):
- print "{0:^32}".format(cntr)
- for vlan in sorted(l2_stats[cntr], key=lambda vl: l2_stats[cntr][vl], reverse=True)[:top]:
- print "{0:<32} {1:<20d} {2:<20d}".format(vlan, l2_stats[cntr][vlan], l2_stats_2[cntr][vlan])
- print "{0:>32} {1:<20d}".format("Total", l2_pps)
- print "-" * 80
- print "{0:<32} {1:<20} {2:<20}".format("VLAN", " ", "Total")
- for cntr in sorted(l2_stats_2):
- total = sum([l2_stats_2[cntr][x] for x in l2_stats_2[cntr]])
- if total > 0:
- print "{0:^32}".format(cntr)
- for vlan in sorted(l2_stats_2[cntr], key=lambda vl: l2_stats_2[cntr][vl], reverse=True)[:top]:
- if l2_stats_2[cntr][vlan] > 0:
- print "{0:<32} {1:<20} {2:<20d}".format(vlan, " ", l2_stats_2[cntr][vlan])
- print
- print "-" * 80
- print "Layer 3 slow-path statistics (global)"
- print "-" * 80
- print "{0:<32} {1:<20} {2:<20}".format("Counter", "PPS", "Total")
- ip_stats = defaultdict(dict)
- for cntr in ip_stats_2['global']:
- ip_stats['global'][cntr] = {}
- for stat in ip_stats_2['global'][cntr]:
- pps = (ip_stats_2['global'][cntr][stat] - ip_stats_1['global'][cntr][stat]) / interval
- if pps > 0:
- ip_stats['global'][cntr][stat] = pps
- for cntr in sorted(ip_stats['global']):
- if len(ip_stats['global'][cntr]) > 0:
- print "{0:^32}".format(cntr)
- for stat in sorted(ip_stats['global'][cntr], key=lambda st: ip_stats['global'][cntr][st],
- reverse=True)[:top]:
- print "{0:<32} {1:<20d} {2:<20d}".format(stat, ip_stats['global'][cntr][stat],
- ip_stats_2['global'][cntr][stat])
- print "-" * 80
- print "{0:<32} {1:<20} {2:<20}".format("Counter", " ", "Total")
- for cntr in sorted(ip_stats_2['global']):
- total = sum([ip_stats_2['global'][cntr][x] for x in ip_stats_2['global'][cntr]])
- if total > 0:
- print "{0:^32}".format(cntr)
- for stat in sorted(ip_stats_2['global'][cntr], key=lambda st: ip_stats_2['global'][cntr][st],
- reverse=True)[:top]:
- if ip_stats_2['global'][cntr][stat] > 0:
- print "{0:<32} {1:<20} {2:<20d}".format(stat, " ", ip_stats_2['global'][cntr][stat])
- print
- print "-" * 80
- print "ExtremeXOS packet interface statistics (ExPkt)"
- print "-" * 80
- print "{0:<32} {1:<20} {2:<20}".format("Port", "PPS", "Total")
- expkt_stats = defaultdict(dict)
- for cntr in expkt_stats_2:
- for port in expkt_stats_2.get(cntr):
- pps = (expkt_stats_2.get(cntr).get(port) - expkt_stats_1.get(cntr).get(port)) / interval
- if pps > 0:
- expkt_stats[cntr][port] = pps
- for cntr in sorted(expkt_stats):
- print "{0:^32}".format(cntr)
- for port in sorted(expkt_stats[cntr], key=lambda p: expkt_stats[cntr][p], reverse=True)[:top]:
- print "{0:<32} {1:<20d} {2:<20d}".format(port, expkt_stats[cntr][port], expkt_stats_2[cntr][port])
- print "-" * 80
- print "{0:<32} {1:<20} {2:<20}".format("Port", " ", "Total")
- for cntr in sorted(expkt_stats_2):
- total = sum([expkt_stats_2[cntr][x] for x in expkt_stats_2[cntr]])
- if total > 0:
- print "{0:^32}".format(cntr)
- for port in sorted(expkt_stats_2[cntr], key=lambda p: expkt_stats_2[cntr][p], reverse=True)[:top]:
- if expkt_stats_2[cntr][port] > 0:
- print "{0:<32} {1:<20} {2:<20d}".format(port, " ", expkt_stats_2[cntr][port])
- print
Add Comment
Please, Sign In to add comment