Ejemplo n.º 1
0
def make_components(ilo, dev):
    for i, (label, size, speed) in enumerate(ilo.memories):
        mem, created = Memory.concurrent_get_or_create(device=dev, index=i + 1)
        mem.size = size
        mem.speed = speed
        mem.label = label
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            size=mem.size,
            speed=mem.speed,
            priority=SAVE_PRIORITY,
        )
        mem.save(priority=SAVE_PRIORITY)

    for i, (label, speed, cores, extra, family) in enumerate(ilo.cpus):
        cpu, created = Processor.concurrent_get_or_create(device=dev,
                                                          index=i + 1)
        cpu.label = label
        cpu.model, c = ComponentModel.create(
            ComponentType.processor,
            speed=speed,
            cores=cores,
            family=family,
            name='CPU %s %dMHz, %s-core' % (family, speed, cores),
            priority=SAVE_PRIORITY,
        )
        cpu.save(priority=SAVE_PRIORITY)
Ejemplo n.º 2
0
def run_http_ibm_system_x(ip):
    session_id = get_session_id(ip)
    management_url = "http://%s/wsman" % ip
    model_name = get_model_name(management_url, session_id)
    sn = get_sn(management_url, session_id)
    macs = get_mac_addresses(management_url, session_id)
    ethernets = [Eth(label=label, mac=mac, speed=0)
                 for (label, mac) in macs]
    ipaddr, ip_created = IPAddress.concurrent_get_or_create(address=ip)
    ipaddr.is_management = True
    ipaddr.save()
    dev = Device.create(
        ethernets=ethernets,
        model_name=model_name,
        sn=sn,
        model_type=DeviceType.rack_server,
    )
    dev.management = ipaddr
    dev.save(priority=SAVE_PRIORITY)
    ipaddr.device = dev
    ipaddr.save()
    detected_memory = get_memory(management_url, session_id)
    detected_memory_indexes = [x.get('index') for x in detected_memory]
    for m in dev.memory_set.exclude(index__in=detected_memory_indexes):
        m.delete()
    for m in detected_memory:
        index = m['index']
        mem, _ = Memory.concurrent_get_or_create(index=index, device=dev)
        mem.label = m['label']
        mem.size = m['size']
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            size=mem.size,
            priority=SAVE_PRIORITY,
        )
        mem.save(priority=SAVE_PRIORITY)
    detected_processors = get_processors(management_url, session_id)
    detected_processors_keys = [x.get('index') for x in detected_processors]
    for cpu in dev.processor_set.exclude(index__in=detected_processors_keys):
        cpu.delete()
    # add new
    for p in detected_processors:
        processor_model, _ = ComponentModel.create(
            ComponentType.processor,
            speed=p.get('speed'),
            family=p.get('family'),
            cores=p.get('cores'),
            name=p.get('label'),
            priority=SAVE_PRIORITY,
        )
        processor, _ = Processor.concurrent_get_or_create(
            device=dev,
            index=p.get('index'),
        )
        processor.label = p.get('label')
        processor.model = processor_model
        processor.speed = p.get('speed')
        processor.save(priority=SAVE_PRIORITY)
    return model_name
Ejemplo n.º 3
0
def run_http_ibm_system_x(ip):
    session_id = get_session_id(ip)
    management_url = "http://%s/wsman" % ip
    model_name = get_model_name(management_url, session_id)
    sn = get_sn(management_url, session_id)
    macs = get_mac_addresses(management_url, session_id)
    ethernets = [Eth(label=label, mac=mac, speed=0) for (label, mac) in macs]
    ipaddr, ip_created = IPAddress.concurrent_get_or_create(address=ip)
    ipaddr.is_management = True
    ipaddr.save()
    dev = Device.create(
        ethernets=ethernets,
        model_name=model_name,
        sn=sn,
        model_type=DeviceType.rack_server,
    )
    dev.management = ipaddr
    dev.save(priority=SAVE_PRIORITY)
    ipaddr.device = dev
    ipaddr.save()
    detected_memory = get_memory(management_url, session_id)
    detected_memory_indexes = [x.get('index') for x in detected_memory]
    for m in dev.memory_set.exclude(index__in=detected_memory_indexes):
        m.delete()
    for m in detected_memory:
        index = m['index']
        mem, _ = Memory.concurrent_get_or_create(index=index, device=dev)
        mem.label = m['label']
        mem.size = m['size']
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            size=mem.size,
            priority=SAVE_PRIORITY,
        )
        mem.save(priority=SAVE_PRIORITY)
    detected_processors = get_processors(management_url, session_id)
    detected_processors_keys = [x.get('index') for x in detected_processors]
    for cpu in dev.processor_set.exclude(index__in=detected_processors_keys):
        cpu.delete()
    # add new
    for p in detected_processors:
        processor_model, _ = ComponentModel.create(
            ComponentType.processor,
            speed=p.get('speed'),
            family=p.get('family'),
            cores=p.get('cores'),
            name=p.get('label'),
            priority=SAVE_PRIORITY,
        )
        processor, _ = Processor.concurrent_get_or_create(
            device=dev,
            index=p.get('index'),
        )
        processor.label = p.get('label')
        processor.model = processor_model
        processor.speed = p.get('speed')
        processor.save(priority=SAVE_PRIORITY)
    return model_name
Ejemplo n.º 4
0
def _save_storage(dev, data):
    detected_storage = []
    for disk in data:
        model_name = "{} {}".format(
            disk['manufacturer'],
            disk['model']
        )
        size = int(int(disk['size']) / 1024 / 1024 / 1024)
        model, _ = ComponentModel.create(
            ComponentType.disk,
            size=size,
            family=model_name,
            priority=SAVE_PRIORITY,
        )
        storage, _ = Storage.concurrent_get_or_create(
            device=dev,
            sn=disk['sn'],
            mount_point=None,
        )
        storage.model = model
        storage.label = model.name
        storage.size = size
        storage.save(priority=SAVE_PRIORITY)
        detected_storage.append(storage.pk)
    dev.storage_set.exclude(pk__in=detected_storage).delete()
Ejemplo n.º 5
0
def _save_shares(dev, shares):
    wwns = []
    for share_id, (label, wwn, snapshot_size, size, share_type,
                   speed, full) in shares.iteritems():
        wwn = normalize_wwn(wwn)
        wwns.append(wwn)
        model, created = ComponentModel.create(
            ComponentType.share,
            name='3PAR %s disk share' % share_type,
            family=share_type,
            speed=speed,
            priority=0,   # FIXME: why 0?
        )
        share, created = DiskShare.concurrent_get_or_create(
            wwn=wwn,
            defaults={'device': dev},
        )
        if not created:
            if dev.id != share.device.id:
                raise ValueError(
                    'DiskShare %r: Conflict of devices %r and %r!' % (
                        share,
                        dev,
                        share.device,
                    )
                )
        share.model = model
        share.label = label
        share.full = full
        share.size = size
        share.share_id = share_id
        share.snapshot_size = snapshot_size
        share.save()
    dev.diskshare_set.exclude(wwn__in=wwns).delete()
Ejemplo n.º 6
0
def _save_shares(dev, volumes):
    wwns = []
    for (label, serial, size, volume_type, speed) in volumes:
        wwn = normalize_wwn(serial)
        wwns.append(wwn)
        model, created = ComponentModel.create(
            ComponentType.share,
            speed=speed,
            family=volume_type,
            name='MSA %s disk share' % volume_type,
            priority=0,  # FIXME: why 0?
        )
        share, created = DiskShare.concurrent_get_or_create(
            wwn=wwn,
            defaults={'device': dev},
        )
        if not created:
            if dev.id != share.device.id:
                raise ValueError(
                    'DiskShare %r: Conflict of devices %r and %r!' % (
                        share,
                        dev,
                        share.device,
                    ))
        share.model = model
        share.label = label
        share.size = size * 512 / 1024 / 1024
        share.snapshot_size = 0
        share.save()
    dev.diskshare_set.exclude(wwn__in=wwns).delete()
Ejemplo n.º 7
0
def save_processors(processors, dev, is_virtual=False):
    indexes = []
    for p in processors:
        cpuname = p.get("label")
        if is_virtual and not cpuname.lower().startswith("virtual"):
            cpuname = "Virtual %s" % cpuname
        try:
            index = int(p.get("index")[3:]) + 1  # CPU0
            speed = int(p.get("speed"))
            cores = int(p.get("cores"))
        except ValueError:
            continue
        indexes.append(index)
        cpu, created = Processor.concurrent_get_or_create(
            device=dev, index=index, defaults={"label": cpuname, "speed": speed, "cores": cores}
        )
        if not created:
            cpu.label = cpuname
            cpu.speed = speed
            cpu.cores = cores
        is64bit = p.get("is64bit") == "true"
        name = "CPU %s%s %s%s" % (
            "64bit " if is64bit else "",
            cpuname,
            "%dMhz" % speed if speed else "",
            " multicore" if cores > 1 else "",
        )
        cpu.model, c = ComponentModel.create(
            ComponentType.processor, cores=cores, family=cpuname, speed=speed, name=name, priority=SAVE_PRIORITY
        )
        cpu.save(priority=SAVE_PRIORITY)
    for cpu in dev.processor_set.exclude(index__in=indexes):
        cpu.delete()
