Exemple #1
0
class CiFlushingImporter(CiImporter):
    """
    Main class that performs importing. It iterates the data source and for each row
    uses RowMapper to create the OSH. Each created OSH goes to vector.
    """

    def __init__(self, dataSource, ciMapping, Framework, skipEmptyValues = 0):
        CiImporter.__init__(self, dataSource, ciMapping, Framework, skipEmptyValues)
        #How many CI's are contained in each "sendObjects" call
        self.bulk_size = 2000
        #Internal Vector used to buffer results prior to sending.
        self.internal_vector = ObjectStateHolderVector()
        #The current count of CI's in the internal_vector
        self.counter = 0

    def flushObjects(self):
        self.Framework.sendObjects(self.internal_vector)
        self.Framework.flushObjects()
        self.internal_vector.clear()
        self.counter = 0

    def createCis(self):
        try:
            self.dataSource.open()
            while self.dataSource.next():
                osh = self.createCi(self.rowMapper, self.dataSource)
                if osh:
                    #Add CI to the internal vector
                    self.internal_vector.add(osh)
                    #Increment the counter
                    self.counter += 1
                    #Check if we are ready to flush the internal_vector
                    if self.counter >= self.bulk_size:
                        self.flushObjects()

        finally:
            #Check for any remaining items if we finished before reaching another bulk.
            if self.counter > 0:
                self.flushObjects()
            self.dataSource.close()
        # Return empty vector since all data were sent
        return ObjectStateHolderVector()
Exemple #2
0
class CiFlushingImporter(CiImporter):
    """
    Main class that performs importing. It iterates the data source and for each row
    uses RowMapper to create the OSH. Each created OSH goes to vector.
    """
    def __init__(self, dataSource, ciMapping, Framework, skipEmptyValues=0):
        CiImporter.__init__(self, dataSource, ciMapping, Framework,
                            skipEmptyValues)
        #How many CI's are contained in each "sendObjects" call
        self.bulk_size = 2000
        #Internal Vector used to buffer results prior to sending.
        self.internal_vector = ObjectStateHolderVector()
        #The current count of CI's in the internal_vector
        self.counter = 0

    def flushObjects(self):
        self.Framework.sendObjects(self.internal_vector)
        self.Framework.flushObjects()
        self.internal_vector.clear()
        self.counter = 0

    def createCis(self):
        try:
            self.dataSource.open()
            while self.dataSource.next():
                osh = self.createCi(self.rowMapper, self.dataSource)
                if osh:
                    #Add CI to the internal vector
                    self.internal_vector.add(osh)
                    #Increment the counter
                    self.counter += 1
                    #Check if we are ready to flush the internal_vector
                    if self.counter >= self.bulk_size:
                        self.flushObjects()

        finally:
            #Check for any remaining items if we finished before reaching another bulk.
            if self.counter > 0:
                self.flushObjects()
            self.dataSource.close()
        # Return empty vector since all data were sent
        return ObjectStateHolderVector()
Exemple #3
0
def DiscoveryMain(Framework):
    ipAddress = Framework.getDestinationAttribute('ip_address')
    discoveredPorts = Framework.getParameter('ports') or None
    useNMap = Framework.getParameter('useNMap') == 'true'
    nmapPath = Framework.getParameter('nmapPath') or None
    scanUDP = Framework.getParameter('scanUDP') == 'true'
    UDPports = Framework.getParameter('UDPports') or None
    UDPports = UDPports and UDPports.strip()
    connectTimeOut = int(Framework.getParameter('connectTimeOut'))

    #if we need to check host's reachability:
    if Framework.getParameter('checkIfIpIsReachable').lower() == 'true':
        if not netutils.pingIp(Framework, ipAddress,
                               Framework.getParameter('pingTimeOut')):
            logger.debug('Could not connect to ', ipAddress, ' by ping')
            msg = 'Target host is not reachable'
            warningObject = errorobject.createError(
                errorcodes.CONNECTION_FAILED_NO_PROTOCOL_WITH_DETAILS,
                ['', msg], msg)
            logger.reportWarningObject(warningObject)
            return

    OSHVResult = ObjectStateHolderVector()
    hostOsh = modeling.createHostOSH(ipAddress)
    OSHVResult.add(hostOsh)

    cfgFile = Framework.getConfigFile(
        CollectorsParameters.KEY_COLLECTORS_SERVERDATA_PORTNUMBERTOPORTNAME)

    onlyKnownPorts = Framework.getParameter('checkOnlyKnownPorts')
    onlyKnownPorts = (onlyKnownPorts and onlyKnownPorts.lower() == 'true')

    portsList = getPorts(discoveredPorts and discoveredPorts.strip(),
                         PortType.TCP, cfgFile, onlyKnownPorts)
    if scanUDP:
        if onlyKnownPorts and not UDPports:
            UDPports = '*'
        portsList.extend(
            getPorts(UDPports, PortType.UDP, cfgFile, onlyKnownPorts))

    portsToDiscover = filter(lambda port: port.isDiscover, portsList)

    isConnectedPortFound = False
    useFallback = False
    if useNMap:
        # Nmap flow supports udp and tcp ports
        client = Framework.createClient(
            ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME)
        try:
            shell = shellutils.ShellFactory().createShell(client)
            fs = file_system.createFileSystem(shell)
            try:
                if nmapPath and fs.isDirectory(nmapPath):
                    path_tool = NtPath()
                    nmapPath = path_tool.join(nmapPath,
                                              nmap.NMAP_EXECUTABLES[1])
            except PathNotFoundException:
                logger.warn("Specified directory \"%s\" is not exists." %
                            nmapPath)

            if nmapPath and not nmap.NmapPathValidator.get(fs).validate(
                    nmapPath):
                logger.warn(
                    "Specified Nmap path \"%s\" is not exists. Trying the system path..."
                    % nmapPath)
                nmapPath = None

            nmapDiscover = nmap.getByShell(shell, nmapPath)

            nmapVersion = nmapDiscover.getVersion()
            if not nmapVersion:
                raise Exception('Cannot get nmap version')
            logger.debug("Found nmap %s" % nmapVersion)
            nmapVersion = float(nmapVersion)
            if nmapVersion < 5.21:
                raise Exception("Not supported version of nmap found.")

            tcpPorts = [
                port.getPortNumber() for port in portsToDiscover
                if port and port.getProtocolName() == 'tcp'
                and port.isIpInRange(ipAddress)
            ]
            udpPorts = [
                port.getPortNumber() for port in portsToDiscover
                if port and port.getProtocolName() == 'udp'
                and port.isIpInRange(ipAddress)
            ]

            discoveredPorts = nmapDiscover.doPortScan(ipAddress, tcpPorts,
                                                      udpPorts)

            portsNameByPortInfo = {}
            for port in portsToDiscover:
                port_names = portsNameByPortInfo.setdefault(
                    (port.getProtocol(), port.getPortNumber()), [])
                port_names.append(port.portName)

            if discoveredPorts:
                isConnectedPortFound = True
                for port_info in discoveredPorts:
                    port_names = portsNameByPortInfo.get(port_info, [])
                    OSHVResult.addAll(
                        reportPort(hostOsh, ipAddress, port_names, *port_info))
        except:
            logger.debugException(
                "Nmap executing failed. Try to use default behavior...")
            logger.reportWarning("Nmap executing failed")
            useFallback = True

    if useFallback or not useNMap:
        # Old flow supports only TCP ports
        for port in portsToDiscover:
            if port.isIpInRange(ipAddress):
                if port.getProtocol() == PortType.UDP.getProtocol():
                    logger.warn(
                        "UDP port scan is not supporting by default behavior. Skipping..."
                    )
                elif port.getProtocol() == PortType.TCP.getProtocol() and (
                        netutils.checkTcpConnectivity(
                            ipAddress, port.getPortNumber(), connectTimeOut)):
                    OSHVResult.addAll(
                        reportPort(hostOsh, ipAddress, [port.portName],
                                   port.getProtocol(), port.getPortNumber()))
                    #we found one connected port -> we need to add hostOsh to OSHVResult
                    isConnectedPortFound = True

    #in case we didn't find any port, return nothing
    if not isConnectedPortFound:
        OSHVResult.clear()
        msg = 'None of specified ports were discovered on destination host'
        warningObject = errorobject.createError(
            errorcodes.CONNECTION_FAILED_NO_PROTOCOL_WITH_DETAILS, ['', msg],
            msg)
        logger.reportWarningObject(warningObject)

    return OSHVResult
