Example #1
0
def createLayer2Topology(interfaceEnd1, interfaceEnd2):
    OSHV = ObjectStateHolderVector()
    end1Node = modeling.createCompleteHostOSH('node', str(interfaceEnd1.xHash))
    end2Node = modeling.createCompleteHostOSH('node', str(interfaceEnd2.xHash))
    interface1 = modeling.createInterfaceOSH(
        interfaceEnd1.ifMac, end1Node, interfaceEnd1.ifDescr,
        interfaceEnd1.ifIndex, interfaceEnd1.ifType,
        interfaceEnd1.ifAdminStatus, interfaceEnd1.ifOperStatus,
        interfaceEnd1.ifSpeed, interfaceEnd1.ifName, interfaceEnd1.ifAlias)
    interface2 = modeling.createInterfaceOSH(
        interfaceEnd2.ifMac, end2Node, interfaceEnd2.ifDescr,
        interfaceEnd2.ifIndex, interfaceEnd2.ifType,
        interfaceEnd2.ifAdminStatus, interfaceEnd2.ifOperStatus,
        interfaceEnd2.ifSpeed, interfaceEnd2.ifName, interfaceEnd2.ifAlias)
    layer2Osh = ObjectStateHolder('layer2_connection')
    layer2Osh.setAttribute(
        'layer2_connection_id',
        str(hash(interfaceEnd1.ifMac + interfaceEnd2.ifMac)))
    member1 = modeling.createLinkOSH('member', layer2Osh, interface1)
    member2 = modeling.createLinkOSH('member', layer2Osh, interface2)
    OSHV.add(end1Node)
    OSHV.add(end2Node)
    OSHV.add(interface1)
    OSHV.add(interface2)
    OSHV.add(layer2Osh)
    OSHV.add(member1)
    OSHV.add(member2)
    return OSHV
 def _buildVirtualServerOsh(self):
     domainName = DomainScopeManager.getDomainByIp(self.ipAddress.strip())
     name = '%s:%s %s' % (self.ipAddress, self.port, domainName)
     virtualServerOsh = modeling.createCompleteHostOSH('clusteredservice', name, None, self.name)
     self.ipOSH = modeling.createIpOSH(self.ipAddress)
     self.linkIpOSH = modeling.createLinkOSH('contained', virtualServerOsh, self.ipOSH)
     self.osh = virtualServerOsh
Example #3
0
 def build(self, vm):
     if vm is None: raise ValueError("VM is None")
     if not vm._hostKey: raise ValueError("VM has no host key")
     
     hostOsh = modeling.createCompleteHostOSH('node', vm._hostKey)
     
     return hostOsh
Example #4
0
def build_network_device(device):
    '''
    Build Layer 2 connection end.
    @type param: NetworkDevice -> OSH
    '''
    device_osh = None
    device_ip_address_osh = None
    device_interface_osh = None
    device_member_ip = None
    if device.ucmdb_id:
        device_osh = modeling.createOshByCmdbIdString('node', device.ucmdb_id)
    if device.mac_address:
        if not device_osh:
            device_osh = modeling.createCompleteHostOSH(
                'node', device.mac_address)
        device_interface_osh = modeling.createInterfaceOSH(
            device.mac_address, device_osh)
    if device.ip_address:
        if not device_osh:
            device_osh = modeling.createHostOSH(device.ip_address)
        device_ip_address_osh = modeling.createIpOSH(device.ip_address)
        device_member_ip = modeling.createLinkOSH('contained', device_osh,
                                                  device_ip_address_osh)
    if device.port:
        if device_interface_osh:
            device_interface_osh.setAttribute('interface_name', device.port)
        elif device_osh:
            device_interface_osh = ObjectStateHolder('interface')
            device_interface_osh.setContainer(device_osh)
            device_interface_osh.setAttribute('interface_name', device.port)
    return device_osh, device_ip_address_osh, device_interface_osh, device_member_ip
Example #5
0
def build_network_device(device):
    '''
    Build Layer 2 connection end.
    @type param: NetworkDevice -> OSH
    '''
    device_osh = None
    device_ip_address_osh = None
    device_interface_osh = None
    device_member_ip = None
    if device.ucmdb_id:
        device_osh = modeling.createOshByCmdbIdString('node', device.ucmdb_id)
    if device.mac_address:
        if not device_osh:
            device_osh = modeling.createCompleteHostOSH('node', device.mac_address)
        device_interface_osh = modeling.createInterfaceOSH(device.mac_address, device_osh)
    if device.ip_address:
        if not device_osh:
            device_osh = modeling.createHostOSH(device.ip_address)
        device_ip_address_osh = modeling.createIpOSH(device.ip_address)
        device_member_ip = modeling.createLinkOSH('contained', device_osh, device_ip_address_osh)
    if device.port:
        if device_interface_osh:
            device_interface_osh.setAttribute('interface_name', device.port)
        elif device_osh:
            device_interface_osh = ObjectStateHolder('interface')
            device_interface_osh.setContainer(device_osh)
            device_interface_osh.setAttribute('interface_name', device.port)
    return device_osh, device_ip_address_osh, device_interface_osh, device_member_ip
def build_node_osh(mac, vendor):
    if not (vendor and mac):
        return None
    node_osh = modeling.createCompleteHostOSH("node", mac)
    node_osh.setStringAttribute('vendor', vendor)
    node_osh.setStringAttribute('description', 'Thin Client')
    return node_osh
Example #7
0
 def _buildVirtualServerOsh(self):
     domainName = DomainScopeManager.getDomainByIp(self.ipAddress.strip())
     name = '%s:%s %s' % (self.ipAddress, self.port, domainName)
     virtualServerOsh = modeling.createCompleteHostOSH('clusteredservice', name, None, self.name)
     self.ipOSH = modeling.createIpOSH(self.ipAddress)
     self.linkIpOSH = modeling.createLinkOSH('contained', virtualServerOsh, self.ipOSH)
     self.osh = virtualServerOsh
Example #8
0
    def build(self, vm):
        if vm is None: raise ValueError("VM is None")
        if not vm._hostKey: raise ValueError("VM has no host key")

        hostOsh = modeling.createCompleteHostOSH('node', vm._hostKey)

        return hostOsh
def build_node_osh(mac, vendor):
    if not (vendor and mac):
        return None
    node_osh = modeling.createCompleteHostOSH( "node", mac)
    node_osh.setStringAttribute('vendor', vendor)
    node_osh.setStringAttribute('description', 'Thin Client')
    return node_osh
Example #10
0
 def build(self, switch, domain):
     if not switch: raise ValueError("virtual switch is None")
     
     if not domain._hostKey:
         raise ValueError("Virtual switch '%s' cannot be reported since the host key of parent domain is unknown" % switch.getName())
     
     hostKey = "_".join([domain._hostKey, domain.getName(), switch.getName()])
     hostKeyMd5 = _getMd5OfString(hostKey)
     
     switchOsh = modeling.createCompleteHostOSH('ldom_virtual_switch', hostKeyMd5)
     hostBuilder = modeling.HostBuilder(switchOsh)
     hostBuilder.setAsLanSwitch(1)
     hostBuilder.setAsVirtual(1)
     switchOsh = hostBuilder.build()
     
     switchOsh.setStringAttribute('name', switch.getName())
         
     if switch.defaultVlanId.value() is not None:
         switchOsh.setIntegerAttribute('vsw_default_vlan_id', switch.defaultVlanId.value())
         
     if switch.switchId.value() is not None:
         switchOsh.setIntegerAttribute('vsw_id', switch.switchId.value())
     
     if switch.mtu.value() is not None:
         switchOsh.setIntegerAttribute('vsw_mtu', switch.mtu.value())
     
     return switchOsh
Example #11
0
def buildRackOSH(rackName, rackAssetNumber, rackSerialNumber, rowName,
                 gridLocation, spaceName, floorName, buildingName, rackOshDict,
                 datacenterOshDict):
    try:
        rackDictKey = '%s ** %s ** %s ** %s ** %s ** %s ** %s ** %s' % (
            rackName, rackAssetNumber, rackSerialNumber, rowName, gridLocation,
            spaceName, floorName, buildingName)
        if rackDictKey in rackOshDict.keys():
            debugPrint(
                3, '[buildRackOSH] Already processed RACK <%s>! Skipping...' %
                rackName)
            return rackOshDict[rackDictKey]
        else:
            debugPrint(2, '[buildRackOSH] RACK <%s> found' % rackName)
            rackOSH = modeling.createCompleteHostOSH('rack', '')
            #rackOSH = modeling.createCompleteHostOSH('rack', str(hash(rackDictKey)))
            populateOSH(
                rackOSH, {
                    'name': rackName,
                    'serial_number': rackSerialNumber,
                    'floor': floorName,
                    'space_name': spaceName,
                    'grid_location': gridLocation,
                    'row_name': rowName
                })
            rackOSH.setContainer(datacenterOshDict[buildingName])
            rackOshDict[rackDictKey] = rackOSH
            return rackOSH
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.debug('[' + SCRIPT_NAME +
                     ':buildRackOSH] Exception: <%s>' % excInfo)
        pass
Example #12
0
    def build(self, switch, domain):
        if not switch:
            raise ValueError("virtual switch is None")

        if not domain._hostKey:
            raise ValueError(
                "Virtual switch '%s' cannot be reported since the host key of parent domain is unknown"
                % switch.getName()
            )

        hostKey = "_".join([domain._hostKey, domain.getName(), switch.getName()])
        hostKeyMd5 = _getMd5OfString(hostKey)

        switchOsh = modeling.createCompleteHostOSH("ldom_virtual_switch", hostKeyMd5)
        hostBuilder = modeling.HostBuilder(switchOsh)
        hostBuilder.setAsLanSwitch(1)
        hostBuilder.setAsVirtual(1)
        switchOsh = hostBuilder.build()

        switchOsh.setStringAttribute("name", switch.getName())

        if switch.defaultVlanId.value() is not None:
            switchOsh.setIntegerAttribute("vsw_default_vlan_id", switch.defaultVlanId.value())

        if switch.switchId.value() is not None:
            switchOsh.setIntegerAttribute("vsw_id", switch.switchId.value())

        if switch.mtu.value() is not None:
            switchOsh.setIntegerAttribute("vsw_mtu", switch.mtu.value())

        return switchOsh
