Пример #1
0
 def test_processors(self):
     model = ComponentModel(type=ComponentType.processor, name="CPU Xeon 2533MHz, 4-core")
     model.save()
     for i in xrange(4):
         p = Processor(label="ziew", model=model, device=self.device, index=i)
         p.save()
     data = get_device_data(Device.objects.get(sn="123456789"))
     processors = data["processors"]
     self.assertEqual(len(processors), 4)
     self.assertEqual(processors[0]["label"], "ziew")
     self.assertEqual(processors[0]["model_name"], "CPU Xeon 2533MHz, 4-core")
     self.assertEqual(processors[0]["cores"], 4)
     self.assertEqual(processors[3]["index"], 3)
Пример #2
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()
Пример #3
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)
Пример #4
0
def save_processors(processors, dev):
    indexes = []
    for p in processors:
        cpuname = p.get('label')
        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)
        cpu.label = cpuname
        cpu.speed = speed
        cpu.cores = cores
        is64bit = p.get('is64bit') == 'true'
        extra = '%s %s %s ' % (p.get('manufacturer'),
            p.get('version'), '64bit' if is64bit else '')
        name = 'CPU %s%s %s %s' % (
            '64bit ' if is64bit else '',
            cpuname, '%dMhz' % speed if speed else '',
            'multicore' if cores else '')
        cpu.model, c = ComponentModel.concurrent_get_or_create(
            speed=speed, type=ComponentType.processor.id,
            family=cpuname,
            cores=cores, extra_hash=hashlib.md5(extra).hexdigest())
        cpu.model.extra = extra
        cpu.model.name = name
        cpu.model.save(priority=SAVE_PRIORITY)
        cpu.save(priority=SAVE_PRIORITY)
    for cpu in dev.processor_set.exclude(index__in=indexes):
        cpu.delete()
Пример #5
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.concurrent_get_or_create(
            size=mem.size, speed=mem.speed, type=ComponentType.memory.id,
            family='', extra_hash='')
        if c:
            mem.model.name = 'RAM %dMiB, %dMHz' % (mem.size, mem.speed)
            mem.model.save()
        mem.save()

    for i, (label, speed, cores, extra, family) in enumerate(ilo.cpus):
        cpu, created = Processor.concurrent_get_or_create(device=dev,
                                                          index=i + 1)
        family = family or ''
        cpu.label = label
        cpu.model, c = ComponentModel.concurrent_get_or_create(
            speed=speed, type=ComponentType.processor.id, extra=extra,
            extra_hash=hashlib.md5(extra).hexdigest(), cores=cores,
            family=family)
        if c:
            cpu.model.name = 'CPU %s %dMHz, %s-core' % (family, speed, cores)
            cpu.model.save()
        cpu.save()
Пример #6
0
def _add_dev_cpu(pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        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'])
    extra = '\n'.join(
        '%s: %s' % (k, v) for (k, v) in sorted(pairs.iteritems())
        if v not in ('Not Available', '', None, 'Not Available (Not Available)'
                     ) and k not in ('Processor cores', 'Processor family',
                                     'Speed', 'system>', 'Mach type/model'))
    cpu.model, c = ComponentModel.concurrent_get_or_create(
        cores=cores,
        speed=speed,
        type=ComponentType.processor.id,
        extra_hash=hashlib.md5(extra).hexdigest(),
        extra=extra,
        family=family)
    cpu.model.name = 'CPU %s %d MHz, %s-core' % (family, speed, cores)
    cpu.model.save(priority=SAVE_PRIORITY)
    cpu.save(priority=SAVE_PRIORITY)
Пример #7
0
def _add_dev_cpu(pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        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'])
    extra = '\n'.join('%s: %s' % (k, v) for (k, v) in sorted(pairs.iteritems()) if
        v not in ('Not Available', '', None, 'Not Available (Not Available)') and
        k not in ('Processor cores', 'Processor family',
                  'Speed', 'system>', 'Mach type/model'))
    cpu.model, c = ComponentModel.concurrent_get_or_create(
        cores=cores, speed=speed, type=ComponentType.processor.id,
        extra_hash=hashlib.md5(extra).hexdigest(), extra=extra,
        family=family)
    cpu.model.name = 'CPU %s %d MHz, %s-core' % (family, speed, cores)
    cpu.model.save(priority=SAVE_PRIORITY)
    cpu.save(priority=SAVE_PRIORITY)
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)
Пример #9
0
def handle_lshw_processors(dev, processors, is_virtual=False, priority=0):
    if isinstance(processors, dict):
        processors = [processors]
    detected_cpus = {}
    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)
        family = processor['version'] or ''
        caps = processor['capabilities']
        extra = "\n".join([": ".join((key, ' '.join(e for e in
            untangle(caps[key]) if e) or '')) for key in sorted(caps.keys())])
        model, c = ComponentModel.concurrent_get_or_create(
            speed=speed, type=ComponentType.processor.id,
            extra_hash=hashlib.md5(extra).hexdigest(), family=family,
            cores=0)
        model.extra = extra
        model.name = processor['product'] or 'CPU {} {}MHz'.format(family,
            speed)
        model.save(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)
Пример #10
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()
Пример #11
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)
Пример #12
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)
Пример #13
0
def _add_dev_cpu(pairs, parent, raw, counts, dev_id):
    try:
        model = pairs["Mach type/model"]
    except KeyError:
        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"])
    extra = "\n".join(
        "%s: %s" % (k, v)
        for (k, v) in sorted(pairs.iteritems())
        if v not in ("Not Available", "", None, "Not Available (Not Available)")
        and k not in ("Processor cores", "Processor family", "Speed", "system>", "Mach type/model")
    )
    cpu.model, c = ComponentModel.concurrent_get_or_create(
        cores=cores,
        speed=speed,
        type=ComponentType.processor.id,
        extra_hash=hashlib.md5(extra).hexdigest(),
        extra=extra,
        family=family,
    )
    cpu.model.name = "CPU %s %d MHz, %s-core" % (family, speed, cores)
    cpu.model.save(priority=SAVE_PRIORITY)
    cpu.save(priority=SAVE_PRIORITY)
Пример #14
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.save(priority=SAVE_PRIORITY)
        mem.model, c = ComponentModel.concurrent_get_or_create(
            name='RAM %s %dMiB' % (mem.label, mem.size), size=mem.size,
            type=ComponentType.memory.id,
            family=mem.label, cores=0
        )
        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.concurrent_get_or_create(
            name=p.get('label'),
            speed=p.get('speed'),
            type=ComponentType.processor.id,
            family=p.get('family'),
            cores=p.get('cores')
        )
        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()
    return model_name
Пример #15
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
Пример #16
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.concurrent_get_or_create(
            family=proc.label, speed=speed, type=ComponentType.processor.id,
            cores=cores, extra_hash='', size=0)
        if c:
            proc.model.name = ('CPU %s %dMHz %d-core' %
                                (proc.label, speed, cores))[:50]
            proc.model.save()
        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
            speed = 0
            mem.model, c = ComponentModel.concurrent_get_or_create(
                name='RAM %s %dMiB' % (mem.label, size), size=size, speed=speed,
                type=ComponentType.memory.id, extra='', extra_hash='',
                family=mem.label, cores=0)
            mem.save()
            mem_index += 1
            total_mem_index += 1
        cpu_index += 1