def doWMI(client, wmiOSH, ip_address, ip_domain, hostForLinkOSH, host_cmdbid=None, host_key=None, host_macs=None, ucmdb_version=None):
    '''@types: WmiClient, ObjectStateHolder, IPAddress, str, ObjectStateHolder, str, str, list[str], int -> ObjectStateHolderVector
    @param ip_address: Destination IP address
    '''
    wmiProvider = wmiutils.getWmiProvider(client)
    hostDiscoverer = host_win_wmi.WmiHostDiscoverer(wmiProvider)
    hostInfo = hostDiscoverer.discoverHostInfo()
    machineName = hostInfo.hostName

    interfacesDiscover = networking_win_wmi.WmiInterfaceDiscoverer(wmiProvider,
                                                ip_address)

    vector = ObjectStateHolderVector()
    interfaceList = interfacesDiscover.getInterfaces()
    parentLinkList = ObjectStateHolderVector()

    isVirtual = 1
    resultEmpty = 1
    interfacesToUpdateList = []
    for interface in interfaceList:
        ips = interface.ips
        MACAddress = interface.macAddress
        masks = interface.masks
        Description = interface.description
        dhcpEnabled = interface.dhcpEnabled

        resultEmpty = 0
        for ipIndex, ipAddress in enumerate(__iterate_valid_ips(ips)):
            IPSubnet = (masks and masks[ipIndex]) or None
            if str(ip_address) == str(ipAddress):
                isVirtual = 0

            ipOSH = modeling.createIpOSH(ipAddress)

            # Test if the same ip and interface are in the list allready

            logger.debug('Found ip address: ', ipAddress, ', MACAddress: ', MACAddress, ', IPSubnet: ', IPSubnet, ', Description: ', Description)
                # create OSH for IP and add it to the list
                # create OSH for Network and add it to the list
            __vector = getIPNetworkMemebrList(ipAddress, IPSubnet, '', dhcpEnabled, Description)
            if __vector.size() > 0:
                vector.addAll(__vector)

            if netutils.isValidMac(MACAddress):
                # create link interface to its ip only it has an ip defined
                interfaceOSH = modeling.createInterfaceOSH(MACAddress, hostForLinkOSH, Description)
                parentLinkList.add(modeling.createLinkOSH('containment', interfaceOSH, ipOSH))
                interfacesToUpdateList.append(interfaceOSH)

    # Check if the Input IP is virtual, we do not want to return the WMI
    if isVirtual == 1:
        logger.warn('Destination is not among discovered IPs assuming virtual. WMI object will not be reported.')
        vIPOSH = modeling.createIpOSH(ip_address)
        vIPOSH.setBoolAttribute('isvirtual', 1)
        vector.add(vIPOSH)

    if resultEmpty == 1:
        logger.warn('WMI was able to connect, but WMI Query returns no results')
        vector.clear()
        return vector

    # create the host and all the objects
    if len(interfaceList) > 0:
        hostOSH = None
        try:
            hostOSH = modeling.createCompleteHostOSHByInterfaceList('nt',
                            interfaceList, 'Windows', machineName, None,
                            host_cmdbid, host_key, host_macs, ucmdb_version)
        except:
            hostOSH = modeling.createHostOSH(str(ip_address), 'nt')
            logger.debugException('Could not find a valid MAC address for key on ip : %s. Creating incomplete host\n' % ip_address)
            logger.warn('Could not find a valid MAC address for key on ip : %s. Creating incomplete host\n' % ip_address)

        # select from Win32_OperatingSystem
        _wmiQuery = 'select Caption,Version,ServicePackMajorVersion,ServicePackMinorVersion,BuildNumber,Organization,RegisteredUser,TotalVisibleMemorySize,LastBootUpTime,OtherTypeDescription,description from Win32_OperatingSystem'
        resultSet = client.executeQuery(_wmiQuery)  # @@CMD_PERMISION wmi protocol execution
        osinstalltype = None
        if resultSet.next():
            Caption = resultSet.getString(1)
            Version = resultSet.getString(2)
            ServicePackMajorVersion = resultSet.getString(3)
            ServicePackMinorVersion = resultSet.getString(4)
            BuildNumber = resultSet.getString(5)
            Organization = resultSet.getString(6)
            RegisteredUser = resultSet.getString(7)
            TotalVisibleMemorySize = resultSet.getString(8)
            LastBootUpTime = resultSet.getString(9)
            OtherTypeDescription = resultSet.getString(10)
            description = resultSet.getString(11)

            (vendor, osName, osinstalltype) = separateCaption(Caption, OtherTypeDescription)
            hostOSH.setAttribute('host_vendor', vendor)
            modeling.setHostOsName(hostOSH, osName)
            hostOSH.setAttribute('host_osinstalltype', osinstalltype)

            setLastBootUpTime(hostOSH, LastBootUpTime)
            biosUUID = getBIOSUUID(client)
            defaultGateway = getDefaultGateway(client)

            hostOSH.setAttribute('host_osversion', Version)
            sp = ServicePackMajorVersion + '.' + ServicePackMinorVersion
            if checkSpVersion(sp):
                hostOSH.setAttribute('nt_servicepack', sp)
            hostOSH.setAttribute('host_osrelease', str(BuildNumber))
            hostOSH.setAttribute('nt_registrationorg', Organization)
            hostOSH.setAttribute('nt_registeredowner', RegisteredUser)
            hostOSH.setAttribute('nt_physicalmemory', TotalVisibleMemorySize)
            hostOSH.setAttribute('host_hostname', machineName)
            modeling.setHostBiosUuid(hostOSH, biosUUID)
            modeling.setHostDefaultGateway(hostOSH, defaultGateway)
            modeling.setHostOsFamily(hostOSH, 'windows')

            hostOSH = HostBuilder(hostOSH).setDescription(description).build()

        _wmiQuery2 = 'select Manufacturer,NumberOfProcessors,Model,Domain from Win32_ComputerSystem'
        resultSet = client.executeQuery(_wmiQuery2)  # @@CMD_PERMISION wmi protocol execution

        if resultSet.next():
            Manufacturer = resultSet.getString(1)
            if ((Manufacturer != None) and (Manufacturer.find('system manufacturer') == -1)):
                modeling.setHostManufacturerAttribute(hostOSH, Manufacturer.strip())
            NumberOfProcessors = resultSet.getString(2)
            hostOSH.setAttribute('nt_processorsnumber', int(NumberOfProcessors))
            Model = resultSet.getString(3)
            modeling.setHostModelAttribute(hostOSH, Model)
            osDomain = resultSet.getString(4)
            hostOSH.setAttribute('host_osdomain', osDomain.strip())

        biosAssetTag = hostDiscoverer.getBiosAssetTag()
        if biosAssetTag:
            hostOSH.setAttribute('bios_asset_tag', biosAssetTag)

        _wmiQuery4 = 'SELECT SerialNumber FROM Win32_BIOS'
        resultSet = client.executeQuery(_wmiQuery4)  # @@CMD_PERMISSION wmi protocol execution
        if resultSet.next():
            serialNumber = processSerialNumber(resultSet.getString(1))
            if not serialNumber:
                wmiBaseBoardSerialNumber = 'SELECT SerialNumber FROM Win32_BaseBoard'
                resultSet = client.executeQuery(wmiBaseBoardSerialNumber)  # @@CMD_PERMISION wmi protocol execution
                serialNumber = processSerialNumber(str(resultSet))
            modeling.setHostSerialNumberAttribute(hostOSH, serialNumber)

        try:
            paeEnabled = getPAEState(client)
            if paeEnabled and paeEnabled.lower() in ['1', 'true']:
                hostOSH.setBoolAttribute('pae_enabled', 1)
            elif paeEnabled and paeEnabled.lower() in ['0', 'false']:
                hostOSH.setBoolAttribute('pae_enabled', 0)
        except Exception, ex:
            logger.warn('Failed getting PAE state. %s' % ex)

        try:
            osArchitecture = getOsArchitecture(client)
            if osinstalltype and osinstalltype.find('64') != -1:
                osArchitecture = '64-bit'
            if osArchitecture:
                hostOSH.setStringAttribute('os_architecture', osArchitecture)
        except Exception, ex:
            logger.warn('Failed getting OS Architecture value. %s' % ex)
