def appendToDictionary(originalDict, newDict):
    dict = {}
    if isNull(originalDict):
        originalDict = dict
    for (x, y) in newDict.items():
        if isNotNull(y) and not originalDict.has_key(x):
            originalDict[x] = y
    return originalDict
def appendToDictionary(originalDict, newDict):
    dict = {}
    if isNull(originalDict):
        originalDict = dict
    for (x, y) in newDict.items():
        if isNotNull(y) and not originalDict.has_key(x):
            originalDict[x] = y
    return originalDict
def appendToList(originalList, newList):
    tempList = []
    if isNull(originalList):
        originalList = tempList
    for val in newList:
        if isNotNull(val):
            originalList.append(val)
    return originalList
def appendToList(originalList, newList):
    tempList = []
    if isNull(originalList):
        originalList = tempList
    for val in newList:
        if isNotNull(val):
            originalList.append(val)
    return originalList
def processiSeriesResources(ls, lparOsh, knownPortsConfigFile, Framework):

    # Process LPAR iSeries Resources
    _vector = ObjectStateHolderVector()

    #===========================================================================
    # Run commands and create OSHs
    #===========================================================================
    (cpuLists, cecTable, memTable) = ev1_getCpulistOutput(ls)
    # If we don't get output then the Iseries command failed and we need to get out
    if isNull(cpuLists):
        return _vector
    ''' Create IBM Frame OSH '''

    _vector.addAll(osh_createIBMframeOsh(lparOsh, cecTable))

    # CPU List Command ---------------------------------------------------------
    ''' Create CPU OSH '''
    createCpu = Framework.getParameter('discover_CPUs')
    if isNotNull(createCpu) and string.lower(createCpu) == 'true':
        _vector.addAll(osh_createCpuOsh(lparOsh, cpuLists))
    ''' Update the memory in the lpar OSH'''

    _vector.addAll(osh_updateMemOsh(lparOsh, memTable))
    ''' TCPIP Stacks Command '''
    tcpStacks, primaryIP = ev2_getTCPStack(ls)

    # Create IP OSHs ------------------------------------------------------------

    _vector.addAll(osh_createIpOsh(lparOsh, tcpStacks))

    # Create Network OSHs ------------------------------------------------------------

    _vector.addAll(osh_createNetworkOsh(lparOsh, tcpStacks))

    # Discover Subsystems -------------------------------------------------------

    createSubsystem = Framework.getParameter('discover_Subsystems')
    if isNotNull(createSubsystem) and string.lower(createSubsystem) == 'true':
        ssiList = ev3_getSubsystemOutput(ls)
        _vector.addAll(osh_createSubsystemsOsh(lparOsh, ssiList))

    # Discover Auxillary Storage Pools and Disks------------------------------------------------------

    createASP = Framework.getParameter('discover_ASP')
    if isNotNull(createASP) and string.lower(createASP) == 'true':
        aspList, diskList = ev4_getAspandDisk(ls)
        _vector.addAll(osh_createASPOsh(lparOsh, aspList, diskList))

    createSoftware = Framework.getParameter('discover_Software')
    if isNotNull(createSoftware) and string.lower(createSoftware) == 'true':

        prodLists = ev5_getInstalledSoftware(ls)
        ''' Create Iseries Software CIs '''
        _vector.addAll(osh_createSoftwareOsh(lparOsh, prodLists))

    createTCP = Framework.getParameter('discover_TCP_UDP')
    if isNotNull(createTCP) and string.lower(createTCP) == 'true':
        connections = ev6_getTcpConnOutput(ls)
        ''' Create Iseries Connection CIs '''
        _vector.addAll(
            osh_createTcpConnectionsOsh(lparOsh, primaryIP,
                                        knownPortsConfigFile, connections))

    return _vector