Пример #17
0
def handle_dmidecode(info, ethernets=(), save_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=save_priority)
    for i, cpu_info in enumerate(info['cpu']):
        extra = ',\n'.join(cpu_info['flags'])
        extra = ('threads: %d\n' %
                 cpu_info['threads'] if cpu_info['threads'] else '') + extra
        if cpu_info['64bit']:
            extra = '64bit\n' + extra
        model, created = ComponentModel.concurrent_get_or_create(
            speed=cpu_info['speed'] or 0,
            cores=cpu_info['cores'] or 0,
            family=cpu_info['family'],
            extra_hash=hashlib.md5(extra).hexdigest(),
            type=ComponentType.processor.id,
        )
        if created:
            model.name = cpu_info['model']
            model.extra = extra
            model.save()
        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.concurrent_get_or_create(
            speed=mem_info['speed'] or 0,
            size=mem_info['size'] or 0,
            type=ComponentType.memory.id,
        )
        if created:
            model.name = 'RAM %s %dMiB' % (mem_info['type'], mem_info['size'])
            model.save()
        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
Пример #18
0
 def test_processors(self):
     model = ComponentModel(
         type=ComponentType.processor,
         name="CPU Xeon 2533MHz, 4-core",
     )
     model.save()
     for i in xrange(4):
         p = Processor(
             label="ziew",
             model=model,
             device=self.device,
             index=i,
         )
         p.save()
     data = get_device_data(Device.objects.get(sn='123456789'))
     processors = data['processors']
     self.assertEqual(len(processors), 4)
     self.assertEqual(processors[0]['label'], "ziew")
     self.assertEqual(processors[0]['model_name'], "CPU Xeon 2533MHz, 4-core")
     self.assertEqual(processors[0]['cores'], 4)
     self.assertEqual(processors[3]['index'], 3)
Пример #19
0
def handle_dmidecode(info, ethernets=(), save_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=save_priority
    )
    for i, cpu_info in enumerate(info['cpu']):
        extra = ',\n'.join(cpu_info['flags'])
        extra = ('threads: %d\n' % cpu_info['threads']
                 if cpu_info['threads'] else '') + extra
        if cpu_info['64bit']:
            extra = '64bit\n' + extra
        model, created = ComponentModel.concurrent_get_or_create(
            speed=cpu_info['speed'] or 0,
            cores=cpu_info['cores'] or 0,
            family=cpu_info['family'],
            extra_hash=hashlib.md5(extra).hexdigest(),
            type=ComponentType.processor.id,
        )
        if created:
            model.name = cpu_info['model']
            model.extra = extra
            model.save()
        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.concurrent_get_or_create(
            speed=mem_info['speed'] or 0,
            size=mem_info['size'] or 0,
            type=ComponentType.memory.id,
        )
        if created:
            model.name = 'RAM %s %dMiB' % (mem_info['type'], mem_info['size'])
            model.save()
        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
Пример #20
0
 def test_processors(self):
     model = ComponentModel(
         type=ComponentType.processor,
         name="CPU Xeon 2533MHz, 4-core",
     )
     model.save()
     for i in xrange(4):
         p = Processor(
             label="ziew",
             model=model,
             device=self.device,
             index=i,
         )
         p.save()
     data = get_device_data(Device.objects.get(sn='123456789'))
     processors = data['processors']
     self.assertEqual(len(processors), 4)
     self.assertEqual(processors[0]['label'], "ziew")
     self.assertEqual(processors[0]['model_name'],
                      "CPU Xeon 2533MHz, 4-core")
     self.assertEqual(processors[0]['cores'], 4)
     self.assertEqual(processors[3]['index'], 3)
Пример #21
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
Пример #22
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
Пример #23
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()
Пример #24
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()
Пример #25
0
def create_device(device, cpu=None, memory=None, storage=None):
    dev = Device.create(
        model=device.get('model'),
        model_name=device.get('model_name'),
        model_type=device.get('model_type'),
        sn=device.get('sn'),
        venture=device.get('venture'),
        parent=device.get('parent'),
        price=device.get('price'),
        deprecation_kind=device.get('deprecation_kind'),
        purchase_date=device.get('purchase_date'),
    )
    dev.name = device.get('name')
    dev.save()
    if cpu:
        model = create_model(device, cpu, ComponentType.processor)
        for cpu in duplicate_item(cpu):
            Processor(
                device=dev,
                model=model,
                label=cpu.get('label'),
            ).save()
    if memory:
        model = create_model(device, memory, ComponentType.memory)
        for memory in duplicate_item(memory):
            Memory(
                device=dev,
                model=model,
                speed=memory.get('speed'),
                size=memory.get('size'),
                label=memory.get('label'),
            ).save()
    if storage:
        model = create_model(device, storage, ComponentType.disk)
        for storage in duplicate_item(storage):
            Storage(
                device=dev,
                model=model,
                label=storage.get('label'),
            ).save()
    return dev
Пример #26
0
def handle_lshw_processors(dev, processors, is_virtual=False, priority=0):
    if isinstance(processors, dict):
        processors = [processors]
    detected_cpus = {}
    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)
        family = processor['version'] or ''
        caps = processor['capabilities']
        extra = "\n".join([
            ": ".join((key, ' '.join(e
                                     for e in untangle(caps[key]) if e) or ''))
            for key in sorted(caps.keys())
        ])
        model, c = ComponentModel.concurrent_get_or_create(
            speed=speed,
            type=ComponentType.processor.id,
            extra_hash=hashlib.md5(extra).hexdigest(),
            family=family,
            cores=0)
        model.extra = extra
        model.name = processor['product'] or 'CPU {} {}MHz'.format(
            family, speed)
        model.save(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)
Пример #27
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)
Пример #28
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()
Пример #29
0
def save_processors(processors, dev):
    indexes = []
    for p in processors:
        cpuname = p.get('label')
        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)
        cpu.label = cpuname
        cpu.speed = speed
        cpu.cores = cores
        is64bit = p.get('is64bit') == 'true'
        extra = '%s %s %s ' % (p.get('manufacturer'), p.get('version'),
                               '64bit' if is64bit else '')
        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.concurrent_get_or_create(
            speed=speed,
            type=ComponentType.processor.id,
            family=cpuname,
            size=cores,
            cores=cores,
            extra_hash=hashlib.md5(extra.encode('utf-8')).hexdigest())
        if c:
            cpu.model.extra = extra
            cpu.model.name = name
            cpu.model.save(priority=SAVE_PRIORITY)
        cpu.save(priority=SAVE_PRIORITY)
    for cpu in dev.processor_set.exclude(index__in=indexes):
        cpu.delete()
