Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: snmptop utility. #142

Merged
merged 9 commits into from
Jul 15, 2017
Prev Previous commit
Next Next commit
Added: almost working snmptop
  • Loading branch information
strizhechenko committed Jul 15, 2017
commit f956a7c188823c124ad3a4fcb5fb5d3d89501111
6 changes: 5 additions & 1 deletion netutils_linux_monitoring/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def make_table(header, align_map=None, rows=None):
""" Wrapper for pretty table """
table = PrettyTable()
table.horizontal_char = table.vertical_char = table.junction_char = ' '
table.field_names = header
try:
table.field_names = header
except Exception as err:
print_(header)
raise err
if align_map:
for field, align in zip(header, align_map):
table.align[field] = align
Expand Down
42 changes: 34 additions & 8 deletions netutils_linux_monitoring/snmptop.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# coding=utf-8

import json
from six import print_
from functools import reduce
from operator import add
from six import print_, iteritems
from netutils_linux_monitoring.base_top import BaseTop
from netutils_linux_monitoring.layout import make_table


class SnmpTop(BaseTop):
protos = ['IP', 'TCP', 'UDP', 'ICMP']

@staticmethod
def make_parser(parser=None):
if not parser:
Expand All @@ -19,21 +24,42 @@ def __int(self, line):

def parse(self):
with open(self.options.snmp_file) as file_fd:
lines = file_fd.readlines()
lines = [self.__int(line) for line in file_fd.readlines()]
return {
"IP": dict(zip(lines[0].split()[1:], lines[1].split()[1:])),
"ICMP": dict(zip(lines[2].split()[1:], lines[3].split()[1:])),
"TCP": dict(zip(lines[6].split()[3:], lines[7].split()[1:])),
"UDP": dict(zip(lines[8].split()[3:], lines[9].split()[1:])),
"IP": list(zip(lines[0][1:], lines[1][1:])),
"ICMP": list(zip(lines[2][1:], lines[3][1:])),
"TCP": list(zip(lines[6][3:], lines[7][1:])),
"UDP": list(zip(lines[8][3:], lines[9][1:])),
}


def __repr__(self):
return json.dumps(self.parse(), indent=4)
return str(make_table(self.make_header(), self.make_align_map(), self.make_rows()))

def make_header(self):
return ['IP', ' ', 'TCP', '', 'UDP', ' ', 'ICMP', ' ']

def make_rows(self):
rows = []
max_len = max(len(subdict) for subdict in snmp.values())
for index in range(max_len):
row = list()
for proto in self.protos:
if index >= len(snmp[proto]):
row.extend(['', ''])
continue
row.extend([snmp[proto][index][0], snmp[proto][index][1]])
rows.append(row)
return rows

def make_align_map(self):
return ['l', 'r'] * len(self.protos)


if __name__ == '__main__':
import sys

sys.argv = [sys.argv[0], '--snmp-file=tests/proc_net_snmp/snmp1']
top = SnmpTop()
top.options = top.make_parser().parse_args()
snmp = top.parse()
print_(top)