def osh_createTcpConnectionsOsh(lparOsh, primaryIP, knownPortsConfigFile,
                                connections):
    str_containment = 'containment'
    _vector = ObjectStateHolderVector()

    ignoreLocalConnections = 0  ## ER: parameterize

    probeName = CollectorsParameters.getValue(
        CollectorsParameters.KEY_COLLECTORS_PROBE_NAME)

    for conn in connections:
        dstPort = ''
        dstAddr = ''
        srcAddr = ''
        srcPort = ''

        id = conn[5]
        #(dstAddr, dstPort) = _getIpPortFromSocket(localSocket, primaryIP)
        dstAddr = conn[2].strip()
        if dstAddr == '0.0.0.0' or dstAddr == '127.0.0.1':
            dstAddr = primaryIP.strip()
        dstPort = conn[3].strip()
        state = conn[9].strip()

        #(srcAddr, srcPort) = _getIpPortFromSocket(foreignSocket, primaryIP)
        if upper(state) == 'ESTABLISH':
            srcAddr = conn[0].strip()
            srcPort = conn[1].strip()
        if srcAddr == '127.0.0.1':
            srcAddr = primaryIP.strip()

        if ignoreLocalConnections and (srcAddr == dstAddr):
            continue

        if isNotNull(dstAddr):
            destination = '%s:%s' % (dstAddr, dstPort)
            logger.debug('[', state, '] Current connection: ', srcAddr, ' -> ',
                         destination)

            # create destination (server) IP and Host --------------------------
            dstIpOsh = modeling.createIpOSH(dstAddr)
            if isNotNull(probeName):
                dstIpOsh.setAttribute('ip_probename', probeName)
            dstHostOsh = None
            if isNotNull(lparOsh):
                dstHostOsh = lparOsh
            else:
                dstHostOsh = modeling.createHostOSH(dstAddr)
            dstContainedLinkOsh = modeling.createLinkOSH(
                str_containment, dstHostOsh, dstIpOsh)
            _vector.add(dstIpOsh)
            _vector.add(dstHostOsh)
            _vector.add(dstContainedLinkOsh)

            # create destination service address object ------------------------
            portTypeEnum = TCP_PORT_TYPE_ENUM
            portName = knownPortsConfigFile.getTcpPortName(int(dstPort))
            if upper(state) == 'UDP':
                portTypeEnum = UDP_PORT_TYPE_ENUM
                portName = knownPortsConfigFile.getUdpPortName(int(dstPort))
            if isNull(portName):
                portName = dstPort
            serverPortOsh = modeling.createServiceAddressOsh(
                dstHostOsh, dstAddr, int(dstPort), portTypeEnum, portName)
            _vector.add(serverPortOsh)

            if isNotNull(srcAddr):
                # create source (client) IP and Host ---------------------------
                srcIpOsh = modeling.createIpOSH(srcAddr)
                if isNotNull(probeName):
                    srcIpOsh.setAttribute('ip_probename', probeName)
                srcHostOsh = modeling.createHostOSH(srcAddr)
                srcContainedLinkOsh = modeling.createLinkOSH(
                    str_containment, srcHostOsh, srcIpOsh)
                _vector.add(srcIpOsh)
                _vector.add(srcHostOsh)
                _vector.add(srcContainedLinkOsh)

                # create client-server links -----------------------------------
                _vector.add(
                    _createClientServerLinkOsh(dstPort,
                                               serverPortOsh, portName,
                                               lower(state), srcIpOsh))

                # create client server dependency links ------------------------
                _vector.add(
                    _createClientServerDependencyLinkOsh(
                        dstHostOsh, dstPort, srcHostOsh, portName))
    return _vector