Ejemplo n.º 8
0
def _add_dev_memory(ip, pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        DiscoveryWarning(
            message="Memory model unknown",
            plugin=__name__,
            device=parent,
            ip=ip,
        ).save()
        return
    counts.mem += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.mem
    mem, created = Memory.concurrent_get_or_create(device=parent, index=index)
    size = int(pairs['Size'].replace('GB', '')) * 1024
    speed = int(pairs.get('Speed', '0').replace('MHz', ''))
    mem.label = pairs.get('Mach type/model', '')
    mem.model, c = ComponentModel.create(
        ComponentType.memory,
        size=size,
        speed=speed,
        priority=SAVE_PRIORITY,
    )
    mem.save(priority=SAVE_PRIORITY)
Ejemplo n.º 9
0
def _save_cpu(dev, data):
    detected_cpus = []
    for cpu in data:
        try:
            index = int(cpu['socket'].split('.')[-1])
        except (ValueError, IndexError):
            continue
        model, _ = ComponentModel.create(
            ComponentType.processor,
            speed=cpu['speed'],
            family=cpu['family'],
            cores=cpu['cores_count'],
            name = cpu['model'],
            priority=SAVE_PRIORITY,
        )
        processor, _ = Processor.concurrent_get_or_create(
            device=dev,
            index=index
        )
        processor.label = cpu['model']
        processor.model = model
        processor.speed = cpu['speed']
        processor.cores = cpu['cores_count']
        processor.save(priority=SAVE_PRIORITY)
        detected_cpus.append(processor.pk)
    dev.processor_set.exclude(pk__in=detected_cpus).delete()
Ejemplo n.º 10
0
def _add_dev_cpu(ip, pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        DiscoveryWarning(
            message="Processor model unknown",
            plugin=__name__,
            device=parent,
            ip=ip,
        ).save()
        return
    counts.cpu += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.cpu
    cpu, created = Processor.concurrent_get_or_create(
        device=parent,
        index=index,
    )
    cpu.label = pairs['Mach type/model']
    family = pairs['Processor family']
    if family.startswith('Intel '):
        family = cpu.label[len('Intel '):]
    speed = int(float(pairs['Speed'].replace('GHz', '')) * 1000)
    cores = int(pairs['Processor cores'])
    cpu.model, c = ComponentModel.create(
        ComponentType.processor,
        speed=speed,
        cores=cores,
        name='CPU %s %d MHz, %s-core' % (family, speed, cores),
        family=family,
        priority=SAVE_PRIORITY,
    )
    cpu.save(priority=SAVE_PRIORITY)
Ejemplo n.º 11
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get('size'))
            speed = int(row.get('speed')) if row.get('speed') else 0
        except ValueError:
            pass
        label = row.get('label')
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = 'RAM %dMiB' % size
        mem.speed = speed
        family = 'Virtual Windows' if 'Virtual' in label else 'Windows'
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            family=family,
            size=size,
            speed=speed,
            priority=SAVE_PRIORITY,
        )
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Ejemplo n.º 12
0
def _add_dev_memory(ip, pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        DiscoveryWarning(
            message="Memory model unknown",
            plugin=__name__,
            device=parent,
            ip=ip,
        ).save()
        return
    counts.mem += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.mem
    mem, created = Memory.concurrent_get_or_create(device=parent, index=index)
    size = int(pairs['Size'].replace('GB', '')) * 1024
    speed = int(pairs.get('Speed', '0').replace('MHz', ''))
    mem.label = pairs.get('Mach type/model', '')
    mem.model, c = ComponentModel.create(
        ComponentType.memory,
        size=size,
        speed=speed,
        priority=SAVE_PRIORITY,
    )
    mem.save(priority=SAVE_PRIORITY)
Ejemplo n.º 13
0
def handle_hpacu(dev, disks, priority=0):
    for disk_handle, disk in disks.iteritems():
        if not disk.get('serial_number'):
            continue
        stor, created = Storage.concurrent_get_or_create(
            device=dev, sn=disk['serial_number'], mount_point=None,
        )
        stor.device = dev
        size_value, size_unit = disk['size'].split()
        stor.size = int(float(size_value) / units.size_divisor[size_unit])
        stor.speed = int(disk.get('rotational_speed', 0))
        stor.label = '{} {}'.format(' '.join(disk['model'].split()),
                                    disk['interface_type'])
        disk_default = dict(
            model='unknown',
            firmware_revision='unknown',
            interface_type='unknown',
            size='unknown',
            rotational_speed='unknown',
            status='unknown',
        )
        disk_default.update(disk)
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            speed=stor.speed,
            family=stor.label,
            priority=priority,
        )
        stor.save(priority=priority)
Ejemplo n.º 14
0
def _save_shares(dev, shares):
    wwns = []
    for share_id, (label, wwn, snapshot_size, size, share_type, speed,
                   full) in shares.iteritems():
        wwn = normalize_wwn(wwn)
        wwns.append(wwn)
        model, created = ComponentModel.create(
            ComponentType.share,
            name='3PAR %s disk share' % share_type,
            family=share_type,
            speed=speed,
            priority=0,  # FIXME: why 0?
        )
        share, created = DiskShare.concurrent_get_or_create(
            wwn=wwn,
            defaults={'device': dev},
        )
        if not created:
            if dev.id != share.device.id:
                raise ValueError(
                    'DiskShare %r: Conflict of devices %r and %r!' % (
                        share,
                        dev,
                        share.device,
                    ))
        share.model = model
        share.label = label
        share.full = full
        share.size = size
        share.share_id = share_id
        share.snapshot_size = snapshot_size
        share.save()
    dev.diskshare_set.exclude(wwn__in=wwns).delete()
Ejemplo n.º 15
0
def cisco_component(dev, inv):
    comp_type = cisco_type(inv['pid'])
    name = inv['descr']
    if not name.lower().startswith('cisco'):
        name = 'Cisco %s' % name
    model, created = ComponentModel.create(
        comp_type,
        family=inv['pid'],
        name=name,
        priority=0,  # FIXME: why 0?
    )
    comp, created = GenericComponent.concurrent_get_or_create(
        sn=inv['sn'],
        defaults={
            'device': dev,
        },
    )
    if comp.device == dev:
        comp.model = model
        comp.label = inv['name'][:255]
        comp.save()
    else:
        DiscoveryWarning(
            message="GenericComponent(id={}) belongs to Device(id={})".format(
                comp.id,
                comp.device.id,
            ),
            plugin=__name__,
            device=dev,
        ).save()
        comp = None
    return comp
Ejemplo n.º 16
0
def handle_lshw_processors(dev, processors, is_virtual=False, priority=0):
    if isinstance(processors, dict):
        processors = [processors]
    detected_cpus = {}
    family = None
    for processor in processors:
        family = processor["version"] or ("Virtual CPU" if is_virtual else processor["product"])
        if family:
            break
    if not family:
        return  # skip CPU changes if we cannot determine family
    for i, processor in enumerate(processors):
        if processor["disabled"] == "true" or not processor["size"]:
            continue
        label = "CPU {}".format(i + 1)
        speed = int(processor["size"]["value"] or 0)  # 'size', sic!
        speed /= units.speed_divisor[processor["size"]["units"]]
        speed = int(speed)
        model, c = ComponentModel.create(
            ComponentType.processor,
            speed=speed,
            family=family,
            name=processor["product"] or "CPU {} {}MHz".format(family, speed),
            priority=priority,
        )
        detected_cpus[i + 1] = label, model
    for cpu in dev.processor_set.all():
        label, model = detected_cpus.get(cpu.index, (None, None))
        if cpu.label != label or cpu.model != model:
            cpu.delete()
    for index, (label, model) in detected_cpus.iteritems():
        cpu, created = Processor.concurrent_get_or_create(device=dev, index=index)
        cpu.label = label
        cpu.model = model
        cpu.save(priority=priority)