Example #13
0
def _build_fc_switch_osh(switch):
    '@types: Switch -> osh'
    ipAddress = switch.address
    if ipAddress and ip_addr.isValidIpAddress(ipAddress):
        fcSwitchOSH = modeling.createHostOSH(str(ipAddress), 'fcswitch')
    else:
        logger.debug('IP address not available for Switch <%s> with ID <%s>!! Creating Switch with ID as primary key...' % (switch.name, switch.id))
        hostKey = switch.id + ' (ECC ID)'
        fcSwitchOSH = modeling.createCompleteHostOSH('fcswitch', hostKey)
        fcSwitchOSH.setAttribute('data_note', 'IP address unavailable in ECC - Duplication of this CI is possible')

    used_ports = None
    if switch.portcount != None and switch.portcount_free != None:
        used_ports = switch.portcount - switch.portcount_free

    populateOSH(fcSwitchOSH, {
        'data_description': switch.sn,
        'fcswitch_wwn': switch.sn,
        'data_name': switch.name,
        'host_model': switch.host_model,
        'fcswitch_version': switch.version,
        'host_vendor': switch.host_vendor,
        'fcswitch_domainid': switch.domain_id,
        'fcswitch_availableports': switch.portcount,
        'fcswitch_freeports': switch.portcount_free,
        'fcswitch_connectedports': used_ports
    })
    fcSwitchOSH.setListAttribute('node_role', ['switch'])
    return fcSwitchOSH
Example #14
0
 def build_complete_host(self, key):
     r''' Build generic host
     @types: str -> ObjectSateHolder
     @raise ValueError: Host key is not specified
     '''
     if not (key and key.strip()):
         raise ValueError("Host key is not specified")
     return modeling.createCompleteHostOSH(self.CIT, key)
 def build(self):
     domainName = DomainScopeManager.getDomainByIp(self.vServer.ip)
     name = '%s:%s %s' % (self.vServer.ip, self.vServer.port, domainName)
     vServerOsh = modeling.createCompleteHostOSH('cluster_resource_group', name, None, self.vServer.name)
     ipOSH = modeling.createIpOSH(self.vServer.ip)
     linkIpOSH = modeling.createLinkOSH('contained', vServerOsh, ipOSH)
     ownership_link = modeling.createLinkOSH('ownership', self.netscaler_software_osh, vServerOsh)
     vipServiceOsh = modeling.createServiceAddressOsh(vServerOsh, self.vServer.ip, self.vServer.port,
                                                      modeling.SERVICEADDRESS_TYPE_TCP, self.vServer.server_type)
     self.osh = vServerOsh
     return self.newVector(vServerOsh, ipOSH, ownership_link, linkIpOSH, vipServiceOsh)
Example #16
0
def createLayer2Topology(interfaceEnd1, interfaceEnd2):
    OSHV = ObjectStateHolderVector()
    end1Node = modeling.createCompleteHostOSH("node", str(interfaceEnd1.xHash))
    end2Node = modeling.createCompleteHostOSH("node", str(interfaceEnd2.xHash))
    interface1 = modeling.createInterfaceOSH(
        interfaceEnd1.ifMac,
        end1Node,
        interfaceEnd1.ifDescr,
        interfaceEnd1.ifIndex,
        interfaceEnd1.ifType,
        interfaceEnd1.ifAdminStatus,
        interfaceEnd1.ifOperStatus,
        interfaceEnd1.ifSpeed,
        interfaceEnd1.ifName,
        interfaceEnd1.ifAlias,
    )
    interface2 = modeling.createInterfaceOSH(
        interfaceEnd2.ifMac,
        end2Node,
        interfaceEnd2.ifDescr,
        interfaceEnd2.ifIndex,
        interfaceEnd2.ifType,
        interfaceEnd2.ifAdminStatus,
        interfaceEnd2.ifOperStatus,
        interfaceEnd2.ifSpeed,
        interfaceEnd2.ifName,
        interfaceEnd2.ifAlias,
    )
    layer2Osh = ObjectStateHolder("layer2_connection")
    layer2Osh.setAttribute("layer2_connection_id", str(hash(interfaceEnd1.ifMac + interfaceEnd2.ifMac)))
    member1 = modeling.createLinkOSH("member", layer2Osh, interface1)
    member2 = modeling.createLinkOSH("member", layer2Osh, interface2)
    OSHV.add(end1Node)
    OSHV.add(end2Node)
    OSHV.add(interface1)
    OSHV.add(interface2)
    OSHV.add(layer2Osh)
    OSHV.add(member1)
    OSHV.add(member2)
    return OSHV
Example #17
0
    def build(self, domain):
        if domain is None: raise ValueError("domain is None")
        if not domain._hostKey: raise ValueError("host key is empty")

        hostOsh = modeling.createCompleteHostOSH('node', domain._hostKey)
        if domain._hostname:
            hostOsh.setAttribute('name', domain._hostname)
            
        if domain.memorySize.value() is not None:
            memorySizeMegabytes = int(domain.memorySize.value() / (1024*1024)) 
            modeling.setHostMemorySizeAttribute(hostOsh, memorySizeMegabytes)    
        
        return hostOsh
Example #18
0
 def build(self, node):
     if node is None: raise ValueError("node is None")
     nodeOsh = modeling.createCompleteHostOSH('node', node._hostKey)
     
     hostName = node.getName()
     #should not be an IP, should be valid
     if hostName and not re.match(r"\d+\.\d+\.\d+\.\d+$", hostName):
         tokens = re.split(r"\.", hostName)
         if tokens:
             hostName = tokens[0]
         nodeOsh.setStringAttribute('name', hostName)
     
     return nodeOsh
Example #19
0
def reportHostByIp(ip):
    r''' Build basic host topology
    @types: ip_addr._BaseIP -> ObjectSateHolder, ObjectSateHolder, ObjectSateHolderVector
    @return: tuple of built objects - host, IP and vector respectively
    '''
    if not ip:
        raise ValueError("Host IP address is not specified")
    vector = ObjectStateHolderVector()
    ipOsh = modeling.createIpOSH(ip)
    hostOsh = modeling.createCompleteHostOSH('node', str(ip))
    vector.add(modeling.createLinkOSH('containment', hostOsh, ipOsh))
    vector.add(hostOsh)
    vector.add(ipOsh)
    return hostOsh, ipOsh, vector
Example #20
0
def reportHostByIp(ip):
    r''' Build basic host topology
    @types: ip_addr._BaseIP -> ObjectSateHolder, ObjectSateHolder, ObjectSateHolderVector
    @return: tuple of built objects - host, IP and vector respectively
    '''
    if not ip:
        raise ValueError("Host IP address is not specified")
    vector = ObjectStateHolderVector()
    ipOsh = modeling.createIpOSH(ip)
    hostOsh = modeling.createCompleteHostOSH('node', str(ip))
    vector.add(modeling.createLinkOSH('containment', hostOsh, ipOsh))
    vector.add(hostOsh)
    vector.add(ipOsh)
    return hostOsh, ipOsh, vector
Example #21
0
 def build(self, controlDomain):
     hostKey = controlDomain._hostname
     if not hostKey: raise ValueError("cannot create LDOM server since control domain hostname is unknown")
     hostKey = "_".join([hostKey, 'hardware']) 
     ldomServerOsh = modeling.createCompleteHostOSH('ldom_server', hostKey)
     ldomServerOsh.setStringAttribute('name', hostKey)
     ldomServerOsh.setBoolAttribute('host_isvirtual', 0)
     ldomServerOsh.setStringAttribute('discovered_vendor', 'Sun Microsystems')
     if controlDomain is not None:
         if controlDomain.serialNumber:
             ldomServerOsh.setStringAttribute('serial_number', controlDomain.serialNumber)
         if controlDomain.model:
             ldomServerOsh.setStringAttribute('discovered_model', controlDomain.model)
     return ldomServerOsh
Example #22
0
    def build(self, domain):
        if domain is None:
            raise ValueError("domain is None")
        if not domain._hostKey:
            raise ValueError("host key is empty")

        hostOsh = modeling.createCompleteHostOSH("node", domain._hostKey)
        if domain._hostname:
            hostOsh.setAttribute("name", domain._hostname)

        if domain.memorySize.value() is not None:
            memorySizeMegabytes = int(domain.memorySize.value() / (1024 * 1024))
            modeling.setHostMemorySizeAttribute(hostOsh, memorySizeMegabytes)

        return hostOsh
Example #23
0
 def build(self, controlDomain):
     hostKey = controlDomain._hostname
     if not hostKey:
         raise ValueError("cannot create LDOM server since control domain hostname is unknown")
     hostKey = "_".join([hostKey, "hardware"])
     ldomServerOsh = modeling.createCompleteHostOSH("ldom_server", hostKey)
     ldomServerOsh.setStringAttribute("name", hostKey)
     ldomServerOsh.setBoolAttribute("host_isvirtual", 0)
     ldomServerOsh.setStringAttribute("discovered_vendor", "Sun Microsystems")
     if controlDomain is not None:
         if controlDomain.serialNumber:
             ldomServerOsh.setStringAttribute("serial_number", controlDomain.serialNumber)
         if controlDomain.model:
             ldomServerOsh.setStringAttribute("discovered_model", controlDomain.model)
     return ldomServerOsh
