def DiscoveryMain(Framework): fileName = Framework.getParameter('file_name').replace( '%PROBE_MGR_RESOURCES_DIR%', CollectorsParameters.PROBE_MGR_RESOURCES_DIR) string_list_delimiter = Framework.getParameter('string_list_delimiter') integer_list_delimiter = Framework.getParameter('integer_list_delimiter') relationship_attr_delimiter = Framework.getParameter( 'relationship_attr_delimiter') set_empty_value_flag = Boolean.parseBoolean( Framework.getParameter('set_empty_value')) if not (fileName and string_list_delimiter and integer_list_delimiter and relationship_attr_delimiter): logger.reportError('Not all job parameters are set.') return try: workbook = xlsutils.openXlFile(fileName) if workbook: classModel = ConfigFilesManagerImpl.getInstance( ).getCmdbClassModel() classModelUtil = xlsutils.ClassModelUtils(classModel, integer_list_delimiter, string_list_delimiter) importer = WorkSheetImporter(workbook, classModelUtil, set_empty_value=set_empty_value_flag) return importer.processWorkbook() except JavaException, ex: logger.reportError(ex.getMessage()) ex.printStackTrace() logger.errorException('')
def DiscoveryMain(Framework): connectionDataManager = None try: logger.debug('Replicating topology from HP OneView') connectionDataManager = FrameworkBasedConnectionDataManager(Framework) if not connectionDataManager.validate(): return mappingFileFolder = os.path.join( CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder(), HP_ONE_VIEW_CONFIG_FOLDER) mappingFileManager = OneviewMappingFileManager(mappingFileFolder) mappingFile = getMappingFileFromFramework(Framework) if mappingFile: return replicateTopologyUsingMappingFile( os.path.join(mappingFileFolder, mappingFile), connectionDataManager, mappingFileManager) else: Framework.reportError('No mapping file found.') logger.errorException("No mapping file found.") except: Framework.reportError('Failed to pull data from OneView.') logger.errorException('Failed to pull data from OneView.') finally: if connectionDataManager: connectionDataManager.closeClient()
def discoverDB2Windows(shellUtils, dbConnStr, appServerOSH, OSHVResult): db_name = dbConnStr cmd = 'db2cmd /c /w /i db2 list database directory' data = shellUtils.execCmd(cmd) #@@CMD_PERMISION siebel protocol execution params = getParams(data, 'Database name', db_name, 'Node name', None) nodeName = params[0] if nodeName: cmd = 'db2cmd /c /w /i db2 list node directory' data = shellUtils.execCmd( cmd) #@@CMD_PERMISION siebel protocol execution params = getParams(data, 'Node name', nodeName, 'Hostname', 'Service name') hostName = params[0] serviceName = params[1] if hostName: db_sid = db_name # NF: translate serviceName into port number db_port = serviceName db_type = 'db2' try: host_ip = netutils.getHostAddress(hostName) logger.debug('building DB2 Server OSH: ', db_type, host_ip, db_name, db_port) buildDBObjects(db_type, host_ip, db_port, db_name, db_sid, appServerOSH, OSHVResult) except: logger.errorException('failed to create db2 server on ', hostName, ' - ')
def discovery(Framework, connectionManager=None): connectionDataManager = connectionManager try: logger.debug('Replicating topology from HP ucs') if not connectionDataManager: connectionDataManager = FrameworkBasedConnectionDataManager( Framework) if not connectionDataManager.validate(): return mappingFileFolder = os.path.join( CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder(), MAPPING_CONFIG_FOLDER) mappingFileManager = UCSMappingFileManager(mappingFileFolder) mappingFile = getMappingFileFromFramework(Framework) if mappingFile: return replicateTopologyUsingMappingFile( os.path.join(mappingFileFolder, mappingFile), connectionDataManager, mappingFileManager) else: Framework.reportError('No mapping file found.') logger.errorException("No mapping file found.") except Exception, e: Framework.reportError('%s' % e) logger.errorException('%s' % e)
def DiscoveryMain(Framework): try: logger.debug('Replicating toplogy from ServiceNow') connectionDataManager = FrameworkBasedConnectionDataManager(Framework) mappingFileFolder = os.path.join( CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder(), 'servicenow') mappingFileManager = OldMappingFileManager(mappingFileFolder) global chunkSize chunkSize = getStepSizeFromFramework(Framework) mappingFile = getMappingFileFromFramework(Framework) if mappingFile: return replicateTopologyUsingMappingFile( os.path.join(mappingFileFolder, mappingFile), connectionDataManager, mappingFileManager) else: return replicateTopologyFromServiceNow(connectionDataManager, mappingFileManager) except: Framework.reportError( 'Failed to pull data from ServiceNow. See RemoteProcess log on the Probe for details' ) logger.errorException('Failed to pull data from ServiceNow')
def DiscoveryMain(Framework): connectionDataManager = None try: logger.debug('Replicating topology from HP OneView') connectionDataManager = FrameworkBasedConnectionDataManager(Framework) if not connectionDataManager.validate(): return mappingFileFolder = os.path.join(CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder(), HP_ONE_VIEW_CONFIG_FOLDER) mappingFileManager = OneviewMappingFileManager(mappingFileFolder) mappingFile = getMappingFileFromFramework(Framework) if mappingFile: return replicateTopologyUsingMappingFile(os.path.join(mappingFileFolder, mappingFile), connectionDataManager, mappingFileManager) else: Framework.reportError('No mapping file found.') logger.errorException("No mapping file found.") except: Framework.reportError('Failed to pull data from OneView.') logger.errorException('Failed to pull data from OneView.') finally: if connectionDataManager: connectionDataManager.closeClient()
def getServers(client, username, enterprise, gatewayOSH, siteOSH, Framework, OSHVResult): #serverListing = srvrmgr.sendCommand('list servers') serverListing = client.executeCmd('list servers show SBLSRVR_NAME, HOST_NAME, INSTALL_DIR, SBLMGR_PID, SV_DISP_STATE, SBLSRVR_STATE, START_TIME, END_TIME, SBLSRVR_STATUS, SV_SRVRID')#@@CMD_PERMISION siebel protocol execution serverTbl = siebel_common.makeTable(serverListing) # sample output #SBLSRVR_NAME HOST_NAME INSTALL_DIR SBLMGR_PID SV_DISP_STATE SBLSRVR_STATE START_TIME END_TIME SBLSRVR_STATUS #------------ --------- ------------------ ---------- ------------- ------------- ------------------- -------- -------------------------------- #sblapp1_AS sblapp1 d:\sea752\siebsrvr 1904 Running Running 2004-08-10 15:43:46 7.5.3.3 [16172] LANG_INDEPENDENT #sblapp2 sblapp2 d:\sea752\siebsrvr 1336 Running Running 2004-08-01 03:29:42 7.5.3.3 [16172] LANG_INDEPENDENT #sblapp1 sblapp1 d:\sea752\siebsrvr LIBMsg: No strings available for this language setting # #3 rows returned. svrcount = 0 for serverEntry in serverTbl: try: # jython doesn't have out parameters, so this is a bypass with # and ArrayList return value that has 2 items. serverObj = serverEntry[0] serverDataRow = serverEntry[1] serverOSH = makeAppServer(client, username, enterprise, serverObj, serverDataRow, siteOSH, Framework, OSHVResult) if serverOSH != None: OSHVResult.add(modeling.createLinkOSH('depend', serverOSH,gatewayOSH)) OSHVResult.add(modeling.createLinkOSH('member', siteOSH,serverOSH)) svrcount += 1 except: logger.errorException('Failed to get server') logger.debug('parsed ', str(svrcount), 'app servers')
def _updateNnmNode(nnmConnection, nnmStub, cmdbId, nnmId, framework): ''' NnmConnection, Stub, string, string, Framework -> None ''' if not nnmConnection.version in [-1, 8, 9]: raise ValueError("Invalid NNM version %s" % nnmConnection.version) customAttributes = [CustomAttribute("UCMDB_ID", cmdbId)] caarray = CustomAttributeArray(customAttributes) try: if nnmConnection.version in [-1, 9]: nnmStub.addCustomAttributes(nnmId, caarray) else: nnmStub.updateCustomAttributes(nnmId, caarray) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) err = stacktrace[2] if re.search('Connection refused', err): logger.errorException('Failed to update NNM server') raise NnmException('Failed to update NNM server') elif re.search('addCustomAttributes', err) and nnmConnection.version == -1: raise UnsupportedNnmMethodException() else: logger.warnException('Failed to update node with id %s in NNM server for UCMDB ID %s' % (nnmId, cmdbId)) framework.reportWarning("Failed to update node with in NNM server")
def discoverMemory(shell, myVec, hostOSH): ''' Discover physical memory by NTCMD Shell, oshVector, osh @command: meminfo ''' cmdMemInfo = 'meminfo' ntcmdErrStr = 'Remote command returned 1(0x1)' localFile = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + CollectorsParameters.FILE_SEPARATOR + 'meminfo.exe' remoteFile = shell.copyFileIfNeeded(localFile) if not remoteFile: logger.warn('Failed copying %s' % cmdMemInfo) return buffer = shell.execCmd(remoteFile)#V@@CMD_PERMISION ntcmd protocol execution logger.debug('Output of ', remoteFile, ': ', buffer) if buffer.find(ntcmdErrStr) != -1: logger.warn('Failed getting memory info') else: logger.debug('Got memory info - parsing...') buffer = buffer.strip() size = 0 try: matchSize = re.search('Total: (\d+) KB', buffer) if matchSize: size = int(matchSize.group(1)) memory.report(myVec, hostOSH, size) except: logger.errorException('Error in discoverMemory()')
def discoverDB2Windows(shellUtils, dbConnStr, appServerOSH, OSHVResult): db_name = dbConnStr cmd = 'db2cmd /c /w /i db2 list database directory' data = shellUtils.execCmd(cmd)#@@CMD_PERMISION siebel protocol execution params = getParams(data, 'Database name', db_name, 'Node name', None) nodeName = params[0] if nodeName: cmd = 'db2cmd /c /w /i db2 list node directory' data = shellUtils.execCmd(cmd)#@@CMD_PERMISION siebel protocol execution params = getParams(data, 'Node name', nodeName, 'Hostname', 'Service name') hostName = params[0] serviceName = params[1] if hostName: db_sid = db_name # NF: translate serviceName into port number db_port = serviceName db_type='db2' try: host_ip = netutils.getHostAddress(hostName) logger.debug('building DB2 Server OSH: ', db_type, host_ip, db_name, db_port) buildDBObjects(db_type,host_ip,db_port, db_name, db_sid, appServerOSH, OSHVResult) except: logger.errorException('failed to create db2 server on ', hostName, ' - ')
def _processCIs(cis): ''' -> map(string, string)''' resultCmdbToNnmIds = {} try: ciSize = cis.getCIs().size() logger.debug('Retrieved UCMDB IDs of %d Hosts from the UCMDB server' % ciSize) for i in range (0, ciSize): ci = cis.getCIs().get(i) props = ci.getProps() strprops = props.getStrProps().getStrProps() for j in range (0, strprops.size()): cmdbId = ci.getID().getString() nnmId = None nnmIdValue = strprops.get(j).getValue() if nnmIdValue: nnmIdTokens = nnmIdValue.split(ID_SEPARATOR) if nnmIdTokens and len(nnmIdTokens) == 2: nnmId = nnmIdTokens[0] and nnmIdTokens[0].strip() if cmdbId and nnmId: resultCmdbToNnmIds[cmdbId] = nnmId else: logger.warn("Invalid ID values: [%s, %s], pair is ignored" % (cmdbId, nnmIdValue)) except: logger.errorException('Failed to process CIs') return {} return resultCmdbToNnmIds
def __discoverInstalledSoftware(Framework, OSHVResult, client): discoverSoftwareOld = Boolean.parseBoolean( Framework.getParameter('discoverInstalledSoftwareByOldMechanism')) softNameToInstSoftOSH = {} try: if discoverSoftwareOld: #we close client here since in the software discovery we had to open another client #since we changing namespace and reference registry instead of wmi logger.debug( 'The software is discovered using old mechanism. This mechanism is very non-efficient and thus discovery might take time.' ) client.close() client = None wmi_dis_software_lib.mainFunction(Framework, OSHVResult, softNameToInstSoftOSH) #reopen general WMI client since it will be used in Plug-ins logger.debug("Reopening WMI client") client = createWmiClient(Framework) else: wmi_dis_software_lib.mainFunctionWithWbem(Framework, client, OSHVResult, softNameToInstSoftOSH) except: errobj = errorobject.createError( errorcodes.FAILED_DISCOVERING_RESOURCE_WITH_CLIENT_TYPE, ['software', 'wmi'], 'Failed to discover software by wmi') logger.reportErrorObject(errobj) logger.errorException('Failed to discover software by wmi') return (softNameToInstSoftOSH, client)
def collectData(self,sqlServerId): try: oshv = ObjectStateHolderVector() self.getPublishers(oshv,sqlServerId) except: logger.errorException("couldnt get cluster configuration for server: ", sqlServerId.toString()) logger.reportWarning() return oshv
def isApplicable(self, context): self.__client = context.client try: self.__process = context.application.getProcesses()[0] if self.__process: return 1 except: logger.errorException(sys.exc_info()[1])
def __getAttributeType(self, ciType, attributeName): #TODO: memoize this function try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(ciType, attributeName) return attributeDefinition.getType() except: logger.errorException("%s.%s" % (ciType, attributeName)) raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
def getLocalShell(self): """ Creates and caches local shell client. Must not be used outside of class. """ try: return self.framework.createClient(ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME) except: logger.errorException('Failed to create LocalShell client')
def __getAttributeType(self, ciType, attributeName): try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(ciType, attributeName) return attributeDefinition.getType() except: if DEBUG: return 'string' logger.errorException("%s.%s" % (ciType, attributeName)) raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
def parseLSOFListen(self, line, listenIpPorts): try: IpPortIpPortStatusListenArray = None protocol = modeling.TCP_PROTOCOL protocolName = 'tcp' try: IpPortIpPortStatusListenArray = re.search(self.TCPRegExp, line) if IpPortIpPortStatusListenArray != None: linkStatus = IpPortIpPortStatusListenArray.group(3) if linkStatus.find(self.LISTEN_str) == -1: return TCPDiscovery.OK except: return TCPDiscovery.OK if (IpPortIpPortStatusListenArray == None) and (self.UDPRegExp != None): try: IpPortIpPortStatusListenArray = re.search(self.UDPRegExp, line) protocol = modeling.UDP_PROTOCOL protocolName = 'udp' except: return TCPDiscovery.OK if IpPortIpPortStatusListenArray == None: return TCPDiscovery.OK pid = IpPortIpPortStatusListenArray.group(1) listenipPort = IpPortIpPortStatusListenArray.group(2) ip = listenipPort.split(':')[0] if not self.isIpv4(ip): logger.debug ('Skipping not valid IPv4 address %s' % ip) return TCPDiscovery.OK if not ip_addr.isValidIpAddress(ip) and not str(ip).startswith('0.'): errorMessage = 'On parsing listen ports by lsof for protocol ' + protocolName + ' extracted invalid ip:<' + ip + '>' logger.warn(errorMessage) errobj = errorobject.createError(errorcodes.IP_PARSING_ERROR, [protocolName, ip, 'On parsing listen ports by lsof'], errorMessage) logger.reportWarningObject(errobj) return TCPDiscovery.OK #if ip_addr.IPAddress(ip).get_is_loopback(): # return TCPDiscovery.OK port = listenipPort.split(':')[1] #sometimes we get on UDP something like this: #postmaste 2412 sfmdb 7u IPv4 0xe00000015f8ed100 0t0 UDP 127.0.0.1:49176->127.0.0.1:49176 #in this case split by ':' brings us at port 49176->127.0.0.1 port = re.search('\d+',port).group(0) #this is TCP port and we add it to port_process table for future connections discovery self._addTcpData(ip, port, pid, 1, protocol, listenIpPorts = listenIpPorts) return TCPDiscovery.OK except: logger.errorException('parseLSOFListen:failed to process TCP entry: ', line) return TCPDiscovery.ERROR
def getLocalShell(self): """ Creates and caches local shell client. Must not be used outside of class. """ try: return self.framework.createClient( ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME) except: logger.errorException('Failed to create LocalShell client')
def isApplicable(self, context): self.__context = context try: if (not self.__client) and context.client.getClientType() == ClientsConsts.WMI_PROTOCOL_NAME: self.__client = self.__createWMIClient(self.__context.framework, 'root\\DEFAULT') else: self.__client = context.client return context.application.getProcess('sqlservr.exe') is not None except: logger.errorException(sys.exc_info()[1])
def collectData(self, sqlServerId): try: oshv = ObjectStateHolderVector() self.getPublishers(oshv, sqlServerId) except: logger.errorException( "couldnt get cluster configuration for server: ", sqlServerId.toString()) logger.reportWarning() return oshv
def __getAttributeType(self, ciType, attributeName): #TODO: memoize this function try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition( ciType, attributeName) return attributeDefinition.getType() except: logger.errorException("%s.%s" % (ciType, attributeName)) raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
def isApplicable(self, context): self.__client = context.client try: for allowedProc in self.__allowedProcesses: process = context.application.getProcess(allowedProc) if process: self.__path = process.executablePath if self.__path: return 1 except: logger.errorException(sys.exc_info()[1])
def __getAttributeType(self, ciType, attributeName): try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition( ciType, attributeName) return attributeDefinition.getType() except: if DEBUG: return 'string' logger.errorException("%s.%s" % (ciType, attributeName)) raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
def getIfName(self): try: aList = self.snmpClient.executeQuery(Switch.ifName).asTable()#@@CMD_PERMISION snmp protocol execution if aList: for i in xrange(len(aList)): (a,b) = aList[i] aList[i] = (a.upper(),b.upper()) except: logger.errorException('error in retrieving interface mac address') else: for anIfName in aList: self.swIfName.update({anIfName[0]: anIfName[1]})
def getIfIndexDescr(self): try: aList = self.snmpClient.executeQuery(Switch.ifIndexDescr).asTable()#@@CMD_PERMISION snmp protocol execution) if aList: for i in xrange(len(aList)): (a,b) = aList[i] aList[i] = (a.upper(),b.upper()) except: logger.errorException('error in retrieving interface indexes') else: for ifIndexDescr in aList: self.swIfIndexDescr.update({ifIndexDescr[0]: ifIndexDescr[1]})
def doQuery(dbQueryClient, query): try: resultSet = None try: resultSet = dbQueryClient.executeQuery(query) except: logger.errorException('Failed executing query: <', query, '> on <', dbQueryClient.getIpAddress(), '> Exception:') return resultSet except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':doQuery] Exception: <%s>' % excInfo) pass
def _execute(self, cmdLine, parse_callback): try: output = self._shell.execCmd(cmdLine, self._shell.getDefaultCommandTimeout() * 4) except: logger.errorException("NMAP command execution failed") raise NmapExecutionFailedException("NMAP command execution failed") else: if self._shell.getLastCmdReturnCode() != 0: logger.errorException("NMAP command execution failed.") raise NmapExecutionFailedException("NMAP command execution failed") else: return parse_callback(output)
def isApplicable(self, context): self.__context = context try: if (not self.__client) and context.client.getClientType( ) == ClientsConsts.WMI_PROTOCOL_NAME: self.__client = self.__createWMIClient( self.__context.framework, 'root\\DEFAULT') else: self.__client = context.client return context.application.getProcess('sqlservr.exe') is not None except: logger.errorException(sys.exc_info()[1])
def _createNnmStub(nnmConnection): ''' NnmConnection -> NmsNodeBindingStub ''' try: url = nnmConnection.getUrl() stub = NmsNodeBindingStub(url, None) stub.setHeader("http://com.hp.software", "HPInternalIntegrator", "true") stub.setUsername(nnmConnection.username) stub.setPassword(nnmConnection.password) return stub except: logger.errorException('Failed to create NNM stub') raise NnmException('Failed to create NNM stub')
def DiscoveryMain(Framework): ''' Discovery process consists of two steps: 1. Connect domain controller and get whole topology 2. Strive to connect to the same controller with the same credentials but in role of global catalog. 2.1 GC indexes more hierarchical data but less object specific data, so not all data will be rediscovered. ''' vector = ObjectStateHolderVector() ## Destination Attribute Section hostId = Framework.getDestinationAttribute('hostId') credentialsId = Framework.getDestinationAttribute('credentials_id') applicationPort = Framework.getDestinationAttribute("application_port") serviceAddressPort = Framework.getDestinationAttribute('port') OU_REPORTING_PARAM = 'reportOUAsConfigurationDocument' isOuUnitsTreeReportedAsConfig = Framework.getParameter(OU_REPORTING_PARAM) isOuUnitsTreeReportedAsConfig = parseBoolean(isOuUnitsTreeReportedAsConfig) tryToDiscoverGlobalCatalogFlag = Boolean.parseBoolean( Framework.getParameter('tryToDiscoverGlobalCatalog')) globalCatalogPort = Framework.getParameter('globalCatalogPort') if not applicationPort or applicationPort == 'NA': applicationPort = serviceAddressPort try: result = DiscoveryResult() vector.addAll( _discoverTopology(Framework, credentialsId, hostId, applicationPort, None, isOuUnitsTreeReportedAsConfig, result)) #no reason to connect to the GC if port is specified in credentials if (tryToDiscoverGlobalCatalogFlag and str(globalCatalogPort).isdigit() and globalCatalogPort != applicationPort): vector.addAll( _discoverTopology(Framework, credentialsId, hostId, globalCatalogPort, tryToDiscoverGlobalCatalogFlag, isOuUnitsTreeReportedAsConfig, result)) dtoToOsh = result.getMap(DOMAINT_DTO_TO_CONFIG_OSH_TYPE) fptools.each(vector.add, dtoToOsh.values()) except Exception, e: msg = 'Failure in discovering Active Directory Topology. %s' % e Framework.reportError(msg) logger.debug(logger.prepareFullStackTrace(msg)) logger.errorException(msg)
def doQuery(oracleQueryClient, query): 'Perform a SQL query using the given connection and return a result set' try: resultSet = None try: resultSet = oracleQueryClient.executeQuery(query) except: logger.errorException('Failed executing query: <', query, '> on <', oracleQueryClient.getIpAddress(), '> Exception:') return resultSet except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':doQuery] Exception: <%s>' % excInfo) pass
def isApplicable(self, context): self.__client = context.client try: if self.__client.isWinOs(): self.__isWinOs = 1 self.__process = context.application.getProcess('oracle.exe') else: return 1 if self.__process: return 1 except: logger.errorException(sys.exc_info()[1])
def executeUpdate(self, sql): st = None try: try: st = self.conn.createStatement() return st.executeUpdate(sql) except: error = 'Failed to execute sql ' + sql logger.errorException(error) errobj = errorobject.createError(errorcodes.FAILED_TO_EXECUTE_SQL, [sql], error) logger.reportErrorObject(errobj) finally: self.closeStatement(st)
def getIfName(self): try: aList = self.snmpClient.executeQuery(Switch.ifName).asTable( ) #@@CMD_PERMISION snmp protocol execution if aList: for i in xrange(len(aList)): (a, b) = aList[i] aList[i] = (a.upper(), b.upper()) except: logger.errorException('error in retrieving interface mac address') else: for anIfName in aList: self.swIfName.update({anIfName[0]: anIfName[1]})
def getIfIndexDescr(self): try: aList = self.snmpClient.executeQuery(Switch.ifIndexDescr).asTable( ) #@@CMD_PERMISION snmp protocol execution) if aList: for i in xrange(len(aList)): (a, b) = aList[i] aList[i] = (a.upper(), b.upper()) except: logger.errorException('error in retrieving interface indexes') else: for ifIndexDescr in aList: self.swIfIndexDescr.update({ifIndexDescr[0]: ifIndexDescr[1]})
def getUCMDBIDs(cmdbConnection, nnmServerIp, framework): ''' CmdbConnection, string, Framewrok -> map(string, string) ''' cmdbToNnmIds = {} cmdbService = _createUcmdbService(cmdbConnection) cmdbContext = CmdbContext() cmdbContext.setCallerApplication(SCRIPT_NAME) request = _createRequestObject(nnmServerIp, cmdbContext) try: response = cmdbService.getFilteredCIsByType(request) cis = response.getCIs() chunkInfo = response.getChunkInfo() chunkNum = chunkInfo.getNumberOfChunks() if chunkInfo is not None and chunkNum > 0: logger.debug("Found %d chunks of data in UCMDB" % chunkNum) for i in range(1, chunkNum + 1): logger.debug('Retrieved chunk (%d) of data from the UCMDB server' % i) chunkRequest = ChunkRequest() chunkRequest.setChunkInfo(chunkInfo) chunkRequest.setChunkNumber(i) req = PullTopologyMapChunks() req.setChunkRequest(chunkRequest) req.setCmdbContext(cmdbContext) try: res = cmdbService.pullTopologyMapChunks(req) topologyMap = res.getTopologyMap() nodeListSize = topologyMap.getCINodes().getCINodes().size() for i in range(0, nodeListSize): cis = topologyMap.getCINodes().getCINodes().get(i).getCIs() resultsMap = _processCIs(cis) cmdbToNnmIds.update(resultsMap) except: logger.errorException('Failed to retrieve chunk data from UCMDB server') else: resultsMap = _processCIs(cis) cmdbToNnmIds.update(resultsMap) except: logger.errorException('Failed to retrieve UCMDB IDs') raise CmdbException('Failed to retrieve IDs from CMDB') return cmdbToNnmIds
def executeUpdate(self, sql): st = None try: try: st = self.conn.createStatement() return st.executeUpdate(sql) except: error = 'Failed to execute sql ' + sql logger.errorException(error) errobj = errorobject.createError( errorcodes.FAILED_TO_EXECUTE_SQL, [sql], error) logger.reportErrorObject(errobj) finally: self.closeStatement(st)
def DiscoveryMain(Framework): ''' Discovery process consists of two steps: 1. Connect domain controller and get whole topology 2. Strive to connect to the same controller with the same credentials but in role of global catalog. 2.1 GC indexes more hierarchical data but less object specific data, so not all data will be rediscovered. ''' vector = ObjectStateHolderVector() ## Destination Attribute Section hostId = Framework.getDestinationAttribute('hostId') credentialsId = Framework.getDestinationAttribute('credentials_id') applicationPort = Framework.getDestinationAttribute("application_port") serviceAddressPort = Framework.getDestinationAttribute('port') OU_REPORTING_PARAM = 'reportOUAsConfigurationDocument' isOuUnitsTreeReportedAsConfig = Framework.getParameter(OU_REPORTING_PARAM) isOuUnitsTreeReportedAsConfig = parseBoolean(isOuUnitsTreeReportedAsConfig) tryToDiscoverGlobalCatalogFlag = Boolean.parseBoolean( Framework.getParameter('tryToDiscoverGlobalCatalog')) globalCatalogPort = Framework.getParameter('globalCatalogPort') if not applicationPort or applicationPort == 'NA': applicationPort = serviceAddressPort try: result = DiscoveryResult() vector.addAll(_discoverTopology(Framework, credentialsId, hostId, applicationPort, None, isOuUnitsTreeReportedAsConfig, result)) #no reason to connect to the GC if port is specified in credentials if (tryToDiscoverGlobalCatalogFlag and str(globalCatalogPort).isdigit() and globalCatalogPort != applicationPort): vector.addAll(_discoverTopology(Framework, credentialsId, hostId, globalCatalogPort, tryToDiscoverGlobalCatalogFlag, isOuUnitsTreeReportedAsConfig, result)) dtoToOsh = result.getMap(DOMAINT_DTO_TO_CONFIG_OSH_TYPE) fptools.each(vector.add, dtoToOsh.values()) except Exception, e: msg = 'Failure in discovering Active Directory Topology. %s' % e Framework.reportError(msg) logger.debug(logger.prepareFullStackTrace(msg)) logger.errorException(msg)
def getProcessesToProcess(self): if not self.shouldRun(): return rs = None try: try: self.buildProcessMap() st = self.getPreparedStatement(ProcessToProcess.P2PSQL) logger.debug(st) rs = st.executeQuery() while(rs.next()): SrcListen = rs.getBoolean('SrcListen') DstListen = rs.getBoolean('DstListen') if SrcListen and (not DstListen): self.buildTcpConnTopology(rs, 'dst', 'src') elif DstListen and (not SrcListen): self.buildTcpConnTopology(rs, 'src', 'dst') else: srcPrefered = self.isPreferedService(rs, 'src') dstPrefered = self.isPreferedService(rs, 'dst') if srcPrefered and (not dstPrefered): self.buildTcpConnTopology(rs, 'dst', 'src') elif dstPrefered and (not srcPrefered): self.buildTcpConnTopology(rs, 'src', 'dst') else: # we don't known which endpoint is listening, # so we can't set the link direction srcip = rs.getString('srcAddr') srcport = rs.getInt('srcPort') dstip = rs.getString('dstAddr') dstport = rs.getInt('dstPort') connString = '%s:%d %s:%d' % (srcip, srcport, dstip, dstport) logger.warn('process to process topology: ' 'Listen endpoint is unknown, skipping %s' % connString) except: error = 'Failed to fetch processes to process communication' logger.errorException(error) errobj = errorobject.createError(errorcodes.PROCESS_TO_PROCESS_FAILED, None, error) logger.reportErrorObject(errobj) finally: if rs: try: rs.close() except: pass self.conn.close()
def parseTcpListenPorts(self, line, listenIpPorts): linkStatus = '' try: IpPortIpPortStatusListenArray = None protocol = modeling.TCP_PROTOCOL protocolName = 'tcp' try: IpPortIpPortStatusListenArray = re.compile(self.TCPRegExp).search(line) if IpPortIpPortStatusListenArray != None: linkStatus = IpPortIpPortStatusListenArray.group(5).upper() if (linkStatus.find(self.LISTEN_str) == -1) and (linkStatus.find("LISTEN") == -1): return TCPDiscovery.OK except: return TCPDiscovery.OK if (IpPortIpPortStatusListenArray == None) and (self.UDPRegExp != None) and (len(self.UDPRegExp) > 0): try: IpPortIpPortStatusListenArray = re.search(self.UDPRegExp, line) if (IpPortIpPortStatusListenArray != None) and len(IpPortIpPortStatusListenArray.groups()) == 3: linkStatus = IpPortIpPortStatusListenArray.group(3) protocol = modeling.UDP_PROTOCOL protocolName = 'udp' except: return TCPDiscovery.OK if IpPortIpPortStatusListenArray == None: return TCPDiscovery.OK ip = self.__parseIpv6(IpPortIpPortStatusListenArray.group(1)) port = IpPortIpPortStatusListenArray.group(2) if not ip_addr.isValidIpAddress(ip): errorMessage = 'On parsing listen ports by netstat for protocol ' + protocolName + ' extracted invalid ip:<' + ip + '>' logger.warn(errorMessage) errobj = errorobject.createError(errorcodes.IP_PARSING_ERROR, [protocolName, ip, 'On parsing listen ports by netstat'], errorMessage) logger.reportWarningObject(errobj) return TCPDiscovery.OK #if ip_addr.IPAddress(ip).get_is_loopback(): # return TCPDiscovery.OK pid = self.getPid(linkStatus) self._addTcpData(ip, port, pid, 1, protocol, listenIpPorts = listenIpPorts) return TCPDiscovery.OK except: logger.errorException('parseTcpListenPorts:failed to process TCP entry: ', line) return TCPDiscovery.ERROR
def process(self, context): applicationOsh = context.application.getOsh() # The application_ip assigned by applications.py is shell ip and it may not be correct # We prefer to get the correct one through ListenerShellPlugin applicationOsh.setAttribute('application_ip',None) applicationOsh.setAttribute('application_port',None) try: if self.__isWinOs: self.getWindowsVersion(applicationOsh) else: self.getUnixVersion(applicationOsh) except: errMsg = 'Failed executing command: ' + self.__cmd + '. Exception received: %s' % (sys.exc_info()[1]) logger.errorException(errMsg)
def getHostNameFromWmi(Framework): logger.debug('Getting host name from Win32_ComputerSystem...') try: try: client = Framework.createClient() resultSet = client.executeQuery('SELECT Name FROM Win32_ComputerSystem')#@@CMD_PERMISION wmi protocol execution if resultSet.next(): hostName = resultSet.getString(1) except: logger.errorException('Failed to obtain host name') finally: if client: client.close() return hostName
def DiscoveryMain(Framework): vector = ObjectStateHolderVector() codePage = Framework.getCodePage() props = Properties() props.setProperty(BaseAgent.ENCODING, codePage) shell = None try: client = Framework.createClient(props) shell = shellutils.ShellFactory().createShell(client) dnsResolver = dns_resolver.NsLookupDnsResolver(shell) language = shell.osLanguage logger.debug("Using '%s' language bundle" % language.bundlePostfix) bundle = shellutils.getLanguageBundle('langMsCluster', language, Framework) clusterCmd = ms_cluster_discoverer.createClusterCmd(shell, bundle) if clusterCmd.isUsingCmd(): bundle = clusterCmd.detectLangBandle(Framework) clusterCmd.setBundle(bundle) vector.addAll(_discoverTopology(clusterCmd, bundle, dnsResolver)) except NoInstanceFound: errobj = errorobject.createError( errorcodes.MS_CLUSTER_INSTANCES_NOT_FOUND, None, 'MS cluster instances not found in discovery') logger.reportWarningObject(errobj) except: msg = str(sys.exc_info()[1]) logger.debugException(msg) if (msg.lower().find('timeout') > -1): errobj = errorobject.createError( errorcodes.CONNECTION_TIMEOUT_NO_PROTOCOL, None, 'Connection timed out - reactivate with larger timeout value') logger.reportErrorObject(errobj) logger.debugException('Connection timed out') else: errobj = errormessages.resolveError(msg, 'ntcmd') logger.reportErrorObject(errobj) logger.errorException(msg) try: shell and shell.closeClient() except: logger.debugException() logger.error("Unable to close shell") return vector
def isApplicable(self, context): self.__shell = context.client try: if self.__shell.isWinOs(): self.__isWinOs = 1 self.__isRACWin = self.isRAC() self.__process = context.application.getProcess('tnslsnr.exe') self.__processInfo = context.application.getProcessInfo('tnslsnr.exe') else: self.__process = context.application.getProcess('tnslsnr') self.__processInfo = context.application.getProcessInfo('tnslsnr') if self.__process: return 1 except: logger.errorException(sys.exc_info()[1])
def setVersion(mysqlOsh, path, client): try: if client.isWinOs(): version, fullVersion = getWindowsVersion(path, client) else: version, fullVersion = getUnixVersion(path, client) if version: mysqlOsh.setAttribute("application_version_number", version) mysqlOsh.setAttribute("application_version", fullVersion) modeling.setDatabaseVersion(mysqlOsh, version) logger.debug("MySQL version : " + version) else: logger.error('Failed getting MySQL version') except: errMsg = 'Failed getting MySQL version. Exception received: %s' % (sys.exc_info()[1]) logger.errorException(errMsg)
def _createUcmdbService(cmdbConnection): ''' CmdbConnection -> UcmdbServiceStub ''' serviceStub = None try: url = cmdbConnection.getUrl() logger.debug('UCMDB Webservice URL: %s' % url) serviceStub = UcmdbServiceStub(url.toString()) auth = HttpTransportProperties.Authenticator() auth.setUsername(cmdbConnection.username) auth.setPassword(cmdbConnection.password) serviceStub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth) except: logger.errorException('Failed to create stub') raise CmdbException('Failed to create stub') return serviceStub
def insertEntitiesProbeDb(self, entitiesbulk): st = None try: try: sql = entitiesbulk[0].getInsertSQL() st = self.conn.prepareStatement(sql) for entity in entitiesbulk: entity.setValues(st) st.addBatch() st.executeBatch() except: error = 'Failed to add entities of type ' + str(entitiesbulk[0].getEntityType()) + ' to Probe database' logger.errorException(error) errobj = errorobject.createError(errorcodes.FAILED_ADDING_ENTITIES_TO_PROBE_DB, [str(entitiesbulk[0].getEntityType())], error) logger.reportErrorObject(errobj) finally: self.closeStatement(st)
def validate(self): result = True try: self.getUsername() except: logger.warnException('No username.') result = False try: self.getPassword() except: logger.warnException('No password.') result = False try: self.getConnectionUrl() except: logger.errorException('No connection url.') result = False return result
def deleteCIs(CONTEXT, allObjectChildren): for objectElement in allObjectChildren: className = objectElement.getAttributeValue('name') mamId = objectElement.getAttributeValue('mamId') nameSpace = objectElement.getAttributeValue('nameSpace') # if className is nameSpace:className (className, nameSpace) = getClassAndNamespace(className, nameSpace) aClassKey = CMDBClassNameKey(className, nameSpace) try: remedyId = restoreExternalCiId(mamId) if remedyId: logger.info('Atrium ID restored %s' % remedyId) CMDBInstance.delete(CONTEXT, aClassKey, 'TOPO.DDM', remedyId, CMDBInstance.CMDB_CASCADE_DELETE_FOLLOW_WEAK_RELATIONSHIPS) else: logger.error('Failed to delete CI. Atrium ID by UCMDB ID %s not found' % mamId) except: logger.errorException('Failed to delete Atrium CI')