コード例 #1
0
ファイル: snmp_autoload.py プロジェクト: CoYe/qualisnmp
    def __init__(self, ip, port=161, community='private'):
        """ Initialize SNMP environment and read tables.

        Read entPhysicalTable and ifTable.
        entPhysicalTable is saved in self.entPhysicalTable
        ifTable is saved in self.ifTable

        :param ip: device IP.
        :param port: device SNMP port.
        :param community: device community.
        """
        super(AutoLoad, self).__init__()

        self.snmp = QualiSnmp(ip, port, community)
        self.entPhysicalTable = self.snmp.walk(
            ('ENTITY-MIB', 'entPhysicalTable'))
        self.ifTable = self.snmp.walk(('IF-MIB', 'ifTable'))
コード例 #2
0
    def __init__(self, ip, port=161, community="private"):
        """ Initialize SNMP environment and read tables.

        Read entPhysicalTable and ifTable.
        entPhysicalTable is saved in self.entPhysicalTable
        ifTable is saved in self.ifTable

        :param ip: device IP.
        :param port: device SNMP port.
        :param community: device community.
        """
        super(AutoLoad, self).__init__()

        self.snmp = QualiSnmp(ip, port, community)
        self.entPhysicalTable = self.snmp.walk(("ENTITY-MIB", "entPhysicalTable"))
        self.ifTable = self.snmp.walk(("IF-MIB", "ifTable"))
コード例 #3
0
class AutoLoad(object):
    """ Base class for Quali SNMP based Autoload functionality. """

    autolad_parents = {
        "'port'": ["'module'", "'chassis'"],
        "'powerSupply'": ["'chassis'"],
        "'module'": ["'chassis'"],
        "'container'": ["'chassis'"],
        "'chassis'": [],
    }
    """ Dictionary mapping from autload entity to its valid autoload parents. """

    def __init__(self, ip, port=161, community="private"):
        """ Initialize SNMP environment and read tables.

        Read entPhysicalTable and ifTable.
        entPhysicalTable is saved in self.entPhysicalTable
        ifTable is saved in self.ifTable

        :param ip: device IP.
        :param port: device SNMP port.
        :param community: device community.
        """
        super(AutoLoad, self).__init__()

        self.snmp = QualiSnmp(ip, port, community)
        self.entPhysicalTable = self.snmp.walk(("ENTITY-MIB", "entPhysicalTable"))
        self.ifTable = self.snmp.walk(("IF-MIB", "ifTable"))

    def get_hierarchy(self, *types):
        """
        :return: device autoload hierarchy in the following format:
        |        {root index: [child1 index, child2 index, ...],
        |         child1 index: [child11 index, child12 index, ...]}
        |        ...}
        :todo: add support for multi chassis
        """

        hierarchy = {}
        ports = self.entPhysicalTable.filter_by_column("Class", "'port'")
        pss = self.entPhysicalTable.filter_by_column("Class", "'powerSupply'")
        for entity in dict(ports.items() + pss.items()).values():
            parents = self.get_parents(entity)
            for p in range(len(parents) - 1, 0, -1):
                parent_index = int(parents[p]["suffix"])
                index = int(parents[p - 1]["suffix"])
                if not hierarchy.get(parent_index):
                    hierarchy[parent_index] = []
                hierarchy[parent_index].append(index)
        for parent, childrenin in hierarchy.items():
            hierarchy[parent] = list(set(childrenin))
        return hierarchy

    def get_mapping(self):
        """ Get mapping from entPhysicalTable to ifTable.

        Build mapping based on entAliasMappingTable if exists else build manually based on
        entPhysicalDescr <-> ifDescr mapping.

        :return: simple mapping from entPhysicalTable index to ifTable index:
        |        {entPhysicalTable index: ifTable index, ...}
        """

        mapping = OrderedDict()
        entAliasMappingTable = self.snmp.walk(("ENTITY-MIB", "entAliasMappingTable"))
        if entAliasMappingTable:
            for port in self.entPhysicalTable.filter_by_column("Class", "'port'"):
                entAliasMappingIdentifier = entAliasMappingTable[port]["entAliasMappingIdentifier"]
                mapping[port] = int(entAliasMappingIdentifier.split(".")[-1])
        else:
            mapping = self._descr_based_mapping()

        return mapping

    #
    # Auxiliary public methods.
    #

    def get_parents(self, entity, *_parents):
        """
        :param entity: entity to return parents for.
        :return: autoload parents, up to the chassis, of the requested entity.
        """

        parents_l = list(_parents)
        parents_l.append(entity)
        if entity["entPhysicalClass"] == "'chassis'":
            return parents_l
        else:
            return self.get_parents(self.get_parent(entity), *parents_l)

    def get_parent(self, entity):
        """
        :param entity: entity to return parent for.
        :return: autoload parent of the requested entity.
        """

        parent = self.entPhysicalTable[int(entity["entPhysicalContainedIn"])]
        if parent["entPhysicalClass"] in self.autolad_parents[entity["entPhysicalClass"]]:
            return parent
        else:
            return self.get_parent(parent)

    def _descr_based_mapping(self):
        """ Manually calculate mapping from entityTable to ifTable.

        :return: simple mapping from entPhysicalTable index to ifTable index:
        |        {entPhysicalTable index: ifTable index, ...}
        """

        mapping = OrderedDict()
        for port in self.entPhysicalTable.filter_by_column("Class", "'port'").values():
            entPhysicalDescr = port["entPhysicalDescr"]
            module_index, port_index = re.findall("\d+", entPhysicalDescr)
            ifTable_re = ".*" + module_index + "/" + port_index
            for interface in self.ifTable.values():
                if re.search(ifTable_re, interface["ifDescr"]):
                    mapping[int(port["suffix"])] = int(interface["suffix"])
                    continue
        return mapping