Example #24
0
    def addOshToVector(self, resultVector):
        osh = ObjectStateHolder('ms_nlb_cluster')
        props = self.props
        osh.setAttribute('vendor', 'microsoft_corp')
        osh.setAttribute('cluster_ip_address', props['ClusterIPAddress'])
        osh.setAttribute('cluster_network_mask', props['ClusterNetworkMask'])
        osh.setAttribute('mcast_ip_address', props['McastIPAddress'])
        osh.setAttribute('cluster_domain_name', props['ClusterName'])

        #have to be transformed as MAC address
        clusterNetworkAddress = props['ClusterNetworkAddress']
        if netutils.isValidMac(clusterNetworkAddress):
            clusterNetworkAddress = netutils.parseMac(clusterNetworkAddress)
        else:
            msg = 'Invalid network address %s' % str(clusterNetworkAddress)
            logger.reportWarning('Invalid network address')
            logger.warn(msg)

        osh.setAttribute('cluster_network_address', clusterNetworkAddress)
        osh.setBoolAttribute('ip_to_mac_enable',
                             resolveBoolean(props['IPToMACEnable']))
        osh.setBoolAttribute('multicast_support_enable',
                             resolveBoolean(props['MulticastSupportEnable']))
        osh.setBoolAttribute('igmp_support',
                             resolveBoolean(props['IGMPSupport']))
        osh.setBoolAttribute('remote_control_enabled',
                             resolveBoolean(props.get('RemoteControlEnabled')))
        osh.setAttribute('data_name', 'MS NLB Cluster')
        resultVector.add(osh)

        clusteredServer = modeling.createCompleteHostOSH(
            'cluster_resource_group', clusterNetworkAddress)
        clusteredServer.setStringAttribute('name', props['ClusterName'])
        resultVector.add(clusteredServer)
        resultVector.add(
            modeling.createLinkOSH('contained', osh, clusteredServer))

        resultVector.add(self.clusterIpOSH)
        resultVector.add(
            modeling.createLinkOSH('contained', clusteredServer,
                                   self.clusterIpOSH))
        resultVector.add(self.hostOSH)
        resultVector.add(
            modeling.createLinkOSH('contained', self.hostOSH,
                                   self.clusterIpOSH))
        self.config.setContainer(osh)
        resultVector.add(self.config)
        self.nlbClusterOSH = osh
def createLayer2Topology(serverOsh, iface, interfaceEnd2):
    OSHV = ObjectStateHolderVector()
    end2Node = modeling.createCompleteHostOSH('node',str(interfaceEnd2.xHash))
    interface1 = modeling.createInterfaceOSH(iface['mac'], serverOsh, name = iface['name'])
    interface2 = modeling.createInterfaceOSH(interfaceEnd2.ifMac, end2Node, interfaceEnd2.ifDescr, interfaceEnd2.ifIndex, interfaceEnd2.ifType, interfaceEnd2.ifAdminStatus, interfaceEnd2.ifOperStatus, interfaceEnd2.ifSpeed, interfaceEnd2.ifName, interfaceEnd2.ifAlias)
    layer2Osh = ObjectStateHolder('layer2_connection')
    layer2Osh.setAttribute('layer2_connection_id',str(hash(iface['mac']+interfaceEnd2.ifMac)))
    member1 = modeling.createLinkOSH('member', layer2Osh, interface1)
    member2 = modeling.createLinkOSH('member', layer2Osh, interface2)
    OSHV.add(serverOsh)
    OSHV.add(end2Node)
    OSHV.add(interface1)
    OSHV.add(interface2)
    OSHV.add(layer2Osh)
    OSHV.add(member1)
    OSHV.add(member2)
    return OSHV
Example #26
0
 def build(self):
     domainName = DomainScopeManager.getDomainByIp(self.vServer.ip)
     name = '%s:%s %s' % (self.vServer.ip, self.vServer.port, domainName)
     vServerOsh = modeling.createCompleteHostOSH('cluster_resource_group',
                                                 name, None,
                                                 self.vServer.name)
     ipOSH = modeling.createIpOSH(self.vServer.ip)
     linkIpOSH = modeling.createLinkOSH('contained', vServerOsh, ipOSH)
     ownership_link = modeling.createLinkOSH('ownership',
                                             self.netscaler_software_osh,
                                             vServerOsh)
     vipServiceOsh = modeling.createServiceAddressOsh(
         vServerOsh, self.vServer.ip, self.vServer.port,
         modeling.SERVICEADDRESS_TYPE_TCP, self.vServer.server_type)
     self.osh = vServerOsh
     return self.newVector(vServerOsh, ipOSH, ownership_link, linkIpOSH,
                           vipServiceOsh)
def buildRackOSH(rackName, rackAssetNumber, rackSerialNumber, rowName, gridLocation, spaceName, floorName, buildingName, rackOshDict, datacenterOshDict):
	try:
		rackDictKey = '%s ** %s ** %s ** %s ** %s ** %s ** %s ** %s' % (rackName, rackAssetNumber, rackSerialNumber, rowName, gridLocation, spaceName, floorName, buildingName)
		if rackDictKey in rackOshDict.keys():
			debugPrint(3, '[buildRackOSH] Already processed RACK <%s>! Skipping...' % rackName)
			return rackOshDict[rackDictKey]
		else:
			debugPrint(2, '[buildRackOSH] RACK <%s> found' % rackName)
			rackOSH = modeling.createCompleteHostOSH('rack', '')
			#rackOSH = modeling.createCompleteHostOSH('rack', str(hash(rackDictKey)))
			populateOSH(rackOSH, {'name':rackName, 'serial_number':rackSerialNumber, 'floor':floorName, 'space_name':spaceName, 'grid_location':gridLocation, 'row_name':rowName})
			rackOSH.setContainer(datacenterOshDict[buildingName])
			rackOshDict[rackDictKey] = rackOSH
			return rackOSH
	except:
		excInfo = logger.prepareJythonStackTrace('')
		logger.debug('[' + SCRIPT_NAME + ':buildRackOSH] Exception: <%s>' % excInfo)
		pass
def buildChassisOSH(deviceAssetClass, deviceAssetSubClass, deviceName, deviceAssetNumber, deviceSerialNumber, deviceManufacturer, deviceModel, deviceModelInfo, hostOshDict, datacenterOshDict):
	try:
		chassisOSH = None
		chassisDictKey = '%s ** %s ** %s' % (deviceName, deviceAssetNumber, deviceSerialNumber)
		## Check if this CHASSIS was already processed
		if chassisDictKey in hostOshDict.keys():
			debugPrint(3, '[buildChassisOSH] Already processed CHASSIS <%s>! Skipping...' % deviceName)
			chassisOSH = hostOshDict[chassisDictKey]
		## If not build a new OSH
		else:
			## Determine CI Type for CHASSIS
			chassisCitName = 'chassis'
			if deviceAssetSubClass and deviceAssetSubClass == 'BLADE':
				chassisCitName = 'node'
				if deviceModel.lower().find('netra') >= 0:
					chassisCitName = 'unix'
				if deviceModelInfo.lower().find('bladecenter') >= 0:
					chassisCitName = 'enclosure'
			elif deviceAssetSubClass and deviceAssetSubClass == 'PATCH PANEL':
				debugPrint(3, '[buildChassisOSH] Skipping CHASSIS <%s> because it is a patch panel' % deviceName)
				return None
			debugPrint(4, '[buildChassisOSH] Creating CHASSIS with CIT <%s>' % chassisCitName)
			## Check if serial number is unique
			if deviceSerialNumber:
				for hostDictKey in hostOshDict.keys():
					serialNumber = hostDictKey.split(' ** ')[2]
					if serialNumber and deviceSerialNumber.lower() == serialNumber.lower():
						deviceSerialNumber = None
						logger.warn('Duplicate Serial Number <%s> found on <%s> <%s>! Discarding serial number...' % (serialNumber, chassisCitName, deviceName))
						break
			## Build CHASSIS
			debugPrint(2, '[buildChassisOSH] CHASSIS <%s> found' % deviceName)
			chassisOSH = modeling.createCompleteHostOSH(chassisCitName, '')
			populateOSH(chassisOSH, {'name':deviceName, 'serial_number':deviceSerialNumber, 'discovered_model':'%s (%s)'%(deviceModel, deviceModelInfo), 'discovered_vendor':deviceManufacturer})
			if chassisCitName in ['chassis', 'bladecenter']:
				populateOSH(chassisOSH, {'chassis_model':'%s %s'%(deviceModel, deviceModelInfo), 'chassis_id':deviceAssetNumber, 'chassis_vendor':deviceManufacturer})
			if not deviceSerialNumber:
				populateOSH(chassisOSH, {'host_key':str(hash(chassisDictKey)), 'data_note':'Serial Number not available in Aperture VISTA. Duplication of this CI is possible'})
			hostOshDict[chassisDictKey] = chassisOSH
		return chassisOSH
	except:
		excInfo = logger.prepareJythonStackTrace('')
		logger.debug('[' + SCRIPT_NAME + ':buildChassisOSH] Exception: <%s>' % excInfo)
		pass
def buildNodeOSH(deviceAssetClass, deviceAssetSubClass, deviceName, deviceAssetNumber, deviceSerialNumber, deviceManufacturer, deviceModel, deviceModelInfo, hostOshDict, datacenterOshDict):
	try:
		nodeOSH = None
		nodeDictKey = '%s ** %s ** %s' % (deviceName, deviceAssetNumber, deviceSerialNumber)
		## Check if this NODE was already processed
		if nodeDictKey in hostOshDict.keys():
			debugPrint(3, '[buildNodeOSH] Already processed NODE <%s>! Skipping...' % deviceName)
			nodeOSH = hostOshDict[nodeDictKey]
		## If not, build a new OSH
		else:
			## Determine CI Type for NODE
			nodeCitName = 'host_node'
			if deviceAssetClass == 'ARRAY':
				nodeCitName = 'storagearray'
			elif deviceAssetClass == 'ROUTER':
				nodeCitName = 'router'
			elif deviceAssetClass == 'NETWORK SWITCH':
				nodeCitName = 'switch'
			if deviceManufacturer and deviceManufacturer.lower() == 'sun':
				nodeCitName = 'unix'
			debugPrint(4, '[buildNodeOSH] Creating NODE with CIT <%s>' % nodeCitName)
			## Check if serial number is unique
			if deviceSerialNumber:
				for hostDictKey in hostOshDict.keys():
					serialNumber = hostDictKey.split(' ** ')[2]
					if serialNumber and deviceSerialNumber.lower() == serialNumber.lower():
						deviceSerialNumber = None
						logger.warn('Duplicate Serial Number <%s> found on <%s> <%s>! Discarding serial number...' % (serialNumber, nodeCitName, deviceName))
						break
			##  Build NODE
			debugPrint(2, '[buildNodeOSH] NODE <%s> found' % deviceName)
			nodeOSH = modeling.createCompleteHostOSH(nodeCitName, '')
			populateOSH(nodeOSH, {'name':deviceName, 'serial_number':deviceSerialNumber, 'discovered_model':'%s (%s)'%(deviceModel, deviceModelInfo), 'discovered_vendor':deviceManufacturer})
			if not deviceSerialNumber:
				populateOSH(nodeOSH, {'host_key':str(hash(nodeDictKey)), 'data_note':'Serial Number not available in Aperture VISTA. Duplication of this CI is possible'})
			hostOshDict[nodeDictKey] = nodeOSH
		return nodeOSH
	except:
		excInfo = logger.prepareJythonStackTrace('')
		logger.debug('[' + SCRIPT_NAME + ':buildNodeOSH] Exception: <%s>' % excInfo)
		pass