Ejemplo n.º 17
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get('size'))
            speed = int(row.get('speed')) if row.get('speed') else 0
        except ValueError:
            pass
        label = row.get('label')
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = 'RAM %dMiB' % size
        mem.speed = speed
        family = 'Virtual Windows' if 'Virtual' in label else 'Windows'
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            family=family,
            size=size,
            speed=speed,
            priority=SAVE_PRIORITY,
        )
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Ejemplo n.º 18
0
def handle_lshw_fibre_cards(dev, lshw, is_virtual=False, priority=0):
    buses = []
    for bus in jpath.get_all('..bus', lshw):
        if not bus:
            continue
        if isinstance(bus, list):
            buses.extend(bus)
        else:
            buses.append(bus)
    buses = filter(lambda item: item['id'].startswith('fiber'), buses)
    buses.sort(key=lambda item: item['handle'])
    handled_buses = set()
    detected_fc_cards = set()
    for bus in buses:
        handle = unicode(bus['handle'])
        m = re.search(r"([1-9][0-9]*)", handle)
        if not m:
            continue
        physid = m.group(1)
        if physid in handled_buses:
            continue
        fib, created = FibreChannel.concurrent_get_or_create(
            device=dev, physical_id=physid)
        fib.label = "{} {}".format(bus['vendor'], bus['product'])
        fib.model, c = ComponentModel.create(
            ComponentType.fibre,
            family=bus['vendor'],
            name=bus['product'],
            priority=priority,
        )
        fib.save(priority=priority)
        handled_buses.add(physid)
        detected_fc_cards.add(fib.pk)
    dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