Пример #30
0
def run_ssh_aix(ip):
    ssh = _connect_ssh(ip)
    try:
        ethernets = []
        for model_line in _ssh_lines(ssh, "lsattr -El sys0 | grep ^modelname"):
            machine_model = model_line.split(None, 2)[1]
            break
        for mac_line in _ssh_lines(ssh, "netstat -ia | grep link"):
            interface, mtu, net, mac, rest = mac_line.split(None, 4)
            if "." not in mac:
                continue
            octets = mac.split(".")
            mac = "".join("%02x" % int(o, 16) for o in octets).upper()
            ethernets.append(Eth(label=interface, mac=mac, speed=0))
        disks = {}
        for disk_line in _ssh_lines(ssh, "lsdev -c disk"):
            disk, rest = disk_line.split(None, 1)
            wwn = None
            model = None
            for line in _ssh_lines(ssh, "lscfg -vl %s" % disk):
                if "Serial Number..." in line:
                    label, sn = line.split(".", 1)
                    sn = sn.strip(". \n")
                elif "Machine Type and Model." in line:
                    label, model = line.split(".", 1)
                    model = model.strip(". \n")
            disks[disk] = (model, sn)
    finally:
        ssh.close()
    dev = Device.create(
        ethernets=ethernets,
        model_type=DeviceType.rack_server,
        model_name="%s AIX" % MODELS.get(machine_model, machine_model),
    )
    ipaddr = IPAddress.objects.get(address=ip)
    ipaddr.device = dev
    ipaddr.save()
    wwns = []
    sns = []
    stors = []
    for disk, (model_name, sn) in disks.iteritems():
        if not sn:
            continue
        if model_name == "VV":
            wwns.append(sn)
        else:
            stors.append((disk, model_name, sn))
            sns.append(sn)
    for mount in dev.disksharemount_set.exclude(share__wwn__in=wwns):
        mount.delete()
    for stor in dev.storage_set.exclude(sn__in=sns):
        stor.delete()
    for wwn in wwns:
        try:
            share = DiskShare.objects.get(wwn=wwn)
        except DiskShare.DoesNotExist:
            continue
        wwn = network.normalize_wwn(sn[-4:] + sn[:-4])
        mount, created = DiskShareMount.concurrent_get_or_create(share=share, device=dev, is_virtual=False)
        mount.volume = disk
        mount.save()
    for disk, model_name, sn in stors:
        model, mcreated = ComponentModel.concurrent_get_or_create(
            type=ComponentType.disk.id, family=model_name, extra_hash=""
        )
        model.name = model_name
        model.save()
        stor, created = Storage.concurrent_get_or_create(device=dev, sn=sn)
        stor.model = model
        stor.label = disk
        stor.save()

    mem, created = Memory.concurrent_get_or_create(device=dev, index=0)
    mem.label = "Memory"
    mem.model, c = ComponentModel.concurrent_get_or_create(
        size=0, speed=0, type=ComponentType.memory.id, family="pSeries", extra_hash=""
    )
    mem.model.name = "pSeries Memory"
    mem.model.save()
    mem.save()
    cpu, created = Processor.concurrent_get_or_create(device=dev, index=0)
    cpu.label = "CPU"
    cpu.model, c = ComponentModel.concurrent_get_or_create(
        speed=0, cores=0, type=ComponentType.processor.id, extra_hash="", family="pSeries CPU"
    )
    cpu.model.name = "pSeries CPU"
    cpu.model.save()
    cpu.save()

    return machine_model
Пример #31
0
def _add_virtual_machine(ssh, vmid, parent, master, storage):
    stdin, stdout, stderr = ssh.exec_command("cat /etc/qemu-server/%d.conf" % vmid)
    disks = {}
    lan_model = None
    name = "unknown"
    for line in stdout:
        line = line.strip()
        key, value = line.split(":", 1)
        if key.startswith("vlan"):
            lan_model, lan_mac = value.split("=", 1)
        elif key == "name":
            name = value.strip()
        elif key == "sockets":
            cpu_count = int(value.strip())
        elif key.startswith("ide") or key.startswith("virtio"):
            disks[key] = value.strip()
    if lan_model is None:
        return None
    dev = Device.create(
        model_name="Proxmox qemu kvm",
        model_type=DeviceType.virtual_server,
        ethernets=[Eth(label=lan_model, mac=lan_mac, speed=0)],
        parent=parent,
        management=master,
        name=name,
    )
    wwns = []
    for slot, disk in disks.iteritems():
        params = {}
        if "," in disk:
            disk, rawparams = disk.split(",", 1)
            for kv in rawparams.split(","):
                if not kv.strip():
                    continue
                k, v = kv.split("=", 1)
                params[k] = v.strip()
        if ":" in disk:
            vg, lv = disk.split(":", 1)
        else:
            vg = ""
            lv = disk
        if vg == "local":
            model, created = ComponentModel.concurrent_get_or_create(
                type=ComponentType.disk.id, family="QEMU disk image"
            )
            if created:
                model.save()
            storage, created = Storage.concurrent_get_or_create(device=dev, mount_point=lv)
            storage.size = _get_local_disk_size(ssh, lv)
            storage.model = model
            storage.label = slot
            storage.save()
            continue
        if vg in ("", "local", "pve-local"):
            continue
        vol = "%s:%s" % (vg, lv)
        try:
            wwn, size = storage[lv]
        except KeyError:
            logger.warning("Volume %r does not exist." % lv)
            continue
        try:
            share = DiskShare.objects.get(wwn=wwn)
            wwns.append(wwn)
        except DiskShare.DoesNotExist:
            logger.warning("A share with WWN %r does not exist." % wwn)
            continue
        mount, created = DiskShareMount.concurrent_get_or_create(share=share, device=dev)
        mount.is_virtual = True
        mount.server = parent
        mount.size = size
        mount.volume = vol
        mount.save()
    for ds in dev.disksharemount_set.filter(server=parent).exclude(share__wwn__in=wwns):
        ds.delete()
    for ds in dev.disksharemount_set.filter(is_virtual=True).exclude(share__wwn__in=wwns):
        ds.delete()

    cpu_model, cpu_model_created = ComponentModel.concurrent_get_or_create(
        speed=0, type=ComponentType.processor.id, family="QEMU Virtual", cores=0
    )
    if cpu_model_created:
        cpu_model.name = "QEMU Virtual CPU version 0.12.4"
        cpu_model.save()
    for i in range(cpu_count):
        cpu, cpu_created = Processor.concurrent_get_or_create(device=dev, index=i + 1)
        if cpu_created:
            cpu.label = "CPU {}".format(i + 1)
            cpu.model = cpu_model
            cpu.family = "QEMU Virtual"
            cpu.save()

    dev.save(update_last_seen=True)
    return dev
Пример #32
0
def _parse_smbios(dev, smbios, facts, is_virtual):
    smbios, _ = network.smbios(as_string=zlib.decompress(smbios))
    # 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.concurrent_get_or_create(
            size=mem.size, speed=0, type=ComponentType.memory.id,
            family=family, extra_hash='')
        name = 'RAM %dMiB' % mem.size
        if family:
            name = '%s %s' % (family, name)
        mem.model.name = name
        mem.model.save(priority=SAVE_PRIORITY)
        mem.save(priority=SAVE_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)
        extra = "CPUID: {}".format(cpu['CPUID'])
        model, c = ComponentModel.concurrent_get_or_create(
            speed=speed, type=ComponentType.processor.id, extra=extra,
            extra_hash=hashlib.md5(extra).hexdigest(), family=family,
            cores=0)
        model.name = " ".join(cpu.get('Version', family).split())
        model.save(priority=SAVE_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=SAVE_PRIORITY)
