def _extractAddressAndPort(self, oid):
        try:
            oid_parts = oid.split('.')
            addr_type = int(oid_parts[0])
            port = int(oid_parts[-1])
            addr_parts = oid_parts[1:-1]
            result = []

            if len(addr_parts) != addr_type:
                log.debug("Invalid oid %s", oid) 
                return []
            if not self.PORT_MIN <= port <= self.PORT_MAX:
                log.debug("Port value exceeds range for oid %s", oid)
                return []

            addr = IpUtil.bytesToCanonIp(addr_parts)
            if not self.ignoredAddr(addr):
                log.debug("Adding addr: %s" % addr)
                result.append((addr, port))
            else:
                log.debug("Ignoring addr: %s" % addr)

            # Any IPv6 address is inclusive of any IPv4 addresses as well
            if addr == IpUtil.IPV6_ANY_ADDRESS:
                result.append((IpUtil.IPV4_ANY_ADDRESS, port))
        except Exception, e:
            log.debug("Unable to process oid %s: %s", oid, e)
            return []
예제 #2
0
    def process(self, device, results, log):
        """
        From SNMP info gathered from the device, convert them
        to interface objects.
        """
        getdata, tabledata = results
        log.info('Modeler %s processing data for device %s', self.name(), device.id)
        log.debug("%s tabledata = %s", device.id, tabledata)
        rm = self.relMap()
        iptable = tabledata.get("ipAddrTable")
        sourceTable = 'ipAddrTable'
        if not iptable:
            iptable = tabledata.get("ipNetToMediaTable")
            if iptable:
                log.info("Unable to use ipAddrTable -- using ipNetToMediaTable instead")
                sourceTable = 'ipNetToMediaTable'
            else:
                log.warn("Unable to get data for %s from either ipAddrTable or"
                          " ipNetToMediaTable" % device.id)
                iptable = dict()

        # Add in IPv6 info
        ipv6table = tabledata.get("ipAddressIfIndex")
        if ipv6table:
            iptable.update(ipv6table)

        iftable = tabledata.get("iftable")
        if iftable is None:
            log.error("Unable to get data for %s for iftable -- skipping model" % device.id)
            return None

        ifalias = tabledata.get("ifalias", {})

        self.prepIfTable(log, iftable, ifalias)

        omtable = {}
        duplex = tabledata.get("duplex", {})
        for key, iface in iftable.items():
            if key in duplex:
                iftable[key]['duplex'] = duplex[key].get('duplex', 0)
            else:
                iftable[key]['duplex'] = 0

        for ip, row in iptable.items():
            #FIXME - not getting ifindex back from HP printer
            if 'ifindex' not in row:
                log.debug( "IP entry for %s is missing ifindex" % ip)
                continue

            ip_parts = ip.split('.')
            # If the ipAddrTable key has five octets, that probably
            # means this is a classless subnet (that is, <256).  Usually,
            # the first 4 octets will be the ipAddress we care about.
            # Regardless, we will be using the ip address in the row
            # later anyway.

            try:
                if len(ip_parts) == 5 and sourceTable == 'ipAddrTable':
                    addr_type = IpUtil.IPV4_ADDR_TYPE
                    ip = IpUtil.bytesToCanonIp(ip_parts[:-1])

                # If we are using the ipNetToMediaTable, we use the
                # last 4 octets.
                elif len(ip_parts) == 5 and sourceTable == 'ipNetToMediaTable':
                    addr_type = IpUtil.IPV4_ADDR_TYPE
                    if row['iptype'] != 1:
                        log.debug("iptype (%s) is not 1 -- skipping" % (
                                 row['iptype'] ))
                        continue
                    ip = IpUtil.bytesToCanonIp(ip_parts[1:])
                    log.warn("Can't find netmask -- using /24")
                    row['netmask'] = '255.255.255.0'

                elif len(ip_parts) == 16:
                    addr_type = IpUtil.IPV6_ADDR_TYPE
                    ip = IpUtil.bytesToCanonIp(ip_parts)

            except Exception:
                log.warn("The %s address for ifindex %s is incorrect: %s",
                         addr_type, row['ifindex'], ip)
                continue

            strindex = str(row['ifindex'])
            if strindex not in omtable and strindex not in iftable:
                log.warn("Skipping %s as it points to missing ifindex %s",
                            row.get('ipAddress',""), row.get('ifindex',""))
                continue

            if strindex not in omtable:
                om = self.processInt(log, device, iftable[strindex])
                if not om:
                    continue
                rm.append(om)
                omtable[strindex] = om
                del iftable[strindex]
            elif strindex in omtable:
                om = omtable[strindex]
            else:
                log.warn("The IP %s points to missing ifindex %s -- skipping" % (
                         ip, strindex) )
                continue

            if not hasattr(om, 'setIpAddresses'):
                om.setIpAddresses = []
            if 'ipAddress' in row:
                ip = row['ipAddress']
            if 'netmask' in row:
                ip = ip + "/" + str(self.maskToBits(row['netmask'].strip()))

            # Ignore IP addresses with a 0.0.0.0 netmask.
            if ip.endswith("/0"):
                log.warn("Ignoring IP address with 0.0.0.0 netmask: %s", ip)
            else:
                om.setIpAddresses.append(ip)

        for iface in iftable.values():
            om = self.processInt(log, device, iface)
            if om:
                rm.append(om)

        return rm