Ejemplo n.º 19
0
def _add_dev_cpu(ip, pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        DiscoveryWarning(
            message="Processor model unknown",
            plugin=__name__,
            device=parent,
            ip=ip,
        ).save()
        return
    counts.cpu += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.cpu
    cpu, created = Processor.concurrent_get_or_create(
        device=parent,
        index=index,
    )
    cpu.label = pairs['Mach type/model']
    family = pairs['Processor family']
    if family.startswith('Intel '):
        family = cpu.label[len('Intel '):]
    speed = int(float(pairs['Speed'].replace('GHz', '')) * 1000)
    cores = int(pairs['Processor cores'])
    cpu.model, c = ComponentModel.create(
        ComponentType.processor,
        speed=speed,
        cores=cores,
        name='CPU %s %d MHz, %s-core' % (family, speed, cores),
        family=family,
        priority=SAVE_PRIORITY,
    )
    cpu.save(priority=SAVE_PRIORITY)
Ejemplo n.º 20
0
def _save_shares(dev, volumes):
    wwns = []
    for (label, serial, size, volume_type, speed) in volumes:
        wwn = normalize_wwn(serial)
        wwns.append(wwn)
        model, created = ComponentModel.create(
            ComponentType.share,
            speed=speed,
            family=volume_type,
            name='MSA %s disk share' % volume_type,
            priority=0,  # FIXME: why 0?
        )
        share, created = DiskShare.concurrent_get_or_create(
            wwn=wwn,
            defaults={'device': dev},
        )
        if not created:
            if dev.id != share.device.id:
                raise ValueError(
                    'DiskShare %r: Conflict of devices %r and %r!' % (
                        share,
                        dev,
                        share.device,
                    )
                )
        share.model = model
        share.label = label
        share.size = size * 512 / 1024 / 1024
        share.snapshot_size = 0
        share.save()
    dev.diskshare_set.exclude(wwn__in=wwns).delete()
Ejemplo n.º 21
0
def handle_lshw_fibre_cards(dev, lshw, is_virtual=False, priority=0):
    buses = []
    for bus in jpath.get_all('..bus', lshw):
        if not bus:
            continue
        if isinstance(bus, list):
            buses.extend(bus)
        else:
            buses.append(bus)
    buses = filter(lambda item: item['id'].startswith('fiber'), buses)
    buses.sort(key=lambda item: item['handle'])
    handled_buses = set()
    detected_fc_cards = set()
    for bus in buses:
        handle = unicode(bus['handle'])
        m = re.search(r"([1-9][0-9]*)", handle)
        if not m:
            continue
        physid = m.group(1)
        if physid in handled_buses:
            continue
        fib, created = FibreChannel.concurrent_get_or_create(
            device=dev, physical_id=physid)
        fib.label = "{} {}".format(bus['vendor'], bus['product'])
        fib.model, c = ComponentModel.create(
            ComponentType.fibre,
            family=bus['vendor'],
            name=bus['product'],
            priority=priority,
        )
        fib.save(priority=priority)
        handled_buses.add(physid)
        detected_fc_cards.add(fib.pk)
    dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
Ejemplo n.º 22
0
def handle_smartctl(dev, disks, priority=0):
    for disk_handle, disk in disks.iteritems():
        if not disk.get("serial_number") or disk.get("device_type") != "disk":
            continue
        if {"user_capacity", "vendor", "product", "transport_protocol"} - set(disk.keys()):
            # not all required keys present
            continue
        if disk["vendor"].lower() in DISK_VENDOR_BLACKLIST:
            continue
        if disk["product"].lower() in DISK_PRODUCT_BLACKLIST:
            continue
        stor, created = Storage.concurrent_get_or_create(device=dev, sn=disk["serial_number"], mount_point=None)
        stor.device = dev
        size_value, size_unit, rest = disk["user_capacity"].split(" ", 2)
        size_value = size_value.replace(",", "")
        stor.size = int(int(size_value) / units.size_divisor[size_unit])
        stor.speed = int(disk.get("rotational_speed", 0))
        label_meta = [" ".join(disk["vendor"].split()), disk["product"]]
        if "transport_protocol" in disk:
            label_meta.append(disk["transport_protocol"])
        stor.label = " ".join(label_meta)
        disk_default = dict(
            vendor="unknown",
            product="unknown",
            revision="unknown",
            transport_protocol="unknown",
            user_capacity="unknown",
        )
        disk_default.update(disk)
        stor.model, c = ComponentModel.create(
            ComponentType.disk, size=stor.size, speed=stor.speed, family=stor.label, priority=priority
        )
        stor.save(priority=priority)
Ejemplo n.º 23
0
def cisco_component(dev, inv, ip_address=None):
    comp_type = cisco_type(inv['pid'])
    name = inv['descr']
    if not name.lower().startswith('cisco'):
        name = 'Cisco %s' % name
    model, created = ComponentModel.create(
        comp_type,
        family=inv['pid'],
        name=name,
        priority=0,   # FIXME: why 0?
    )
    comp, created = GenericComponent.concurrent_get_or_create(
        sn=inv['sn'],
        defaults={
            'device': dev,
        },
    )
    if comp.device == dev:
        comp.model = model
        comp.label = inv['name'][:255]
        comp.save()
    elif ip_address:
        DiscoveryWarning(
            message="GenericComponent(id={}) belongs to Device(id={})".format(
                comp.id,
                comp.device.id,
            ),
            plugin=__name__,
            device=dev,
            ip=ip_address,
        ).save()
        comp = None
    else:
        comp = None
    return comp
Ejemplo n.º 24
0
def handle_dmidecode(info, ethernets=(), priority=0):
    """Take the data collected by parse_dmidecode and apply it to a device."""

    # It's either a rack or a blade server, who knows?
    # We will let other plugins determine that.
    dev = Device.create(
        ethernets=ethernets,
        sn=info['sn'],
        uuid=info['uuid'],
        model_name='DMI ' + info['model'],
        model_type=DeviceType.unknown,
        priority=priority,
    )
    for i, cpu_info in enumerate(info['cpu']):
        model, created = ComponentModel.create(
            ComponentType.processor,
            speed=cpu_info['speed'] or 0,
            cores=cpu_info['cores'] or 0,
            family=cpu_info['family'],
            name=cpu_info['model'],
            priority=priority,
        )
        cpu, created = Processor.concurrent_get_or_create(device=dev,
                                                          index=i + 1)
        if created:
            cpu.label = cpu_info['label']
            cpu.model = model
            cpu.save()
    for cpu in dev.processor_set.filter(index__gt=i + 1):
        cpu.delete()
    for i, mem_info in enumerate(info['mem']):
        model, created = ComponentModel.create(
            ComponentType.memory,
            speed=mem_info['speed'] or 0,
            size=mem_info['size'] or 0,
            family=mem_info['type'],
            priority=priority,
        )
        mem, created = Memory.concurrent_get_or_create(device=dev, index=i + 1)
        if created:
            mem.label = mem_info['label']
            mem.model = model
            mem.save()
    for mem in dev.memory_set.filter(index__gt=i + 1):
        mem.delete()
    return dev
Ejemplo n.º 25
0
def handle_dmidecode(info, ethernets=(), priority=0):
    """Take the data collected by parse_dmidecode and apply it to a device."""

    # It's either a rack or a blade server, who knows?
    # We will let other plugins determine that.
    dev = Device.create(
        ethernets=ethernets,
        sn=info["sn"],
        uuid=info["uuid"],
        model_name="DMI " + info["model"],
        model_type=DeviceType.unknown,
        priority=priority,
    )
    for i, cpu_info in enumerate(info["cpu"]):
        model, created = ComponentModel.create(
            ComponentType.processor,
            speed=cpu_info["speed"] or 0,
            cores=cpu_info["cores"] or 0,
            family=cpu_info["family"],
            name=cpu_info["model"],
            priority=priority,
        )
        cpu, created = Processor.concurrent_get_or_create(device=dev, index=i + 1)
        if created:
            cpu.label = cpu_info["label"]
            cpu.model = model
            cpu.save()
    for cpu in dev.processor_set.filter(index__gt=i + 1):
        cpu.delete()
    for i, mem_info in enumerate(info["mem"]):
        model, created = ComponentModel.create(
            ComponentType.memory,
            speed=mem_info["speed"] or 0,
            size=mem_info["size"] or 0,
            family=mem_info["type"],
            priority=priority,
        )
        mem, created = Memory.concurrent_get_or_create(device=dev, index=i + 1)
        if created:
            mem.label = mem_info["label"]
            mem.model = model
            mem.save()
    for mem in dev.memory_set.filter(index__gt=i + 1):
        mem.delete()
    return dev
Ejemplo n.º 26
0
def _component(model_type, pairs, parent, raw):
    model_type = ComponentType.unknown
    if "Mach type/model" in pairs:
        model_name = "%s (%s)" % (pairs["Mach type/model"], pairs["Part no."])
    elif "Part no." in pairs:
        model_name = pairs["Part no."]
    else:
        raise DeviceError("No model/part no.")
    if not model_name.startswith("IBM"):
        model_name = "IBM " + model_name
    sn = pairs.get("Mach serial number")
    if sn in SERIAL_BLACKLIST:
        sn = None
    if not sn:
        sn = pairs["FRU serial no."]
    if sn in SERIAL_BLACKLIST:
        sn = None
    if not sn:
        raise DeviceError("No serial no.")
    name = pairs.get("Name") or pairs.get("Product Name") or model_name
    if "Management" in model_name:
        model_type = ComponentType.management
    elif "Fibre Channel" in model_name:
        # FIXME: merge GenericComponent(model__type=fibre) and FibreChannel
        model_type = ComponentType.fibre
        return None
    elif "Power" in model_name:
        model_type = ComponentType.power
    elif "Media" in model_name:
        model_type = ComponentType.media
    elif "Cooling" in model_name:
        model_type = ComponentType.cooling
    elif "Fan" in model_name:
        model_type = ComponentType.cooling
    model, mcreated = ComponentModel.create(model_type, family=model_name, name=model_name, priority=SAVE_PRIORITY)
    component, created = GenericComponent.concurrent_get_or_create(sn=sn, defaults=dict(device=parent))
    component.model = model
    component.label = name
    firmware = pairs.get("AMM firmware") or pairs.get("FW/BIOS") or pairs.get("Main Application 2")
    if firmware:
        component.hard_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"])
    else:
        firmware = pairs.get("Power Module Cooling Device firmware rev.")
        if firmware:
            component.hard_firmware = "rev %s" % firmware
    firmware = pairs.get("Boot ROM") or pairs.get("Main Application 1") or pairs.get("Blade Sys Mgmt Processor")
    if firmware:
        component.boot_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"])
    firmware = pairs.get("Blade Sys Mgmt Processor")
    if firmware:
        component.mgmt_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"])
    firmware = pairs.get("Diagnostics")
    if firmware:
        component.diag_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"])
    component.save(update_last_seen=True, priority=SAVE_PRIORITY)
    return component
Ejemplo n.º 27
0
def save_storage(storage, dev):
    mount_points = []
    for item in storage:
        sn = item.get('sn')
        mount_point = item.get('mountpoint')
        if not sn or not mount_point:
            continue
        label = item.get('label')
        try:
            size = int(item.get('size'))
        except ValueError:
            continue
        model_name = '{} {}MiB'.format(label, size)
        model, c = ComponentModel.create(
            ComponentType.disk,
            size=size,
            family=model_name,
            priority=SAVE_PRIORITY,
        )
        stor = None
        try:
            stor = Storage.objects.get(device=dev, mount_point=mount_point)
            if stor.sn != sn:
                try:
                    stor_found_by_sn = Storage.objects.get(sn=sn)
                    if all((stor_found_by_sn.model == model,
                            stor_found_by_sn.size == size,
                            stor_found_by_sn.label == label)):
                        stor.mount_point = None
                        stor.save(priotity=SAVE_PRIORITY)
                        stor = stor_found_by_sn
                        stor.device = dev
                        stor.mount_point = mount_point
                    else:
                        stor = None
                except Storage.DoesNotExist:
                    stor.sn = sn
        except Storage.DoesNotExist:
            try:
                stor = Storage.objects.get(sn=sn)
                if all((stor.model == model, stor.size == size,
                        stor.label == label)):
                    stor.device = dev
                    stor.mount_point = mount_point
                else:
                    stor = None
            except Storage.DoesNotExist:
                stor = Storage(device=dev, mount_point=mount_point, sn=sn)
        if stor:
            stor.model = model
            stor.label = label
            stor.size = size
            stor.save(priority=SAVE_PRIORITY)
        mount_points.append(mount_point)
    dev.storage_set.exclude(mount_point__in=mount_points).update(
        mount_point=None)
Ejemplo n.º 28
0
def save_storage(storage, dev):
    mount_points = []
    for item in storage:
        sn = item.get("sn")
        mount_point = item.get("mountpoint")
        if not sn or not mount_point:
            continue
        label = item.get("label")
        try:
            size = int(item.get("size"))
        except ValueError:
            continue
        model_name = "{} {}MiB".format(label, size)
        model, c = ComponentModel.create(ComponentType.disk, size=size, family=model_name, priority=SAVE_PRIORITY)
        stor = None
        try:
            stor = Storage.objects.get(device=dev, mount_point=mount_point)
            if stor.sn != sn:
                try:
                    stor_found_by_sn = Storage.objects.get(sn=sn)
                    if all(
                        (
                            stor_found_by_sn.model == model,
                            stor_found_by_sn.size == size,
                            stor_found_by_sn.label == label,
                        )
                    ):
                        stor.mount_point = None
                        stor.save(priotity=SAVE_PRIORITY)
                        stor = stor_found_by_sn
                        stor.device = dev
                        stor.mount_point = mount_point
                    else:
                        stor = None
                except Storage.DoesNotExist:
                    stor.sn = sn
        except Storage.DoesNotExist:
            try:
                stor = Storage.objects.get(sn=sn)
                if all((stor.model == model, stor.size == size, stor.label == label)):
                    stor.device = dev
                    stor.mount_point = mount_point
                else:
                    stor = None
            except Storage.DoesNotExist:
                stor = Storage(device=dev, mount_point=mount_point, sn=sn)
        if stor:
            stor.model = model
            stor.label = label
            stor.size = size
            stor.save(priority=SAVE_PRIORITY)
        mount_points.append(mount_point)
    dev.storage_set.exclude(mount_point__in=mount_points).update(mount_point=None)
Ejemplo n.º 29
0
def handle_facts_disks(dev, facts, is_virtual=False):
    disks = {}
    _cur_key = None
    for k, v in facts.iteritems():
        if not k.startswith('disk_'):
            continue
        k = k[5:]
        if k.endswith('_product'):
            _cur_key = 'product'
            k = k[:-8]
        elif k.endswith('_revision'):
            _cur_key = 'revision'
            k = k[:-9]
        elif k.endswith('_size'):
            _cur_key = 'size'
            k = k[:-5]
        elif k.endswith('_vendor'):
            _cur_key = 'vendor'
            k = k[:-7]
        elif k.endswith('_serial'):
            _cur_key = 'serial'
            k = k[:-7]
        else:
            continue
        disks.setdefault(k, {})[_cur_key] = v.strip()
    for label, disk in disks.iteritems():
        try:
            if 'size' not in disk or not int(disk['size']):
                continue
        except ValueError:
            continue
        if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST:
            continue
        if disk['product'].lower() in DISK_PRODUCT_BLACKLIST:
            continue
        sn = disk.get('serial', '').strip()
        stor, created = Storage.concurrent_get_or_create(
            device=dev,
            mount_point=label,
            sn=sn if sn else None,
        )
        stor.size = disk['size'] = int(int(disk['size']) / 1024 / 1024)
        stor.label = '{} {} {}'.format(disk['vendor'].strip(),
                                       disk['product'].strip(),
                                       disk['revision'].strip())
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            family=disk['vendor'].strip(),
            priority=SAVE_PRIORITY,
        )
        stor.save(priority=SAVE_PRIORITY)