Exemple #5
0
class TopologyBuilder:

    def __init__(self, interfacesList, hostDo, destinationIp, ntcmdObj,
                  dnsServerIpList=[], dhcpServerIpList=[],
                  winsServerIpList=[], hostCmdbId=None, hostKey=None,
                  hostMacs=None, ucmdbVersion=None):
        self.resultVector = ObjectStateHolderVector()
        self.dnsServerIpList = dnsServerIpList
        self.dhcpServerIpList = dhcpServerIpList
        self.winsServerIpList = winsServerIpList
        self.interfacesList = interfacesList
        self.hostDo = hostDo
        self.hostOsh = None
        self.hostCmdbId = hostCmdbId
        self.hostKey = hostKey
        self.hostMacs = hostMacs
        self.ucmdbVersion = ucmdbVersion
        self.destinationIp = destinationIp
        self.ntcmdObj = ntcmdObj

    def build(self):
        self.buildDnsServers()
        self.buildDhcpServers()
        self.buildWinsServers()
        self.buildInterfaces()
        self.setHostAttributes()

    def setDhcpDiscovererResults(self, dhcpServerIpList):
        self.dhcpServerIpList = dhcpServerIpList

    def setWinsDiscovererResults(self, winsServerIpList):
        self.winsServerIpList = winsServerIpList

    def buildWinsServers(self):
        if self.winsServerIpList:
            for winsIpAddr in self.winsServerIpList:
                if ip_addr.isValidIpAddress(winsIpAddr, filter_client_ip=True):
                    winsHostOsh = modeling.createHostOSH(str(winsIpAddr))
                    winsAppOsh = modeling.createWinsOsh(str(winsIpAddr), winsHostOsh)
                    self.resultVector.add(winsHostOsh)
                    self.resultVector.add(winsAppOsh)

    def buildDhcpServers(self):
        if self.dhcpServerIpList:
            for dhcpIpAddr in self.dhcpServerIpList:
                if ip_addr.isValidIpAddress(dhcpIpAddr, filter_client_ip=True):
                    dhcpHostOsh = modeling.createHostOSH(str(dhcpIpAddr))
                    dhcpAppOsh = modeling.createDhcpOsh(str(dhcpIpAddr), dhcpHostOsh)
                    self.resultVector.add(dhcpHostOsh)
                    self.resultVector.add(dhcpAppOsh)

    def setHostAttributes(self):
        if self.hostOsh:
            if self.hostDo.hostName:
                self.hostOsh.setAttribute('host_hostname', self.hostDo.hostName)
            if self.hostDo.hostOsName:
                modeling.setHostOsName(self.hostOsh, self.hostDo.hostOsName)
            if self.hostDo.description:
                self.hostOsh = modeling.HostBuilder(self.hostOsh).setDescription(self.hostDo.description).build()
            if self.hostDo.servicePack:
                self.hostOsh.setAttribute('nt_servicepack', self.hostDo.servicePack)
            if self.hostDo.buildNumber:
                self.hostOsh.setAttribute('host_osrelease', self.hostDo.buildNumber)
            if self.hostDo.ntVersion:
                self.hostOsh.setAttribute('host_osversion', self.hostDo.ntVersion)
            if self.hostDo.installType:
                self.hostOsh.setAttribute('host_osinstalltype', self.hostDo.installType)
            if self.hostDo.vendor:
                self.hostOsh.setAttribute('host_vendor', self.hostDo.vendor)
            if self.hostDo.registeredOwner:
                self.hostOsh.setAttribute('nt_registeredowner', self.hostDo.registeredOwner)
            if self.hostDo.organization:
                self.hostOsh.setStringAttribute('nt_registrationorg', self.hostDo.organization)
            if self.hostDo.physicalMemory:
                self.hostOsh.setAttribute('nt_physicalmemory', self.hostDo.physicalMemory)
            if self.hostDo.biosAssetTag:
                self.hostOsh.setStringAttribute('bios_asset_tag', self.hostDo.biosAssetTag)
            if self.hostDo.osDomain:
                self.hostOsh.setStringAttribute('host_osdomain', self.hostDo.osDomain)
            if self.hostDo.winProcessorsNumber:
                self.hostOsh.setIntegerAttribute('nt_processorsnumber', self.hostDo.winProcessorsNumber)

            if self.hostDo.serialNumber:
                modeling.setHostSerialNumberAttribute(self.hostOsh, self.hostDo.serialNumber)
            if self.hostDo.hostModel:
                modeling.setHostModelAttribute(self.hostOsh, self.hostDo.hostModel)
            if self.hostDo.hostManufacturer:
                modeling.setHostManufacturerAttribute(self.hostOsh, self.hostDo.hostManufacturer)
            if self.hostDo.udUniqueId:
                self.hostOsh.setAttribute("ud_unique_id", self.hostDo.udUniqueId)
            if self.hostDo.paeEnabled and self.hostDo.paeEnabled.lower() in ['1', 'true']:
                self.hostOsh.setBoolAttribute("pae_enabled", 1)
            elif self.hostDo.paeEnabled and self.hostDo.paeEnabled.lower() in ['0', 'false']:
                self.hostOsh.setBoolAttribute("pae_enabled", 0)
            if self.hostDo.installType and self.hostDo.installType.encode('ascii','ignore').lower().find('ia64') != -1:
                self.hostDo.osArchitecture = 'ia64'
            elif self.hostDo.installType and self.hostDo.installType.encode('ascii','ignore').find('64') != -1:
                self.hostDo.osArchitecture = '64-bit'
            if self.hostDo.osArchitecture:
                self.hostOsh.setStringAttribute('os_architecture', self.hostDo.osArchitecture)

            modeling.setHostBiosUuid(self.hostOsh, self.hostDo.biosUUID)
            modeling.setHostDefaultGateway(self.hostOsh, self.hostDo.defaultGateway)
            modeling.setHostOsFamily(self.hostOsh, self.hostDo.osFamily)
            # fill in list of DNS servers
            if self.dnsServerIpList:
                list_ = StringVector(map(str, self.dnsServerIpList))
                attr = AttributeStateHolder('dns_servers', list_)
                self.hostOsh.setListAttribute(attr)
            self.resultVector.add(self.hostOsh)

    def buildInterfaces(self):
        """ We create and report such topology for destination ip in following cases:
                            |host is complete     | host is incomplete|
        ip is virtual        |all except NTCMD    |only virtual ip    |
        ip is not virtual    |all topology        |all topology        |
        """
        try:
            self.hostOsh = modeling.createCompleteHostOSHByInterfaceList('nt', self.interfacesList, self.hostDo.hostOsName, self.hostDo.hostName, self.hostDo.lastBootDate, self.hostCmdbId, self.hostKey, self.hostMacs, self.ucmdbVersion)
            if self.hostDo.ipIsVirtual:
                logger.warn('IP: ', self.destinationIp, ' does not appear in the result buffer of the interfaces output - assuming virtual')
                logger.warn('Host topology will be reported without NTCMD CI.')
        except:
            self.hostOsh = modeling.createHostOSH(str(self.destinationIp), 'nt', self.hostDo.hostOsName, self.hostDo.hostName)
            logger.warn('Could not find a valid MAC address for key on ip : ', self.destinationIp)
            if self.hostDo.ipIsVirtual:
                logger.warn('IP: ', self.destinationIp, ' does not appear in the result buffer of the interfaces output - assuming virtual')
                logger.warn('Only IP marked as virtual will be reported.')
                self.resultVector.clear()
                virtualIpOsh = modeling.createIpOSH(str(self.destinationIp))
                virtualIpOsh.setBoolAttribute('isvirtual', 1)
                self.resultVector.add(virtualIpOsh)
                self.hostOsh = None
                return
        #process interfaces looking for nic teaming interfaces.
        #looking up for interfaces with same mac
        macToInterfaceListMap = {}
        for interface in self.interfacesList:
            intList = macToInterfaceListMap.get(interface.macAddress, [])
            intList.append(interface)
            macToInterfaceListMap[interface.macAddress] = intList
        #checking if interface has a Team key word in it's name
        for interfaceList in macToInterfaceListMap.values():
            if interfaceList and len(interfaceList) < 2:
                continue
            for interf in interfaceList:
                if (interf.name and re.search('[Tt]eam', interf.name)) or (interf.description and re.search('[Tt]eam', interf.description)):
                    #picking up interface with max interfaceIndex value and setting it aggregate role
                    try:
                        iface = reduce(lambda x,y: int(x.interfaceIndex) > int(y.interfaceIndex) and x or y, interfaceList)
                        iface.role = 'aggregate_interface'
                    except:
                        logger.debugException('')

        interfacesVector = modeling.createInterfacesOSHV(self.interfacesList, self.hostOsh)
        roleManager = InterfaceRoleManager()
        for interface in self.interfacesList:
            interfaceOsh = interface.getOsh()
            if not interfaceOsh: continue
            roleManager.assignInterfaceRole(interface)
            self.resultVector.add(interfaceOsh)
            if interface.ips:
                for ipIndex in range(len(interface.ips)):
                    ipAddress = interface.ips[ipIndex]
                    ipMask = None
                    try:
                        ipMask = interface.masks[ipIndex]
                    except:
                        pass
                    if ipAddress.is_loopback or ipAddress.is_unspecified:
                        logger.debug('Invalid IP retrieved %s. Skipping.' % ipAddress)
                        continue
                    ipProp = modeling.getIpAddressPropertyValue(str(ipAddress), ipMask, interface.dhcpEnabled, interface.description)

                    ipOsh = modeling.createIpOSH(ipAddress, ipMask, None, ipProp)
                    if self.hostDo.ipIsVirtual and ipAddress == self.destinationIp:
                        ipOsh.setBoolAttribute('isvirtual', 1)
                    self.resultVector.add(ipOsh)
                    self.resultVector.add(modeling.createLinkOSH('containment', self.hostOsh, ipOsh))
                    self.resultVector.add(modeling.createLinkOSH('containment', interfaceOsh, ipOsh))
                    if ipAddress.version != 6:
                        networkOsh = modeling.createNetworkOSH(str(ipAddress), ipMask)
                        self.resultVector.add(networkOsh)
                        self.resultVector.add(modeling.createLinkOSH('member', networkOsh, ipOsh))
                        self.resultVector.add(modeling.createLinkOSH('member', networkOsh, self.hostOsh))

    def buildDnsServers(self):
        if self.dnsServerIpList:
            # accept IPv4 addresses only
            # currently IPv6 addresses can not be assigned to any probe
            # and are not a part of reconciliation, so node CI can not be
            # created based on instance of IPv6 address
            isIPv4 = lambda ip: ip_addr.IPAddress(ip).get_version() == 4
            for dnsIpAddr in filter(isIPv4, self.dnsServerIpList):
                if ip_addr.isValidIpAddress(dnsIpAddr, filter_client_ip=True):
                    dnsHostOsh = modeling.createHostOSH(str(dnsIpAddr))
                    dnsAppOsh = modeling.createDnsOsh(str(dnsIpAddr), dnsHostOsh)
                    self.resultVector.add(dnsHostOsh)
                    self.resultVector.add(dnsAppOsh)

    def addResultsToVector(self, resultsVector):

        if self.resultVector:
            resultsVector.addAll(self.resultVector)
        # when the destination IP is virtual, and it exists in NATIpAddress.xml
        # Continue to create the shell object other than ignore it
        if self.hostDo.ipIsVirtual:
            if self.hostDo.ipIsNATed:
                vIPOSH = modeling.createIpOSH(self.destinationIp)
                vIPOSH.setBoolAttribute('isvirtual', 1)
                resultsVector.add(vIPOSH)
                resultsVector.add(modeling.createLinkOSH('contained', self.hostOsh, vIPOSH))
            else:
                return
        self.ntcmdObj.setContainer(self.hostOsh)
        resultsVector.add(self.ntcmdObj)
def getNodes(localDbClient, netDeviceOSHV, portOSHV, allowDnsLookup, queryChunkSize, localFramework):
    try:
        resultVector = ObjectStateHolderVector()
        ipAddrList = macAddrList = []
        nodeOshDict = {} # {'NodeName':NodeOSH}
        nodeResultSetData = {} #{'NodeName':[NodeData]} #


        ## Get total number of nodes in the database
        numNodes = 0
        nodeCountQuery = 'SELECT COUNT(1) FROM lmsdatagrp.End_Hosts'
        nodeCountResultSet = ciscoworks_utils.doQuery(localDbClient, nodeCountQuery)
        ## Return if query returns no results
        if nodeCountResultSet == None:
            logger.warn('[' + SCRIPT_NAME + ':getNodes] No Nodes found')
            return None
        ## We have query results!
        while nodeCountResultSet.next():
            numNodes = int(ciscoworks_utils.getStringFromResultSet(nodeCountResultSet, 1))

        ## Determine chunk count
        ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getNodes] Got <%s> nodes...' % numNodes)
        numChunks = int(numNodes / queryChunkSize) + 1
        ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getNodes] Got <%s> chunks...' % numChunks)


        for chunkIndex in range(0, numChunks):
            queryStartRow = chunkIndex*queryChunkSize
            if queryStartRow == 0:
                queryStartRow = 1
            nodeQuery = '''SELECT TOP %s START AT %s
                                HostName, DeviceName, Device, MACAddress, IPAddress, SubnetMask,
                                Port, PortName, VLAN, VlanId, associatedRouters
                            FROM lmsdatagrp.End_Hosts
                            WHERE MACAddress IS NOT NULL AND NOT MACAddress='' ''' % (queryChunkSize, queryStartRow)
            nodeResultSet = ciscoworks_utils.doQuery(localDbClient, nodeQuery)
            ## Return if query returns no results
            if nodeResultSet == None:
                logger.warn('No Nodes found')
                return None

            ## We have query results!
            while nodeResultSet.next():
                nodeOSH = ipOSH = None
                ## Get values from result set
                nodeName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 1)
                netDeviceName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 2)
                netDeviceIP = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 3)
                nodeMacAddress = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 4)
                ipAddress = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 5)
                nodeSubnetMask = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 6)
                portName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 7)
                portDesc = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 8)
                vlanName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 9)
                vlanID = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 10)

                ## Build Node OSH
                if ipAddress and netutils.isValidIp(ipAddress):
                    end_device_ip_address = ipAddress
                elif nodeName and netutils.isValidIp(nodeName):
                    end_device_ip_address = nodeName
                else:
                    end_device_ip_address = None
                (nodeOSH, interfaceOSH, ipOSH) = processNodeInfo(end_device_ip_address, nodeMacAddress, nodeName, nodeSubnetMask, ipAddrList, macAddrList, nodeOshDict, allowDnsLookup)
                if nodeOSH:
                    resultVector.add(nodeOSH)
                else:
                    ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNodes] Unable to build CI for Node <%s> with MAC address <%s>' % (nodeName, nodeMacAddress))
                    continue
                if interfaceOSH:
                    resultVector.add(interfaceOSH)
                if ipOSH:
                    resultVector.add(ipOSH)
                    resultVector.add(modeling.createLinkOSH('containment', nodeOSH, ipOSH))

                ## Build Net Device OSH
                if netDeviceIP and netutils.isValidIp(netDeviceIP):
                    net_device_ip_address = netDeviceIP
                elif netDeviceName and netutils.isValidIp(netDeviceName):
                    net_device_ip_address = netDeviceName
                else:
                    net_device_ip_address = None
                (netDeviceOSH, netDeviceIpOSH) = processNetDeviceInfo(net_device_ip_address, netDeviceName, ipAddrList, netDeviceOSHV, allowDnsLookup)
                netDeviceCmdbID = None
                ## Add Net Device to OSHV only if it is a new one
                if netDeviceOSH:
                    ## Check if this NetDevice is from the CMDB
                    try:
                        netDeviceCmdbID = netDeviceOSH.getCmdbId().toString()
                    except:
                        ## An exception will be thrown for all Net Device OSHs that were not already in the UCMDB, ignore it
                        pass
                    if not netDeviceCmdbID:
                        resultVector.add(netDeviceOSH)
                else:
                    ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNodes] Unable to build CI for Net Device <%s> with IP <%s>' % (netDeviceName, netDeviceIP))
                    continue
                if netDeviceIpOSH and netDeviceOSH:
                    resultVector.add(netDeviceIpOSH)
                    resultVector.add(modeling.createLinkOSH('containment', netDeviceOSH, netDeviceIpOSH))
                #report Layer 2 topology
                end_node_mac_address = None
                if nodeMacAddress and netutils.isValidMac(nodeMacAddress):
                    end_node_mac_address = netutils.parseMac(nodeMacAddress)
                resultVector.addAll(build_layer2_connection(netDeviceOSH, portName, net_device_ip_address, nodeOSH, end_node_mac_address, interfaceOSH))


                ## Build PORT and VLAN CIs
                if netDeviceCmdbID and netDeviceOSH and netutils.isValidMac(nodeMacAddress):
                    portOSH = processPortInfo(portName, portDesc, vlanName, vlanID, portOSHV, netutils.parseMac(nodeMacAddress), netDeviceCmdbID, netDeviceName, netDeviceOSH)
                    if portOSH:
                        resultVector.add(portOSH)
            nodeResultSet.close()

            ## Send results to server
            localFramework.sendObjects(resultVector)
            localFramework.flushObjects()
            resultVector.clear()
        return resultVector
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.warn('[' + SCRIPT_NAME + ':getNodes] Exception: <%s>' % excInfo)
        pass