def processEviewConfFiles(Framework, localshell):
    _vector = ObjectStateHolderVector()
    fileMonitor = file_mon_utils.FileMonitor(Framework, localshell,
                                             ObjectStateHolderVector(), None,
                                             None)
    folder = Framework.getParameter('EViewInstallationFolder')
    if isNull(folder):
        logger.reportError(
            'Job parameter - EViewInstallationFolder is empty. Set the path to the EView client installation root and rerun job.'
        )
        return _vector

    appPath = fileMonitor.rebuildPath(folder) + "\\bin\\ev400hostcmd.exe"
    confFolder = fileMonitor.rebuildPath(folder) + "\\conf\\"
    confFiles = None
    try:
        confFiles = fileMonitor.listFilesWindows(confFolder,
                                                 EViewConfFileNameFilter())
    except:
        logger.reportError(
            'Unable to get EView configuration files from folder: %s' %
            confFolder)
        return _vector

    # Create iSeries & EView agent objects -----------------------------------------
    if isNull(confFiles):
        logger.reportError(
            'Unable to get EView configuration files from folder: %s' %
            confFolder)
        return _vector
    elif len(confFiles) < 1:
        logger.reportError(
            'Unable to get EView configuration files from folder: %s' %
            confFolder)
        return _vector
    else:
        for file in confFiles:

            nodeName = file[13:len(
                file
            )]  # The name of the configuration file is ev400_config_<NODE_NAME>
            logger.debug('Node = ', nodeName)
            if eview400_lib.isNotNull(nodeName):

                #===================================================================
                # connect to each node with configuration and only
                # create EView CI for the ones that connect
                #===================================================================

                ls = eview400_lib.EvShell(Framework, nodeName, appPath)
                # Get EView agent version ------------------------------------------

                eviewVersion = ev1_getEviewVersion(ls)
                if eview400_lib.isNotNull(eviewVersion):
                    logger.debug(
                        'Successfully executed command against EView agent on node: ',
                        nodeName)

                    # Get the iSeries  info -------------------------------------------------
                    (model, serialnbr, osversion,
                     sysname) = ev2_getiSeriesOutput(ls)

                    # Get the default IP of the LPAR -------------------------------
                    defaultIp, macAddress = ev_getIPofLpar(ls)

                    if isNull(defaultIp):
                        logger.reportWarning(
                            'Unable to get IP Address of LPAR: %s. Continuing with next LPAR.'
                            % nodeName)
                        continue
                    else:

                        # Create iSeries OSH ---------------------------------
                        iSeriesOsh = osh_createiSeriesOsh(
                            defaultIp, model, osversion, serialnbr, sysname,
                            macAddress)
                        _vector.add(iSeriesOsh)

                        if isNotNull(iSeriesOsh):
                            # Create IP OSH and link it to zOS OSH -------------------------
                            _vector.addAll(
                                osh_createIpOsh(iSeriesOsh, defaultIp))

                            # Create EView Agent OSH and link it to the zOS OSH ------------
                            eviewOSH = osh_createEviewOsh(
                                localshell, iSeriesOsh, appPath, confFolder,
                                file, nodeName, eviewVersion, defaultIp)
                            _vector.add(eviewOSH)
                else:
                    warnMsg = 'Unable to connect to: %s' % nodeName
                    logger.warn(warnMsg)
                    warnObj = errorobject.createError(
                        errorcodes.CONNECTION_FAILED, None, warnMsg)
                    logger.reportWarningObject(warnObj)

    return _vector
def processiSeriesResources(ls, lparOsh, knownPortsConfigFile, Framework):
    
    # Process LPAR iSeries Resources 
    _vector = ObjectStateHolderVector()
    
    #===========================================================================
    # Run commands and create OSHs
    #===========================================================================
    (cpuLists ,cecTable, memTable) = ev1_getCpulistOutput(ls)
    # If we don't get output then the Iseries command failed and we need to get out
    if isNull (cpuLists):
        return _vector
    ''' Create IBM Frame OSH '''
    
    _vector.addAll(osh_createIBMframeOsh(lparOsh ,cecTable))
     
    # CPU List Command ---------------------------------------------------------
     
    ''' Create CPU OSH '''
    createCpu = Framework.getParameter('discover_CPUs')
    if isNotNull(createCpu) and string.lower(createCpu) == 'true':     
        _vector.addAll(osh_createCpuOsh(lparOsh, cpuLists))
        
    ''' Update the memory in the lpar OSH'''   
         
    _vector.addAll(osh_updateMemOsh(lparOsh, memTable))
    
    ''' TCPIP Stacks Command '''
    tcpStacks, primaryIP = ev2_getTCPStack(ls)    
 
    # Create IP OSHs ------------------------------------------------------------
    
    _vector.addAll(osh_createIpOsh(lparOsh, tcpStacks))
    
    # Create Network OSHs ------------------------------------------------------------
    
    _vector.addAll(osh_createNetworkOsh(lparOsh, tcpStacks))
    
    # Discover Subsystems -------------------------------------------------------
    
    createSubsystem = Framework.getParameter('discover_Subsystems')
    if isNotNull(createSubsystem) and string.lower(createSubsystem) == 'true':      
        ssiList = ev3_getSubsystemOutput(ls)            
        _vector.addAll(osh_createSubsystemsOsh(lparOsh, ssiList))
    

    # Discover Auxillary Storage Pools and Disks------------------------------------------------------
    
    createASP = Framework.getParameter('discover_ASP')
    if isNotNull(createASP) and string.lower(createASP) == 'true':      
        aspList, diskList = ev4_getAspandDisk(ls)       
        _vector.addAll(osh_createASPOsh(lparOsh, aspList, diskList))
     
      
 
    createSoftware = Framework.getParameter('discover_Software')
    if isNotNull(createSoftware) and string.lower(createSoftware) == 'true':
    
        prodLists = ev5_getInstalledSoftware(ls)   
        
        ''' Create Iseries Software CIs '''
        _vector.addAll(osh_createSoftwareOsh(lparOsh, prodLists))
        
    createTCP = Framework.getParameter('discover_TCP_UDP')
    if isNotNull(createTCP) and string.lower(createTCP) == 'true':
        connections = ev6_getTcpConnOutput(ls)
                  
        ''' Create Iseries Connection CIs '''
        _vector.addAll(osh_createTcpConnectionsOsh(lparOsh, primaryIP, knownPortsConfigFile, connections))

    
    return _vector