Пример #33
0
def _add_virtual_machine(ssh, vmid, parent, master, storages, hypervisor_ip):
    stdin, stdout, stderr = ssh.exec_command("cat /etc/qemu-server/%d.conf" %
                                             vmid)
    lines = stdout.readlines()
    if not lines:
        # Proxmox 2 uses a different directory structure
        stdin, stdout, stderr = ssh.exec_command(
            "cat /etc/pve/nodes/*/qemu-server/%d.conf" % vmid)
        lines = stdout.readlines()
    disks = {}
    lan_model = None
    name = 'unknown'
    for line in lines:
        line = line.strip()
        if line.startswith('#') or ':' not in line:
            continue
        key, value = line.split(':', 1)
        if key.startswith('vlan'):
            lan_model, lan_mac = value.split('=', 1)
        elif key.startswith('net'):
            lan_model, lan_mac = value.split('=', 1)
            if ',' in lan_mac:
                lan_mac = lan_mac.split(',', 1)[0]
        elif key == 'name':
            name = value.strip()
        elif key == 'sockets':
            cpu_count = int(value.strip())
        elif key.startswith('ide') or key.startswith('virtio'):
            disks[key] = value.strip()
    if lan_model is None:
        DiscoveryWarning(message="No LAN for virtual server %r." % vmid,
                         plugin=__name__,
                         device=parent,
                         ip=hypervisor_ip).save()
        return None
    dev = Device.create(model_name='Proxmox qemu kvm',
                        model_type=DeviceType.virtual_server,
                        ethernets=[Eth(label=lan_model, mac=lan_mac, speed=0)],
                        parent=parent,
                        management=master,
                        name=name)
    wwns = []
    for slot, disk in disks.iteritems():
        params = {}
        if ',' in disk:
            disk, rawparams = disk.split(',', 1)
            for kv in rawparams.split(','):
                if not kv.strip():
                    continue
                k, v = kv.split('=', 1)
                params[k] = v.strip()
        if ':' in disk:
            vg, lv = disk.split(':', 1)
        else:
            vg = ''
            lv = disk
        if vg == 'local':
            size = _get_local_disk_size(ssh, lv, parent, hypervisor_ip)
            if not size > 0:
                continue
            model, created = ComponentModel.create(
                ComponentType.disk,
                family='QEMU disk image',
                priority=SAVE_PRIORITY,
            )
            # The model's size is deliberately 0 because it's a virtual
            # volume: there would be potentially as many models as volumes.
            storage, created = Storage.concurrent_get_or_create(device=dev,
                                                                mount_point=lv)
            storage.size = size
            storage.model = model
            storage.label = slot
            storage.save()
            continue
        if vg in ('', 'local', 'pve-local'):
            continue
        vol = '%s:%s' % (vg, lv)
        try:
            wwn, size = storages[lv]
        except KeyError:
            logger.warning('Volume %r does not exist.' % lv)
            DiscoveryWarning(
                message="Volume %r does not exist." % lv,
                plugin=__name__,
                device=dev,
                ip=hypervisor_ip,
            ).save()
            continue
        try:
            share = DiskShare.objects.get(wwn=wwn)
            wwns.append(wwn)
        except DiskShare.DoesNotExist:
            logger.warning('A share with serial %r does not exist.' % wwn)
            DiscoveryWarning(
                message="A share with serial %r does not exist." % wwn,
                plugin=__name__,
                device=dev,
                ip=hypervisor_ip,
            ).save()
            continue
        mount, created = DiskShareMount.concurrent_get_or_create(share=share,
                                                                 device=dev)
        mount.is_virtual = True
        mount.server = parent
        mount.size = size
        mount.volume = vol
        mount.save()
    for ds in dev.disksharemount_set.filter(server=parent).exclude(
            share__wwn__in=wwns):
        ds.delete()
    for ds in dev.disksharemount_set.filter(is_virtual=True).exclude(
            share__wwn__in=wwns):
        ds.delete()

    cpu_model, cpu_model_created = ComponentModel.create(
        ComponentType.processor,
        family='QEMU Virtual',
        name='QEMU Virtual CPU version 0.12.4',  # FIXME: why so specific?
        priority=SAVE_PRIORITY,
    )
    for i in range(cpu_count):
        cpu, cpu_created = Processor.concurrent_get_or_create(device=dev,
                                                              index=i + 1)
        if cpu_created:
            cpu.label = 'CPU {}'.format(i + 1)
            cpu.model = cpu_model
            cpu.family = 'QEMU Virtual'
            cpu.save()

    dev.save(update_last_seen=True, priority=SAVE_PRIORITY)
    return dev
Пример #34
0
def run_ssh_xen(ipaddr, parent):
    ssh = _connect_ssh(ipaddr.address)
    try:
        vms = get_running_vms(ssh)
        macs = get_macs(ssh)
        disks = get_disks(ssh)
        shares = hardware.get_disk_shares(ssh)
    finally:
        ssh.close()

    for dev in parent.child_set.exclude(
            sn__in=[vm_uuid for (vm_name, vm_uuid, vm_cores, vm_memory) in vms]
        ):
        dev.deleted = True
        dev.save()
    for vm_name, vm_uuid, vm_cores, vm_memory in vms:
        ethernets = [Eth('vif %d' % i, mac, 0) for
                     i, mac in enumerate(macs.get(vm_name, []))]
        dev = Device.create(ethernets=ethernets, parent=parent, sn=vm_uuid,
                model_type=DeviceType.virtual_server,
                model_name='XEN Virtual Server', priority=SAVE_PRIORITY)
        dev.name = vm_name
        dev.save(priority=SAVE_PRIORITY)
        cpu_model, cpu_model_created = ComponentModel.concurrent_get_or_create(
            speed=0, type=ComponentType.processor.id, family='XEN Virtual',
            cores=0)
        if cpu_model_created:
            cpu_model.name = 'XEN Virtual CPU'
            cpu_model.save()
        for i in xrange(vm_cores):
            cpu, created = Processor.concurrent_get_or_create(device=dev,
                                                              index=i + 1)
            if created:
                cpu.label = 'CPU %d' % i
                cpu.model = cpu_model
                cpu.family = 'XEN Virtual'
                cpu.save()
        for cpu in dev.processor_set.filter(index__gt=vm_cores+1):
            cpu.delete()
        mem_model, mem_model_created = ComponentModel.concurrent_get_or_create(
            speed=0, type=ComponentType.memory.id, family='XEN Virtual',
            cores=0, size=0)
        if mem_model_created:
            mem_model.name = 'XEN Virtual Memory'
            mem_model.save()
        memory, created = Memory.concurrent_get_or_create(device=dev, index=1)
        memory.size = vm_memory
        memory.model = mem_model
        memory.label = 'XEN Memory'
        memory.save()
        for mem in dev.memory_set.filter(index__gt=1):
            mem.delete()
        disk_model, created = ComponentModel.concurrent_get_or_create(
                type=ComponentType.disk.id, family='XEN virtual disk')
        if created:
            disk_model.name = 'XEN virtual disk'
            disk_model.save()
        vm_disks = disks.get(vm_name, [])
        wwns = []
        for uuid, sr_uuid, size, device in vm_disks:
            wwn, mount_size = shares.get('VHD-%s' % sr_uuid, (None, None))
            if wwn:
                try:
                    share = DiskShare.objects.get(wwn=wwn)
                    wwns.append(wwn)
                except DiskShare.DoesNotExist:
                    logger.warning('A share with serial %r does not exist.' % wwn)
                    DiscoveryWarning(
                        message="A share with serial %r does not exist." % wwn,
                        plugin=__name__,
                        device=dev,
                        ip=ipaddr,
                    ).save()
                    continue
                mount, created = DiskShareMount.concurrent_get_or_create(
                        share=share, device=dev)
                mount.is_virtual = True
                mount.server = parent
                mount.size = mount_size
                mount.volume = device
                mount.save()
            else:
                storage, created = Storage.concurrent_get_or_create(
                    device=dev, mount_point=device, sn=uuid)
                storage.size = size
                storage.model = disk_model
                storage.label = device
                storage.save()
        for disk in dev.storage_set.exclude(sn__in={
            uuid for uuid, x , y , z in vm_disks
        }):
            disk.delete()
        for ds in dev.disksharemount_set.filter(
                server=parent).exclude(share__wwn__in=wwns):
            ds.delete()
        for ds in dev.disksharemount_set.filter(
                is_virtual=True).exclude(share__wwn__in=wwns):
            ds.delete()
    return ', '.join(vm_name for (vm_name, vm_uuid, vm_cores, vm_memory) in vms)