Exemple #7
0
class TopologyBuilder:
    def __init__(self,
                 interfacesList,
                 hostDo,
                 destinationIp,
                 ntcmdObj,
                 dnsServerIpList=[],
                 dhcpServerIpList=[],
                 winsServerIpList=[],
                 hostCmdbId=None,
                 hostKey=None,
                 hostMacs=None,
                 ucmdbVersion=None):
        self.resultVector = ObjectStateHolderVector()
        self.dnsServerIpList = dnsServerIpList
        self.dhcpServerIpList = dhcpServerIpList
        self.winsServerIpList = winsServerIpList
        self.interfacesList = interfacesList
        self.hostDo = hostDo
        self.hostOsh = None
        self.hostCmdbId = hostCmdbId
        self.hostKey = hostKey
        self.hostMacs = hostMacs
        self.ucmdbVersion = ucmdbVersion
        self.destinationIp = destinationIp
        self.ntcmdObj = ntcmdObj

    def build(self):
        self.buildDnsServers()
        self.buildDhcpServers()
        self.buildWinsServers()
        self.buildInterfaces()
        self.setHostAttributes()

    def setDhcpDiscovererResults(self, dhcpServerIpList):
        self.dhcpServerIpList = dhcpServerIpList

    def setWinsDiscovererResults(self, winsServerIpList):
        self.winsServerIpList = winsServerIpList

    def buildWinsServers(self):
        if self.winsServerIpList:
            for winsIpAddr in self.winsServerIpList:
                if ip_addr.isValidIpAddress(winsIpAddr, filter_client_ip=True):
                    winsHostOsh = modeling.createHostOSH(str(winsIpAddr))
                    winsAppOsh = modeling.createWinsOsh(
                        str(winsIpAddr), winsHostOsh)
                    self.resultVector.add(winsHostOsh)
                    self.resultVector.add(winsAppOsh)

    def buildDhcpServers(self):
        if self.dhcpServerIpList:
            for dhcpIpAddr in self.dhcpServerIpList:
                if ip_addr.isValidIpAddress(dhcpIpAddr, filter_client_ip=True):
                    dhcpHostOsh = modeling.createHostOSH(str(dhcpIpAddr))
                    dhcpAppOsh = modeling.createDhcpOsh(
                        str(dhcpIpAddr), dhcpHostOsh)
                    self.resultVector.add(dhcpHostOsh)
                    self.resultVector.add(dhcpAppOsh)

    def setHostAttributes(self):
        if self.hostOsh:
            if self.hostDo.hostName:
                self.hostOsh.setAttribute('host_hostname',
                                          self.hostDo.hostName)
            if self.hostDo.hostOsName:
                modeling.setHostOsName(self.hostOsh, self.hostDo.hostOsName)
            if self.hostDo.description:
                self.hostOsh = modeling.HostBuilder(
                    self.hostOsh).setDescription(
                        self.hostDo.description).build()
            if self.hostDo.servicePack:
                self.hostOsh.setAttribute('nt_servicepack',
                                          self.hostDo.servicePack)
            if self.hostDo.buildNumber:
                self.hostOsh.setAttribute('host_osrelease',
                                          self.hostDo.buildNumber)
            if self.hostDo.ntVersion:
                self.hostOsh.setAttribute('host_osversion',
                                          self.hostDo.ntVersion)
            if self.hostDo.installType:
                self.hostOsh.setAttribute('host_osinstalltype',
                                          self.hostDo.installType)
            if self.hostDo.vendor:
                self.hostOsh.setAttribute('host_vendor', self.hostDo.vendor)
            if self.hostDo.registeredOwner:
                self.hostOsh.setAttribute('nt_registeredowner',
                                          self.hostDo.registeredOwner)
            if self.hostDo.organization:
                self.hostOsh.setStringAttribute('nt_registrationorg',
                                                self.hostDo.organization)
            if self.hostDo.physicalMemory:
                self.hostOsh.setAttribute('nt_physicalmemory',
                                          self.hostDo.physicalMemory)
            if self.hostDo.biosAssetTag:
                self.hostOsh.setStringAttribute('bios_asset_tag',
                                                self.hostDo.biosAssetTag)
            if self.hostDo.osDomain:
                self.hostOsh.setStringAttribute('host_osdomain',
                                                self.hostDo.osDomain)
            if self.hostDo.winProcessorsNumber:
                self.hostOsh.setIntegerAttribute(
                    'nt_processorsnumber', self.hostDo.winProcessorsNumber)

            if self.hostDo.serialNumber:
                modeling.setHostSerialNumberAttribute(self.hostOsh,
                                                      self.hostDo.serialNumber)
            if self.hostDo.hostModel:
                modeling.setHostModelAttribute(self.hostOsh,
                                               self.hostDo.hostModel)
            if self.hostDo.hostManufacturer:
                modeling.setHostManufacturerAttribute(
                    self.hostOsh, self.hostDo.hostManufacturer)
            if self.hostDo.udUniqueId:
                self.hostOsh.setAttribute("ud_unique_id",
                                          self.hostDo.udUniqueId)
            if self.hostDo.paeEnabled and self.hostDo.paeEnabled.lower() in [
                    '1', 'true'
            ]:
                self.hostOsh.setBoolAttribute("pae_enabled", 1)
            elif self.hostDo.paeEnabled and self.hostDo.paeEnabled.lower() in [
                    '0', 'false'
            ]:
                self.hostOsh.setBoolAttribute("pae_enabled", 0)
            if self.hostDo.installType and self.hostDo.installType.encode(
                    'ascii', 'ignore').lower().find('ia64') != -1:
                self.hostDo.osArchitecture = 'ia64'
            elif self.hostDo.installType and self.hostDo.installType.encode(
                    'ascii', 'ignore').find('64') != -1:
                self.hostDo.osArchitecture = '64-bit'
            if self.hostDo.osArchitecture:
                self.hostOsh.setStringAttribute('os_architecture',
                                                self.hostDo.osArchitecture)

            modeling.setHostBiosUuid(self.hostOsh, self.hostDo.biosUUID)
            modeling.setHostDefaultGateway(self.hostOsh,
                                           self.hostDo.defaultGateway)
            modeling.setHostOsFamily(self.hostOsh, self.hostDo.osFamily)
            # fill in list of DNS servers
            if self.dnsServerIpList:
                list_ = StringVector(map(str, self.dnsServerIpList))
                attr = AttributeStateHolder('dns_servers', list_)
                self.hostOsh.setListAttribute(attr)
            self.resultVector.add(self.hostOsh)

    def buildInterfaces(self):
        """ We create and report such topology for destination ip in following cases:
                            |host is complete     | host is incomplete|
        ip is virtual        |all except NTCMD    |only virtual ip    |
        ip is not virtual    |all topology        |all topology        |
        """
        try:
            self.hostOsh = modeling.createCompleteHostOSHByInterfaceList(
                'nt', self.interfacesList, self.hostDo.hostOsName,
                self.hostDo.hostName, self.hostDo.lastBootDate,
                self.hostCmdbId, self.hostKey, self.hostMacs,
                self.ucmdbVersion)
            if self.hostDo.ipIsVirtual:
                logger.warn(
                    'IP: ', self.destinationIp,
                    ' does not appear in the result buffer of the interfaces output - assuming virtual'
                )
                logger.warn('Host topology will be reported without NTCMD CI.')
        except:
            self.hostOsh = modeling.createHostOSH(str(self.destinationIp),
                                                  'nt', self.hostDo.hostOsName,
                                                  self.hostDo.hostName)
            logger.warn('Could not find a valid MAC address for key on ip : ',
                        self.destinationIp)
            if self.hostDo.ipIsVirtual:
                logger.warn(
                    'IP: ', self.destinationIp,
                    ' does not appear in the result buffer of the interfaces output - assuming virtual'
                )
                logger.warn('Only IP marked as virtual will be reported.')
                self.resultVector.clear()
                virtualIpOsh = modeling.createIpOSH(str(self.destinationIp))
                virtualIpOsh.setBoolAttribute('isvirtual', 1)
                self.resultVector.add(virtualIpOsh)
                self.hostOsh = None
                return
        #process interfaces looking for nic teaming interfaces.
        #looking up for interfaces with same mac
        macToInterfaceListMap = {}
        for interface in self.interfacesList:
            intList = macToInterfaceListMap.get(interface.macAddress, [])
            intList.append(interface)
            macToInterfaceListMap[interface.macAddress] = intList
        #checking if interface has a Team key word in it's name
        for interfaceList in macToInterfaceListMap.values():
            if interfaceList and len(interfaceList) < 2:
                continue
            for interf in interfaceList:
                if (interf.name and re.search('[Tt]eam', interf.name)) or (
                        interf.description
                        and re.search('[Tt]eam', interf.description)):
                    #picking up interface with max interfaceIndex value and setting it aggregate role
                    try:
                        iface = reduce(
                            lambda x, y: int(x.interfaceIndex) > int(
                                y.interfaceIndex) and x or y, interfaceList)
                        iface.role = 'aggregate_interface'
                    except:
                        logger.debugException('')

        interfacesVector = modeling.createInterfacesOSHV(
            self.interfacesList, self.hostOsh)
        roleManager = InterfaceRoleManager()
        for interface in self.interfacesList:
            interfaceOsh = interface.getOsh()
            if not interfaceOsh: continue
            roleManager.assignInterfaceRole(interface)
            self.resultVector.add(interfaceOsh)
            if interface.ips:
                for ipIndex in range(len(interface.ips)):
                    ipAddress = interface.ips[ipIndex]
                    ipMask = None
                    try:
                        ipMask = interface.masks[ipIndex]
                    except:
                        pass
                    if ipAddress.is_loopback or ipAddress.is_unspecified:
                        logger.debug('Invalid IP retrieved %s. Skipping.' %
                                     ipAddress)
                        continue
                    ipProp = modeling.getIpAddressPropertyValue(
                        str(ipAddress), ipMask, interface.dhcpEnabled,
                        interface.description)

                    ipOsh = modeling.createIpOSH(ipAddress, ipMask, None,
                                                 ipProp)
                    if self.hostDo.ipIsVirtual and ipAddress == self.destinationIp:
                        ipOsh.setBoolAttribute('isvirtual', 1)
                    self.resultVector.add(ipOsh)
                    self.resultVector.add(
                        modeling.createLinkOSH('containment', self.hostOsh,
                                               ipOsh))
                    self.resultVector.add(
                        modeling.createLinkOSH('containment', interfaceOsh,
                                               ipOsh))
                    if ipAddress.version != 6:
                        networkOsh = modeling.createNetworkOSH(
                            str(ipAddress), ipMask)
                        self.resultVector.add(networkOsh)
                        self.resultVector.add(
                            modeling.createLinkOSH('member', networkOsh,
                                                   ipOsh))
                        self.resultVector.add(
                            modeling.createLinkOSH('member', networkOsh,
                                                   self.hostOsh))

    def buildDnsServers(self):
        if self.dnsServerIpList:
            # accept IPv4 addresses only
            # currently IPv6 addresses can not be assigned to any probe
            # and are not a part of reconciliation, so node CI can not be
            # created based on instance of IPv6 address
            isIPv4 = lambda ip: ip_addr.IPAddress(ip).get_version() == 4
            for dnsIpAddr in filter(isIPv4, self.dnsServerIpList):
                if ip_addr.isValidIpAddress(dnsIpAddr, filter_client_ip=True):
                    dnsHostOsh = modeling.createHostOSH(str(dnsIpAddr))
                    dnsAppOsh = modeling.createDnsOsh(str(dnsIpAddr),
                                                      dnsHostOsh)
                    self.resultVector.add(dnsHostOsh)
                    self.resultVector.add(dnsAppOsh)

    def addResultsToVector(self, resultsVector):

        if self.resultVector:
            resultsVector.addAll(self.resultVector)
        # when the destination IP is virtual, and it exists in NATIpAddress.xml
        # Continue to create the shell object other than ignore it
        if self.hostDo.ipIsVirtual:
            if self.hostDo.ipIsNATed:
                vIPOSH = modeling.createIpOSH(self.destinationIp)
                vIPOSH.setBoolAttribute('isvirtual', 1)
                resultsVector.add(vIPOSH)
                resultsVector.add(
                    modeling.createLinkOSH('contained', self.hostOsh, vIPOSH))
            else:
                return
        self.ntcmdObj.setContainer(self.hostOsh)
        resultsVector.add(self.ntcmdObj)