def osh_createTcpConnectionsOsh(lparOsh,  primaryIP, knownPortsConfigFile, connections):
    str_containment = 'containment'
    _vector = ObjectStateHolderVector()
    
    ignoreLocalConnections = 0 ## ER: parameterize

    probeName = CollectorsParameters.getValue(CollectorsParameters.KEY_COLLECTORS_PROBE_NAME)
    
    for conn in connections:
        dstPort = ''
        dstAddr = ''
        srcAddr = ''
        srcPort = ''
        
        id = conn[5]
        #(dstAddr, dstPort) = _getIpPortFromSocket(localSocket, primaryIP)
        dstAddr = conn[2].strip()
        if dstAddr == '0.0.0.0' or dstAddr == '127.0.0.1':
            dstAddr = primaryIP.strip()
        dstPort = conn[3].strip()
        state = conn[9].strip()        
            
        
        
        
        #(srcAddr, srcPort) = _getIpPortFromSocket(foreignSocket, primaryIP)
        if upper(state) == 'ESTABLISH':
            srcAddr = conn[0].strip()
            srcPort = conn[1].strip()
        if srcAddr == '127.0.0.1':
            srcAddr = primaryIP.strip()
                
            
        if ignoreLocalConnections and (srcAddr == dstAddr):
            continue
            
        if isNotNull(dstAddr):
            destination = '%s:%s' % (dstAddr, dstPort)
            logger.debug('[', state, '] Current connection: ', srcAddr, ' -> ', destination)
                
            # create destination (server) IP and Host --------------------------
            dstIpOsh = modeling.createIpOSH(dstAddr)
            if isNotNull(probeName):
                dstIpOsh.setAttribute('ip_probename', probeName)
            dstHostOsh = None
            if isNotNull(lparOsh):
                dstHostOsh = lparOsh
            else:
                dstHostOsh = modeling.createHostOSH(dstAddr)
            dstContainedLinkOsh = modeling.createLinkOSH(str_containment, dstHostOsh, dstIpOsh)
            _vector.add(dstIpOsh)
            _vector.add(dstHostOsh)
            _vector.add(dstContainedLinkOsh)
                
            # create destination service address object ------------------------
            portTypeEnum =  TCP_PORT_TYPE_ENUM
            portName = knownPortsConfigFile.getTcpPortName(int(dstPort))
            if upper(state) == 'UDP':
                portTypeEnum =  UDP_PORT_TYPE_ENUM            
                portName = knownPortsConfigFile.getUdpPortName(int(dstPort))
            if isNull(portName):
                portName = dstPort
            serverPortOsh = modeling.createServiceAddressOsh(dstHostOsh, dstAddr, int(dstPort), portTypeEnum, portName)
            _vector.add(serverPortOsh)
    
            if isNotNull(srcAddr):
                # create source (client) IP and Host ---------------------------
                srcIpOsh = modeling.createIpOSH(srcAddr)
                if isNotNull(probeName):
                    srcIpOsh.setAttribute('ip_probename', probeName)
                srcHostOsh = modeling.createHostOSH(srcAddr)
                srcContainedLinkOsh = modeling.createLinkOSH(str_containment, srcHostOsh, srcIpOsh)
                _vector.add(srcIpOsh)
                _vector.add(srcHostOsh)
                _vector.add(srcContainedLinkOsh)
                    
                # create client-server links -----------------------------------
                _vector.add(_createClientServerLinkOsh(dstPort, serverPortOsh, portName, lower(state), srcIpOsh))
                    
                # create client server dependency links ------------------------
                _vector.add(_createClientServerDependencyLinkOsh(dstHostOsh, dstPort, srcHostOsh, portName))
    return _vector