Ejemplo n.º 30
0
def handle_facts_disks(dev, facts, is_virtual=False):
    disks = {}
    _cur_key = None
    for k, v in facts.iteritems():
        if not k.startswith('disk_'):
            continue
        k = k[5:]
        if k.endswith('_product'):
            _cur_key = 'product'
            k = k[:-8]
        elif k.endswith('_revision'):
            _cur_key = 'revision'
            k = k[:-9]
        elif k.endswith('_size'):
            _cur_key = 'size'
            k = k[:-5]
        elif k.endswith('_vendor'):
            _cur_key = 'vendor'
            k = k[:-7]
        elif k.endswith('_serial'):
            _cur_key = 'serial'
            k = k[:-7]
        else:
            continue
        disks.setdefault(k, {})[_cur_key] = v.strip()
    for label, disk in disks.iteritems():
        try:
            if 'size' not in disk or not int(disk['size']):
                continue
        except ValueError:
            continue
        if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST:
            continue
        if disk['product'].lower() in DISK_PRODUCT_BLACKLIST:
            continue
        sn = disk.get('serial', '').strip()
        stor, created = Storage.concurrent_get_or_create(
            device=dev,
            mount_point=label,
            sn=sn if sn else None,
        )
        stor.size = disk['size'] = int(int(disk['size']) / 1024 / 1024)
        stor.label = '{} {} {}'.format(
            disk['vendor'].strip(), disk['product'].strip(),
            disk['revision'].strip())
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            family=disk['vendor'].strip(),
            priority=SAVE_PRIORITY,
        )
        stor.save(priority=SAVE_PRIORITY)
Ejemplo n.º 31
0
def handle_lshw_storage(dev, lshw, is_virtual=False, priority=0):
    mount_points, storages = get_storage_from_lshw(lshw)
    dev.storage_set.filter(mount_point__in=mount_points).delete()
    for storage in storages:
        sn = storage["sn"] if storage["sn"] else None
        stor, created = Storage.concurrent_get_or_create(sn=sn, mount_point=storage["mount_point"], device=dev)
        stor.size = storage["size"]
        stor.speed = storage["speed"]
        stor.label = storage["label"]
        stor.model, c = ComponentModel.create(
            ComponentType.disk, size=stor.size, speed=stor.speed, family=stor.label, priority=priority
        )
        stor.save(priority=priority)
Ejemplo n.º 32
0
def save_fibre_channel(fcs, dev):
    detected_fc_cards = []
    for f in fcs:
        pid = f.get("physicalid")
        model = f.get("model")
        manufacturer = f.get("manufacturer")
        fib, created = FibreChannel.concurrent_get_or_create(device=dev, physical_id=pid)
        fib.label = f.get("label")
        fib.model, c = ComponentModel.create(
            ComponentType.fibre, family=manufacturer or fib.label, name=model or fib.label, priority=SAVE_PRIORITY
        )
        fib.save(priority=SAVE_PRIORITY)
        detected_fc_cards.append(fib.pk)
    dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
Ejemplo n.º 33
0
def splunk(**options):
    splunk = Splunk()
    splunk.start()
    percent = splunk.progress
    while percent < 100:
        if options['verbose']:
            print(percent)
        time.sleep(30)
        percent = splunk.progress
    hosts = {}
    total_mb = 0
    for item in splunk.results:
        host = item['host']
        mb = float(item['MBytes'])
        total_mb += mb
        if host in hosts:
            hosts[host] += mb
        else:
            hosts[host] = mb
    if options['verbose']:
        print(len(hosts), 'hosts used', total_mb, ' MiBs total.')
    for host, usage in hosts.iteritems():
        ip = IPAddress.objects.filter(hostname__startswith=host).order_by(
            '-last_seen')
        if not ip.count():
            if options['verbose']:
                print('Warning: host', host, 'not found in device database.')
            continue
        dev = ip[0].device
        if not dev:
            if options['verbose']:
                print('Warning: host', host, 'not tied to a device in the '
                    'database.')
            continue
        name = 'Splunk Volume 100 GiB'
        symbol = 'splunkvolume'
        model, created = ComponentModel.create(
            ComponentType.unknown,
            family=symbol,
            name=name,
            priority=0,
        )
        res, created = SplunkUsage.concurrent_get_or_create(
            device=dev,
            day=date.today(),
            defaults={'model': model},
        )
        res.size = usage
        res.save()
    return True, 'done.', options
Ejemplo n.º 34
0
def splunk(**options):
    splunk = Splunk()
    splunk.start()
    percent = splunk.progress
    while percent < 100:
        if options['verbose']:
            print(percent)
        time.sleep(30)
        percent = splunk.progress
    hosts = {}
    total_mb = 0
    for item in splunk.results:
        host = item['host']
        mb = float(item['MBytes'])
        total_mb += mb
        if host in hosts:
            hosts[host] += mb
        else:
            hosts[host] = mb
    if options['verbose']:
        print(len(hosts), 'hosts used', total_mb, ' MiBs total.')
    for host, usage in hosts.iteritems():
        ip = IPAddress.objects.filter(
            hostname__startswith=host).order_by('-last_seen')
        if not ip.count():
            if options['verbose']:
                print('Warning: host', host, 'not found in device database.')
            continue
        dev = ip[0].device
        if not dev:
            if options['verbose']:
                print('Warning: host', host, 'not tied to a device in the '
                      'database.')
            continue
        name = 'Splunk Volume 100 GiB'
        symbol = 'splunkvolume'
        model, created = ComponentModel.create(
            ComponentType.unknown,
            family=symbol,
            name=name,
            priority=0,
        )
        res, created = SplunkUsage.concurrent_get_or_create(
            device=dev,
            day=date.today(),
            defaults={'model': model},
        )
        res.size = usage
        res.save()
    return True, 'done.', options
Ejemplo n.º 35
0
def handle_megaraid(dev, disks, priority=0):
    for (controller_handle, disk_handle), disk in disks.iteritems():
        disk['vendor'], disk['product'], disk['serial_number'] = \
            _handle_inquiry_data(
                disk.get('inquiry_data', ''),
                controller_handle, disk_handle)

        if not disk.get('serial_number') or disk.get('media_type') not in (
                'Hard Disk Device', 'Solid State Device'):
            continue
        if {'coerced_size', 'vendor', 'product', 'pd_type'} - \
                set(disk.keys()):
            # not all required keys present
            continue
        if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST:
            continue
        if disk['product'].lower() in DISK_PRODUCT_BLACKLIST:
            continue
        stor, created = Storage.concurrent_get_or_create(
            device=dev,
            sn=disk['serial_number'],
            mount_point=None,
        )
        stor.device = dev
        size_value, size_unit, rest = disk['coerced_size'].split(' ', 2)
        size_value = size_value.replace(',', '')
        stor.size = int(float(size_value) / units.size_divisor[size_unit])
        stor.speed = int(disk.get('rotational_speed', 0))
        label_meta = [' '.join(disk['vendor'].split()), disk['product']]
        if 'pd_type' in disk:
            label_meta.append(disk['pd_type'])
        stor.label = ' '.join(label_meta)
        disk_default = dict(
            vendor='unknown',
            product='unknown',
            device_firmware_level='unknown',
            pd_type='unknown',
            coerced_size='unknown',
        )
        disk_default.update(disk)
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            speed=stor.speed,
            family=stor.label,
            priority=priority,
        )
        stor.save(priority=priority)
Ejemplo n.º 36
0
def _save_fc_cards(dev, data):
    detected_fc_cards = []
    for card in data:
        model, _ = ComponentModel.create(
            ComponentType.fibre,
            family=card['label'],
            name=card['label'],
            priority=SAVE_PRIORITY,
        )
        fc, _ = FibreChannel.concurrent_get_or_create(
            device=dev, physical_id=card['physical_id'])
        fc.model = model
        fc.label = card['label']
        fc.save(priority=SAVE_PRIORITY)
        detected_fc_cards.append(fc.pk)
    dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