Пример #35
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)
Пример #36
0
def _add_virtual_machine(ssh, vmid, parent, master, storages, hypervisor_ip):
    stdin, stdout, stderr = ssh.exec_command(
        "cat /etc/qemu-server/%d.conf" % vmid)
    lines = stdout.readlines()
    if not lines:
        # Proxmox 2 uses a different directory structure
        stdin, stdout, stderr = ssh.exec_command(
            "cat /etc/pve/nodes/*/qemu-server/%d.conf" % vmid)
        lines = stdout.readlines()
    disks = {}
    lan_model = None
    name = 'unknown'
    for line in lines:
        line = line.strip()
        if line.startswith('#') or ':' not in line:
            continue
        key, value = line.split(':', 1)
        if key.startswith('vlan'):
            lan_model, lan_mac = value.split('=', 1)
        elif key.startswith('net'):
            lan_model, lan_mac = value.split('=', 1)
            if ',' in lan_mac:
                lan_mac = lan_mac.split(',', 1)[0]
        elif key == 'name':
            name = value.strip()
        elif key == 'sockets':
            cpu_count = int(value.strip())
        elif key.startswith('ide') or key.startswith('virtio'):
            disks[key] = value.strip()
    if lan_model is None:
        DiscoveryWarning(
            message="No LAN for virtual server %r." % vmid,
            plugin=__name__,
            device=parent,
            ip=hypervisor_ip
        ).save()
        return None
    dev = Device.create(
        model_name='Proxmox qemu kvm',
        model_type=DeviceType.virtual_server,
        ethernets=[Eth(label=lan_model, mac=lan_mac, speed=0)],
        parent=parent,
        management=master,
        name=name
    )
    wwns = []
    for slot, disk in disks.iteritems():
        params = {}
        if ',' in disk:
            disk, rawparams = disk.split(',', 1)
            for kv in rawparams.split(','):
                if not kv.strip():
                    continue
                k, v = kv.split('=', 1)
                params[k] = v.strip()
        if ':' in disk:
            vg, lv = disk.split(':', 1)
        else:
            vg = ''
            lv = disk
        if vg == 'local':
            size = _get_local_disk_size(ssh, lv, parent, hypervisor_ip)
            if not size > 0:
                continue
            model, created = ComponentModel.create(
                ComponentType.disk,
                family='QEMU disk image',
                priority=SAVE_PRIORITY,
            )
            # The model's size is deliberately 0 because it's a virtual
            # volume: there would be potentially as many models as volumes.
            storage, created = Storage.concurrent_get_or_create(
                device=dev, mount_point=lv)
            storage.size = size
            storage.model = model
            storage.label = slot
            storage.save()
            continue
        if vg in ('', 'local', 'pve-local'):
            continue
        vol = '%s:%s' % (vg, lv)
        try:
            wwn, size = storages[lv]
        except KeyError:
            logger.warning('Volume %r does not exist.' % lv)
            DiscoveryWarning(
                message="Volume %r does not exist." % lv,
                plugin=__name__,
                device=dev,
                ip=hypervisor_ip,
            ).save()
            continue
        try:
            share = DiskShare.objects.get(wwn=wwn)
            wwns.append(wwn)
        except DiskShare.DoesNotExist:
            logger.warning('A share with serial %r does not exist.' % wwn)
            DiscoveryWarning(
                message="A share with serial %r does not exist." % wwn,
                plugin=__name__,
                device=dev,
                ip=hypervisor_ip,
            ).save()
            continue
        mount, created = DiskShareMount.concurrent_get_or_create(
            share=share, device=dev)
        mount.is_virtual = True
        mount.server = parent
        mount.size = size
        mount.volume = vol
        mount.save()
    for ds in dev.disksharemount_set.filter(
            server=parent).exclude(share__wwn__in=wwns):
        ds.delete()
    for ds in dev.disksharemount_set.filter(
            is_virtual=True).exclude(share__wwn__in=wwns):
        ds.delete()

    cpu_model, cpu_model_created = ComponentModel.create(
        ComponentType.processor,
        family='QEMU Virtual',
        name='QEMU Virtual CPU version 0.12.4',   # FIXME: why so specific?
        priority=SAVE_PRIORITY,
    )
    for i in range(cpu_count):
        cpu, cpu_created = Processor.concurrent_get_or_create(device=dev,
                                                              index=i + 1)
        if cpu_created:
            cpu.label = 'CPU {}'.format(i + 1)
            cpu.model = cpu_model
            cpu.family = 'QEMU Virtual'
            cpu.save()

    dev.save(update_last_seen=True, priority=SAVE_PRIORITY)
    return dev
