Exemplo n.º 1
0
 def test_memory(self):
     for i in xrange(8):
         m = Memory(label="ziew", size=128, device=self.device, index=i)
         m.save()
     data = get_device_data(Device.objects.get(sn="123456789"))
     memory = data["memory"]
     self.assertEqual(len(memory), 8)
     self.assertEqual(memory[0]["label"], "ziew")
     self.assertEqual(memory[0]["size"], 128)
     self.assertEqual(memory[3]["index"], 3)
Exemplo n.º 2
0
 def test_memory(self):
     for i in xrange(8):
         m = Memory(
             label="ziew",
             size=128,
             device=self.device,
             index=i,
         )
         m.save()
     data = get_device_data(Device.objects.get(sn='123456789'))
     memory = data['memory']
     self.assertEqual(len(memory), 8)
     self.assertEqual(memory[0]['label'], "ziew")
     self.assertEqual(memory[0]['size'], 128)
     self.assertEqual(memory[3]['index'], 3)
Exemplo n.º 3
0
 def test_memory(self):
     for i in xrange(8):
         m = Memory(
             label="ziew",
             size=128,
             device=self.device,
             index=i,
         )
         m.save()
     data = get_device_data(Device.objects.get(sn='123456789'))
     memory = data['memory']
     self.assertEqual(len(memory), 8)
     self.assertEqual(memory[0]['label'], "ziew")
     self.assertEqual(memory[0]['size'], 128)
     self.assertEqual(memory[3]['index'], 3)