def buildVirtualHostOsh(oshv, f5, virtualHost):
    """
    @type oshv:         ObjectStateHolderVector
    @type f5:           ObjectStateHolder
    @type virtualHost:  VirtualHost
    @rtype: ObjectStateHolder
    """
    domainName = DomainScopeManager.getDomainByIp(virtualHost.getIp().strip())
    name = '%s:%s %s' % (virtualHost.getIp(), virtualHost.getPort(), domainName)
    virtualHostOsh = modeling.createCompleteHostOSH('cluster_resource_group', name, None, virtualHost.getName())
    #virtualHostOsh.setAttribute('name', virtualHost.getName())
    ipOsh = modeling.createIpOSH(virtualHost.getIp())
    oshv.add(modeling.createLinkOSH('containment', virtualHostOsh, ipOsh))

    ipPortOSH = buildIpServiceEndPointOsh(virtualHostOsh, virtualHost)
    # ipPortOSH.setContainer(virtualHostOsh)
    oshv.add(virtualHostOsh)
    oshv.add(ipOsh)
    oshv.add(ipPortOSH)
    oshv.add(modeling.createLinkOSH('owner', f5, virtualHostOsh))
    return virtualHostOsh
Example #31
0
 def addOshToVector(self, resultVector):
     osh = ObjectStateHolder('ms_nlb_cluster')
     props = self.props
     osh.setAttribute('vendor', 'microsoft_corp')
     osh.setAttribute('cluster_ip_address', props['ClusterIPAddress'])
     osh.setAttribute('cluster_network_mask', props['ClusterNetworkMask'])
     osh.setAttribute('mcast_ip_address', props['McastIPAddress'])
     osh.setAttribute('cluster_domain_name', props['ClusterName'])
     
     #have to be transformed as MAC address
     clusterNetworkAddress = props['ClusterNetworkAddress']
     if netutils.isValidMac(clusterNetworkAddress):
         clusterNetworkAddress = netutils.parseMac(clusterNetworkAddress)
     else:
         msg = 'Invalid network address %s' % str(clusterNetworkAddress)
         logger.reportWarning('Invalid network address')
         logger.warn(msg)
     
     osh.setAttribute('cluster_network_address', clusterNetworkAddress)
     osh.setBoolAttribute('ip_to_mac_enable', resolveBoolean(props['IPToMACEnable']))
     osh.setBoolAttribute('multicast_support_enable', resolveBoolean(props['MulticastSupportEnable']))
     osh.setBoolAttribute('igmp_support', resolveBoolean(props['IGMPSupport']))
     osh.setBoolAttribute('remote_control_enabled', resolveBoolean(props.get('RemoteControlEnabled')))
     osh.setAttribute('data_name', 'MS NLB Cluster')
     resultVector.add(osh)
     
     clusteredServer = modeling.createCompleteHostOSH('cluster_resource_group', clusterNetworkAddress)
     clusteredServer.setStringAttribute('name', props['ClusterName'])
     resultVector.add(clusteredServer)        
     resultVector.add(modeling.createLinkOSH('contained', osh, clusteredServer))
     
     resultVector.add(self.clusterIpOSH)
     resultVector.add(modeling.createLinkOSH('contained', clusteredServer, self.clusterIpOSH))
     resultVector.add(self.hostOSH)
     resultVector.add(modeling.createLinkOSH('contained', self.hostOSH, self.clusterIpOSH))
     self.config.setContainer(osh)
     resultVector.add(self.config)
     self.nlbClusterOSH = osh
Example #32
0
def buildVirtualHostOsh(oshv, f5, virtualHost):
    """
    @type oshv:         ObjectStateHolderVector
    @type f5:           ObjectStateHolder
    @type virtualHost:  VirtualHost
    @rtype: ObjectStateHolder
    """
    domainName = DomainScopeManager.getDomainByIp(virtualHost.getIp().strip())
    name = '%s:%s %s' % (virtualHost.getIp(), virtualHost.getPort(),
                         domainName)
    virtualHostOsh = modeling.createCompleteHostOSH('cluster_resource_group',
                                                    name, None,
                                                    virtualHost.getName())
    #virtualHostOsh.setAttribute('name', virtualHost.getName())
    ipOsh = modeling.createIpOSH(virtualHost.getIp())
    oshv.add(modeling.createLinkOSH('containment', virtualHostOsh, ipOsh))

    ipPortOSH = buildIpServiceEndPointOsh(virtualHostOsh, virtualHost)
    # ipPortOSH.setContainer(virtualHostOsh)
    oshv.add(virtualHostOsh)
    oshv.add(ipOsh)
    oshv.add(ipPortOSH)
    oshv.add(modeling.createLinkOSH('owner', f5, virtualHostOsh))
    return virtualHostOsh
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
Example #34
0
def buildChassisOSH(deviceAssetClass, deviceAssetSubClass, deviceName,
                    deviceAssetNumber, deviceSerialNumber, deviceManufacturer,
                    deviceModel, deviceModelInfo, hostOshDict,
                    datacenterOshDict):
    try:
        chassisOSH = None
        chassisDictKey = '%s ** %s ** %s' % (deviceName, deviceAssetNumber,
                                             deviceSerialNumber)
        ## Check if this CHASSIS was already processed
        if chassisDictKey in hostOshDict.keys():
            debugPrint(
                3,
                '[buildChassisOSH] Already processed CHASSIS <%s>! Skipping...'
                % deviceName)
            chassisOSH = hostOshDict[chassisDictKey]
        ## If not build a new OSH
        else:
            ## Determine CI Type for CHASSIS
            chassisCitName = 'chassis'
            if deviceAssetSubClass and deviceAssetSubClass == 'BLADE':
                chassisCitName = 'node'
                if deviceModel.lower().find('netra') >= 0:
                    chassisCitName = 'unix'
                if deviceModelInfo.lower().find('bladecenter') >= 0:
                    chassisCitName = 'enclosure'
            elif deviceAssetSubClass and deviceAssetSubClass == 'PATCH PANEL':
                debugPrint(
                    3,
                    '[buildChassisOSH] Skipping CHASSIS <%s> because it is a patch panel'
                    % deviceName)
                return None
            debugPrint(
                4, '[buildChassisOSH] Creating CHASSIS with CIT <%s>' %
                chassisCitName)
            ## Check if serial number is unique
            if deviceSerialNumber:
                for hostDictKey in hostOshDict.keys():
                    serialNumber = hostDictKey.split(' ** ')[2]
                    if serialNumber and deviceSerialNumber.lower(
                    ) == serialNumber.lower():
                        deviceSerialNumber = None
                        logger.warn(
                            'Duplicate Serial Number <%s> found on <%s> <%s>! Discarding serial number...'
                            % (serialNumber, chassisCitName, deviceName))
                        break
            ## Build CHASSIS
            debugPrint(2, '[buildChassisOSH] CHASSIS <%s> found' % deviceName)
            chassisOSH = modeling.createCompleteHostOSH(chassisCitName, '')
            populateOSH(
                chassisOSH, {
                    'name': deviceName,
                    'serial_number': deviceSerialNumber,
                    'discovered_model': '%s (%s)' %
                    (deviceModel, deviceModelInfo),
                    'discovered_vendor': deviceManufacturer
                })
            if chassisCitName in ['chassis', 'bladecenter']:
                populateOSH(
                    chassisOSH, {
                        'chassis_model': '%s %s' %
                        (deviceModel, deviceModelInfo),
                        'chassis_id': deviceAssetNumber,
                        'chassis_vendor': deviceManufacturer
                    })
            if not deviceSerialNumber:
                populateOSH(
                    chassisOSH, {
                        'host_key':
                        str(hash(chassisDictKey)),
                        'data_note':
                        'Serial Number not available in Aperture VISTA. Duplication of this CI is possible'
                    })
            hostOshDict[chassisDictKey] = chassisOSH
        return chassisOSH
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.debug('[' + SCRIPT_NAME +
                     ':buildChassisOSH] Exception: <%s>' % excInfo)
        pass
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()

    DISCOVERED_IP = Framework.getDestinationAttribute('ip_address')
    DISCOVERED_IP_ID = Framework.getDestinationAttribute('ip_id')    