Ejemplo n.º 37
0
def handle_megaraid(dev, disks, priority=0):
    for (controller_handle, disk_handle), disk in disks.iteritems():
        disk['vendor'], disk['product'], disk['serial_number'] = \
            _handle_inquiry_data(
                disk.get('inquiry_data', ''),
                controller_handle, disk_handle
            )

        if not disk.get('serial_number') or disk.get('media_type') not in (
                'Hard Disk Device', 'Solid State Device'):
            continue
        if {'coerced_size', 'vendor', 'product', 'pd_type'} - \
                set(disk.keys()):
            # not all required keys present
            continue
        if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST:
            continue
        if disk['product'].lower() in DISK_PRODUCT_BLACKLIST:
            continue
        stor, created = Storage.concurrent_get_or_create(
            device=dev, sn=disk['serial_number'], mount_point=None,
        )
        stor.device = dev
        size_value, size_unit, rest = disk['coerced_size'].split(' ', 2)
        size_value = size_value.replace(',', '')
        stor.size = int(float(size_value) / units.size_divisor[size_unit])
        stor.speed = int(disk.get('rotational_speed', 0))
        label_meta = [' '.join(disk['vendor'].split()), disk['product']]
        if 'pd_type' in disk:
            label_meta.append(disk['pd_type'])
        stor.label = ' '.join(label_meta)
        disk_default = dict(
            vendor='unknown',
            product='unknown',
            device_firmware_level='unknown',
            pd_type='unknown',
            coerced_size='unknown',
        )
        disk_default.update(disk)
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            speed=stor.speed,
            family=stor.label,
            priority=priority,
        )
        stor.save(priority=priority)
Ejemplo n.º 38
0
def _save_fc_cards(dev, data):
    detected_fc_cards = []
    for card in data:
        model, _ = ComponentModel.create(
            ComponentType.fibre,
            family=card['label'],
            name=card['label'],
            priority=SAVE_PRIORITY,
        )
        fc, _ = FibreChannel.concurrent_get_or_create(
            device=dev, physical_id=card['physical_id']
        )
        fc.model = model
        fc.label = card['label']
        fc.save(priority=SAVE_PRIORITY)
        detected_fc_cards.append(fc.pk)
    dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
Ejemplo n.º 39
0
def _save_memory(dev, data):
    detected_memory = []
    for mem, index in zip(data, range(1, len(data) + 1)):
        model, _ = ComponentModel.create(
            ComponentType.memory,
            size=int(mem['size']),
            speed=int(mem['speed']),
            priority=SAVE_PRIORITY,
        )
        memory, _ = Memory.concurrent_get_or_create(index=index, device=dev)
        memory.label = "{} {}".format(mem['manufacturer'], mem['model'])
        memory.size = mem['size']
        memory.speed = mem['speed']
        memory.model = model
        memory.save(priority=SAVE_PRIORITY)
        detected_memory.append(memory.pk)
    dev.memory_set.exclude(pk__in=detected_memory).delete()
Ejemplo n.º 40
0
def _save_memory(dev, data):
    detected_memory = []
    for mem, index in zip(data, range(1, len(data) + 1)):
        model, _ = ComponentModel.create(
            ComponentType.memory,
            size=int(mem['size']),
            speed=int(mem['speed']),
            priority=SAVE_PRIORITY,
        )
        memory, _ = Memory.concurrent_get_or_create(index=index, device=dev)
        memory.label = "{} {}".format(mem['manufacturer'], mem['model'])
        memory.size = mem['size']
        memory.speed = mem['speed']
        memory.model = model
        memory.save(priority=SAVE_PRIORITY)
        detected_memory.append(memory.pk)
    dev.memory_set.exclude(pk__in=detected_memory).delete()
Ejemplo n.º 41
0
def save_fibre_channel(fcs, dev):
    detected_fc_cards = []
    for f in fcs:
        pid = f.get('physicalid')
        model = f.get('model')
        manufacturer = f.get('manufacturer')
        fib, created = FibreChannel.concurrent_get_or_create(device=dev,
                                                             physical_id=pid)
        fib.label = f.get('label')
        fib.model, c = ComponentModel.create(
            ComponentType.fibre,
            family=manufacturer or fib.label,
            name=model or fib.label,
            priority=SAVE_PRIORITY,
        )
        fib.save(priority=SAVE_PRIORITY)
        detected_fc_cards.append(fib.pk)
    dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
Ejemplo n.º 42
0
def handle_lshw_memory(dev, bus_memory, is_virtual=False, priority=0):
    memory_banks = []
    for _mem in bus_memory:
        # we're interested only in the system memory, not in caches etc.
        if _mem['id'] == 'memory':
            memory_banks = _mem['memory']
            break
        elif _mem['id'].startswith('memory:'):
            memory_banks.extend(_mem['memory'])
    index = 0
    if isinstance(memory_banks, dict):
        memory_banks = [memory_banks]
    detected_memory = {}
    for memory in memory_banks:
        if 'size' not in memory:
            # empty slot
            continue
        index += 1
        size = int(memory['size']['value'] or 0)
        size /= units.size_divisor[memory['size']['units']]
        size = int(size)
        label = memory['slot']
        family = 'Virtual' if is_virtual else ''
        model, created = ComponentModel.create(
            ComponentType.memory,
            family=family,
            size=size,
            priority=priority,
        )
        detected_memory[index] = label, model
    for mem in dev.memory_set.all():
        label, model = detected_memory.get(mem.index, (None, None))
        if mem.label != label or mem.model != model:
            mem.delete()
    for index, (label, model) in detected_memory.iteritems():
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        if created:
            mem.model = model
            mem.size = model.size
            mem.speed = model.speed
            mem.label = label
            mem.save(priority=priority)
Ejemplo n.º 43
0
def save_processors(processors, dev, is_virtual=False):
    indexes = []
    for p in processors:
        cpuname = p.get('label')
        if is_virtual and not cpuname.lower().startswith('virtual'):
            cpuname = "Virtual %s" % cpuname
        try:
            index = int(p.get('index')[3:]) + 1  # CPU0
            speed = int(p.get('speed'))
            cores = int(p.get('cores'))
        except ValueError:
            continue
        indexes.append(index)
        cpu, created = Processor.concurrent_get_or_create(
            device=dev,
            index=index,
            defaults={
                'label': cpuname,
                'speed': speed,
                'cores': cores,
            },
        )
        if not created:
            cpu.label = cpuname
            cpu.speed = speed
            cpu.cores = cores
        is64bit = p.get('is64bit') == 'true'
        name = 'CPU %s%s %s%s' % (
            '64bit ' if is64bit else '',
            cpuname, '%dMhz' % speed if speed else '',
            ' multicore' if cores > 1 else '')
        cpu.model, c = ComponentModel.create(
            ComponentType.processor,
            cores=cores,
            family=cpuname,
            speed=speed,
            name=name,
            priority=SAVE_PRIORITY,
        )
        cpu.save(priority=SAVE_PRIORITY)
    for cpu in dev.processor_set.exclude(index__in=indexes):
        cpu.delete()
Ejemplo n.º 44
0
def handle_lshw_memory(dev, bus_memory, is_virtual=False, priority=0):
    memory_banks = []
    for _mem in bus_memory:
        # we're interested only in the system memory, not in caches etc.
        if _mem['id'] == 'memory':
            memory_banks = _mem['memory']
            break
        elif _mem['id'].startswith('memory:'):
            memory_banks.extend(_mem['memory'])
    index = 0
    if isinstance(memory_banks, dict):
        memory_banks = [memory_banks]
    detected_memory = {}
    for memory in memory_banks:
        if 'size' not in memory:
            # empty slot
            continue
        index += 1
        size = int(memory['size']['value'] or 0)
        size /= units.size_divisor[memory['size']['units']]
        size = int(size)
        label = memory['slot']
        family = 'Virtual' if is_virtual else ''
        model, created = ComponentModel.create(
            ComponentType.memory,
            family=family,
            size=size,
            priority=priority,
        )
        detected_memory[index] = label, model
    for mem in dev.memory_set.all():
        label, model = detected_memory.get(mem.index, (None, None))
        if mem.label != label or mem.model != model:
            mem.delete()
    for index, (label, model) in detected_memory.iteritems():
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        if created:
            mem.model = model
            mem.size = model.size
            mem.speed = model.speed
            mem.label = label
            mem.save(priority=priority)
Ejemplo n.º 45
0
def handle_smartctl(dev, disks, priority=0):
    for disk_handle, disk in disks.iteritems():
        if not disk.get('serial_number') or disk.get('device_type') != 'disk':
            continue
        if {'user_capacity', 'vendor', 'product', 'transport_protocol'} - \
                set(disk.keys()):
            # not all required keys present
            continue
        if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST:
            continue
        if disk['product'].lower() in DISK_PRODUCT_BLACKLIST:
            continue
        stor, created = Storage.concurrent_get_or_create(
            device=dev,
            sn=disk['serial_number'],
            mount_point=None,
        )
        stor.device = dev
        size_value, size_unit, rest = disk['user_capacity'].split(' ', 2)
        size_value = size_value.replace(',', '')
        stor.size = int(int(size_value) / units.size_divisor[size_unit])
        stor.speed = int(disk.get('rotational_speed', 0))
        label_meta = [' '.join(disk['vendor'].split()), disk['product']]
        if 'transport_protocol' in disk:
            label_meta.append(disk['transport_protocol'])
        stor.label = ' '.join(label_meta)
        disk_default = dict(
            vendor='unknown',
            product='unknown',
            revision='unknown',
            transport_protocol='unknown',
            user_capacity='unknown',
        )
        disk_default.update(disk)
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            speed=stor.speed,
            family=stor.label,
            priority=priority,
        )
        stor.save(priority=priority)