Пример #37
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
Пример #38
0
def run_ssh_xen(ipaddr, parent):
    ssh = _connect_ssh(ipaddr.address)
    try:
        vms = get_running_vms(ssh)
        macs = get_macs(ssh)
        disks = get_disks(ssh)
        shares = hardware.get_disk_shares(ssh)
    finally:
        ssh.close()

    for dev in parent.child_set.exclude(sn__in=[
            vm_uuid for (vm_name, vm_uuid, vm_cores, vm_memory) in vms
    ]):
        dev.deleted = True
        dev.save()
    for vm_name, vm_uuid, vm_cores, vm_memory in vms:
        ethernets = [
            Eth('vif %d' % i, mac, 0)
            for i, mac in enumerate(macs.get(vm_name, []))
        ]
        dev = Device.create(ethernets=ethernets,
                            parent=parent,
                            sn=vm_uuid,
                            model_type=DeviceType.virtual_server,
                            model_name='XEN Virtual Server',
                            priority=SAVE_PRIORITY)
        dev.name = vm_name
        dev.save(priority=SAVE_PRIORITY)
        cpu_model, cpu_model_created = ComponentModel.concurrent_get_or_create(
            speed=0,
            type=ComponentType.processor.id,
            family='XEN Virtual',
            cores=0)
        if cpu_model_created:
            cpu_model.name = 'XEN Virtual CPU'
            cpu_model.save()
        for i in xrange(vm_cores):
            cpu, created = Processor.concurrent_get_or_create(device=dev,
                                                              index=i + 1)
            if created:
                cpu.label = 'CPU %d' % i
                cpu.model = cpu_model
                cpu.family = 'XEN Virtual'
                cpu.save()
        for cpu in dev.processor_set.filter(index__gt=vm_cores + 1):
            cpu.delete()
        mem_model, mem_model_created = ComponentModel.concurrent_get_or_create(
            speed=0,
            type=ComponentType.memory.id,
            family='XEN Virtual',
            cores=0,
            size=0)
        if mem_model_created:
            mem_model.name = 'XEN Virtual Memory'
            mem_model.save()
        memory, created = Memory.concurrent_get_or_create(device=dev, index=1)
        memory.size = vm_memory
        memory.model = mem_model
        memory.label = 'XEN Memory'
        memory.save()
        for mem in dev.memory_set.filter(index__gt=1):
            mem.delete()
        disk_model, created = ComponentModel.concurrent_get_or_create(
            type=ComponentType.disk.id, family='XEN virtual disk')
        if created:
            disk_model.name = 'XEN virtual disk'
            disk_model.save()
        vm_disks = disks.get(vm_name, [])
        wwns = []
        for uuid, sr_uuid, size, device in vm_disks:
            wwn, mount_size = shares.get('VHD-%s' % sr_uuid, (None, None))
            if wwn:
                try:
                    share = DiskShare.objects.get(wwn=wwn)
                    wwns.append(wwn)
                except DiskShare.DoesNotExist:
                    logger.warning('A share with serial %r does not exist.' %
                                   wwn)
                    DiscoveryWarning(
                        message="A share with serial %r does not exist." % wwn,
                        plugin=__name__,
                        device=dev,
                        ip=ipaddr,
                    ).save()
                    continue
                mount, created = DiskShareMount.concurrent_get_or_create(
                    share=share, device=dev)
                mount.is_virtual = True
                mount.server = parent
                mount.size = mount_size
                mount.volume = device
                mount.save()
            else:
                storage, created = Storage.concurrent_get_or_create(
                    device=dev, mount_point=device, sn=uuid)
                storage.size = size
                storage.model = disk_model
                storage.label = device
                storage.save()
        for disk in dev.storage_set.exclude(
                sn__in={uuid
                        for uuid, x, y, z in vm_disks}):
            disk.delete()
        for ds in dev.disksharemount_set.filter(server=parent).exclude(
                share__wwn__in=wwns):
            ds.delete()
        for ds in dev.disksharemount_set.filter(is_virtual=True).exclude(
                share__wwn__in=wwns):
            ds.delete()
    return ', '.join(vm_name
                     for (vm_name, vm_uuid, vm_cores, vm_memory) in vms)
Пример #39
0
def _add_virtual_machine(ssh, vmid, parent, master, storages):
    stdin, stdout, stderr = ssh.exec_command(
            "cat /etc/qemu-server/%d.conf" % vmid)
    lines = stdout.readlines()
    if not lines:
        # Proxmox 2 uses a different directory structure
        stdin, stdout, stderr = ssh.exec_command(
            "cat /etc/pve/nodes/*/qemu-server/%d.conf" % vmid)
        lines = stdout.readlines()
    disks = {}
    lan_model = None
    name = 'unknown'
    for line in lines:
        line = line.strip()
        key, value = line.split(':', 1)
        if key.startswith('vlan'):
            lan_model, lan_mac = value.split('=', 1)
        elif key.startswith('net'):
            lan_model, lan_mac = value.split('=', 1)
            if ',' in lan_mac:
                lan_mac = lan_mac.split(',', 1)[0]
        elif key == 'name':
            name = value.strip()
        elif key == 'sockets':
            cpu_count = int(value.strip())
        elif key.startswith('ide') or key.startswith('virtio'):
            disks[key] = value.strip()
    if lan_model is None:
        return None
    dev = Device.create(
            model_name='Proxmox qemu kvm',
            model_type=DeviceType.virtual_server,
            ethernets=[Eth(label=lan_model, mac=lan_mac, speed=0)],
            parent=parent,
            management=master,
            name=name
        )
    wwns = []
    for slot, disk in disks.iteritems():
        params = {}
        if ',' in disk:
            disk, rawparams = disk.split(',', 1)
            for kv in rawparams.split(','):
                if not kv.strip():
                    continue
                k, v = kv.split('=', 1)
                params[k] = v.strip()
        if ':' in disk:
            vg, lv = disk.split(':', 1)
        else:
            vg = ''
            lv = disk
        if vg == 'local':
            model, created = ComponentModel.concurrent_get_or_create(
                type=ComponentType.disk.id, family='QEMU disk image')
            if created:
                model.save()
            storage, created = Storage.concurrent_get_or_create(
                device=dev, mount_point=lv)
            storage.size = _get_local_disk_size(ssh, lv)
            storage.model = model
            storage.label = slot
            storage.save()
            continue
        if vg in ('', 'local', 'pve-local'):
            continue
        vol = '%s:%s' % (vg, lv)
        try:
            wwn, size = storages[lv]
        except KeyError:
            logger.warning('Volume %r does not exist.' % lv)
            continue
        try:
            share = DiskShare.objects.get(wwn=wwn)
            wwns.append(wwn)
        except DiskShare.DoesNotExist:
            logger.warning('A share with WWN %r does not exist.' % wwn)
            continue
        mount, created = DiskShareMount.concurrent_get_or_create(
                share=share, device=dev)
        mount.is_virtual = True
        mount.server = parent
        mount.size = size
        mount.volume = vol
        mount.save()
    for ds in dev.disksharemount_set.filter(
            server=parent).exclude(share__wwn__in=wwns):
        ds.delete()
    for ds in dev.disksharemount_set.filter(
            is_virtual=True).exclude(share__wwn__in=wwns):
        ds.delete()

    cpu_model, cpu_model_created = ComponentModel.concurrent_get_or_create(
        speed=0, type=ComponentType.processor.id, family='QEMU Virtual',
        cores=0)
    if cpu_model_created:
        cpu_model.name = 'QEMU Virtual CPU version 0.12.4'
        cpu_model.save()
    for i in range(cpu_count):
        cpu, cpu_created = Processor.concurrent_get_or_create(device=dev,
                                                              index=i+1)
        if cpu_created:
            cpu.label = 'CPU {}'.format(i + 1)
            cpu.model = cpu_model
            cpu.family = 'QEMU Virtual'
            cpu.save()

    dev.save(update_last_seen=True)
    return dev
