|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +from copy import deepcopy |
| 4 | +from random import randint |
| 5 | + |
| 6 | +from six import iteritems |
| 7 | + |
| 8 | +from netutils_linux_monitoring.base_top import BaseTop |
| 9 | +from netutils_linux_monitoring.layout import make_table |
| 10 | + |
| 11 | + |
| 12 | +class SnmpTop(BaseTop): |
| 13 | + """ Utility for monitoring IP/TCP/UDP/ICMP parts of network stack based on /proc/net/snmp values """ |
| 14 | + |
| 15 | + protos = ['IP', 'TCP', 'UDP', 'ICMP'] |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def make_parser(parser=None): |
| 19 | + """ :returns: parser with options for snmptop """ |
| 20 | + if not parser: |
| 21 | + parser = BaseTop.make_parser() |
| 22 | + parser.add_argument('--snmp-file', default='/proc/net/snmp', |
| 23 | + help='Option for testing on MacOS purpose.') |
| 24 | + return parser |
| 25 | + |
| 26 | + def __int(self, line): |
| 27 | + return [self.int(item) for item in line.strip().split()] |
| 28 | + |
| 29 | + def eval(self): |
| 30 | + """ Evaluates difference between snmp metrics """ |
| 31 | + self.diff = deepcopy(self.current) |
| 32 | + for proto, data in iteritems(self.diff): |
| 33 | + for i, metric in enumerate(data): |
| 34 | + _, value = metric |
| 35 | + if isinstance(value, int): |
| 36 | + if self.options.random: |
| 37 | + self.diff[proto][i][1] = randint(0, 1000) |
| 38 | + else: |
| 39 | + self.diff[proto][i][1] -= self.previous[proto][i][1] |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def __listify(list_of_tuples): |
| 43 | + """ |
| 44 | + :param list_of_tuples: list[tuple] |
| 45 | + :return: list[list] |
| 46 | + """ |
| 47 | + return [list(tpl) for tpl in list_of_tuples] |
| 48 | + |
| 49 | + def parse(self): |
| 50 | + """ :returns: dict[proto] = list[list[str(key), int(value)]] """ |
| 51 | + with open(self.options.snmp_file) as file_fd: |
| 52 | + lines = [self.__int(line) for line in file_fd.readlines()] |
| 53 | + return { |
| 54 | + 'IP': self.__listify(zip(lines[0][1:], lines[1][1:])), |
| 55 | + 'ICMP': self.__listify(zip(lines[2][1:], lines[3][1:])), |
| 56 | + 'TCP': self.__listify(zip(lines[6][1:], lines[7][1:])), |
| 57 | + 'UDP': self.__listify(zip(lines[8][1:], lines[9][1:])) |
| 58 | + } |
| 59 | + |
| 60 | + def __repr__(self): |
| 61 | + table = make_table(self.make_header(), self.make_align_map(), self.make_rows()) |
| 62 | + return self.__repr_table__(table) |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def make_header(): |
| 66 | + """ :returns: header for prettytable output (provides unique invisible whitespace-headers) |
| 67 | +
|
| 68 | + 6, 5, 4 spaces are for column blinking avoidance. |
| 69 | + """ |
| 70 | + return ['IP', ' ' * 6, 'TCP', ' ' * 5, 'UDP', ' ' * 4, 'ICMP', ''] |
| 71 | + |
| 72 | + def make_rows(self): |
| 73 | + """ :returns: rows for prettytable output (filled with empty values) """ |
| 74 | + rows = [] |
| 75 | + repr_source = self.repr_source() |
| 76 | + max_len = max(len(subdict) for subdict in repr_source.values()) |
| 77 | + for index in range(max_len): |
| 78 | + row = list() |
| 79 | + for proto in self.protos: |
| 80 | + if index >= len(repr_source[proto]): |
| 81 | + row.extend(['', '']) |
| 82 | + continue |
| 83 | + row.extend([repr_source[proto][index][0], repr_source[proto][index][1]]) |
| 84 | + rows.append(row) |
| 85 | + return rows |
| 86 | + |
| 87 | + def make_align_map(self): |
| 88 | + """ :returns: align map for prettytable output (key <-, value -> for each proto """ |
| 89 | + return ['l', 'r'] * len(self.protos) |
0 commit comments