Ejemplo n.º 46
0
def save_processors(processors, dev, is_virtual=False):
    indexes = []
    for p in processors:
        cpuname = p.get('label')
        if is_virtual and not cpuname.lower().startswith('virtual'):
            cpuname = "Virtual %s" % cpuname
        try:
            index = int(p.get('index')[3:]) + 1  # CPU0
            speed = int(p.get('speed'))
            cores = int(p.get('cores'))
        except ValueError:
            continue
        indexes.append(index)
        cpu, created = Processor.concurrent_get_or_create(
            device=dev,
            index=index,
            defaults={
                'label': cpuname,
                'speed': speed,
                'cores': cores,
            },
        )
        if not created:
            cpu.label = cpuname
            cpu.speed = speed
            cpu.cores = cores
        is64bit = p.get('is64bit') == 'true'
        name = 'CPU %s%s %s%s' % (
            '64bit ' if is64bit else '',
            cpuname, '%dMhz' % speed if speed else '',
            ' multicore' if cores > 1 else '')
        cpu.model, c = ComponentModel.create(
            ComponentType.processor,
            cores=cores,
            family=cpuname,
            speed=speed,
            name=name,
            priority=SAVE_PRIORITY,
        )
        cpu.save(priority=SAVE_PRIORITY)
    for cpu in dev.processor_set.exclude(index__in=indexes):
        cpu.delete()
Ejemplo n.º 47
0
def handle_3ware(dev, disks, priority=0):
    for disk_handle, disk in disks.iteritems():
        if not disk.get("serial"):
            continue
        stor, created = Storage.concurrent_get_or_create(device=dev, sn=disk["serial"], mount_point=None)
        stor.device = dev
        size_value, size_unit, trash = disk["capacity"].split(None, 2)
        stor.size = int(float(size_value) / units.size_divisor[size_unit])
        stor.label = disk["model"]
        disk_default = dict(
            model="unknown",
            firmware_revision="unknown",
            interface_type="unknown",
            size="unknown",
            rotational_speed="unknown",
            status="unknown",
        )
        disk_default.update(disk)
        stor.model, c = ComponentModel.create(ComponentType.disk, size=stor.size, family=stor.label, priority=priority)
        stor.save(priority=priority)
Ejemplo n.º 48
0
def handle_lshw_storage(dev, lshw, is_virtual=False, priority=0):
    mount_points, storages = get_storage_from_lshw(lshw)
    dev.storage_set.filter(mount_point__in=mount_points).delete()
    for storage in storages:
        sn = storage['sn'] if storage['sn'] else None
        stor, created = Storage.concurrent_get_or_create(
            sn=sn,
            mount_point=storage['mount_point'],
            device=dev,
        )
        stor.size = storage['size']
        stor.speed = storage['speed']
        stor.label = storage['label']
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            speed=stor.speed,
            family=stor.label,
            priority=priority,
        )
        stor.save(priority=priority)
Ejemplo n.º 49
0
def handle_smartctl(dev, disks, priority=0):
    for disk_handle, disk in disks.iteritems():
        if not disk.get('serial_number') or disk.get('device_type') != 'disk':
            continue
        if {'user_capacity', 'vendor', 'product', 'transport_protocol'} - \
                set(disk.keys()):
            # not all required keys present
            continue
        if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST:
            continue
        if disk['product'].lower() in DISK_PRODUCT_BLACKLIST:
            continue
        stor, created = Storage.concurrent_get_or_create(
            device=dev, sn=disk['serial_number'], mount_point=None,
        )
        stor.device = dev
        size_value, size_unit, rest = disk['user_capacity'].split(' ', 2)
        size_value = size_value.replace(',', '')
        stor.size = int(int(size_value) / units.size_divisor[size_unit])
        stor.speed = int(disk.get('rotational_speed', 0))
        label_meta = [' '.join(disk['vendor'].split()), disk['product']]
        if 'transport_protocol' in disk:
            label_meta.append(disk['transport_protocol'])
        stor.label = ' '.join(label_meta)
        disk_default = dict(
            vendor='unknown',
            product='unknown',
            revision='unknown',
            transport_protocol='unknown',
            user_capacity='unknown',
        )
        disk_default.update(disk)
        stor.model, c = ComponentModel.create(
            ComponentType.disk,
            size=stor.size,
            speed=stor.speed,
            family=stor.label,
            priority=priority,
        )
        stor.save(priority=priority)
Ejemplo n.º 50
0
def _save_storage(dev, data):
    detected_storage = []
    for disk in data:
        model_name = "{} {}".format(disk['manufacturer'], disk['model'])
        size = int(int(disk['size']) / 1024 / 1024 / 1024)
        model, _ = ComponentModel.create(
            ComponentType.disk,
            size=size,
            family=model_name,
            priority=SAVE_PRIORITY,
        )
        storage, _ = Storage.concurrent_get_or_create(
            device=dev,
            sn=disk['sn'],
            mount_point=None,
        )
        storage.model = model
        storage.label = model.name
        storage.size = size
        storage.save(priority=SAVE_PRIORITY)
        detected_storage.append(storage.pk)
    dev.storage_set.exclude(pk__in=detected_storage).delete()
Ejemplo n.º 51
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get("size"))
            speed = int(row.get("speed")) if row.get("speed") else 0
        except ValueError:
            pass
        label = row.get("label")
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = "RAM %dMiB" % size
        mem.speed = speed
        family = "Virtual Windows" if "Virtual" in label else "Windows"
        mem.model, c = ComponentModel.create(
            ComponentType.memory, family=family, size=size, speed=speed, priority=SAVE_PRIORITY
        )
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Ejemplo n.º 52
0
def handle_lshw_processors(dev, processors, is_virtual=False, priority=0):
    if isinstance(processors, dict):
        processors = [processors]
    detected_cpus = {}
    family = None
    for processor in processors:
        family = processor['version'] or (
            'Virtual CPU' if is_virtual else processor['product']
        )
        if family:
            break
    if not family:
        return   # skip CPU changes if we cannot determine family
    for i, processor in enumerate(processors):
        if processor['disabled'] == 'true' or not processor['size']:
            continue
        label = 'CPU {}'.format(i + 1)
        speed = int(processor['size']['value'] or 0)   # 'size', sic!
        speed /= units.speed_divisor[processor['size']['units']]
        speed = int(speed)
        model, c = ComponentModel.create(
            ComponentType.processor,
            speed=speed,
            family=family,
            name=processor['product'] or 'CPU {} {}MHz'.format(family, speed),
            priority=priority
        )
        detected_cpus[i + 1] = label, model
    for cpu in dev.processor_set.all():
        label, model = detected_cpus.get(cpu.index, (None, None))
        if cpu.label != label or cpu.model != model:
            cpu.delete()
    for index, (label, model) in detected_cpus.iteritems():
        cpu, created = Processor.concurrent_get_or_create(
            device=dev, index=index)
        cpu.label = label
        cpu.model = model
        cpu.save(priority=priority)
Ejemplo n.º 53
0
    def make_component(name, symbol, key, multiplier, unit):
        if key not in tenant:
            return
        model, created = ComponentModel.create(
            ComponentType.unknown,
            family=symbol,
            name=name,
            priority=0,
        )
        res, created = GenericComponent.concurrent_get_or_create(
            sn='%s-%s-%s' % (symbol, tenant['tenant_id'], region),
            defaults=dict(
                model=model,
                device=dev,
            ),
        )

        if created:
            res.label = unit
        res.save()
        if model.group and model.group.price:
            value = tenant[key] / multiplier
            cost = value * model.group.price / (model.group.size_modifier or 1)
            total_daily_cost[0] += cost
Ejemplo n.º 54
0
def _save_cpu(dev, data):
    detected_cpus = []
    for cpu in data:
        try:
            index = int(cpu['socket'].split('.')[-1])
        except (ValueError, IndexError):
            continue
        model, _ = ComponentModel.create(
            ComponentType.processor,
            speed=cpu['speed'],
            family=cpu['family'],
            cores=cpu['cores_count'],
            name=cpu['model'],
            priority=SAVE_PRIORITY,
        )
        processor, _ = Processor.concurrent_get_or_create(device=dev,
                                                          index=index)
        processor.label = cpu['model']
        processor.model = model
        processor.speed = cpu['speed']
        processor.cores = cpu['cores_count']
        processor.save(priority=SAVE_PRIORITY)
        detected_cpus.append(processor.pk)
    dev.processor_set.exclude(pk__in=detected_cpus).delete()