Пример #40
0
def run_ssh_aix(ip):
    ssh = _connect_ssh(ip)
    try:
        ethernets = []
        for model_line in _ssh_lines(ssh, 'lsattr -El sys0 | grep ^modelname'):
            machine_model = model_line.split(None, 2)[1]
            break
        for mac_line in _ssh_lines(ssh, 'netstat -ia | grep link'):
            interface, mtu, net, mac, rest = mac_line.split(None, 4)
            if '.' not in mac:
                continue
            octets = mac.split('.')
            mac = ''.join('%02x' % int(o, 16) for o in octets).upper()
            ethernets.append(Eth(label=interface, mac=mac, speed=0))
        disks = {}
        os_storage_size = 0
        for disk_line in _ssh_lines(ssh, 'lsdev -c disk'):
            disk, rest = disk_line.split(None, 1)
            wwn = None
            model = None
            for line in _ssh_lines(ssh, 'lscfg -vl %s' % disk):
                if 'hdisk' in line:
                    match = re.search(r'\(([0-9]+) MB\)', line)
                    if match:
                        os_storage_size += int(match.group(1))
                elif 'Serial Number...' in line:
                    label, sn = line.split('.', 1)
                    sn = sn.strip('. \n')
                elif 'Machine Type and Model.' in line:
                    label, model = line.split('.', 1)
                    model = model.strip('. \n')
            disks[disk] = (model, sn)
        os_version = ''
        for line in _ssh_lines(ssh, 'oslevel'):
            os_version = line.strip()
            break
        os_memory = 0
        for line in _ssh_lines(ssh, 'lsattr -El sys0 | grep ^realmem'):
            match = re.search(r'[0-9]+', line)
            if match:
                os_memory = int(int(match.group(0)) / 1024)
            break
        os_corescount = 0
        for line in _ssh_lines(ssh, 'lparstat -i|grep ^Active\ Phys'):
            match = re.search(r'[0-9]+', line)
            if match:
                os_corescount += int(match.group(0))
    finally:
        ssh.close()
    dev = Device.create(ethernets=ethernets,
                        model_type=DeviceType.rack_server,
                        model_name='%s AIX' %
                        MODELS.get(machine_model, machine_model))
    ipaddr = IPAddress.objects.get(address=ip)
    ipaddr.device = dev
    ipaddr.save()
    wwns = []
    sns = []
    stors = []
    for disk, (model_name, sn) in disks.iteritems():
        if not sn:
            continue
        if model_name == 'VV':
            wwns.append(sn)
        else:
            stors.append((disk, model_name, sn))
            sns.append(sn)
    for mount in dev.disksharemount_set.exclude(share__wwn__in=wwns):
        mount.delete()
    for stor in dev.storage_set.exclude(sn__in=sns):
        stor.delete()
    for wwn in wwns:
        try:
            share = DiskShare.objects.get(wwn=wwn)
        except DiskShare.DoesNotExist:
            continue
        wwn = normalize_wwn(sn[-4:] + sn[:-4])
        mount, created = DiskShareMount.concurrent_get_or_create(
            share=share, device=dev, is_virtual=False)
        mount.volume = disk
        mount.save()
    for disk, model_name, sn in stors:
        model, mcreated = ComponentModel.concurrent_get_or_create(
            type=ComponentType.disk.id, family=model_name, extra_hash='')
        model.name = model_name
        model.save()
        stor, created = Storage.concurrent_get_or_create(device=dev, sn=sn)
        stor.model = model
        stor.label = disk
        stor.save()

    mem, created = Memory.concurrent_get_or_create(device=dev, index=0)
    mem.label = 'Memory'
    mem.model, c = ComponentModel.concurrent_get_or_create(
        size=0,
        speed=0,
        type=ComponentType.memory.id,
        family='pSeries',
        extra_hash='')
    mem.model.name = 'pSeries Memory'
    mem.model.save()
    mem.save()
    cpu, created = Processor.concurrent_get_or_create(device=dev, index=0)
    cpu.label = 'CPU'
    cpu.model, c = ComponentModel.concurrent_get_or_create(
        speed=0,
        cores=0,
        type=ComponentType.processor.id,
        extra_hash='',
        family='pSeries CPU')
    cpu.model.name = 'pSeries CPU'
    cpu.model.save()
    cpu.save()
    os = OperatingSystem.create(dev=dev,
                                os_name='AIX',
                                version=os_version,
                                family='AIX')
    os.memory = os_memory if os_memory else None
    os.cores_count = os_corescount if os_corescount else None
    os.storage = os_storage_size if os_storage_size else None
    os.save()

    return machine_model
Пример #41
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.concurrent_get_or_create(
            family=proc.label,
            speed=speed,
            type=ComponentType.processor.id,
            cores=cores,
            extra_hash='',
            size=0)
        if c:
            proc.model.name = ('CPU %s %dMHz %d-core' %
                               (proc.label, speed, cores))[:50]
            proc.model.save()
        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
            speed = 0
            mem.model, c = ComponentModel.concurrent_get_or_create(
                name='RAM %s %dMiB' % (mem.label, size),
                size=size,
                speed=speed,
                type=ComponentType.memory.id,
                extra='',
                extra_hash='',
                family=mem.label,
                cores=0)
            mem.save()
            mem_index += 1
            total_mem_index += 1
        cpu_index += 1