#    ip = modeling.createOshByCmdbIdString('ip_address', DISCOVERED_IP_ID)
    ucmdbVersion = modeling.CmdbClassModel().version()
    if ucmdbVersion >= 9:
        ipClass = 'ip_address'
    else:
        ipClass = 'ip'

    protocols = Framework.getAvailableProtocols(DISCOVERED_IP, ClientsConsts.SNMP_PROTOCOL_NAME)

    if protocols.__len__() == 0:
        errStr = 'No credentials defined for the triggered ip'
        logger.debug(errStr)
        Framework.reportWarning(errStr)
    else:
        for protocol in protocols:
            sysplexNameList = None
            lparIpList = None
            
            snmpOSH = modeling.createSnmpOSH(DISCOVERED_IP, None)

            client = None
            try:
                client = Framework.createClient(protocol)
                logger.debug('got snmp agent for: ', DISCOVERED_IP)
                isMultiOid = client.supportMultiOid()
                
                sysplexNameList = client.executeQuery( '1.3.6.1.4.1.2.6.19.2.2.2.41,1.3.6.1.4.1.2.6.19.2.2.2.41,string,1.3.6.1.4.1.2.6.19.2.2.2.40,string' )#@@CMD_PERMISION snmp protocol execution
                sysplexNameList = sysplexNameList.asTable()

                lparIpList = client.executeQuery( '1.3.6.1.2.1.4.20.1.1,1.3.6.1.2.1.4.20.1.2,string' )#@@CMD_PERMISION snmp protocol execution
                lparIpList = lparIpList.asTable()

                mainframeMacList = client.executeQuery( '1.3.6.1.2.1.2.2.1.6,1.3.6.1.2.1.2.2.1.7,hexa' )#@@CMD_PERMISION snmp protocol execution
                mainframeMacList = mainframeMacList.asTable()
                snmpOSH.setAttribute('application_port', client.getPort())
                
                snmpOSH.setAttribute('application_timeout', client.getTimeout())
                snmpOSH.setAttribute('snmp_port', client.getPort())
                snmpOSH.setAttribute('credentials_id', client.getCredentialId())
                snmpOSH.setAttribute('snmp_retry', client.getRetries())
                snmpOSH.setAttribute('snmp_timeout', client.getTimeout())
                if isMultiOid == 1:
                    snmpOSH.setBoolAttribute('snmp_supportmultioid', 1)
                else:
                    snmpOSH.setBoolAttribute('snmp_supportmultioid', 0)
            except:
                logger.debugException('Unexpected SNMP_AGENT Exception:')
                continue

            if client != None:
                client.close()

            mainframeKey = getMainframeKey(mainframeMacList)
            if mainframeKey:
                mainframeOSH = modeling.createCompleteHostOSH('mainframe', mainframeKey, machineName = mainframeKey)
                modeling.setHostOsFamily(mainframeOSH, 'mainframe')
                OSHVResult.add(mainframeOSH)

                for currSysplex in sysplexNameList:
                    if logger.isDebugEnabled():
                        logger.debug( 'SYSPLEX : ', currSysplex[1] )
                        logger.debug( 'LPAR : ', currSysplex[2] )
                        logger.debug( 'MainframeKEY : ', mainframeKey )

                    lparKey = mainframeKey + ':' + currSysplex[2]
                    lparOSH = modeling.createCompleteHostOSH('lpar', lparKey, machineName = currSysplex[2])
                    OSHVResult.add(lparOSH)
                    
                    snmpOSH.setContainer(lparOSH)
                    OSHVResult.add(snmpOSH)

                    # Create contained link between discovered IP and lpar
                    OSHVResult.add(modeling.createLinkOSH('contained', lparOSH, modeling.createOshByCmdbIdString(ipClass, DISCOVERED_IP_ID)))

                    # Create member link between lpar and Mainframe
                    OSHVResult.add(modeling.createLinkOSH('member', mainframeOSH, lparOSH))

                    # Create sysplex
                    sysplexOSH = ObjectStateHolder('sysplex')
                    sysplexOSH.setAttribute('data_name', currSysplex[1])
                    OSHVResult.add(sysplexOSH)

                    # Create member link between lpar and sysplex
                    OSHVResult.add(modeling.createLinkOSH('member', sysplexOSH, lparOSH))

                    # Create member link between mainframe and sysplex
                    OSHVResult.add(modeling.createLinkOSH('member', mainframeOSH, sysplexOSH))

                    # connect all ips to lpar
                    for currLparIp in lparIpList:
                        if netutils.isLocalIp(currLparIp[1]):
                            continue

                        currIpOSH = modeling.createIpOSH(currLparIp[1])
                        OSHVResult.add(currIpOSH)
                        OSHVResult.add(modeling.createLinkOSH('contained', lparOSH, currIpOSH))
                break
            else:
                logger.debug("Failed to get Mainframe key.")
        else:
            errormessages.resolveAndReport("Could not perform snmp connection to %s" % DISCOVERED_IP, ClientsConsts.SNMP_PROTOCOL_NAME, Framework)

    return OSHVResult
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    host_id = Framework.getDestinationAttribute('host_id')

    serverOsh = modeling.createCompleteHostOSH('unix', host_id)
    interface_macs = Framework.getTriggerCIDataAsList('interface_mac')
    interface_names = Framework.getTriggerCIDataAsList('interface_name')
    ## Write implementation to return new result CIs here...
    list_len = len(interface_macs)
    logger.debug('Triggered against %s interfaces' % list_len)

    #do the mac to name and vlan grouping
    interfaces_dict = {}
    for i in xrange(list_len):
        mac = interface_macs[i]
        name = interface_names[i]
        if not mac or mac == 'NA':
            logger.debug('Skipping interface %s with mac %s' % (name, mac))
            continue
        m = re.match('.+[\.@](\d+)', name)
        if not m:
            logger.debug(
                'Skipping interface %s with mac %s since interface name does not contain vlan id'
                % (name, mac))
            continue
        vlan = m.group(1)
        elems = interfaces_dict.get(mac, [])
        elems.append({'mac': mac, 'name': name, 'vlan': vlan})
        interfaces_dict[mac] = elems

    logger.debug('Parsed server interfaces info %s' % interfaces_dict)

    filepath = CollectorsParameters.HOME_DIR + 'runtime/l2reported/'
    try:
        files = os.listdir(filepath)
    except:
        logger.reportWarning('Failed to open folder with saved scan results.')
        return OSHVResult

    mac_to_switch_iface = {}
    for filename in files:
        file = open(filepath + filename, 'r')
        lines = file.readlines()
        file.close()
        switchInterface = Interface(
            map(lambda x: (x and x != 'None' or None) and x,
                lines[0].strip().split(':::')))
        remoteMac = lines[1].strip()
        mac_to_switch_iface[remoteMac] = switchInterface

    #logger.debug('Fetched infor from FS %s' % mac_to_switch_iface)
    for (mac, server_interfaces) in interfaces_dict.items():
        switchInterface = mac_to_switch_iface.get(mac)
        if not switchInterface:
            logger.debug('No Layer2 have been reported previously for MAC %s' %
                         mac)
            continue
        if not switchInterface.vlan_id or switchInterface.vlan_id == 'None':
            logger.debug(
                'Switch interface is not related to any VLAN, skiping')
            continue

        for iface in server_interfaces:
            if iface['vlan'] == switchInterface.vlan_id:
                logger.debug(
                    'A match has been found for vlan %s. Will report Layer2.' %
                    iface['vlan'])
                OSHVResult.addAll(
                    createLayer2Topology(serverOsh, iface, switchInterface))
                break

    return OSHVResult
Example #37
0
def buildNodeOSH(deviceAssetClass, deviceAssetSubClass, deviceName,
                 deviceAssetNumber, deviceSerialNumber, deviceManufacturer,
                 deviceModel, deviceModelInfo, hostOshDict, datacenterOshDict):
    try:
        nodeOSH = None
        nodeDictKey = '%s ** %s ** %s' % (deviceName, deviceAssetNumber,
                                          deviceSerialNumber)
        ## Check if this NODE was already processed
        if nodeDictKey in hostOshDict.keys():
            debugPrint(
                3, '[buildNodeOSH] Already processed NODE <%s>! Skipping...' %
                deviceName)
            nodeOSH = hostOshDict[nodeDictKey]
        ## If not, build a new OSH
        else:
            ## Determine CI Type for NODE
            nodeCitName = 'host_node'
            if deviceAssetClass == 'ARRAY':
                nodeCitName = 'storagearray'
            elif deviceAssetClass == 'ROUTER':
                nodeCitName = 'router'
            elif deviceAssetClass == 'NETWORK SWITCH':
                nodeCitName = 'switch'
            if deviceManufacturer and deviceManufacturer.lower() == 'sun':
                nodeCitName = 'unix'
            debugPrint(
                4, '[buildNodeOSH] Creating NODE with CIT <%s>' % nodeCitName)
            ## Check if serial number is unique
            if deviceSerialNumber:
                for hostDictKey in hostOshDict.keys():
                    serialNumber = hostDictKey.split(' ** ')[2]
                    if serialNumber and deviceSerialNumber.lower(
                    ) == serialNumber.lower():
                        deviceSerialNumber = None
                        logger.warn(
                            'Duplicate Serial Number <%s> found on <%s> <%s>! Discarding serial number...'
                            % (serialNumber, nodeCitName, deviceName))
                        break
            ##  Build NODE
            debugPrint(2, '[buildNodeOSH] NODE <%s> found' % deviceName)
            nodeOSH = modeling.createCompleteHostOSH(nodeCitName, '')
            populateOSH(
                nodeOSH, {
                    'name': deviceName,
                    'serial_number': deviceSerialNumber,
                    'discovered_model': '%s (%s)' %
                    (deviceModel, deviceModelInfo),
                    'discovered_vendor': deviceManufacturer
                })
            if not deviceSerialNumber:
                populateOSH(
                    nodeOSH, {
                        'host_key':
                        str(hash(nodeDictKey)),
                        'data_note':
                        'Serial Number not available in Aperture VISTA. Duplication of this CI is possible'
                    })
            hostOshDict[nodeDictKey] = nodeOSH
        return nodeOSH
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.debug('[' + SCRIPT_NAME +
                     ':buildNodeOSH] Exception: <%s>' % excInfo)
        pass
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()

    DISCOVERED_IP = Framework.getDestinationAttribute('ip_address')
    DISCOVERED_IP_ID = Framework.getDestinationAttribute('ip_id')
    #    ip = modeling.createOshByCmdbIdString('ip_address', DISCOVERED_IP_ID)
    ucmdbVersion = modeling.CmdbClassModel().version()
    if ucmdbVersion >= 9:
        ipClass = 'ip_address'
    else:
        ipClass = 'ip'

    protocols = Framework.getAvailableProtocols(
        DISCOVERED_IP, ClientsConsts.SNMP_PROTOCOL_NAME)

    if protocols.__len__() == 0:
        errStr = 'No credentials defined for the triggered ip'
        logger.debug(errStr)
        Framework.reportWarning(errStr)
    else:
        for protocol in protocols:
            sysplexNameList = None
            lparIpList = None

            snmpOSH = modeling.createSnmpOSH(DISCOVERED_IP, None)

            client = None
            try:
                client = Framework.createClient(protocol)
                logger.debug('got snmp agent for: ', DISCOVERED_IP)
                isMultiOid = client.supportMultiOid()

                sysplexNameList = client.executeQuery(
                    '1.3.6.1.4.1.2.6.19.2.2.2.41,1.3.6.1.4.1.2.6.19.2.2.2.41,string,1.3.6.1.4.1.2.6.19.2.2.2.40,string'
                )  #@@CMD_PERMISION snmp protocol execution
                sysplexNameList = sysplexNameList.asTable()

                lparIpList = client.executeQuery(
                    '1.3.6.1.2.1.4.20.1.1,1.3.6.1.2.1.4.20.1.2,string'
                )  #@@CMD_PERMISION snmp protocol execution
                lparIpList = lparIpList.asTable()

                mainframeMacList = client.executeQuery(
                    '1.3.6.1.2.1.2.2.1.6,1.3.6.1.2.1.2.2.1.7,hexa'
                )  #@@CMD_PERMISION snmp protocol execution
                mainframeMacList = mainframeMacList.asTable()
                snmpOSH.setAttribute('application_port', client.getPort())

                snmpOSH.setAttribute('application_timeout',
                                     client.getTimeout())
                snmpOSH.setAttribute('snmp_port', client.getPort())
                snmpOSH.setAttribute('credentials_id',
                                     client.getCredentialId())
                snmpOSH.setAttribute('snmp_retry', client.getRetries())
                snmpOSH.setAttribute('snmp_timeout', client.getTimeout())
                if isMultiOid == 1:
                    snmpOSH.setBoolAttribute('snmp_supportmultioid', 1)
                else:
                    snmpOSH.setBoolAttribute('snmp_supportmultioid', 0)
            except:
                logger.debugException('Unexpected SNMP_AGENT Exception:')
                continue

            if client != None:
                client.close()

            mainframeKey = getMainframeKey(mainframeMacList)
            if mainframeKey:
                mainframeOSH = modeling.createCompleteHostOSH(
                    'mainframe', mainframeKey, machineName=mainframeKey)
                modeling.setHostOsFamily(mainframeOSH, 'mainframe')
                OSHVResult.add(mainframeOSH)

                for currSysplex in sysplexNameList:
                    if logger.isDebugEnabled():
                        logger.debug('SYSPLEX : ', currSysplex[1])
                        logger.debug('LPAR : ', currSysplex[2])
                        logger.debug('MainframeKEY : ', mainframeKey)

                    lparKey = mainframeKey + ':' + currSysplex[2]
                    lparOSH = modeling.createCompleteHostOSH(
                        'lpar', lparKey, machineName=currSysplex[2])
                    OSHVResult.add(lparOSH)

                    snmpOSH.setContainer(lparOSH)
                    OSHVResult.add(snmpOSH)

                    # Create contained link between discovered IP and lpar
                    OSHVResult.add(
                        modeling.createLinkOSH(
                            'contained', lparOSH,
                            modeling.createOshByCmdbIdString(
                                ipClass, DISCOVERED_IP_ID)))

                    # Create member link between lpar and Mainframe
                    OSHVResult.add(
                        modeling.createLinkOSH('member', mainframeOSH,
                                               lparOSH))

                    # Create sysplex
                    sysplexOSH = ObjectStateHolder('sysplex')
                    sysplexOSH.setAttribute('data_name', currSysplex[1])
                    OSHVResult.add(sysplexOSH)

                    # Create member link between lpar and sysplex
                    OSHVResult.add(
                        modeling.createLinkOSH('member', sysplexOSH, lparOSH))

                    # Create member link between mainframe and sysplex
                    OSHVResult.add(
                        modeling.createLinkOSH('member', mainframeOSH,
                                               sysplexOSH))

                    # connect all ips to lpar
                    for currLparIp in lparIpList:
                        if netutils.isLocalIp(currLparIp[1]):
                            continue

                        currIpOSH = modeling.createIpOSH(currLparIp[1])
                        OSHVResult.add(currIpOSH)
                        OSHVResult.add(
                            modeling.createLinkOSH('contained', lparOSH,
                                                   currIpOSH))
                break
            else:
                logger.debug("Failed to get Mainframe key.")
        else:
            errormessages.resolveAndReport(
                "Could not perform snmp connection to %s" % DISCOVERED_IP,
                ClientsConsts.SNMP_PROTOCOL_NAME, Framework)

    return OSHVResult