コード例 #4
0
ファイル: test.py プロジェクト: QualiSystems/qualisnmp
 def setUp(self):
     self.snmp = QualiSnmp(ip=ip, community=ro_community)
コード例 #5
0
ファイル: test.py プロジェクト: QualiSystems/qualisnmp
class SnmpTest(TestCase):
    """ SNMP class tests.

    :todo: test with assert.
    """

    def setUp(self):
        self.snmp = QualiSnmp(ip=ip, community=ro_community)

    def testGet(self):

        values = self.snmp.get(('ENTITY-MIB', 'entPhysicalDescr', 7))
        print values

        values = self.snmp.get('1.3.6.1.2.1.1.4.0', '1.3.6.1.2.1.1.1.0')
        print values

        values = self.snmp.get('1.3.6.1.2.1.1.4', '1.3.6.1.2.1.1.1')
        print values

        values = self.snmp.get(('SNMPv2-MIB', 'sysContact'), ('SNMPv2-MIB', 'sysDescr'))
        print values

        values = self.snmp.get(('SNMPv2-MIB', 'sysContact', 0), ('SNMPv2-MIB', 'sysDescr', 0))
        print values

        values = self.snmp.get(('SNMPv2-MIB', 'sysContact', 0), '1.3.6.1.2.1.1.1')
        print values

        values = self.snmp.get(('IF-MIB', 'ifPhysAddress', 7))
        print values

        pass

    def testNext(self):

        values = self.snmp.next(('SNMPv2-MIB', 'sysContact'))
        print values

        values = self.snmp.next(('ENTITY-MIB', 'entPhysicalTable'))
        print values

        pass

    def testWalk(self):

        # Get all entries.
        values = self.snmp.walk(('ENTITY-MIB', 'entPhysicalTable'))
        print values
        # Get column (an attribute for for entries).
        values = self.snmp.walk(('ENTITY-MIB', 'entPhysicalDescr'))
        print values
        # Get row (all attribute of an index).
        values = self.snmp.walk(('ENTITY-MIB', 'entPhysicalTable'), 7)
        print values
        # And even a single cell.
        values = self.snmp.walk(('ENTITY-MIB', 'entPhysicalDescr'), 7)
        print values

        load_mib('IP-MIB')
        values = self.snmp.walk(('IP-MIB', 'ipAddrTable'))
        print values

        values = self.snmp.walk(('IF-MIB', 'ifTable'))
        print values

        values = self.snmp.walk(('IF-MIB', 'ifPhysAddress'))
        print values

        pass

    def testTable(self):
        # Get all entries.
        table = self.snmp.walk(('ENTITY-MIB', 'entPhysicalTable'))
        for row in table.get_rows(10, 11).items():
            print row
        for row in table.get_columns('Class', 'Name').items():
            print row
        for row in table.filter_by_column('Class', '3').items():
            print row
        for row in table.filter_by_column('Class', '3', '10').items():
            print row
        for row in table.sort_by_column('ParentRelPos').items():
            print row
        pass
