def createSharedResourceOsh(resource, containerOsh, oshVector): ''' Shared resource OSH creation helper method @return: None, returned value are stored in parameter oshVector ''' is90 = modeling.checkAttributeExists('file_system_export', LOCAL_NAMES) if is90: shareOsh = ObjectStateHolder(NETWORK_SHARE) shareOsh.setAttribute("data_name", resource.path) shareOsh.setAttribute("share_path", resource.path) stringVector = StringVector() for instance in resource.getInstances(): stringVector.add(instance.name) shareOsh.setAttribute(LOCAL_NAMES, stringVector) shareOsh.setContainer(containerOsh) oshVector.add(shareOsh) else: for instance in resource.getInstances(): shareOsh = ObjectStateHolder(NETWORK_SHARE) shareOsh.setAttribute("data_name", instance.name) shareOsh.setAttribute("share_path", resource.path) if instance.description: shareOsh.setAttribute("data_description", instance.description) shareOsh.setContainer(containerOsh) oshVector.add(shareOsh)
def build(self, containerOsh): self.osh = modeling.createInterfaceOSH( self.mac, containerOsh, description=self.description, index=self.index, type=self.type, adminStatus=self.adminStatus, operStatus=self.operationalStatus, speed=self.speed, name=self.name, alias=self.alias) if not self.osh: logger.warn("Interface '%s' cannot be built" % self.name) return for role in self._rolesByClass.values(): role._build(containerOsh) isVirtual = self._hasRole(_VirtualRole) self.osh.setBoolAttribute('isvirtual', isVirtual) if isVirtual: list_ = StringVector(('virtual_interface', )) roleAttribute = AttributeStateHolder('interface_role', list_) self.osh.addAttributeToList(roleAttribute) else: list_ = StringVector(('physical_interface', )) roleAttribute = AttributeStateHolder('interface_role', list_) self.osh.addAttributeToList(roleAttribute) if self.speed: self.osh.setLongAttribute('interface_speed', long(self.speed))
def __buildJmsDestination(self, destination, destinationType): '@types: jms.Destination, str -> ObjectStateHolder' osh = ObjectStateHolder('jmsdestination') osh.setAttribute('name', destination.getName()) if destination.getObjectName(): osh.setAttribute('j2eemanagedobject_objectname', destination.getObjectName()) if destination.getJndiName(): osh.setAttribute('j2eemanagedobject_jndiname', destination.getJndiName()) if destinationType: osh.setAttribute('jmsdestination_type', destinationType) messagescurrent = destination.messagesCurrentCount.value() if messagescurrent is not None: osh.setIntegerAttribute('jmsdestination_messagescurrent', messagescurrent) messagespending = destination.messagesPendingCount.value() if messagespending is not None: osh.setIntegerAttribute('jmsdestination_messagespending', messagespending) messagesreceived = destination.messagesReceivedCount.value() if messagesreceived is not None: osh.setIntegerAttribute('jmsdestination_messagesreceived', messagesreceived) consumerscurrent = destination.consumersCurrentCount.value() if consumerscurrent is not None: osh.setIntegerAttribute('jmsdestination_consumerscurrent', consumerscurrent) subscribers = destination.getDurableSubscribers() if subscribers: vectorOfNames = StringVector() for subscriber in subscribers: vectorOfNames.add(subscriber.getName()) ash = AttributeStateHolder('jmsdestination_durablesubscribers', vectorOfNames) osh.addAttributeToList(ash) return osh
def report(self, sharedResource, containerOsh): vector = self._createVector() sharedResourcesOshv = self.reportSharedResources(sharedResource, containerOsh) it = sharedResourcesOshv.iterator() while it.hasNext(): # for sharedResOsh in self.reportSharedResources(sharedResource, containerOsh): sharedResOsh = it.next() # make linkage of shared resource with all its instances stringVector = StringVector() for instance in sharedResource.getInstances(): stringVector.add(instance.name) sharedResOsh.setAttribute(LOCAL_NAMES, stringVector) vector.addAll(sharedResourcesOshv) return vector
def report(self, oshVector, interaction): if self.acceptorEngine.accepts(interaction.srcNode) and self.acceptorEngine.accepts(interaction.dstNode)\ and (interaction.srcNode.ip, interaction.dstNode.ip) not in self.reportedLinks: trafficLinkOSH = modeling.createLinkOSH('traffic', interaction.srcNode.ipOsh, interaction.dstNode.ipOsh) if self.reportTrafficDetails: from appilog.common.system.types import AttributeStateHolder from appilog.common.system.types.vectors import StringVector octets = 0 packets = 0 portsSet = StringVector() connections = self.ipToIpConnections.get((interaction.srcNode.ip, interaction.dstNode.ip), []) for connection in connections: octets += connection.octetCount and int(connection.octetCount) or 0 packets += connection.packetCount and int(connection.packetCount) or 0 if portsSet.size() < self.maxPorts: portsSet.add(str(connection.srcPort)) if portsSet.size() < self.maxPorts: portsSet.add(str(connection.dstPort)) ash = AttributeStateHolder('traffic_portlist', portsSet) trafficLinkOSH.addAttributeToList(ash) trafficLinkOSH.setLongAttribute('traffic_octets', octets) trafficLinkOSH.setLongAttribute('traffic_pkts', packets) oshVector.add(trafficLinkOSH) self.reportedLinks.append((interaction.srcNode.ip, interaction.dstNode.ip))
def setRole(self, interface, isVirtual): 'modeling.NetworkInterface, bool -> None' osh = interface.getOsh() if osh: osh.setBoolAttribute('isvirtual', isVirtual) if modeling._CMDB_CLASS_MODEL.version() >= 9: # set interface_role attribute depending on interface type - physical/virtual if interface.role: list = StringVector((interface.role, )) else: list = StringVector(isVirtual and ('virtual_interface', ) or ('physical_interface', )) osh.setAttribute(AttributeStateHolder('interface_role', list))
def report(self, sharedResource, containerOsh): vector = self._createVector() sharedResourcesOshv = self.reportSharedResources( sharedResource, containerOsh) it = sharedResourcesOshv.iterator() while it.hasNext(): # for sharedResOsh in self.reportSharedResources(sharedResource, containerOsh): sharedResOsh = it.next() # make linkage of shared resource with all its instances stringVector = StringVector() for instance in sharedResource.getInstances(): stringVector.add(instance.name) sharedResOsh.setAttribute(LOCAL_NAMES, stringVector) vector.addAll(sharedResourcesOshv) return vector
def createCimOsh(ipAddress, containerOsh, credentialId, category=None): ''' Builds a CIM OSH representing successful connection. @param ipAddress: string @param containerOsh: corresponding container OSH @param credentialId: protocol entry @raise ValueError: no credential or no IP. @return: OSH ''' if not credentialId: raise ValueError('CredentialsId must be set') if not netutils.isValidIp(ipAddress): raise ValueError('IP Address must be set') cimOsh = ObjectStateHolder('cim') cimOsh.setAttribute('data_name', Protocol.FULL) cimOsh.setAttribute('credentials_id', credentialId) cimOsh.setAttribute('application_ip', ipAddress) cimOsh.setContainer(containerOsh) if category: list_ = StringVector((category, )) categoryAttribute = AttributeStateHolder('cim_category', list_) cimOsh.addAttributeToList(categoryAttribute) return cimOsh
def _build(self, containerOsh=None): _VirtualRole._build(self, containerOsh) interfaceOsh = self._interface.getOsh() if interfaceOsh and _CMDB.isExistingAttribute( interfaceOsh.getObjectClass(), 'vlan_ids'): list_ = StringVector((self.vlanId, )) vlanIdsAttribute = AttributeStateHolder('vlan_ids', list_) interfaceOsh.addAttributeToList(vlanIdsAttribute)
def setValue(osh, propName, value, force=False): if propName: if value: if isinstance(value, list): osh.addAttributeToList(propName, StringVector(value)) else: osh.setAttribute(propName, value) elif force: osh.setAttribute(propName, None)
def applyToOsh(self, targetOsh): if targetOsh is None: raise ValueError("OSH is None") if self.isVirtual: targetOsh.setBoolAttribute('isvirtual', True) valuelist = StringVector((self.roleValue, )) roleAttribute = AttributeStateHolder('interface_role', valuelist) targetOsh.addAttributeToList(roleAttribute)
def _build(self, containerOsh=None): _VirtualRole._build(self, containerOsh) interfaceOsh = self._interface.getOsh() interfaceOsh.setBoolAttribute('isvirtual', 1) if _CMDB.isExistingAttribute(interfaceOsh.getObjectClass(), 'interface_role'): list_ = StringVector(('aggregate_interface', )) roleAttribute = AttributeStateHolder('interface_role', list_) interfaceOsh.addAttributeToList(roleAttribute)
def _build(self, containerOsh=None): interfaceOsh = self._interface.getOsh() allVlanIds = {} for vlanInterface in self.vlanInterfaces: vlanRole = vlanInterface._getRoleByClass(VlanRole) if vlanRole is not None: allVlanIds[vlanRole.vlanId] = None else: logger.warn("VLAN interface '%s' has no VlanRole" % vlanInterface.name) allVlanIdsList = allVlanIds.keys() if allVlanIdsList: if _CMDB.isExistingAttribute(interfaceOsh.getObjectClass(), 'vlan_ids'): list_ = StringVector(allVlanIdsList) vlanIdsAttribute = AttributeStateHolder('vlan_ids', list_) interfaceOsh.addAttributeToList(vlanIdsAttribute)
def __setStringListAttribute(self, osh, attrName, attrValue, attrDataType): osh.setAttribute(attrName, StringVector(attrValue, self.string_list_delimiter))
def discover_private(self): maxPorts = Integer.parseInt(self.getParameterValue('maxPorts')) tcpOnly = Boolean.parseBoolean(self.getParameterValue('tcpOnly')) #WE ALWAYS CHECK ONLY ONE DIRECTION SINCE WE ALWAYS REPORT FLOWS IN BOTH DIRECTION SO #WE CAN COUNT CLIENTS ONLY ON ONE SIDE #WE ASSUME THAT NETFLOW ALWAYS REPORTS CONNECTIONS IN BOTH DIRECTIONS SO WE WILL GET #OCTETS AND PACKETS COUNT ALWAYS query = ' select SrcAddr ,DstAddr ,DstPort ,count(*) cnt, sum(dPkts) dPkts, sum(dOctets) dOctets, Prot,' query = query + ' case when Port is NULL then 0 else 1 end ListenPort ' query = query + ' from Agg_V5 left join Port_Process on DstAddr=ipaddress and DstPort=port and Prot = Protocol and listen ' if tcpOnly: query = query + ' where Prot=6 ' query = query + ' group by SrcAddr, DstAddr, DstPort ' #for each ip -> ip traffic we first wnat get ports that are listen, than which have more clients #after all ports which have more traffic query = query + ' order by SrcAddr, DstAddr, ListenPort desc, cnt desc, dOctets desc, dPkts desc' #here Prot is asc since TCP ports have higher priority on UDP ports query = query + ', Prot asc ' conn = self.Framework.getProbeDatabaseConnection('TCPDISCOVERY') st = None result = None try: st = conn.createStatement() result = st.executeQuery(query) currSrcAddr = None portsSet = StringVector() currDstAddr = None currLinkID = None octets = 0 packets = 0 dataFound = 0 while result.next(): dataFound = 1 srcAddr = str(result.getString('SrcAddr')) dstAddr = str(result.getString('DstAddr')) dstPort = result.getString('DstPort') cnt = result.getString('cnt') listenPort = result.getInt('ListenPort') if not self.isServerPort(cnt, listenPort, dstPort): continue if not self.shouldInclude(srcAddr, 0): continue if not self.shouldInclude(dstAddr, 1): continue linkID = self.createLinkID(srcAddr, dstAddr) if currLinkID == linkID: octets = octets + result.getInt('dOctets') packets = packets + result.getInt('dPkts') if portsSet.size() < maxPorts: portsSet.add(dstPort) continue elif currLinkID != None: self.addTraffic(currSrcAddr, currDstAddr, portsSet, octets, packets) currLinkID = linkID currSrcAddr = srcAddr currDstAddr = dstAddr portsSet = StringVector() portsSet.add(dstPort) octets = result.getInt('dOctets') packets = result.getInt('dPkts') if not dataFound: self.Framework.reportWarning("No data to process, please check if Host Resources jobs had already run") if currLinkID != None: self.addTraffic(currSrcAddr, currDstAddr, portsSet, octets, packets) finally: if result != None: try: result.close except: pass conn.close(st) conn.close()
def _createServerTypeVector(self, serverTypes): vector = StringVector() for type_ in serverTypes: vector.add(type_) return vector
def __setServiceNames(self, ipServerOSH, endpoint): if endpoint.getPortType(): serviceName = str(endpoint.getPortType()) ipServerOSH.setStringAttribute('ip_service_name', serviceName) serviceNamesList = StringVector((serviceName, )) ipServerOSH.setAttribute('service_names', serviceNamesList)
def updateServiceNames(osh, service_names): if not service_names: raise ValueError('Invalid service_names') osh.setAttribute('service_names', StringVector(service_names)) return osh
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)