Exemplo n.º 1
0
def get_computer(data_dict):
    # Sanity check input data_dict
    if len(data_dict['ComputerSystem']) == 0:
        raise import_error("No ComputerSystem found")

    if len(data_dict['ComputerSystem']) > 1:
        raise import_error("Multipe ComputerSystem found")

    # All search terms must match
    query = models.computer.objects.all()

    # Try finding computer based on serial number
    # note: serial number is not guaranteed to be unique
    serial_number = get_computer_serial_number(data_dict)
    if serial_number is not None:
        new_query = query.filter(auto_serial_number=serial_number)
        if new_query.count() > 0:
            query = new_query
    else:
        query = query.filter(auto_serial_number__isnull=True)

    # Try finding computer based on the name
    computer_name = son(data_dict['ComputerSystem'][0]['Name'])
    if computer_name is not None:
        new_query = query.filter(name = computer_name)
        if new_query.count() > 0:
            query = new_query

    # Try to find computer by MAC addresses. All MAC addresses must match, but
    # only if we already have a record of the MAC address.
    for network in data_dict['NetworkAdapter']:
        if is_physical_network_adapter(network):
            mac_address = helpers.fix_mac_address(network['MACAddress'])
            # if network card is marked as not installed, it can't help us
            nas = models.network_adaptor.objects.filter(mac_address=mac_address, installed_on__isnull=False)
            try:
                # did we get any results?
                first_item = nas[0]
                query = query.filter(installed_hardware__network_adaptor__in=nas)
            except IndexError, e:
                # no network adaptors found
                raise computer_does_not_exist("No matching network cards found for %s"%(mac_address))
Exemplo n.º 2
0
def sync_hardware(data_datetime, computer, data_dict):
    # Update computer details
    computer.name         = son(data_dict['ComputerSystem'][0]['Name'])
    computer.seen_last    = data_datetime
    computer.memory       = son(data_dict['ComputerSystem'][0]['TotalPhysicalMemory'])

    # Update manufacturer, if required
    value = son(data_dict['ComputerSystem'][0]['Manufacturer'])
    if has_changed(computer.auto_manufacturer,computer.manufacturer,value):
        computer.manufacturer = value
    computer.auto_manufacturer = value

    # Update model, if required
    value = son(data_dict['ComputerSystem'][0]['Model'])
    if has_changed(computer.auto_model,computer.model,value):
        computer.model = value
    computer.auto_model = value

    # Try to find the serial number
    serial_number = get_computer_serial_number(data_dict)

    # Update serial number, if required
    if has_changed(computer.auto_serial_number,computer.serial_number,serial_number):
        computer.serial_number = serial_number
    computer.auto_serial_number = serial_number

    # save results
    computer.save()

    # Now do the hardware
    hardware = [ h.pk for h in computer.installed_hardware.all() ]
    used_storage = [ h.pk for h in computer.used_storage.all() ]

    seen_hardware = {}
    for network in data_dict['NetworkAdapter']:
        if is_physical_network_adapter(network):
            mac_address = helpers.fix_mac_address(network['MACAddress'])

            try:
                c = False
                na = models.network_adaptor.objects.get(
                        mac_address=mac_address,
                )
            except models.network_adaptor.DoesNotExist, e:
                c = True
                na = models.network_adaptor()

            # synchronise hardware values
            sync_hardware_item(
                        hardware=na,
                        verbose_name="network adaptor",
                        created=c,
                        computer=computer,
                        data_datetime=data_datetime,
                        seen_hardware=seen_hardware,
                        remove_list=hardware,
                        manufacturer=son(network['Manufacturer']),
                        model=None,
                        serial_number=None,
            )

            na.mac_address = mac_address
            na.name         = son(network['Name'])
            na.network_type = son(network['AdapterType'])

            # Save values
            na.save()