Ejemplo n.º 55
0
def cisco_component(dev, inv, ip_address=None):
    comp_type = cisco_type(inv['pid'])
    name = inv['descr']
    if not name.lower().startswith('cisco'):
        name = 'Cisco %s' % name
    model, created = ComponentModel.create(
        comp_type,
        family=inv['pid'],
        name=name,
        priority=0,   # FIXME: why 0?
    )
    comp, created = GenericComponent.concurrent_get_or_create(
        sn=inv['sn'],
        defaults={
            'device': dev,
        },
    )
    if comp.device == dev:
        comp.model = model
        comp.label = inv['name'][:255]
        comp.save()
    else:
        comp = None
    return comp
Ejemplo n.º 56
0
def handle_smbios(dev, smbios, is_virtual=False, priority=0):
    # memory
    for memory in smbios.get('MEMDEVICE', ()):
        try:
            size, size_units = memory.get('Size', '').split(' ', 1)
            size = int(size)
            size /= units.size_divisor[size_units]
            size = int(size)
        except ValueError:
            continue  # empty slot
        for split_key in ('BANK', 'Slot '):
            try:
                bank = memory.get('Bank Locator').split(split_key)[1]
                bank = int(bank) + 1
                break
            except (IndexError, ValueError):
                bank = None  # unknown bank
        if bank is None:
            continue
        mem, created = Memory.concurrent_get_or_create(device=dev, index=bank)
        if created:
            mem.speed = 0
        mem.label = "{} {}".format(
            memory.get('Device Locator', memory.get('Location Tag', 'DIMM')),
            memory.get('Part Number', '')
        )
        mem.size = size
        manufacturer = memory.get('Manufacturer', 'Manufacturer')
        if not manufacturer.startswith('Manufacturer'):
            mem.label = manufacturer + ' ' + mem.label
        family = 'Virtual' if is_virtual else ''
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            size=mem.size,
            family=family,
            priority=priority,
        )
    # CPUs
    detected_cpus = {}
    for cpu in smbios.get('PROCESSOR', ()):
        m = DENSE_SPEED_REGEX.match(cpu.get('Maximum Speed', ''))
        if not m:
            continue
        if 'enabled' not in cpu.get('Processor Status', ''):
            continue
        speed = int(m.group(1))
        speed_units = m.group(2)
        speed /= units.speed_divisor[speed_units]
        speed = int(speed)
        label = cpu['Location Tag']
        family = cpu['Family']
        if '(other)' in family:
            family = cpu['Manufacturer']
        index_parts = []
        for cpu_part in cpu['Location Tag'].replace('CPU', '').split():
            try:
                index_parts.append(int(cpu_part.strip()))
            except ValueError:
                continue
        index = reduce(lambda x, y: x * y, index_parts)
        model, c = ComponentModel.create(
            ComponentType.processor,
            family=family,
            speed=speed,
            name=" ".join(cpu.get('Version', family).split()),
            priority=priority,
        )
        detected_cpus[index] = label, model
    for cpu in dev.processor_set.all():
        label, model = detected_cpus.get(cpu.index, (None, None))
        if cpu.label != label or cpu.model != model:
            cpu.delete()
    for index, (label, model) in detected_cpus.iteritems():
        cpu, created = Processor.concurrent_get_or_create(
            device=dev, index=index)
        cpu.label = label
        cpu.model = model
        cpu.save(priority=priority)
Ejemplo n.º 57
0
 def _prepare_storage_special_cases(self):
     self.special_dev = Device.create(sn='sn_123_321_123_321',
                                      model_name='SomeDeviceModelName',
                                      model_type=DeviceType.unknown)
     self.temp_dev = Device.create(sn='sn_999_888_777',
                                   model_name='SomeDeviceModelName',
                                   model_type=DeviceType.unknown)
     model_name = 'TestStorage 40960MiB'
     model, _ = ComponentModel.create(
         type=ComponentType.disk,
         priority=0,
         size=40960,
         family=model_name,
     )
     storage, _ = Storage.concurrent_get_or_create(
         device=self.special_dev,
         mount_point='C:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 40960,
         },
     )
     storage, _ = Storage.concurrent_get_or_create(
         device=self.special_dev,
         mount_point='D:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 40960,
             'sn': 'stor_sn_123_321_2',
         },
     )
     storage, _ = Storage.concurrent_get_or_create(
         device=self.special_dev,
         mount_point='E:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 40960,
             'sn': 'stor_sn_123_321_3',
         },
     )
     storage, _ = Storage.concurrent_get_or_create(
         device=self.temp_dev,
         mount_point='G:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 40960,
             'sn': 'stor_sn_123_321_5',
         },
     )
     storage, _ = Storage.concurrent_get_or_create(
         device=self.special_dev,
         mount_point='H:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 40960,
             'sn': 'stor_sn_123_321_6',
         },
     )
     storage, _ = Storage.concurrent_get_or_create(
         device=self.special_dev,
         mount_point='X:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 40960,
             'sn': 'stor_sn_123_321_7',
         },
     )
     # FIXME: this assigns a 40GB model to a 80GB device. How to handles
     # cases like this?
     storage, _ = Storage.concurrent_get_or_create(
         device=self.special_dev,
         mount_point='I:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 81920,
             'sn': 'stor_sn_123_321_8',
         },
     )
     storage, _ = Storage.concurrent_get_or_create(
         device=self.special_dev,
         mount_point='Y:',
         defaults={
             'label': 'TestStorage',
             'model': model,
             'size': 40960,
             'sn': 'stor_sn_123_321_9',
         },
     )
     storage, _ = Storage.concurrent_get_or_create(
         sn='stor_sn_123_321_10',
         defaults={
             'device': self.special_dev,
             'label': 'TestStorage',
             'model': model,
             'size': 81920,
         },
     )
     save_storage([{
         'sn': 'stor_sn_123_321_1',
         'mountpoint': 'C:',
         'label': 'TestStorage',
         'size': 40960,
     }, {
         'sn': 'stor_sn_123_321_2',
         'mountpoint': 'D:',
         'label': 'TestStorage',
         'size': 40960,
     }, {
         'sn': 'stor_sn_123_321_4',
         'mountpoint': 'F:',
         'label': 'TestStorage',
         'size': 40960
     }, {
         'sn': 'stor_sn_123_321_5',
         'mountpoint': 'G:',
         'label': 'TestStorage',
         'size': 40960
     }, {
         'sn': 'stor_sn_123_321_7',
         'mountpoint': 'H:',
         'label': 'TestStorage',
         'size': 40960
     }, {
         'sn': 'stor_sn_123_321_9',
         'mountpoint': 'I:',
         'label': 'TestStorage',
         'size': 81920
     }, {
         'sn': 'stor_sn_123_321_10',
         'mountpoint': 'J:',
         'label': 'TestStorage',
         'size': 40960
     }], self.special_dev)
Ejemplo n.º 58
0
def _add_ipmi_components(device, data):
    # CPUs
    cpu_index = 0
    total_mem_index = 0
    while True:
        cpu = data['MB/P{}'.format(cpu_index)]
        if not cpu:
            break
        if not cpu['Product Name']:
            cpu_index += 1
            continue
        proc, _ = Processor.concurrent_get_or_create(index=cpu_index + 1,
                                                     device=device)
        proc.label = re.sub(' +', ' ', cpu['Product Name']).title()
        speed_match = re.search(r'(\d+\.\d+)GHZ', cpu['Product Name'])
        if speed_match:
            speed = int(float(speed_match.group(1)) * 1000)
        else:
            speed = 0
        cores = 0
        proc.model, c = ComponentModel.create(
            ComponentType.processor,
            family=proc.label,
            speed=speed,
            cores=cores,
            name='CPU %s %dMHz %d-core' % (
                proc.label,
                speed,
                cores,
            ),
            priority=0,  # FIXME: why 0?
        )
        proc.save()
        # Memory
        mem_index = 0
        while True:
            memory = data['MB/P{}/D{}'.format(cpu_index, mem_index)]
            if not memory:
                break
            if not memory['Product Name']:
                mem_index += 1
                total_mem_index += 1
                continue
            size_match = re.search(r'(\d+)GB', memory['Product Name'])
            if not size_match:
                mem_index += 1
                total_mem_index += 1
                continue
            mem, _ = Memory.concurrent_get_or_create(index=total_mem_index + 1,
                                                     device=device)
            mem.label = memory['Product Name']
            size = int(size_match.group(1)) * 1024
            mem.model, c = ComponentModel.create(
                ComponentType.memory,
                size=size,
                priority=0,  # FIXME: why 0?
            )
            mem.save()
            mem_index += 1
            total_mem_index += 1
        cpu_index += 1