def getNetworkDevices(localDbClient, queryChunkSize, ipAddrList, portVlanIdMap, ignoreNodesWithoutIP, allowDnsLookup, localFramework):
    try:
        returnOSHV = ObjectStateHolderVector()

        ## Get total number of network devices in the database
        numDevices = 0
        deviceCountQuery = 'SELECT COUNT(1) FROM lmsdatagrp.NETWORK_DEVICES'
        deviceCountResultSet = ciscoworks_utils.doQuery(localDbClient, deviceCountQuery)
        ## Return if query returns no results
        if deviceCountResultSet == None:
            logger.warn('[' + SCRIPT_NAME + ':getNetworkDevices] No Network Devices found')
            return None
        ## We have query results!
        while deviceCountResultSet.next():
            numDevices = int(ciscoworks_utils.getStringFromResultSet(deviceCountResultSet, 1))

        ## Determine chunk count
        ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getNetworkDevices] Got <%s> Network Devices...' % numDevices)
        numChunks = int(numDevices/queryChunkSize) + 1
        ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getNetworkDevices] Got <%s> chunks...' % numChunks)

        for chunkIndex in range(0, numChunks):
            queryStartRow = chunkIndex*queryChunkSize
            if queryStartRow == 0:
                queryStartRow = 1
            netDeviceQuery = '''SELECT TOP %s START AT %s
                                    netdevices.Device_Id, deviceState.NetworkElementID, netdevices.Device_Display_Name,
                                    netdevices.Host_Name, netdevices.Device_Category, netdevices.Device_Model,
                                    netdevices.Management_IPAddress, deviceState.Global_State
                                FROM lmsdatagrp.NETWORK_DEVICES netdevices JOIN dba.DM_Dev_State deviceState
                                    ON netdevices.Device_Id=deviceState.DCR_ID''' % (queryChunkSize, queryStartRow)
            #netDeviceQuery = '%s WHERE LOWER(netdevices.Device_Display_Name) LIKE \'a%%\'' % netDeviceQuery
            netDeviceResultSet = ciscoworks_utils.doQuery(localDbClient, netDeviceQuery)

            ## Return if query returns no results
            if netDeviceResultSet == None:
                logger.warn('[' + SCRIPT_NAME + ':getNetworkDevices] No Network Devices found in chunk <%s>' % chunkIndex)
                return None

            ## We have query results!
            while netDeviceResultSet.next():
                netDeviceOSH = ipOSH = None
                ## Get values from result set
                netDeviceID = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 1)
                netDeviceElementID = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 2)
                netDeviceDisplayName = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 3)
                netDeviceHostName = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 4)
                netDeviceCategory = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 5)
                netDeviceModel = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 6)
                ipAddress = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 7)
                #netDeviceStateIndex = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 7)
                ## Set device name based on first available value
                netDeviceName = netDeviceDisplayName or netDeviceHostName
                ciscoworks_utils.debugPrint(1, '[' + SCRIPT_NAME + ':getNetworkDevices] Got Device <%s> with ID <%s>' % (netDeviceName, netDeviceElementID))

                ## Get enums for net device
                #deviceStateEnumDict = ciscoworks_utils.getEnum(localDbClient, 'dba.DM_Global_State_Enum')
                physicalTypeEnumDict = ciscoworks_utils.getEnum(localDbClient, 'dba.PhysicalTypeEnum')

                ## Initialize variables for additional data
                netDeviceElementName = netDeviceReportedName = netDeviceDNSDomainName = netDeviceDescription = netDeviceContact = netDeviceLocation = None
                netDeviceOsName = netDeviceOsVersion = netDeviceManufacturer = netDeviceSerialNumber = None
                netDeviceDnsName = None

                ## Get additional details for this device
                netDeviceAdditionalDataQuery = '''SELECT ne.ElementName, ne.ReportedHostName, ne.DNSDomainName, ne.Description,
                                                    ne.PrimaryOwnerContact, ne.ElementLocation,
                                                    os.OSName, os.Version, os.ROMVersion, pe.Manufacturer, pe.SerialNumber
                                                FROM dba.OperatingSystem os, dba.PhysicalElement pe, dba.networkelement ne
                                                WHERE os.NetworkElementID=%s AND ne.NetworkElementID=%s AND pe.NetworkElementID=%s
                                                    AND LOWER(pe.PhysicalType)=%s AND pe.PhysicalElementId IN (1, 2)'''\
                                                % (netDeviceElementID, netDeviceElementID, netDeviceElementID, physicalTypeEnumDict['Chassis/Frame'])
                netDeviceAdditionalDataResultSet = ciscoworks_utils.doQuery(localDbClient, netDeviceAdditionalDataQuery)

                ## Return if query returns no results
                if netDeviceAdditionalDataResultSet == None:
                    logger.warn('[' + SCRIPT_NAME + ':getNetworkDevices] No additional data found for network device <%s> with ID <%s>' % (netDeviceName, netDeviceElementID))
                    return None

                ## We have query results!
                while netDeviceAdditionalDataResultSet.next():
                    ## Get values from result set
                    netDeviceElementName = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 1)
                    netDeviceReportedName = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 2)
                    netDeviceDNSDomainName = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 3)
                    netDeviceDescription = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 4)
                    netDeviceContact = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 5)
                    netDeviceLocation = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 6)
                    netDeviceOsName = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 7)
                    netDeviceOsVersion = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 8)
                    #netDeviceRomVersion = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 9)
                    netDeviceManufacturer = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 10)
                    netDeviceSerialNumber = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 11)
                    ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNetworkDevices] Got additional information for Net Device <%s> with ID <%s>' % (netDeviceName, netDeviceElementID))
                netDeviceAdditionalDataResultSet.close()

                if not netDeviceName:
                    netDeviceName = netDeviceElementName or netDeviceReportedName
                if netDeviceDNSDomainName and not netutils.isValidIp(netDeviceName):
                    #netDeviceName = '%s.%s' % (netDeviceName, netDeviceDNSDomainName)
                    #netDeviceDnsName = netDeviceName.lower()
                    netDeviceDnsName = '%s.%s' % (netDeviceName, netDeviceDNSDomainName)

                ## Determine Net Device CI Type
                netDeviceCiType = 'netdevice'
                netDeviceCategoreToCiTypeMap = {'Routers':'router', 'Switches and Hubs':'switch', 'Content Networking':'switch',
                                                'Cisco Interfaces and Modules':'switch', 'Wireless':'netdevice',
                                                'Voice and Telephony':'netdevice', 'Unknown':'netdevice'}
                if netDeviceCategory in netDeviceCategoreToCiTypeMap.keys():
                    netDeviceCiType = netDeviceCategoreToCiTypeMap[netDeviceCategory]

                ## Discard management IP if this is a duplicate
                if ipAddress and netutils.isValidIp(ipAddress) and ipAddress in ipAddrList:
                    logger.debug('[' + SCRIPT_NAME + ':getNetworkDevices] Duplicate IP address <%s> on Network Device <%s> with ID <%s>!! Discarding IP...' % (ipAddress, netDeviceName, netDeviceElementID))
                    ipAddress = None
                else:
                    ipAddrList.append(ipAddress)
                ## Get the list of IP addresses associated with this device
                ipSubnetDict = getIpSubnetDict(localDbClient, ipAddrList, netDeviceID, netDeviceElementID, netDeviceName)

                # Check if an IP address is available to build the host key
                # If an IP is not available and a DNS name is available, try resolving the IP
                # If not, skip this device
                ## If a management IP is not available, use the first IP in the IP list
                if not ipAddress and ipSubnetDict and len(ipSubnetDict) > 0:
                    ipAddress = ipSubnetDict[0]
                ## Try DNS lookup if an IP is not available
                if not (ipAddress and netutils.isValidIp(ipAddress)) and allowDnsLookup and netDeviceDnsName:
                    ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNetworkDevices] No IP for Device <%s> with DNS name <%s>! Attempting DNS lookup...' % (netDeviceName, netDeviceDnsName))
                    ipAddress = netutils.getHostAddress(netDeviceDnsName)
                if not (ipAddress and netutils.isValidIp(ipAddress)) and allowDnsLookup and netDeviceName:
                    ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNetworkDevices] No IP for Device <%s> with ID <%s>! Attempting DNS lookup...' % (netDeviceName, netDeviceElementID))
                    ipAddress = netutils.getHostAddress(netDeviceName)
                ## Check for a valid IP before creating CIs
                if ipAddress and netutils.isValidIp(ipAddress):
                    netDeviceOSH = modeling.createHostOSH(ipAddress, netDeviceCiType)
                    ipOSH = modeling.createIpOSH(ipAddress, None, netDeviceDnsName, None)
                elif ignoreNodesWithoutIP:
                    logger.debug('[' + SCRIPT_NAME + ':getNetworkDevices] IP address not available for Network Device <%s> with ID <%s>!! Skipping...' % (netDeviceName, netDeviceElementID))
                    continue
                else:
                    logger.debug('[' + SCRIPT_NAME + ':getNetworkDevices] IP address not available for Network Device <%s> with ID <%s>!! Creating Network Device with ID as primary key...' % (netDeviceName, netDeviceElementID))
                    hostKey = netDeviceElementID + ' (CiscoWorks Network Element ID)'
                    netDeviceOSH = modeling.createCompleteHostOSH(netDeviceCiType, hostKey)
                    netDeviceOSH.setAttribute('data_note', 'IP address unavailable in CiscoWorks LMS - Duplication of this CI is possible')

                ## Set the real name of the netDevice
                netDeviceRealName = netDeviceName
                if netDeviceName and netutils.isValidIp(netDeviceName):
                    netDeviceRealName = ''
                ## Add more details to the OSH
                ciscoworks_utils.populateOSH(netDeviceOSH, {'name':netDeviceRealName, 'data_externalid':netDeviceName, 'discovered_description':netDeviceDescription,
                                                        'discovered_contact':netDeviceContact, 'discovered_location':netDeviceLocation, 'discovered_os_name':netDeviceOsName,
                                                        'discovered_os_version':netDeviceOsVersion, 'discovered_model':netDeviceModel, 'serial_number':netDeviceSerialNumber,
                                                        'discovered_vendor':netDeviceManufacturer, 'primary_dns_name':netDeviceDnsName, 'domain_name':netDeviceDNSDomainName})
                ## Set node role
                netDeviceOSH.setListAttribute('node_role', [netDeviceCiType])
                returnOSHV.add(netDeviceOSH)
                returnOSHV.addAll(getNetDevicePortsAndVlans(localDbClient, portVlanIdMap, netDeviceID, netDeviceElementID, netDeviceName, netDeviceOSH))
                returnOSHV.addAll(getModules(localDbClient, netDeviceID, netDeviceName, netDeviceOSH))

                ## Add IPs to OSHV
                if ipOSH:
                    returnOSHV.add(ipOSH)
                    returnOSHV.add(modeling.createLinkOSH('containment', netDeviceOSH, ipOSH))
                if ipSubnetDict and len(ipSubnetDict) > 0:
                    for ipAddy in ipSubnetDict.keys():
                        ipOSH = modeling.createIpOSH(ipAddy, ipSubnetDict[ipAddy], netDeviceDnsName, None)
                        returnOSHV.add(ipOSH)
                        returnOSHV.add(modeling.createLinkOSH('containment', netDeviceOSH, ipOSH))

            netDeviceResultSet.close()

            ## Send results to server
            localFramework.sendObjects(returnOSHV)
            localFramework.flushObjects()
            returnOSHV.clear()

        return returnOSHV
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.warn('[' + SCRIPT_NAME + ':getNetworkDevices] Exception: <%s>' % excInfo)
        pass