def DiscoveryMain(Framework):
    OSHVResult = ObjectStateHolderVector()
    host_id =Framework.getDestinationAttribute('host_id')
    
    serverOsh = modeling.createCompleteHostOSH('unix', host_id)
    interface_macs = Framework.getTriggerCIDataAsList('interface_mac')
    interface_names = Framework.getTriggerCIDataAsList('interface_name') 
    ## Write implementation to return new result CIs here...
    list_len = len(interface_macs)
    logger.debug('Triggered against %s interfaces' % list_len)
    
    #do the mac to name and vlan grouping
    interfaces_dict = {}
    for i in xrange(list_len):
        mac = interface_macs[i]
        name = interface_names[i]
        if not mac or mac == 'NA':
            logger.debug('Skipping interface %s with mac %s' % (name, mac))
            continue
        m = re.match('.+[\.@](\d+)', name)
        if not m:
            logger.debug('Skipping interface %s with mac %s since interface name does not contain vlan id' % (name, mac))
            continue
        vlan = m.group(1)
        elems = interfaces_dict.get(mac, [])
        elems.append({'mac' : mac, 'name' : name, 'vlan' : vlan})
        interfaces_dict[mac] = elems
        
    logger.debug('Parsed server interfaces info %s' % interfaces_dict)
    
    filepath = CollectorsParameters.HOME_DIR + 'runtime/l2reported/'
    try:
        files = os.listdir(filepath)
    except:
        logger.reportWarning('Failed to open folder with saved scan results.')
        return OSHVResult
        
    mac_to_switch_iface = {}
    for filename in files:
        file = open(filepath+filename,'r')
        lines = file.readlines()
        file.close()
        switchInterface = Interface(map(lambda x: (x and x != 'None' or None) and x, lines[0].strip().split(':::')))
        remoteMac = lines[1].strip()
        mac_to_switch_iface[remoteMac] = switchInterface

    #logger.debug('Fetched infor from FS %s' % mac_to_switch_iface)
    for (mac, server_interfaces) in interfaces_dict.items():
        switchInterface = mac_to_switch_iface.get(mac)
        if not switchInterface:
            logger.debug('No Layer2 have been reported previously for MAC %s' % mac)
            continue
        if not switchInterface.vlan_id or switchInterface.vlan_id == 'None':
            logger.debug('Switch interface is not related to any VLAN, skiping')
            continue
            
        for iface in server_interfaces:
            if iface['vlan'] == switchInterface.vlan_id:
                logger.debug('A match has been found for vlan %s. Will report Layer2.' % iface['vlan'])
                OSHVResult.addAll(createLayer2Topology(serverOsh, iface, switchInterface))
                break

    return OSHVResult
def doSnmp(client, isClient, snmpOSH, ip_address, ip_domain, Framework, host_cmdbid, host_key, host_macs):
    '''SnmpClient, osh, str, str, Framework, str, str, list(str) -> ObjectStateHolderVector
    @deprecated
    '''
    networkList = []
    vector = ObjectStateHolderVector()
    ucmdb_version = modeling.CmdbClassModel().version()
    # system table
    snmpQuery = '1.3.6.1.2.1.1.1,1.3.6.1.2.1.1.2,string,1.3.6.1.2.1.1.2,string,1.3.6.1.2.1.1.5,string'
    table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution

    sysname = None
    oid = None
    description = None
    try:
        if table.next():
            description = table.getString(2)
            oid = table.getString(3)
            sysname = table.getString(4)
    except:
        logger.debugException('')

    node_description = description or ''

    sysObjId = oid
    className, vendorName, hostOsName, hostModel, serialNumberOid, serialNumberSNMPMethod = getNodeDetails(description, oid)
    logger.debug("Node details:%s, %s, %s, %s, %s, %s"%(className, vendorName, hostOsName, hostModel, serialNumberOid, serialNumberSNMPMethod))
    if className == 'netprinter':
        sysname = getNetPrinterName(client) or sysname
    hostVersion, hostRelease = getOsVersionAndBuild(hostOsName, description)

