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 []
def start(self, protocol): """ Execute NTP protocol :param protocol: instance of NtpProtocol """ if IpUtil.get_ip_version(protocol.host) == 6: self.port = reactor.listenUDP(0, protocol, interface='::') else: self.port = reactor.listenUDP(0, protocol)
def process(self, device, results, log): log.info('Modeler %s processing ip services data for device %s', self.name(), device.id) self.log = log rm = self.relMap() if not results.strip(): # No output log.error("No output from the command: %s", self.command) return if '### ss output' not in results: parser = NetstatServicesParser(log) else: parser = SsServicesParser(log) try: maxport = getattr(device, 'zIpServiceMapMaxPort', 1024) maxport = int(maxport) except ValueError: maxport = 1024 rm = self.relMap() ports = {} for proto, port, addr in parser.parse(results): if not IpUtil.isRemotelyReachable( addr) or not port: # Can't monitor things we can't reach continue if port > maxport: log.debug( "Ignoring entry greater than zIpServiceMapMaxPort " "(%s): %s %s %s", maxport, addr, proto, port) continue om = ports.get((proto, port), None) if om: if addr in om.ipaddresses: continue log.debug( "Adding %s to the list of addresses listening " "to %s port %s", addr, proto, port) om.ipaddresses.append(addr) else: om = self.objectMap() om.protocol = proto om.port = int(port) om.id = '%s_%05d' % (om.protocol, om.port) log.debug("Found %s listening to %s port %s (%s)", addr, proto, port, om.id) om.setServiceClass = {'protocol': proto, 'port': om.port} om.ipaddresses = [addr] om.discoveryAgent = self.name() ports[(proto, port)] = om rm.append(om) log.debug("Found %d IPServices", len(ports.keys())) return rm
def process(self, device, results, log): log.info('Modeler %s processing ip services data for device %s', self.name(), device.id) self.log = log rm = self.relMap() if not results.strip(): # No output log.error("No output from the command: %s", self.command) return if '### ss output' not in results: parser = NetstatServicesParser(log) else: parser = SsServicesParser(log) try: maxport = getattr(device, 'zIpServiceMapMaxPort', 1024) maxport = int(maxport) except ValueError: maxport = 1024 rm = self.relMap() ports = {} for proto, port, addr in parser.parse(results): if not IpUtil.isRemotelyReachable(addr) or not port: # Can't monitor things we can't reach continue if port > maxport: log.debug("Ignoring entry greater than zIpServiceMapMaxPort " "(%s): %s %s %s", maxport, addr, proto, port) continue om = ports.get((proto, port), None) if om: if addr in om.ipaddresses: continue log.debug("Adding %s to the list of addresses listening " "to %s port %s", addr, proto, port) om.ipaddresses.append(addr) else: om = self.objectMap() om.protocol = proto om.port = int(port) om.id = '%s_%05d' % (om.protocol, om.port) log.debug("Found %s listening to %s port %s (%s)", addr, proto, port, om.id) om.setServiceClass = {'protocol': proto, 'port': om.port} om.ipaddresses = [addr] om.discoveryAgent = self.name() ports[(proto, port)] = om rm.append(om) log.debug("Found %d IPServices", len(ports.keys())) return rm
def ipAddressString(self): manageIp = self._object.manageIp if manageIp: if "%" in manageIp: address, interface = manageIp.split("%") else: address = manageIp interface = None addr_part = IpUtil.IPAddress(address) if interface is None: addr_string = "%s" % addr_part else: addr_string = "%s%%%s" % (addr_part, interface) else: addr_string = None return addr_string
def parse(self, results): """Parses command output in `results` string and returns services information in format: [(proto, port, addr), ...] """ services = [] for line in results.split("\n"): # tcp UNCONN 0 0 *:68 *:* fields = line.split() if len(fields) != 6: continue try: proto = fields[0] # please note protocol alway be tcp due a bug in RH; # see https://bugzilla.redhat.com/show_bug.cgi?id=1063927 if fields[1] == 'UNCONN': proto = 'udp' addr, port = fields[4].rsplit(":", 1) if addr == '*': addr = '0.0.0.0' else: ip_version = IpUtil.get_ip_version(addr) if ip_version is None: # Not a valid ip continue elif ip_version == 6: proto = proto + '6' self.log.debug("Got %s %s port %s", addr, proto, port) port = int(port) except ValueError: self.log.exception( "Failed to parse IPService information '%s'", line) continue services.append((proto, port, addr)) return services
def parse(self, results): """Parses command output in `results` string and returns services information in format: [(proto, port, addr), ...] """ services = [] for line in results.split("\n"): # tcp UNCONN 0 0 *:68 *:* fields = line.split() if len(fields) != 6: continue try: proto = fields[0] # please note protocol alway be tcp due a bug in RH; # see https://bugzilla.redhat.com/show_bug.cgi?id=1063927 if fields[1] == 'UNCONN': proto = 'udp' addr, port = fields[4].rsplit(":", 1) if addr == '*': addr = '0.0.0.0' else: ip_version = IpUtil.get_ip_version(addr) if ip_version is None: # Not a valid ip continue elif ip_version == 6: proto = proto + '6' self.log.debug("Got %s %s port %s", addr, proto, port) port = int(port) except ValueError: self.log.exception("Failed to parse IPService information '%s'", line) continue services.append((proto, port, addr)) return services
def process(self, device, results, log): log.info('Modeler %s processing data for device %s', self.name(), device.id) if not results.strip(): # No output log.error("No output from the command: %s", self.command) return try: maxport = getattr(device, 'zIpServiceMapMaxPort', 1024) maxport = int(maxport) except ValueError: maxport = 1024 rm = self.relMap() ports = {} for line in results.splitlines(): aline = line.split() if len(aline) < 5: continue try: proto, local = aline[0], aline[3] if proto == "raw": continue addr, port = local.rsplit(":", 1) log.debug("Got %s %s port %s", addr, proto, port) if not IpUtil.isRemotelyReachable(addr) or not port: # Can't monitor things we can't reach continue port = int(port) if port > maxport: log.debug("Ignoring entry greater than " \ "zIpServiceMapMaxPort (%s): %s %s %s", maxport, addr, proto, port) continue except ValueError: log.exception("Failed to parse IPService information '%s'", line) continue om = ports.get((proto, port), None) if om: if addr in om.ipaddresses: continue log.debug( "Adding %s to the list of addresses listening to %s port %s", addr, proto, port) om.ipaddresses.append(addr) else: om = self.objectMap() om.protocol = proto om.port = int(port) om.id = '%s_%05d' % (om.protocol, om.port) log.debug("Found %s listening to %s port %s (%s)", addr, proto, port, om.id) om.setServiceClass = {'protocol': proto, 'port': om.port} om.ipaddresses = [ addr, ] om.discoveryAgent = self.name() ports[(proto, port)] = om rm.append(om) log.debug("Found %d IPServices", len(ports.keys())) return rm
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
def isip(self, ip): return iputil.isip(ip)
def makeConfigKey(self, config, subconfig): return config.id, subconfig.cycleTime, IpUtil.ipunwrap(subconfig.ip)
def getIpAddress(self): if self._object.manageIp: return IpUtil.ipToDecimal(self._object.manageIp)
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
def maskToBits(self, mask): """Return the netmask as number of bits 255.255.255.0 -> 24. """ return iputil.maskToBits(mask)
def process(self, device, results, log): log.info('Modeler %s processing data for device %s', self.name(), device.id) if not results.strip(): # No output log.error("No output from the command: %s", self.command) return try: maxport = getattr(device, 'zIpServiceMapMaxPort', 1024) maxport = int(maxport) except ValueError: maxport = 1024 rm = self.relMap() ports = {} for line in results.splitlines(): aline = line.split() if len(aline) < 5: continue try: proto, local = aline[0], aline[3] if proto == "raw": continue addr, port = local.rsplit(":", 1) log.debug("Got %s %s port %s", addr, proto, port) if not IpUtil.isRemotelyReachable(addr) or not port: # Can't monitor things we can't reach continue port = int(port) if port > maxport: log.debug("Ignoring entry greater than " \ "zIpServiceMapMaxPort (%s): %s %s %s", maxport, addr, proto, port) continue except ValueError: log.exception("Failed to parse IPService information '%s'", line) continue om = ports.get((proto, port), None) if om: if addr in om.ipaddresses: continue log.debug("Adding %s to the list of addresses listening to %s port %s", addr, proto, port) om.ipaddresses.append(addr) else: om = self.objectMap() om.protocol = proto om.port = int(port) om.id = '%s_%05d' % (om.protocol,om.port) log.debug("Found %s listening to %s port %s (%s)", addr, proto, port, om.id) om.setServiceClass = {'protocol': proto, 'port':om.port} om.ipaddresses = [addr,] om.discoveryAgent = self.name() ports[(proto, port)] = om rm.append(om) log.debug("Found %d IPServices", len(ports.keys())) return rm
def hexToBits(self, mask): """Return the netmask as number of bits 0xffffff00 -> 24. """ return iputil.hexToBits(mask)