Exemple #9
0
def DiscoveryMain(Framework):
    ipAddress = Framework.getDestinationAttribute('ip_address')
    discoveredPorts = Framework.getParameter('ports') or None
    useNMap = Framework.getParameter('useNMap') == 'true'
    nmapPath = Framework.getParameter('nmapPath') or None
    scanUDP = Framework.getParameter('scanUDP') == 'true'
    UDPports = Framework.getParameter('UDPports') or None
    UDPports = UDPports and UDPports.strip()
    connectTimeOut = int(Framework.getParameter('connectTimeOut'))

    #if we need to check host's reachability:
    if Framework.getParameter('checkIfIpIsReachable').lower() == 'true':
        if not netutils.pingIp(Framework, ipAddress, Framework.getParameter('pingTimeOut')):
            logger.debug('Could not connect to ', ipAddress, ' by ping')
            msg = 'Target host is not reachable'
            warningObject = errorobject.createError(errorcodes.CONNECTION_FAILED_NO_PROTOCOL_WITH_DETAILS, ['', msg],
                                                    msg)
            logger.reportWarningObject(warningObject)
            return

    OSHVResult = ObjectStateHolderVector()
    hostOsh = modeling.createHostOSH(ipAddress)
    OSHVResult.add(hostOsh)

    cfgFile = Framework.getConfigFile(CollectorsParameters.KEY_COLLECTORS_SERVERDATA_PORTNUMBERTOPORTNAME)

    onlyKnownPorts = Framework.getParameter('checkOnlyKnownPorts')
    onlyKnownPorts = (onlyKnownPorts and onlyKnownPorts.lower() == 'true')

    portsList = getPorts(discoveredPorts and discoveredPorts.strip(), PortType.TCP, cfgFile, onlyKnownPorts)
    if scanUDP:
        if onlyKnownPorts and not UDPports:
            UDPports = '*'
        portsList.extend(getPorts(UDPports, PortType.UDP, cfgFile, onlyKnownPorts))

    portsToDiscover = filter(lambda port: port.isDiscover, portsList)

    isConnectedPortFound = False
    useFallback = False
    if useNMap:
        # Nmap flow supports udp and tcp ports
        client = Framework.createClient(ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME)
        try:
            shell = shellutils.ShellFactory().createShell(client)
            fs = file_system.createFileSystem(shell)
            try:
                if nmapPath and fs.isDirectory(nmapPath):
                    path_tool = NtPath()
                    nmapPath = path_tool.join(nmapPath, nmap.NMAP_EXECUTABLES[1])
            except PathNotFoundException:
                logger.warn("Specified directory \"%s\" is not exists." % nmapPath)

            if nmapPath and not nmap.NmapPathValidator.get(fs).validate(nmapPath):
                logger.warn("Specified Nmap path \"%s\" is not exists. Trying the system path..." % nmapPath)
                nmapPath = None

            nmapDiscover = nmap.getByShell(shell, nmapPath)

            nmapVersion = nmapDiscover.getVersion()
            if not nmapVersion:
                raise Exception('Cannot get nmap version')
            logger.debug("Found nmap %s" % nmapVersion)
            nmapVersion = float(nmapVersion)
            if nmapVersion < 5.21:
                raise Exception("Not supported version of nmap found.")

            tcpPorts = [port.getPortNumber() for port in portsToDiscover if
                        port and port.getProtocolName() == 'tcp' and port.isIpInRange(ipAddress)]
            udpPorts = [port.getPortNumber() for port in portsToDiscover if
                        port and port.getProtocolName() == 'udp' and port.isIpInRange(ipAddress)]

            discoveredPorts = nmapDiscover.doPortScan(ipAddress, tcpPorts, udpPorts)

            portsNameByPortInfo = {}
            for port in portsToDiscover:
                port_names = portsNameByPortInfo.setdefault((port.getProtocol(), port.getPortNumber()), [])
                port_names.append(port.portName)

            if discoveredPorts:
                isConnectedPortFound = True
                for port_info in discoveredPorts:
                    port_names = portsNameByPortInfo.get(port_info, [])
                    OSHVResult.addAll(reportPort(hostOsh, ipAddress, port_names, *port_info))
        except:
            logger.debugException("Nmap executing failed. Try to use default behavior...")
            logger.reportWarning("Nmap executing failed")
            useFallback = True

    if useFallback or not useNMap:
        # Old flow supports only TCP ports
        for port in portsToDiscover:
            if port.isIpInRange(ipAddress):
                if port.getProtocol() == PortType.UDP.getProtocol():
                    logger.warn("UDP port scan is not supporting by default behavior. Skipping...")
                elif port.getProtocol() == PortType.TCP.getProtocol() and (
                netutils.checkTcpConnectivity(ipAddress, port.getPortNumber(), connectTimeOut)):
                    OSHVResult.addAll(
                        reportPort(hostOsh, ipAddress, [port.portName], port.getProtocol(), port.getPortNumber()))
                    #we found one connected port -> we need to add hostOsh to OSHVResult
                    isConnectedPortFound = True

    #in case we didn't find any port, return nothing
    if not isConnectedPortFound:
        OSHVResult.clear()
        msg = 'None of specified ports were discovered on destination host'
        warningObject = errorobject.createError(errorcodes.CONNECTION_FAILED_NO_PROTOCOL_WITH_DETAILS, ['', msg], msg)
        logger.reportWarningObject(warningObject)

    return OSHVResult