# since sysUpTime returns a time since the snmp was re-initialized, this
# time doesn't provide us correct answer, therefore this code is disabled
#    snmpQuery = '1.3.6.1.2.1.1.3,1.3.6.1.2.1.1.4,string'
#    upTime = client.executeQuery(snmpQuery)

    if oid != None:
        snmpOSH.setAttribute('snmp_oid', oid)
    if((sysname != None) and (sysname != '') and (sysname != 'unknown')):
        snmpOSH.setAttribute('snmp_sysname', sysname)
    if description != None:
        snmpOSH.setAttribute('snmp_description', description)

    logger.debug('ip_address: ', ip_address, ', sysname: ' , sysname, ', className: ', className, ', oid: ', oid, ', description: ', description)

    #dicovery arp cache available
    arp_available = SNMP_Networking_Utils.isArpCacheAvailable(client)
    snmpOSH.setBoolAttribute('arp_cache_available', arp_available)

    # indx, description & mac

    interfaceList = []
    interfaceDictionary = {}

    ifList = SNMP_Networking_Utils.discoverInterfaceData(client, None)

    for nic in ifList:
        if nic.ifType and int(nic.ifType) == 24:
            continue

        macAddress = str(nic.ifMac).upper()
        if nic.ifIndex != None and nic.ifIndex != '':
            inrfcindex = nic.ifIndex
        description = _stripVirtualMiniportInfo(nic.ifDescr)

        if interfaceDictionary.has_key(inrfcindex):
            logger.debug('this mac was already reported, skip it ... : inrfcindex: ', inrfcindex, ', descrition: ', description, ', macAddress: ', macAddress)
            continue

        logger.debug('inrfcindex: ', inrfcindex, ', description: ', description, ', macAddress: ', macAddress)

        interfaceDictionary[inrfcindex] = macAddress
        networkinterface = modeling.NetworkInterface(description, macAddress, None, None, inrfcindex)
        networkinterface.type = nic.ifType
        networkinterface.adminStatus = nic.ifAdminStatus
        networkinterface.operStatus = nic.ifOperStatus
        networkinterface.speed = nic.ifSpeed
        networkinterface.name = nic.ifName
        networkinterface.alias = nic.ifAlias
        if not networkinterface.name:
            m = re.match('(lan[\d\:\.]+)', description)
            if m:
                networkinterface.name = m.group(1)
                networkinterface.description = None
        interfaceList.append(networkinterface)

    # create the host and all the objects
    if len(interfaceList) > 0:
        macToInterfaceListMap = {}
        for interface in interfaceList:
            macToInterfaceListMap.setdefault(interface.macAddress, []).append(interface)

        for ifaceList in macToInterfaceListMap.values():
            if ifaceList and len(ifaceList) < 2:
                continue
            try:
                iface = reduce(lambda x,y: x.interfaceIndex > y.interfaceIndex and x or y, ifaceList)
                iface.role = 'aggregate_interface'
            except:
                logger.debugException('')
        hostOSH = None

        try:
            # Get the host_key - lowest mac address of the valid interface list
            hostOSH = modeling.createCompleteHostOSHByInterfaceList(className, interfaceList, None, None, None, host_cmdbid, host_key, host_macs, ucmdb_version)
            if (className in netDeviceClasses) and (sysname != None) and (sysname != '') and ((host_cmdbid in ['NA','',None]) or (host_key and (host_key.lower() == sysname.lower()))):
                # JEO @ Fidelity: use SNMP system name for host key of network devices unless its null
                hostOSH.setAttribute('host_key', sysname)

        except:
            logger.debug('Could not find a valid MAC address for key on ip : ', ip_address)
            if (className in netDeviceClasses) and (sysname != None) and (sysname != '') and (host_cmdbid in ['NA','',None]):
                logger.debug('Network device, hostkey is sysname...')
                hostOSH = modeling.createCompleteHostOSH(className, sysname)
            else:
                logger.debug('Creating incomplete host...')
                hostOSH = modeling.createHostOSH(ip_address, className)

        logger.debug('Created [' + className + '] strong key=[' + hostOSH.getAttributeValue('host_key') + ']')

        if((sysname != None) and (sysname != '') and (sysname != 'unknown')):
            hostOSH.setAttribute('snmp_sys_name', sysname)
            # set hostname to SNMP system name less domain suffix which is typical of other data sources
            hostname = sysname.split('.')[0].lower()
            hostOSH.setAttribute('name', hostname)
            logger.debug("hostname=" + hostname)

        defaultGateway = getDefaultGateway(client)
        modeling.setHostDefaultGateway(hostOSH, defaultGateway)
        modeling.setHostOsFamily(hostOSH, None, className)
        if sysObjId and sysObjId.startswith('.') != -1:
            sysObjId = "." + sysObjId
        modeling.setSNMPSysObjectId(hostOSH, sysObjId)

        if((hostOsName != None) and (hostOsName != '') and (hostOsName != 'unknown')):
            modeling.setHostOsName(hostOSH, hostOsName)
        if((vendorName != None) and (vendorName != '') and (vendorName != 'unknown')):
            hostOSH.setAttribute('discovered_os_vendor', vendorName)
        if((hostModel != None) and (hostModel != '') and (hostModel != 'unknown')):
            hostOSH.setAttribute('discovered_model', hostModel)
        if hostRelease is not None:
            hostOSH.setAttribute('host_osrelease', hostRelease)
        if hostVersion is not None:
            hostOSH.setAttribute('discovered_os_version', hostVersion)

# since sysUpTime returns a time since the snmp was re-initialized, this
# time doesn't provide us correct answer, therefore this code is disabled
#        if upTime:
#            today = Calendar.getInstance().getTime()
#            setLastBootDate(hostOSH, upTime, today, Framework)

        vector.add(modeling.finalizeHostOsh(hostOSH))
        snmpOSH.setContainer(hostOSH)
        if className == 'mainframe':
            modeling.setHostOsFamily(hostOSH, 'mainframe')

        interfaceOshToIndex = {}
        roleManager = networking_win.InterfaceRoleManager()
        try:
            reportInterfaceName = Boolean.parseBoolean(Framework.getParameter('reportInterfaceName'))
        except:
            logger.warn('Failed to parse reportInterfaceName parameter falue. Not a Boolean type.')
            reportInterfaceName = True
        for nic in interfaceList:
            if netutils.isValidMac(nic.macAddress):
                nic.osh = modeling.createInterfaceOSH(nic.macAddress, hostOSH, nic.description,
                            nic.interfaceIndex, nic.type, nic.adminStatus, nic.operStatus,
                            nic.speed, nic.name, nic.alias, reportInterfaceName)
                roleManager.assignInterfaceRole(nic)
                interfaceOshToIndex[nic.interfaceIndex] = nic.osh
                vector.add(nic.osh)
            else:
                logger.debug('MAC %s is invalid (name: %s, description: %s, index: %s)' %
                             (nic.macAddress, nic.name, nic.description, nic.interfaceIndex))

        # create the ip's
        logger.debug("create the ip's")
        snmpQuery = '1.3.6.1.2.1.4.20.1.1, 1.3.6.1.2.1.4.20.1.2, string'
        table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution
        ips = table.asTable()
        snmpQuery = '1.3.6.1.2.1.4.20.1.2, 1.3.6.1.2.1.4.20.1.3, string'
        table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution
        ifindexes = table.asTable()
        snmpQuery = '1.3.6.1.2.1.4.20.1.3, 1.3.6.1.2.1.4.20.1.4, string'
        table = client.executeQuery(snmpQuery)#@@CMD_PERMISION snmp protocol execution
        ipmasks = table.asTable()
        try:
            rfcIpV6Query = '1.3.6.1.2.1.55.1.8.1.1,1.3.6.1.2.1.55.1.8.1.5, string'
            rfcIpV6Result = client.executeQuery(rfcIpV6Query)#@@CMD_PERMISION snmp protocol execution
            results = rfcIpV6Result.asTable()
        except:
            logger.warn('Failed to get or process IPv6 MIB results.')

        #try to collect IPv6 addresses from IPv6 address table and connect them to corresponding interface
        logger.debug("Begin to discovery IPv6 related info")
        try:
            ipv6Map = {}
            ipv6AddressTable = SNMP_Networking_Utils.discoverIPv6AddressTable(client)
            if ipv6AddressTable:
                for row in ipv6AddressTable:
                    if row.ipv6AddrStatus != '1':# 1 means valid ip address in arp cache
                        continue
                    ifIndex, ipAddress = _parseIPv6FromIndex(row.ipv6AddrAddress)
                    if ifIndex and ipAddress:
                        try:
                            formatedIP = getValidIP(ipAddress)
                            if formatedIP:
                                ipv6Map[ifIndex] = formatedIP
                        except:
                            pass
            mixedIPAddressTable = SNMP_Networking_Utils.discoverMixedIPAddressTable(client)
            if mixedIPAddressTable:
                for row in mixedIPAddressTable:
                    if row.ipAddressStatus != '1' or row.ipAddressRowStatus != '1':# 1 means valid ip address in arp cache
                        continue
                    ifIndex = row.ipAddressIfIndex
                    ipAddress = _parseMixedIPFromIndex(row.ipAddressAddr)
                    if ifIndex and ipAddress:
                        try:
                            formatedIP = getValidIP(ipAddress)
                            if formatedIP and formatedIP.version == 6:
                                ipv6Map[ifIndex] = formatedIP
                        except:
                            pass

            if ipv6Map and interfaceOshToIndex:
                for ifIndex in ipv6Map.keys():
                    ipv6 = ipv6Map[ifIndex]
                    logger.debug("Discovered IPv6:", ipv6)
                    ipOSH = modeling.createIpOSH(ipv6)
                    vector.add(ipOSH)
                    interfaceOsh = interfaceOshToIndex.get(str(ifIndex))
                    if not interfaceOsh and isClient:
                        logger.info('client ip is not associated with an interface')
                        msg = "Can not find the related interface for client IP: %s" % ip_address
                        logger.reportWarningObject(errormessages.resolveError(msg, 'SNMP'))
                        continue
                    if interfaceOsh:
                        parent = modeling.createLinkOSH('containment', interfaceOsh, ipOSH)
                        vector.add(parent)
                        interfaceMacAddress = interfaceOsh.getAttributeValue('mac_address')
                        if (isClient == TRUE):
                            ipOSH.setAttribute('arp_mac', interfaceMacAddress)
                            if (
                            ipv6 == ip_addr_util.IPAddress(ip_address)):#compare ipv6 by the formatting to avoid same IPv6 with different format
                                snmpOSH.setAttribute('arp_mac', interfaceMacAddress)

                    isCompleteAttribute = hostOSH.getAttribute('host_iscomplete')
                    if isCompleteAttribute is not None and isCompleteAttribute.getBooleanValue() == 1:
                        contained = modeling.createLinkOSH('contained', hostOSH, ipOSH)
                        vector.add(contained)
        except:
            logger.debug(str(sys.exc_info()))

        index = 0
        if len(ips) > 0 and len(ifindexes) > 0:
            for ip in ips:
                try:
                    ip_addr = ip[1]
                    #logger.debug('candidate ip_addr: ', ip_addr)
                    ifIndex = ifindexes[index][1]
                    mask = ipmasks[index][1]
                    index += 1

                    interfaceOsh = interfaceOshToIndex.get(ifIndex)

                    ''' this commented out block should be removed
                    # no such thing as netmask IPs, there are broadcast IPs which have .255 in them
                    # but we would definitely want to discover and nic which was asssigned
                    # such an address -
                    #if(ip_addr.find('255.') == 0 ):
                        #exclude netmask ip
                        #continue
                    '''

                    if netutils.isValidIp(ip_addr) and (not netutils.isLocalIp(ip_addr)):
                        '''
                        netutils.isLocalIp is not finding all local addresses
                        127.0.0.0 through 127.255.255.255 are local
                        additional if clause can be removed when this is fixed
                        see http://www.tcpipguide.com/free/t_IPReservedPrivateandLoopbackAddresses.htm
                        '''
                        # ip is valid and not local (i.e. 0.0.0.0, 127.0.0.1)
                        ipOSH = modeling.createIpOSH(ip_addr, mask)
                    else:
                        # loopbacks are standard; don't complain about them in the logs
                        continue
                    logger.debug('ip_addr: ', ip_addr, ', mask: ', mask)

                    netOSH = modeling.createNetworkOSH(ip_addr, mask)

                    strNetAddr = str(netOSH.getAttribute('network_netaddr'))
                    strNetMask = str(netOSH.getAttribute('network_netMask'))
                    currNet = strNetAddr + strNetMask

                    broadcastIp = netOSH.getAttributeValue('network_broadcastaddress')
                    if ip_address == broadcastIp:
                        raise BroadcastIpDiscoveryException()
                    if not interfaceOsh and isClient:
                        logger.info('client ip is not associated with an interface')
                        msg = "Can not find the related interface for client IP: " + ip_addr
                        logger.reportWarningObject(errormessages.resolveError(msg, 'SNMP'))
                        continue
                    if interfaceOsh:
                        parent = modeling.createLinkOSH('containment', interfaceOsh, ipOSH)
                        vector.add(parent)
                        interfaceMacAddress = interfaceOsh.getAttributeValue('mac_address')
                        if (isClient == TRUE):
                            ipOSH.setAttribute('arp_mac', interfaceMacAddress)
                            if (ip_addr == ip_address):
                                snmpOSH.setAttribute('arp_mac', interfaceMacAddress)

                    member1 = modeling.createLinkOSH('member', netOSH, ipOSH)
                    member2 = modeling.createLinkOSH('member', netOSH, hostOSH)

                    if currNet in networkList:
                        pass
                    else:
                        networkList.append(currNet)
                        vector.add(netOSH)
                        vector.add(member2)

                    vector.add(ipOSH)
                    vector.add(member1)

                    # link IPs to host only in case host is complete
                    # otherwise reconciliation may fail
                    isCompleteAttribute = hostOSH.getAttribute('host_iscomplete')
                    if isCompleteAttribute is not None and isCompleteAttribute.getBooleanValue() == 1:
                        contained = modeling.createLinkOSH('contained', hostOSH, ipOSH)
                        vector.add(contained)

                    if interfaceOsh:
                        parent = modeling.createLinkOSH('containment', interfaceOsh, ipOSH)
                        vector.add(parent)
                except BroadcastIpDiscoveryException, ex:
                    raise ex
                except:
                    pass