Пример #42
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
Пример #43
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)
Пример #44
0
def run_ssh_aix(ip):
    ssh = _connect_ssh(ip)
    try:
        ethernets = []
        for model_line in _ssh_lines(ssh, 'lsattr -El sys0 | grep ^modelname'):
            machine_model = model_line.split(None, 2)[1]
            break
        for mac_line in _ssh_lines(ssh, 'netstat -ia | grep link'):
            interface, mtu, net, mac, rest = mac_line.split(None, 4)
            if '.' not in mac:
                continue
            octets = mac.split('.')
            mac = ''.join('%02x' % int(o, 16) for o in octets).upper()
            ethernets.append(Eth(label=interface, mac=mac, speed=0))
        disks = {}
        os_storage_size = 0
        for disk_line in _ssh_lines(ssh, 'lsdev -c disk'):
            disk, rest = disk_line.split(None, 1)
            wwn = None
            model = None
            for line in _ssh_lines(ssh, 'lscfg -vl %s' % disk):
                if 'hdisk' in line:
                    match = re.search(r'\(([0-9]+) MB\)', line)
                    if match:
                        os_storage_size += int(match.group(1))
                elif 'Serial Number...' in line:
                    label, sn = line.split('.', 1)
                    sn = sn.strip('. \n')
                elif 'Machine Type and Model.' in line:
                    label, model = line.split('.', 1)
                    model = model.strip('. \n')
            disks[disk] = (model, sn)
        os_version = ''
        for line in _ssh_lines(ssh, 'oslevel'):
            os_version = line.strip()
            break
        os_memory = 0
        for line in _ssh_lines(ssh, 'lsattr -El sys0 | grep ^realmem'):
            match = re.search(r'[0-9]+', line)
            if match:
                os_memory = int(int(match.group(0)) / 1024)
            break
        os_corescount = 0
        for line in _ssh_lines(ssh, 'lparstat -i|grep ^Active\ Phys'):
            match = re.search(r'[0-9]+', line)
            if match:
                os_corescount += int(match.group(0))
    finally:
        ssh.close()
    dev = Device.create(
        ethernets=ethernets, model_type=DeviceType.rack_server,
        model_name='%s AIX' % MODELS.get(machine_model, machine_model))
    ipaddr = IPAddress.objects.get(address=ip)
    ipaddr.device = dev
    ipaddr.save()
    wwns = []
    sns = []
    stors = []
    for disk, (model_name, sn) in disks.iteritems():
        if not sn:
            continue
        if model_name == 'VV':
            wwns.append(sn)
        else:
            stors.append((disk, model_name, sn))
            sns.append(sn)
    for mount in dev.disksharemount_set.exclude(share__wwn__in=wwns):
        mount.delete()
    for stor in dev.storage_set.exclude(sn__in=sns):
        stor.delete()
    for wwn in wwns:
        try:
            share = DiskShare.objects.get(wwn=wwn)
        except DiskShare.DoesNotExist:
            continue
        wwn = normalize_wwn(sn[-4:] + sn[:-4])
        mount, created = DiskShareMount.concurrent_get_or_create(
            share=share, device=dev, defaults={'is_virtual':False})
        mount.volume = disk
        mount.save(priority=SAVE_PRIORITY)
    for disk, model_name, sn in stors:
        # FIXME: storage with no size
        model, c = ComponentModel.create(
            ComponentType.disk,
            family=model_name,
            priority=SAVE_PRIORITY,
        )
        stor, created = Storage.concurrent_get_or_create(
            device=dev,
            sn=sn,
            mount_point=None,
        )
        stor.model = model
        stor.label = disk
        stor.save(priority=SAVE_PRIORITY)
    # FIXME: memory with no size
    mem, created = Memory.concurrent_get_or_create(device=dev, index=0)
    mem.label = 'Memory'
    mem.model, c = ComponentModel.create(
        ComponentType.memory,
        family='pSeries',
        priority=SAVE_PRIORITY,
    )
    mem.save(priority=SAVE_PRIORITY)
    # FIXME: CPUs without info
    cpu, created = Processor.concurrent_get_or_create(device=dev, index=0)
    cpu.label = 'CPU'
    cpu.model, c = ComponentModel.create(
        ComponentType.processor,
        family='pSeries',
        name='pSeries CPU',
        priority=SAVE_PRIORITY,
    )
    cpu.save(priority=SAVE_PRIORITY)
    OperatingSystem.create(dev=dev,
        os_name='AIX',
        version=os_version,
        family='AIX',
        memory=os_memory or None,
        cores_count=os_corescount or None,
        storage=os_storage_size or None,
        priority=SAVE_PRIORITY
    )
    return machine_model
Пример #45
0
def _add_virtual_machine(ssh, vmid, parent, master, storages):
    stdin, stdout, stderr = ssh.exec_command("cat /etc/qemu-server/%d.conf" %
                                             vmid)
    lines = stdout.readlines()
    if not lines:
        # Proxmox 2 uses a different directory structure
        stdin, stdout, stderr = ssh.exec_command(
            "cat /etc/pve/nodes/*/qemu-server/%d.conf" % vmid)
        lines = stdout.readlines()
    disks = {}
    lan_model = None
    name = 'unknown'
    for line in lines:
        line = line.strip()
        key, value = line.split(':', 1)
        if key.startswith('vlan'):
            lan_model, lan_mac = value.split('=', 1)
        elif key.startswith('net'):
            lan_model, lan_mac = value.split('=', 1)
            if ',' in lan_mac:
                lan_mac = lan_mac.split(',', 1)[0]
        elif key == 'name':
            name = value.strip()
        elif key == 'sockets':
            cpu_count = int(value.strip())
        elif key.startswith('ide') or key.startswith('virtio'):
            disks[key] = value.strip()
    if lan_model is None:
        return None
    dev = Device.create(model_name='Proxmox qemu kvm',
                        model_type=DeviceType.virtual_server,
                        ethernets=[Eth(label=lan_model, mac=lan_mac, speed=0)],
                        parent=parent,
                        management=master,
                        name=name)
    wwns = []
    for slot, disk in disks.iteritems():
        params = {}
        if ',' in disk:
            disk, rawparams = disk.split(',', 1)
            for kv in rawparams.split(','):
                if not kv.strip():
                    continue
                k, v = kv.split('=', 1)
                params[k] = v.strip()
        if ':' in disk:
            vg, lv = disk.split(':', 1)
        else:
            vg = ''
            lv = disk
        if vg == 'local':
            model, created = ComponentModel.concurrent_get_or_create(
                type=ComponentType.disk.id, family='QEMU disk image')
            if created:
                model.save()
            storage, created = Storage.concurrent_get_or_create(device=dev,
                                                                mount_point=lv)
            storage.size = _get_local_disk_size(ssh, lv)
            storage.model = model
            storage.label = slot
            storage.save()
            continue
        if vg in ('', 'local', 'pve-local'):
            continue
        vol = '%s:%s' % (vg, lv)
        try:
            wwn, size = storages[lv]
        except KeyError:
            logger.warning('Volume %r does not exist.' % lv)
            continue
        try:
            share = DiskShare.objects.get(wwn=wwn)
            wwns.append(wwn)
        except DiskShare.DoesNotExist:
            logger.warning('A share with WWN %r does not exist.' % wwn)
            continue
        mount, created = DiskShareMount.concurrent_get_or_create(share=share,
                                                                 device=dev)
        mount.is_virtual = True
        mount.server = parent
        mount.size = size
        mount.volume = vol
        mount.save()
    for ds in dev.disksharemount_set.filter(server=parent).exclude(
            share__wwn__in=wwns):
        ds.delete()
    for ds in dev.disksharemount_set.filter(is_virtual=True).exclude(
            share__wwn__in=wwns):
        ds.delete()

    cpu_model, cpu_model_created = ComponentModel.concurrent_get_or_create(
        speed=0,
        type=ComponentType.processor.id,
        family='QEMU Virtual',
        cores=0)
    if cpu_model_created:
        cpu_model.name = 'QEMU Virtual CPU version 0.12.4'
        cpu_model.save()
    for i in range(cpu_count):
        cpu, cpu_created = Processor.concurrent_get_or_create(device=dev,
                                                              index=i + 1)
        if cpu_created:
            cpu.label = 'CPU {}'.format(i + 1)
            cpu.model = cpu_model
            cpu.family = 'QEMU Virtual'
            cpu.save()

    dev.save(update_last_seen=True)
    return dev