Exemple #10
0
def getNetworkDevices(localDbClient, queryChunkSize, ipAddrList, portVlanIdMap,
                      ignoreNodesWithoutIP, allowDnsLookup, localFramework):
    try:
        returnOSHV = ObjectStateHolderVector()

        ## Get total number of network devices in the database
        numDevices = 0
        deviceCountQuery = 'SELECT COUNT(1) FROM lmsdatagrp.NETWORK_DEVICES'
        deviceCountResultSet = ciscoworks_utils.doQuery(
            localDbClient, deviceCountQuery)
        ## Return if query returns no results
        if deviceCountResultSet == None:
            logger.warn('[' + SCRIPT_NAME +
                        ':getNetworkDevices] No Network Devices found')
            return None
        ## We have query results!
        while deviceCountResultSet.next():
            numDevices = int(
                ciscoworks_utils.getStringFromResultSet(
                    deviceCountResultSet, 1))

        ## Determine chunk count
        ciscoworks_utils.debugPrint(
            2, '[' + SCRIPT_NAME +
            ':getNetworkDevices] Got <%s> Network Devices...' % numDevices)
        numChunks = int(numDevices / queryChunkSize) + 1
        ciscoworks_utils.debugPrint(
            2, '[' + SCRIPT_NAME +
            ':getNetworkDevices] Got <%s> chunks...' % numChunks)

        for chunkIndex in range(0, numChunks):
            queryStartRow = chunkIndex * queryChunkSize
            if queryStartRow == 0:
                queryStartRow = 1
            netDeviceQuery = '''SELECT TOP %s START AT %s
                                    netdevices.Device_Id, deviceState.NetworkElementID, netdevices.Device_Display_Name,
                                    netdevices.Host_Name, netdevices.Device_Category, netdevices.Device_Model,
                                    netdevices.Management_IPAddress, deviceState.Global_State
                                FROM lmsdatagrp.NETWORK_DEVICES netdevices JOIN dba.DM_Dev_State deviceState
                                    ON netdevices.Device_Id=deviceState.DCR_ID''' % (
                queryChunkSize, queryStartRow)
            #netDeviceQuery = '%s WHERE LOWER(netdevices.Device_Display_Name) LIKE \'a%%\'' % netDeviceQuery
            netDeviceResultSet = ciscoworks_utils.doQuery(
                localDbClient, netDeviceQuery)

            ## Return if query returns no results
            if netDeviceResultSet == None:
                logger.warn(
                    '[' + SCRIPT_NAME +
                    ':getNetworkDevices] No Network Devices found in chunk <%s>'
                    % chunkIndex)
                return None

            ## We have query results!
            while netDeviceResultSet.next():
                netDeviceOSH = ipOSH = None
                ## Get values from result set
                netDeviceID = ciscoworks_utils.getStringFromResultSet(
                    netDeviceResultSet, 1)
                netDeviceElementID = ciscoworks_utils.getStringFromResultSet(
                    netDeviceResultSet, 2)
                netDeviceDisplayName = ciscoworks_utils.getStringFromResultSet(
                    netDeviceResultSet, 3)
                netDeviceHostName = ciscoworks_utils.getStringFromResultSet(
                    netDeviceResultSet, 4)
                netDeviceCategory = ciscoworks_utils.getStringFromResultSet(
                    netDeviceResultSet, 5)
                netDeviceModel = ciscoworks_utils.getStringFromResultSet(
                    netDeviceResultSet, 6)
                ipAddress = ciscoworks_utils.getStringFromResultSet(
                    netDeviceResultSet, 7)
                #netDeviceStateIndex = ciscoworks_utils.getStringFromResultSet(netDeviceResultSet, 7)
                ## Set device name based on first available value
                netDeviceName = netDeviceDisplayName or netDeviceHostName
                ciscoworks_utils.debugPrint(
                    1, '[' + SCRIPT_NAME +
                    ':getNetworkDevices] Got Device <%s> with ID <%s>' %
                    (netDeviceName, netDeviceElementID))

                ## Get enums for net device
                #deviceStateEnumDict = ciscoworks_utils.getEnum(localDbClient, 'dba.DM_Global_State_Enum')
                physicalTypeEnumDict = ciscoworks_utils.getEnum(
                    localDbClient, 'dba.PhysicalTypeEnum')

                ## Initialize variables for additional data
                netDeviceElementName = netDeviceReportedName = netDeviceDNSDomainName = netDeviceDescription = netDeviceContact = netDeviceLocation = None
                netDeviceOsName = netDeviceOsVersion = netDeviceManufacturer = netDeviceSerialNumber = None
                netDeviceDnsName = None

                ## Get additional details for this device
                netDeviceAdditionalDataQuery = '''SELECT ne.ElementName, ne.ReportedHostName, ne.DNSDomainName, ne.Description,
                                                    ne.PrimaryOwnerContact, ne.ElementLocation,
                                                    os.OSName, os.Version, os.ROMVersion, pe.Manufacturer, pe.SerialNumber
                                                FROM dba.OperatingSystem os, dba.PhysicalElement pe, dba.networkelement ne
                                                WHERE os.NetworkElementID=%s AND ne.NetworkElementID=%s AND pe.NetworkElementID=%s
                                                    AND LOWER(pe.PhysicalType)=%s AND pe.PhysicalElementId IN (1, 2)'''\
                                                % (netDeviceElementID, netDeviceElementID, netDeviceElementID, physicalTypeEnumDict['Chassis/Frame'])
                netDeviceAdditionalDataResultSet = ciscoworks_utils.doQuery(
                    localDbClient, netDeviceAdditionalDataQuery)

                ## Return if query returns no results
                if netDeviceAdditionalDataResultSet == None:
                    logger.warn(
                        '[' + SCRIPT_NAME +
                        ':getNetworkDevices] No additional data found for network device <%s> with ID <%s>'
                        % (netDeviceName, netDeviceElementID))
                    return None

                ## We have query results!
                while netDeviceAdditionalDataResultSet.next():
                    ## Get values from result set
                    netDeviceElementName = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 1)
                    netDeviceReportedName = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 2)
                    netDeviceDNSDomainName = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 3)
                    netDeviceDescription = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 4)
                    netDeviceContact = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 5)
                    netDeviceLocation = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 6)
                    netDeviceOsName = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 7)
                    netDeviceOsVersion = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 8)
                    #netDeviceRomVersion = ciscoworks_utils.getStringFromResultSet(netDeviceAdditionalDataResultSet, 9)
                    netDeviceManufacturer = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 10)
                    netDeviceSerialNumber = ciscoworks_utils.getStringFromResultSet(
                        netDeviceAdditionalDataResultSet, 11)
                    ciscoworks_utils.debugPrint(
                        3, '[' + SCRIPT_NAME +
                        ':getNetworkDevices] Got additional information for Net Device <%s> with ID <%s>'
                        % (netDeviceName, netDeviceElementID))
                netDeviceAdditionalDataResultSet.close()

                if not netDeviceName:
                    netDeviceName = netDeviceElementName or netDeviceReportedName
                if netDeviceDNSDomainName and not netutils.isValidIp(
                        netDeviceName):
                    #netDeviceName = '%s.%s' % (netDeviceName, netDeviceDNSDomainName)
                    #netDeviceDnsName = netDeviceName.lower()
                    netDeviceDnsName = '%s.%s' % (netDeviceName,
                                                  netDeviceDNSDomainName)

                ## Determine Net Device CI Type
                netDeviceCiType = 'netdevice'
                netDeviceCategoreToCiTypeMap = {
                    'Routers': 'router',
                    'Switches and Hubs': 'switch',
                    'Content Networking': 'switch',
                    'Cisco Interfaces and Modules': 'switch',
                    'Wireless': 'netdevice',
                    'Voice and Telephony': 'netdevice',
                    'Unknown': 'netdevice'
                }
                if netDeviceCategory in netDeviceCategoreToCiTypeMap.keys():
                    netDeviceCiType = netDeviceCategoreToCiTypeMap[
                        netDeviceCategory]

                ## Discard management IP if this is a duplicate
                if ipAddress and netutils.isValidIp(
                        ipAddress) and ipAddress in ipAddrList:
                    logger.debug(
                        '[' + SCRIPT_NAME +
                        ':getNetworkDevices] Duplicate IP address <%s> on Network Device <%s> with ID <%s>!! Discarding IP...'
                        % (ipAddress, netDeviceName, netDeviceElementID))
                    ipAddress = None
                else:
                    ipAddrList.append(ipAddress)
                ## Get the list of IP addresses associated with this device
                ipSubnetDict = getIpSubnetDict(localDbClient, ipAddrList,
                                               netDeviceID, netDeviceElementID,
                                               netDeviceName)

                # Check if an IP address is available to build the host key
                # If an IP is not available and a DNS name is available, try resolving the IP
                # If not, skip this device
                ## If a management IP is not available, use the first IP in the IP list
                if not ipAddress and ipSubnetDict and len(ipSubnetDict) > 0:
                    ipAddress = ipSubnetDict[0]
                ## Try DNS lookup if an IP is not available
                if not (ipAddress and netutils.isValidIp(ipAddress)
                        ) and allowDnsLookup and netDeviceDnsName:
                    ciscoworks_utils.debugPrint(
                        3, '[' + SCRIPT_NAME +
                        ':getNetworkDevices] No IP for Device <%s> with DNS name <%s>! Attempting DNS lookup...'
                        % (netDeviceName, netDeviceDnsName))
                    ipAddress = netutils.getHostAddress(netDeviceDnsName)
                if not (ipAddress and netutils.isValidIp(ipAddress)
                        ) and allowDnsLookup and netDeviceName:
                    ciscoworks_utils.debugPrint(
                        3, '[' + SCRIPT_NAME +
                        ':getNetworkDevices] No IP for Device <%s> with ID <%s>! Attempting DNS lookup...'
                        % (netDeviceName, netDeviceElementID))
                    ipAddress = netutils.getHostAddress(netDeviceName)
                ## Check for a valid IP before creating CIs
                if ipAddress and netutils.isValidIp(ipAddress):
                    netDeviceOSH = modeling.createHostOSH(
                        ipAddress, netDeviceCiType)
                    ipOSH = modeling.createIpOSH(ipAddress, None,
                                                 netDeviceDnsName, None)
                elif ignoreNodesWithoutIP:
                    logger.debug(
                        '[' + SCRIPT_NAME +
                        ':getNetworkDevices] IP address not available for Network Device <%s> with ID <%s>!! Skipping...'
                        % (netDeviceName, netDeviceElementID))
                    continue
                else:
                    logger.debug(
                        '[' + SCRIPT_NAME +
                        ':getNetworkDevices] IP address not available for Network Device <%s> with ID <%s>!! Creating Network Device with ID as primary key...'
                        % (netDeviceName, netDeviceElementID))
                    hostKey = netDeviceElementID + ' (CiscoWorks Network Element ID)'
                    netDeviceOSH = modeling.createCompleteHostOSH(
                        netDeviceCiType, hostKey)
                    netDeviceOSH.setAttribute(
                        'data_note',
                        'IP address unavailable in CiscoWorks LMS - Duplication of this CI is possible'
                    )

                ## Set the real name of the netDevice
                netDeviceRealName = netDeviceName
                if netDeviceName and netutils.isValidIp(netDeviceName):
                    netDeviceRealName = ''
                ## Add more details to the OSH
                ciscoworks_utils.populateOSH(
                    netDeviceOSH, {
                        'name': netDeviceRealName,
                        'data_externalid': netDeviceName,
                        'discovered_description': netDeviceDescription,
                        'discovered_contact': netDeviceContact,
                        'discovered_location': netDeviceLocation,
                        'discovered_os_name': netDeviceOsName,
                        'discovered_os_version': netDeviceOsVersion,
                        'discovered_model': netDeviceModel,
                        'serial_number': netDeviceSerialNumber,
                        'discovered_vendor': netDeviceManufacturer,
                        'primary_dns_name': netDeviceDnsName,
                        'domain_name': netDeviceDNSDomainName
                    })
                ## Set node role
                netDeviceOSH.setListAttribute('node_role', [netDeviceCiType])
                returnOSHV.add(netDeviceOSH)
                returnOSHV.addAll(
                    getNetDevicePortsAndVlans(localDbClient, portVlanIdMap,
                                              netDeviceID, netDeviceElementID,
                                              netDeviceName, netDeviceOSH))
                returnOSHV.addAll(
                    getModules(localDbClient, netDeviceID, netDeviceName,
                               netDeviceOSH))

                ## Add IPs to OSHV
                if ipOSH:
                    returnOSHV.add(ipOSH)
                    returnOSHV.add(
                        modeling.createLinkOSH('containment', netDeviceOSH,
                                               ipOSH))
                if ipSubnetDict and len(ipSubnetDict) > 0:
                    for ipAddy in ipSubnetDict.keys():
                        ipOSH = modeling.createIpOSH(ipAddy,
                                                     ipSubnetDict[ipAddy],
                                                     netDeviceDnsName, None)
                        returnOSHV.add(ipOSH)
                        returnOSHV.add(
                            modeling.createLinkOSH('containment', netDeviceOSH,
                                                   ipOSH))

            netDeviceResultSet.close()

            ## Send results to server
            localFramework.sendObjects(returnOSHV)
            localFramework.flushObjects()
            returnOSHV.clear()

        return returnOSHV
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.warn('[' + SCRIPT_NAME +
                    ':getNetworkDevices] Exception: <%s>' % excInfo)
        pass