def processEviewConfFiles(Framework, localshell):
    _vector = ObjectStateHolderVector()
    fileMonitor = file_mon_utils.FileMonitor(Framework, localshell, ObjectStateHolderVector(), None, None)
    folder = Framework.getParameter('EViewInstallationFolder')
    if isNull(folder):
        logger.reportError('Job parameter - EViewInstallationFolder is empty. Set the path to the EView client installation root and rerun job.')
        return _vector
    
    appPath = fileMonitor.rebuildPath(folder) + "\\bin\\ev400hostcmd.exe"
    confFolder = fileMonitor.rebuildPath(folder) + "\\conf\\"
    confFiles = None
    try:
        confFiles = fileMonitor.listFilesWindows(confFolder, EViewConfFileNameFilter())
    except:
        logger.reportError('Unable to get EView configuration files from folder: %s' % confFolder)
        return _vector
    
    # Create iSeries & EView agent objects -----------------------------------------
    if isNull(confFiles):
        logger.reportError('Unable to get EView configuration files from folder: %s' % confFolder)
        return _vector
    elif len(confFiles) < 1:
        logger.reportError('Unable to get EView configuration files from folder: %s' % confFolder)
        return _vector
    else:
        for file in confFiles:
            
            nodeName = file[13:len(file)]   # The name of the configuration file is ev400_config_<NODE_NAME>
            logger.debug ('Node = ',nodeName)
            if eview400_lib.isNotNull(nodeName):
                
                #===================================================================
                # connect to each node with configuration and only
                # create EView CI for the ones that connect
                #===================================================================
                
                ls = eview400_lib.EvShell(Framework, nodeName, appPath)
                # Get EView agent version ------------------------------------------
                 
               
                eviewVersion = ev1_getEviewVersion(ls)
                if eview400_lib.isNotNull(eviewVersion):
                    logger.debug('Successfully executed command against EView agent on node: ', nodeName)
                     
                    # Get the iSeries  info -------------------------------------------------
                    (model, serialnbr, osversion, sysname) = ev2_getiSeriesOutput(ls)
                     
                     
                    # Get the default IP of the LPAR -------------------------------
                    defaultIp, macAddress = ev_getIPofLpar(ls)
                    
                    if isNull(defaultIp):
                        logger.reportWarning('Unable to get IP Address of LPAR: %s. Continuing with next LPAR.' % nodeName)
                        continue
                    else:
               
                        # Create iSeries OSH ---------------------------------
                        iSeriesOsh = osh_createiSeriesOsh(defaultIp,  model, osversion, serialnbr, sysname, macAddress)
                        _vector.add(iSeriesOsh)
                        
                        if isNotNull(iSeriesOsh):
                            # Create IP OSH and link it to zOS OSH -------------------------
                            _vector.addAll(osh_createIpOsh(iSeriesOsh, defaultIp))
                            
                            # Create EView Agent OSH and link it to the zOS OSH ------------
                            eviewOSH = osh_createEviewOsh(localshell, iSeriesOsh, appPath, confFolder, file, nodeName, eviewVersion, defaultIp)
                            _vector.add(eviewOSH)
                else:
                    warnMsg = 'Unable to connect to: %s' % nodeName
                    logger.warn(warnMsg)
                    warnObj = errorobject.createError(errorcodes.CONNECTION_FAILED, None, warnMsg)
                    logger.reportWarningObject(warnObj)
                    
    return _vector