Exemplo n.º 4
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)
Exemplo n.º 5
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get('size'))
            speed = int(row.get('speed')) if row.get('speed') else 0
        except ValueError:
            pass
        label = row.get('label')
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = 'RAM %dMiB' % size
        mem.speed = speed
        family = 'Virtual' if 'Virtual' in label else ''
        extra = '%s %dMiB %s %s' % (label, size, speed, row.get('caption'))
        mem.model, c = ComponentModel.concurrent_get_or_create(
            family=family, size=size, speed=speed,
            type=ComponentType.memory.id,
            extra_hash=hashlib.md5(extra.encode('utf-8')).hexdigest())
        mem.model.extra = extra
        mem.model.name = 'RAM Windows %dMiB' % size
        mem.model.save()
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Exemplo n.º 6
0
def _add_dev_memory(ip, pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        DiscoveryWarning(
            message="Memory model unknown",
            plugin=__name__,
            device=parent,
            ip=ip,
        ).save()
        return
    counts.mem += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.mem
    mem, created = Memory.concurrent_get_or_create(device=parent, index=index)
    size = int(pairs['Size'].replace('GB', '')) * 1024
    speed = int(pairs.get('Speed', '0').replace('MHz', ''))
    mem.label = pairs.get('Mach type/model', '')
    mem.model, c = ComponentModel.create(
        ComponentType.memory,
        size=size,
        speed=speed,
        priority=SAVE_PRIORITY,
    )
    mem.save(priority=SAVE_PRIORITY)
Exemplo n.º 7
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get('size'))
            speed = int(row.get('speed')) if row.get('speed') else 0
        except ValueError:
            pass
        label = row.get('label')
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = 'RAM %dMiB' % size
        mem.speed = speed
        family = 'Virtual Windows' if 'Virtual' in label else 'Windows'
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            family=family,
            size=size,
            speed=speed,
            priority=SAVE_PRIORITY,
        )
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Exemplo n.º 8
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get('size'))
            speed = int(row.get('speed')) if row.get('speed') else 0
        except ValueError:
            pass
        label = row.get('label')
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = 'RAM %dMiB' % size
        mem.speed = speed
        family = 'Virtual' if 'Virtual' in label else ''
        extra = '%s %dMiB %s %s' % (label, size, speed, row.get('caption'))
        mem.model, c = ComponentModel.concurrent_get_or_create(
            family=family,
            size=size,
            speed=speed,
            type=ComponentType.memory.id,
            extra_hash=hashlib.md5(extra.encode('utf-8')).hexdigest())
        mem.model.extra = extra
        mem.model.name = 'RAM Windows %dMiB' % size
        mem.model.save()
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Exemplo n.º 9
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get('size'))
            speed = int(row.get('speed')) if row.get('speed') else 0
        except ValueError:
            pass
        label = row.get('label')
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = 'RAM %dMiB' % size
        mem.speed = speed
        family = 'Virtual Windows' if 'Virtual' in label else 'Windows'
        mem.model, c = ComponentModel.create(
            ComponentType.memory,
            family=family,
            size=size,
            speed=speed,
            priority=SAVE_PRIORITY,
        )
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Exemplo n.º 10
0
def _add_dev_memory(ip, pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        DiscoveryWarning(
            message="Memory model unknown",
            plugin=__name__,
            device=parent,
            ip=ip,
        ).save()
        return
    counts.mem += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.mem
    mem, created = Memory.concurrent_get_or_create(device=parent, index=index)
    size = int(pairs['Size'].replace('GB', '')) * 1024
    speed = int(pairs.get('Speed', '0').replace('MHz', ''))
    mem.label = pairs.get('Mach type/model', '')
    mem.model, c = ComponentModel.create(
        ComponentType.memory,
        size=size,
        speed=speed,
        priority=SAVE_PRIORITY,
    )
    mem.save(priority=SAVE_PRIORITY)
Exemplo n.º 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.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()
Exemplo n.º 12
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
Exemplo n.º 13
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
Exemplo n.º 14
0
Arquivo: ipmi.py Projeto: pb-it/ralph
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
Exemplo n.º 15
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
Exemplo n.º 16
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
Exemplo n.º 17
0
def handle_lshw_memory(dev, bus_memory, is_virtual=False, priority=0):
    memory_banks = []
    for _mem in bus_memory:
        # we're interested only in the system memory, not in caches etc.
        if _mem['id'] == 'memory':
            memory_banks = _mem['memory']
            break
        elif _mem['id'].startswith('memory:'):
            memory_banks.extend(_mem['memory'])
    index = 0
    if isinstance(memory_banks, dict):
        memory_banks = [memory_banks]
    detected_memory = {}
    for memory in memory_banks:
        if 'size' not in memory:
            # empty slot
            continue
        index += 1
        size = int(memory['size']['value'] or 0)
        size /= units.size_divisor[memory['size']['units']]
        size = int(size)
        label = memory['slot']
        family = 'Virtual' if is_virtual else ''
        model, created = ComponentModel.concurrent_get_or_create(
            size=size,
            speed=0,
            type=ComponentType.memory.id,
            family=family,
            extra_hash='')
        if created:
            name = 'RAM %dMiB' % size
            if family:
                name = '%s %s' % (family, name)
            model.name = name
            model.save(priority=priority)
        detected_memory[index] = label, model
    for mem in dev.memory_set.all():
        label, model = detected_memory.get(mem.index, (None, None))
        if mem.label != label or mem.model != model:
            mem.delete()
    for index, (label, model) in detected_memory.iteritems():
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        if created:
            mem.model = model
            mem.size = model.size
            mem.speed = model.speed
            mem.label = label
            mem.save(priority=priority)
Exemplo n.º 18
0
def _save_memory(dev, data):
    detected_memory = []
    for mem, index in zip(data, range(1, len(data) + 1)):
        model, _ = ComponentModel.create(
            ComponentType.memory,
            size=int(mem['size']),
            speed=int(mem['speed']),
            priority=SAVE_PRIORITY,
        )
        memory, _ = Memory.concurrent_get_or_create(index=index, device=dev)
        memory.label = "{} {}".format(mem['manufacturer'], mem['model'])
        memory.size = mem['size']
        memory.speed = mem['speed']
        memory.model = model
        memory.save(priority=SAVE_PRIORITY)
        detected_memory.append(memory.pk)
    dev.memory_set.exclude(pk__in=detected_memory).delete()
Exemplo n.º 19
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
Exemplo n.º 20
0
def _save_memory(dev, data):
    detected_memory = []
    for mem, index in zip(data, range(1, len(data) + 1)):
        model, _ = ComponentModel.create(
            ComponentType.memory,
            size=int(mem['size']),
            speed=int(mem['speed']),
            priority=SAVE_PRIORITY,
        )
        memory, _ = Memory.concurrent_get_or_create(index=index, device=dev)
        memory.label = "{} {}".format(mem['manufacturer'], mem['model'])
        memory.size = mem['size']
        memory.speed = mem['speed']
        memory.model = model
        memory.save(priority=SAVE_PRIORITY)
        detected_memory.append(memory.pk)
    dev.memory_set.exclude(pk__in=detected_memory).delete()
Exemplo n.º 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
Exemplo n.º 22
0
def handle_lshw_memory(dev, bus_memory, is_virtual=False, priority=0):
    memory_banks = []
    for _mem in bus_memory:
        # we're interested only in the system memory, not in caches etc.
        if _mem['id'] == 'memory':
            memory_banks = _mem['memory']
            break
        elif _mem['id'].startswith('memory:'):
            memory_banks.extend(_mem['memory'])
    index = 0
    if isinstance(memory_banks, dict):
        memory_banks = [memory_banks]
    detected_memory = {}
    for memory in memory_banks:
        if 'size' not in memory:
            # empty slot
            continue
        index += 1
        size = int(memory['size']['value'] or 0)
        size /= units.size_divisor[memory['size']['units']]
        size = int(size)
        label = memory['slot']
        family = 'Virtual' if is_virtual else ''
        model, created = ComponentModel.concurrent_get_or_create(
            size=size, speed=0, type=ComponentType.memory.id,
            family=family, extra_hash='')
        if created:
            name = 'RAM %dMiB' % size
            if family:
                name = '%s %s' % (family, name)
            model.name = name
            model.save(priority=priority)
        detected_memory[index] = label, model
    for mem in dev.memory_set.all():
        label, model = detected_memory.get(mem.index, (None, None))
        if mem.label != label or mem.model != model:
            mem.delete()
    for index, (label, model) in detected_memory.iteritems():
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        if created:
            mem.model = model
            mem.size = model.size
            mem.speed = model.speed
            mem.label = label
            mem.save(priority=priority)
Exemplo n.º 23
0
Arquivo: lshw.py Projeto: viciu/ralph
def handle_lshw_memory(dev, bus_memory, is_virtual=False, priority=0):
    memory_banks = []
    for _mem in bus_memory:
        # we're interested only in the system memory, not in caches etc.
        if _mem["id"] == "memory":
            memory_banks = _mem["memory"]
            break
        elif _mem["id"].startswith("memory:"):
            memory_banks.extend(_mem["memory"])
    index = 0
    if isinstance(memory_banks, dict):
        memory_banks = [memory_banks]
    detected_memory = {}
    for memory in memory_banks:
        if "size" not in memory:
            # empty slot
            continue
        index += 1
        size = int(memory["size"]["value"] or 0)
        size /= units.size_divisor[memory["size"]["units"]]
        size = int(size)
        label = memory["slot"]
        family = "Virtual" if is_virtual else ""
        model, created = ComponentModel.concurrent_get_or_create(
            size=size, speed=0, type=ComponentType.memory.id, family=family, extra_hash=""
        )
        if created:
            name = "RAM %dMiB" % size
            if family:
                name = "%s %s" % (family, name)
            model.name = name
            model.save(priority=priority)
        detected_memory[index] = label, model
    for mem in dev.memory_set.all():
        label, model = detected_memory.get(mem.index, (None, None))
        if mem.label != label or mem.model != model:
            mem.delete()
    for index, (label, model) in detected_memory.iteritems():
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        if created:
            mem.model = model
            mem.size = model.size
            mem.speed = model.speed
            mem.label = label
            mem.save(priority=priority)
Exemplo n.º 24
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
Exemplo n.º 25
0
def _add_dev_memory(pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        return
    counts.mem += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.mem
    mem, created = Memory.concurrent_get_or_create(device=parent, index=index)
    family = pairs.get('Memory type', '')
    size = int(pairs['Size'].replace('GB', '')) * 1024
    speed = int(pairs.get('Speed', '0').replace('MHz', ''))
    mem.label = pairs.get('Mach type/model', '')
    mem.model, c = ComponentModel.concurrent_get_or_create(
        family=family, size=size, speed=speed, type=ComponentType.memory.id,
        extra_hash='')
    mem.model.name = 'RAM %s %dMiB %dMHz' % (family, size, speed)
    mem.model.save(priority=SAVE_PRIORITY)
    mem.save(priority=SAVE_PRIORITY)
Exemplo n.º 26
0
def _add_dev_memory(pairs, parent, raw, counts, dev_id):
    try:
        model = pairs["Mach type/model"]
    except KeyError:
        return
    counts.mem += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.mem
    mem, created = Memory.concurrent_get_or_create(device=parent, index=index)
    family = pairs.get("Memory type", "")
    size = int(pairs["Size"].replace("GB", "")) * 1024
    speed = int(pairs.get("Speed", "0").replace("MHz", ""))
    mem.label = pairs.get("Mach type/model", "")
    mem.model, c = ComponentModel.concurrent_get_or_create(
        family=family, size=size, speed=speed, type=ComponentType.memory.id, extra_hash=""
    )
    mem.model.name = "RAM %s %dMiB %dMHz" % (family, size, speed)
    mem.model.save(priority=SAVE_PRIORITY)
    mem.save(priority=SAVE_PRIORITY)
Exemplo n.º 27
0
def save_memory(memory, dev):
    indexes = []
    index = 0
    for row in memory:
        index += 1
        indexes.append(index)
        try:
            size = int(row.get("size"))
            speed = int(row.get("speed")) if row.get("speed") else 0
        except ValueError:
            pass
        label = row.get("label")
        mem, created = Memory.concurrent_get_or_create(device=dev, index=index)
        mem.size = size
        mem.label = "RAM %dMiB" % size
        mem.speed = speed
        family = "Virtual Windows" if "Virtual" in label else "Windows"
        mem.model, c = ComponentModel.create(
            ComponentType.memory, family=family, size=size, speed=speed, priority=SAVE_PRIORITY
        )
        mem.save(priority=SAVE_PRIORITY)
    dev.memory_set.exclude(index__in=indexes).delete()
Exemplo n.º 28
0
def _add_dev_memory(pairs, parent, raw, counts, dev_id):
    try:
        model = pairs['Mach type/model']
    except KeyError:
        return
    counts.mem += 1
    try:
        index = int(model.split()[-1])
    except ValueError:
        index = counts.mem
    mem, created = Memory.concurrent_get_or_create(device=parent, index=index)
    family = pairs.get('Memory type', '')
    size = int(pairs['Size'].replace('GB', '')) * 1024
    speed = int(pairs.get('Speed', '0').replace('MHz', ''))
    mem.label = pairs.get('Mach type/model', '')
    mem.model, c = ComponentModel.concurrent_get_or_create(
        family=family,
        size=size,
        speed=speed,
        type=ComponentType.memory.id,
        extra_hash='')
    mem.model.name = 'RAM %s %dMiB %dMHz' % (family, size, speed)
    mem.model.save(priority=SAVE_PRIORITY)
    mem.save(priority=SAVE_PRIORITY)
Exemplo n.º 29
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
Exemplo n.º 30
0
Arquivo: facts.py Projeto: pb-it/ralph
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)
Exemplo n.º 31
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
Exemplo n.º 32
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)
Exemplo n.º 33
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
Exemplo n.º 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)
Exemplo n.º 35
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
Exemplo n.º 36
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)
Exemplo n.º 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
Exemplo n.º 38
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
Exemplo n.º 39
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)