コード例 #6
0
ファイル: snmp_autoload.py プロジェクト: CoYe/qualisnmp
class AutoLoad(object):
    """ Base class for Quali SNMP based Autoload functionality. """

    autolad_parents = {
        "'port'": ["'module'", "'chassis'"],
        "'powerSupply'": ["'chassis'"],
        "'module'": ["'chassis'"],
        "'container'": ["'chassis'"],
        "'chassis'": []
    }
    """ Dictionary mapping from autload entity to its valid autoload parents. """
    def __init__(self, ip, port=161, community='private'):
        """ Initialize SNMP environment and read tables.

        Read entPhysicalTable and ifTable.
        entPhysicalTable is saved in self.entPhysicalTable
        ifTable is saved in self.ifTable

        :param ip: device IP.
        :param port: device SNMP port.
        :param community: device community.
        """
        super(AutoLoad, self).__init__()

        self.snmp = QualiSnmp(ip, port, community)
        self.entPhysicalTable = self.snmp.walk(
            ('ENTITY-MIB', 'entPhysicalTable'))
        self.ifTable = self.snmp.walk(('IF-MIB', 'ifTable'))

    def get_hierarchy(self, *types):
        """
        :return: device autoload hierarchy in the following format:
        |        {root index: [child1 index, child2 index, ...],
        |         child1 index: [child11 index, child12 index, ...]}
        |        ...}
        :todo: add support for multi chassis
        """

        hierarchy = {}
        ports = self.entPhysicalTable.filter_by_column('Class', "'port'")
        pss = self.entPhysicalTable.filter_by_column('Class', "'powerSupply'")
        for entity in dict(ports.items() + pss.items()).values():
            parents = self.get_parents(entity)
            for p in range(len(parents) - 1, 0, -1):
                parent_index = int(parents[p]['suffix'])
                index = int(parents[p - 1]['suffix'])
                if not hierarchy.get(parent_index):
                    hierarchy[parent_index] = []
                hierarchy[parent_index].append(index)
        for parent, childrenin in hierarchy.items():
            hierarchy[parent] = list(set(childrenin))
        return hierarchy

    def get_mapping(self):
        """
        :return: simple mapping from entPhysicalTable index to ifTable index:
        |        {entPhysicalTable index: ifTable index, ...}
        :todo: add support for modules...
        """

        mapping = OrderedDict()
        entAliasMappingTable = self.snmp.walk(
            ('ENTITY-MIB', 'entAliasMappingTable'))
        for port in self.entPhysicalTable.filter_by_column('Class', "'port'"):
            mapping[port] = int(entAliasMappingTable[port]
                                ['entAliasMappingIdentifier'].split('.')[-1])

        return mapping

    #
    # Auxiliary public methods.
    #

    def get_parents(self, entity, *_parents):
        """
        :param entity: entity to return parents for.
        :return: autoload parents, up to the chassis, of the requested entity.
        """

        parents_l = list(_parents)
        parents_l.append(entity)
        if entity['entPhysicalClass'] == "'chassis'":
            return parents_l
        else:
            return self.get_parents(self.get_parent(entity), *parents_l)

    def get_parent(self, entity):
        """
        :param entity: entity to return parent for.
        :return: autoload parent of the requested entity.
        """

        parent = self.entPhysicalTable[int(entity['entPhysicalContainedIn'])]
        if parent['entPhysicalClass'] in self.autolad_parents[
                entity['entPhysicalClass']]:
            return parent
        else:
            return self.get_parent(parent)