예제 #3
0
    def process(self, device, results, log):
        """
        From SNMP info gathered from the device, convert them
        to interface objects.
        """
        getdata, tabledata = results
        log.info('Modeler %s processing data for device %s', self.name(),
                 device.id)
        log.debug("%s tabledata = %s", device.id, tabledata)
        rm = self.relMap()
        iptable = tabledata.get("ipAddrTable")
        sourceTable = 'ipAddrTable'
        if not iptable:
            iptable = tabledata.get("ipNetToMediaTable")
            if iptable:
                log.info(
                    "Unable to use ipAddrTable -- using ipNetToMediaTable instead"
                )
                sourceTable = 'ipNetToMediaTable'
            else:
                log.warn("Unable to get data for %s from either ipAddrTable or"
                         " ipNetToMediaTable" % device.id)
                iptable = dict()

        # Add in IPv6 info
        ipv6table = tabledata.get("ipAddressIfIndex")
        if ipv6table:
            iptable.update(ipv6table)

        iftable = tabledata.get("iftable")
        if iftable is None:
            log.error(
                "Unable to get data for %s for iftable -- skipping model" %
                device.id)
            return None

        ifalias = tabledata.get("ifalias", {})

        self.prepIfTable(log, iftable, ifalias)

        omtable = {}
        duplex = tabledata.get("duplex", {})
        for key, iface in iftable.items():
            if key in duplex:
                iftable[key]['duplex'] = duplex[key].get('duplex', 0)
            else:
                iftable[key]['duplex'] = 0

        for ip, row in iptable.items():
            #FIXME - not getting ifindex back from HP printer
            if 'ifindex' not in row:
                log.debug("IP entry for %s is missing ifindex" % ip)
                continue

            ip_parts = ip.split('.')
            # If the ipAddrTable key has five octets, that probably
            # means this is a classless subnet (that is, <256).  Usually,
            # the first 4 octets will be the ipAddress we care about.
            # Regardless, we will be using the ip address in the row
            # later anyway.

            try:
                if len(ip_parts) == 5 and sourceTable == 'ipAddrTable':
                    addr_type = IpUtil.IPV4_ADDR_TYPE
                    ip = IpUtil.bytesToCanonIp(ip_parts[:-1])

                # If we are using the ipNetToMediaTable, we use the
                # last 4 octets.
                elif len(ip_parts) == 5 and sourceTable == 'ipNetToMediaTable':
                    addr_type = IpUtil.IPV4_ADDR_TYPE
                    if row['iptype'] != 1:
                        log.debug("iptype (%s) is not 1 -- skipping" %
                                  (row['iptype']))
                        continue
                    ip = IpUtil.bytesToCanonIp(ip_parts[1:])
                    log.warn("Can't find netmask -- using /24")
                    row['netmask'] = '255.255.255.0'

                elif len(ip_parts) == 16:
                    addr_type = IpUtil.IPV6_ADDR_TYPE
                    ip = IpUtil.bytesToCanonIp(ip_parts)

            except Exception:
                log.warn("The %s address for ifindex %s is incorrect: %s",
                         addr_type, row['ifindex'], ip)
                continue

            strindex = str(row['ifindex'])
            if strindex not in omtable and strindex not in iftable:
                log.warn("Skipping %s as it points to missing ifindex %s",
                         row.get('ipAddress', ""), row.get('ifindex', ""))
                continue

            if strindex not in omtable:
                om = self.processInt(log, device, iftable[strindex])
                if not om:
                    continue
                rm.append(om)
                omtable[strindex] = om
                del iftable[strindex]
            elif strindex in omtable:
                om = omtable[strindex]
            else:
                log.warn("The IP %s points to missing ifindex %s -- skipping" %
                         (ip, strindex))
                continue

            if not hasattr(om, 'setIpAddresses'):
                om.setIpAddresses = []
            if 'ipAddress' in row:
                ip = row['ipAddress']
            if 'netmask' in row:
                ip = ip + "/" + str(self.maskToBits(row['netmask'].strip()))

            # Ignore IP addresses with a 0.0.0.0 netmask.
            if ip.endswith("/0"):
                log.warn("Ignoring IP address with 0.0.0.0 netmask: %s", ip)
            else:
                om.setIpAddresses.append(ip)

        for iface in iftable.values():
            om = self.processInt(log, device, iface)
            if om:
                rm.append(om)

        return rm