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(snmptop): basetop interface (1/6) + --random support (2/6).
  • Loading branch information
strizhechenko committed Jul 15, 2017
commit 734df422201b7ceb184505ab7f5935379a8b3034
36 changes: 22 additions & 14 deletions netutils_linux_monitoring/snmptop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# coding=utf-8

from six import print_

from random import randint
from six import print_, iteritems
from copy import deepcopy
from netutils_linux_monitoring.base_top import BaseTop
from netutils_linux_monitoring.layout import make_table

Expand All @@ -20,14 +21,25 @@ def make_parser(parser=None):
def __int(self, line):
return [self.int(item) for item in line.strip().split()]

def eval(self):
self.diff = deepcopy(self.current)
for proto, data in iteritems(self.diff):
for n, metric in enumerate(data):
key, value = metric
if isinstance(value, int):
if self.options.random:
self.diff[proto][n][1] = randint(0, 1000)
else:
self.diff[proto][n][1] -= self.previous[proto][n][1]

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

def __repr__(self):
Expand All @@ -38,14 +50,14 @@ def make_header(self):

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

Expand All @@ -54,10 +66,6 @@ def make_align_map(self):


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)
top.run()