Exemple #11
0
def getCICSPrograms(ls,  regiondict , subsystemOSH, excluderestricted, Framework):

## Sample output:
#    
# LIST:DFH$IVPL
# LIST:DFHLIST
# LIST:XYZLIST
# LIST:ZORPHEN
# GROUP:CEE:DFH$IVPL:DFHLIST:XYZLIST
# GROUP:CICSLOGS:XYZLIST
# GROUP:DFHCBTS:DFH$IVPL:DFHLIST:XYZLIST
# GROUP:DFHDCTG:DFH$IVPL:DFHLIST:XYZLIST
# GROUP:DFHIPECI:DFH$IVPL:DFHLIST:XYZLIST
# GROUP:EVOGRP:XYZLIST
# GROUP:SOCKETS:XYZLIST
# GROUP:USERCONS:XYZLIST
# PGM:CALLOPC:   :ASSEMBLER:NO:NO:ENABLED:EVOGRP
# PGM:CALLOPCR:   :   :NO:NO:ENABLED:EVOGRP
# PGM:CEECBLDY:   :   :NO:NO:ENABLED:CEE
# PGM:CEECCICS:   :   :NO:NO:ENABLED:CEE
# PGM:CEECRHP:   :   :NO:NO:ENABLED:CEE
# PGM:CEECZST:   :   :NO:NO:ENABLED:CEE  
# TRN:CIEP:ECI OVER TCP/IP INTERFACE STARTED BY SO DOMAIN:DFHIEP:DFHCICST:ENABLED:NO::DFHTCL00:NO:NO:DFHIPECI
# TRN:EZAP:DISABLE SOCKETS INTERFACE:EZACIC22:DFHCICST:ENABLED:YES::DFHTCL00:NO:NO:SOCKETS
  
#######################################################
    
    # Query the mainframe for CICS Programs and Transactions
    str_name = 'name'
    if UCMDB_VERSION < 9:
        str_name = 'data_name'
    for region in regiondict.keys():
        cicslistdict = {}
        cicsgroupdict = {}
        cicspgmdict = {}
        vector = ObjectStateHolderVector() 
             
        regionwithoption  =  concatenate(region,'-',excluderestricted)
        # check if ev390cicstrans.pl script exists
        output = None
        scriptAbsolutePath = '%sev390cicstrans.pl' % ls.ar
        if ls.evFileExists(scriptAbsolutePath):
            output = ls.evGetCicsTran(regionwithoption)
        else:
            output = ls.evSysInfoCmd(regionwithoption, '12', 'EVOCICST' )
        if not isNull(output) and output.isSuccess() and len(output.cmdResponseList) > 0:         
            for line in  output.cmdResponseList:
                # Skip the CICS systems that are not up
                m = re.search('IEE341I', line)
                if (m):               
                    break               
                splitline = line.split(':')             
                # Look for all the CICS Lists and build OSHs for them
                if splitline[0] == 'LIST':
                    cicsListOSH = ObjectStateHolder('cics_list') 
                    cicsListOSH.setAttribute(str_name, splitline[1] )  
                    cicsListOSH.setContainer(regiondict[region])
                    vector.add (cicsListOSH)                  
                    if not(cicslistdict.has_key(splitline[1])): 
                        cicslistdict[splitline[1]] = cicsListOSH
                # Look for the CICS Groups and build OSHs for them
                elif splitline[0] == 'GROUP':
                    cicsGroupOSH = ObjectStateHolder('cics_group')                    
                    cicsGroupOSH.setAttribute(str_name, splitline[1] )
                    cicsGroupOSH.setContainer(regiondict[region])
                    vector.add (cicsGroupOSH)
                    i = 2 
                    while i < len(splitline):                   
                        cicsListOSH = ObjectStateHolder('cics_list') 
                        cicsListOSH.setAttribute(str_name, splitline[i] )  
                        cicsListOSH.setContainer(regiondict[region])
                        vector.add (cicsListOSH)                        
                        if not(cicslistdict.has_key(splitline[i])):  
                            cicslistdict[splitline[i]] = cicsListOSH
                        vector.add (modeling.createLinkOSH('containment', cicslistdict[splitline[i]], cicsGroupOSH))
                        i = i + 1
                # Look for the CICS Programs 
                elif splitline[0] == 'PGM':
                    cicsProgramOSH = ObjectStateHolder('cics_program')
                    cicsProgramOSH.setAttribute(str_name, splitline[1]) 
                    cicsProgramOSH.setAttribute('description', splitline[2])      
                    cicsProgramOSH.setAttribute('pgm_language', splitline[3]) 
                    cicsProgramOSH.setAttribute('pgm_reload', splitline[4])  
                    cicsProgramOSH.setAttribute('pgm_resident', splitline[5])  
                    cicsProgramOSH.setAttribute('pgm_status', splitline[6])             
                    cicsProgramOSH.setContainer(regiondict[region]) 
                    vector.add (cicsProgramOSH )
                    if not(cicspgmdict.has_key(splitline[1])):  
                            cicspgmdict[splitline[1]] = cicsProgramOSH 
                    i = 7 
                    while i < len(splitline):                   
                        cicsGroupOSH = ObjectStateHolder('cics_group') 
                        cicsGroupOSH.setAttribute(str_name, splitline[i] )  
                        cicsGroupOSH.setContainer(regiondict[region])
                        vector.add (cicsGroupOSH)                     
                        if not(cicsgroupdict.has_key(splitline[i])):  
                            cicsgroupdict[splitline[i]] = cicsGroupOSH
                        vector.add (modeling.createLinkOSH('containment', cicsgroupdict[splitline[i]],  cicsProgramOSH))
                        i = i + 1
                # Look for the CICS Transactions
                elif splitline[0] == 'TRN':
                    cicsTransactionOSH = ObjectStateHolder('cics_transaction')                  
                    cicsTransactionOSH.setAttribute(str_name, splitline[1]) 
                    cicsTransactionOSH.setAttribute('description', splitline[2])      
                    cicsTransactionOSH.setAttribute('trans_status', splitline[5]) 
                    cicsTransactionOSH.setAttribute('trans_protected', splitline[6])  
                    cicsTransactionOSH.setAttribute('trans_class', splitline[8])  
                    cicsTransactionOSH.setAttribute('resource_security', splitline[9]) 
                    cicsTransactionOSH.setAttribute('command_security', splitline[10])             
                    cicsTransactionOSH.setContainer(regiondict[region]) 
                    vector.add (cicsTransactionOSH)
                    i = 11 
                    while i < len(splitline):                   
                        cicsGroupOSH = ObjectStateHolder('cics_group') 
                        cicsGroupOSH.setAttribute(str_name, splitline[i] )  
                        cicsGroupOSH.setContainer(regiondict[region])
                        vector.add (cicsGroupOSH)                     
                        if not(cicsgroupdict.has_key(splitline[i])):  
                            cicsgroupdict[splitline[i]] = cicsGroupOSH
                        vector.add (modeling.createLinkOSH('containment', cicsgroupdict[splitline[i]],  cicsTransactionOSH))
                        i = i + 1
                    if cicspgmdict.has_key(splitline[3]):   
                        vector.add (modeling.createLinkOSH('usage',  cicsTransactionOSH, cicspgmdict[splitline[3]]))
        Framework.sendObjects(vector)
        Framework.flushObjects()
        vector.clear()
        vector = None

    return