Example #41
0
def _build_host(host, hostName):
    '@types: Host, str -> osh'

    hostOS = host.os or ''
    # Get OS information and build appropriate CI Type
    if hostOS:
        debugPrint(4, '[discoverServers] Got Server <%s> with OS <%s>' % (hostName, hostOS))
        if hostOS.lower().find('windows') >= 0:
            hostClass = 'nt'
        elif hostOS.lower().find('netapp') >= 0:
            hostClass = 'netapp_filer'
        elif (hostOS.lower().find('unix') >= 0
              or hostOS.lower().find('solaris') >= 0
              or hostOS.lower().find('hp') >= 0
              or hostOS.lower().find('linux') >= 0
              or hostOS.lower().find('aix') >= 0):
            hostClass = 'unix'
        if hostClass:
            debugPrint(4, '[discoverServers] Using HOST class <%s>' % hostClass)

    # Check for a valid IP before creating CIs
    if ip_addr.isValidIpAddress(host.ip):
        hostOSH = modeling.createHostOSH(str(host.ip), hostClass)
    else:
        logger.debug('[discoverServers] IP address not available for Server <%s> with ID <%s>!! Creating Host with ID as primary key...' % (hostName, host.id))
        hostKey = host.id + ' (ECC ID)'
        hostOSH = modeling.createCompleteHostOSH(hostClass, hostKey)
        hostOSH.setAttribute('data_note', 'IP address unavailable in ECC - Duplication of this CI is possible')

    # # Set node role in UCMDB 9 and above for compatibility with SM integration
    if hostOS and (hostOS.lower().find('xp') > -1 or hostOS.lower().find('vista') > -1 or hostOS.lower().find('professional') > -1 or hostOS.lower().find('windows 7') > -1):
        hostOSH.setListAttribute('node_role', ['desktop'])
    else:
        hostOSH.setListAttribute('node_role', ['server'])
    hostModel = host.model
    if (hostModel
        and (hostModel.lower().find('vmware') > -1
             or hostModel.lower().find('zone') > -1
             or hostModel.lower().find('virtual') > -1)):
        hostOSH.setListAttribute('node_role', ['virtualized_system'])

    # # Set Host OS install Type to match HC jobs
    osInstallType = ''
    if hostOS:
        if hostOS.lower().find('hp-ux') > -1 or hostOS.lower().find('hpux') > -1:
            osInstallType = 'HPUX'
        elif hostOS.lower().find('linux') > -1 or hostOS.lower().find('redhat') > -1 or hostOS.lower().find('suse') > -1:
            osInstallType = 'Linux'
        elif hostOS.lower().find('solaris') > -1 or hostOS.lower().find('sun') > -1:
            osInstallType = 'Solaris'
        elif hostOS.lower().find('aix') > -1:
            osInstallType = 'AIX'
        elif hostOS.lower().find('enterprise x64 edition') > -1:
            osInstallType = 'Server Enterprise x64 Edition'
        elif hostOS.lower().find('enterprise edition') > -1:
            osInstallType = 'Server Enterprise Edition'
        elif hostOS.lower().find('server enterprise') > -1:
            osInstallType = 'Server Enterprise'
        elif hostOS.lower().find('enterprise') > -1:
            osInstallType = 'Enterprise'
        elif hostOS.lower().find('professional') > -1:
            osInstallType = 'Professional'
        elif hostOS.lower().find('standard edition') > -1:
            osInstallType = 'Server Standard Edition'
        elif hostOS.lower().find('standard') > -1:
            osInstallType = 'Server Standard'
        elif hostOS.lower().find('server') > -1:
            osInstallType = 'Server'
        elif hostOS.lower().find('business') > -1:
            osInstallType = 'Business'

    # # Set Host OS to match HC jobs
    if hostOS:
        if hostOS.lower().find('2003') > -1:
            hostOS = 'Windows 2003'
        elif hostOS.lower().find('2008') > -1:
            hostOS = 'Windows 2008'
        elif hostOS.lower().find('2008 R2') > -1:
            hostOS = 'Windows 2008 R2'
        elif hostOS.lower().find('2000') > -1:
            hostOS = 'Windows 2000'
        elif hostOS.lower().find('windows 7') > -1:
            hostOS = 'Windows 7'
        elif hostOS.lower().find('vista') > -1:
            hostOS = 'Windows Vista'
        elif hostOS.lower().find('xp') > -1:
            hostOS = 'Windows XP'
        elif hostOS.lower().find('aix') > -1:
            hostOS = 'AIX'
        elif hostOS.lower().find('solaris') > -1 or hostOS.lower().find('sun') > -1:
            hostOS = 'Solaris'
        elif hostOS.lower().find('linux') > -1 or hostOS.lower().find('redhat') > -1 or hostOS.lower().find('suse') > -1:
            hostOS = 'Linux'
        elif hostOS.lower().find('hp-ux') > -1 or hostOS.lower().find('hpux') > -1:
            hostOS = 'HP-UX'
        else:
            hostOS = ''

    memorySizeInKb = host.installedmemory
    if memorySizeInKb:
        memorySizeInMb = memorySizeInKb / 1024
        modeling.setHostMemorySizeAttribute(hostOSH, memorySizeInMb)

    populateOSH(hostOSH, {'data_name': hostName,
                          'host_model':hostModel,
                          'host_vendor': host.vendorname,
                          'host_os':hostOS,
                          'host_osversion': host.osversion,
                          'host_osinstalltype':osInstallType})
    return hostOSH
Example #42
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
Example #43
0
def processNodeInfo(ipAddress, macAddress, nodeName, subnetMask, ipAddressList, macAddressList, nodeOshDict, allowDnsLookup):
    try:
        nodeOSH = interfaceOSH = ipOSH = None
        ## Try and get a DNS name for this node
        nodeDnsName = None
        if allowDnsLookup and ipAddress and netutils.isValidIp(ipAddress):
            nodeDnsName = netutils.getHostName(ipAddress)
            ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got DNS name <%s> for Node <%s> using IP <%s>' % (nodeDnsName, nodeName, ipAddress))
            if nodeDnsName:
                nodeName = nodeDnsName

        ## Discard IP if this is a duplicate
        if ipAddress and netutils.isValidIp(ipAddress) and ipAddress in ipAddressList:
            logger.debug('Ignoring duplicate IP <%s> on Node <%s>...' % (ipAddress, nodeName))
            ipAddress = None
        else:
            ipAddressList.append(ipAddress)

        ## Set the real name of the netDevice
        nodeRealName = nodeName
        if nodeName and netutils.isValidIp(nodeName):
            nodeRealName = ''

        ## Build a host key and create OSHs
        ## Check for and clean up MAC
        macAddy = None
        if macAddress:
            macAddy = netutils.parseMac(macAddress)
            ## Check for duplicate MAC addresses
            if macAddy in macAddressList:
                logger.debug('Ignoring duplicate MAC Address <%s> on Node <%s>...' % (macAddy, nodeName))
                macAddy = None
            else:
                macAddressList.append(macAddy)
            if netutils.isValidMac(macAddy):
                ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got MAC Address <%s> for Node <%s>' % (macAddy, nodeName))
                nodeOSH = modeling.createCompleteHostOSH('node', macAddy, None, nodeName)
                interfaceOSH = modeling.createInterfaceOSH(macAddy, nodeOSH)
            else:
                ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got invalid MAC Address <%s> for Node <%s>' % (macAddress, nodeName))
                macAddy = None

        ## Check for a valid IP
        if ipAddress and netutils.isValidIp(ipAddress):
            subnetMask = None
            if subnetMask:
                subnetMask = netutils.parseNetMask(subnetMask)
            ipOSH = modeling.createIpOSH(ipAddress, subnetMask, ipAddress, None)
            ## Use IP as a host key if a MAC is not available
            if not macAddy:
                nodeOSH = modeling.createHostOSH(ipAddress, 'node', None, nodeRealName)
        else:
            logger.debug('IP address not available for Node <%s> with MAC address <%s>' % (nodeName, macAddress))

        if not nodeOSH:
            logger.debug('Ignoring Node <%s> because a valid IP/MAC address was not found for it...' % nodeName)

        return (nodeOSH, interfaceOSH, ipOSH)
    except:
        excInfo = logger.prepareJythonStackTrace('')
        logger.warn('[' + SCRIPT_NAME + ':processNodeInfo] Exception: <%s>' % excInfo)
        pass