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()
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
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
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
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
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
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()
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()
def cisco_component(dev, inv): extra = '' # '\n'.join('%s: %s' % i for i in inv.iteritems()) comp_type = cisco_type(inv['pid']) model, created = ComponentModel.concurrent_get_or_create( type=comp_type.id, size=0, speed=0, cores=0, family=inv['pid'], extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) name = inv['descr'] if not name.lower().startswith('cisco'): name = 'Cisco %s' % name if created: model.name = name[:50] model.save() elif model.name != name[:50]: model.name = name[:50] model.save() comp, created = GenericComponent.concurrent_get_or_create(sn=inv['sn'], device=dev) comp.model = model comp.label = inv['name'][:255] comp.save() return comp
def _component(model_type, pairs, parent, raw): model_type = ComponentType.unknown if "Mach type/model" in pairs: model_name = "%s (%s)" % (pairs["Mach type/model"], pairs["Part no."]) elif "Part no." in pairs: model_name = pairs["Part no."] else: raise DeviceError("No model/part no.") if not model_name.startswith("IBM"): model_name = "IBM " + model_name sn = pairs.get("Mach serial number") if sn in SERIAL_BLACKLIST: sn = None if not sn: sn = pairs["FRU serial no."] if sn in SERIAL_BLACKLIST: sn = None if not sn: raise DeviceError("No serial no.") name = pairs.get("Name") or pairs.get("Product Name") or model_name extra = model_name if "Management" in model_name: model_type = ComponentType.management elif "Fibre Channel" in model_name: model_type = ComponentType.fibre return None # FIXME: merge GenericComponent(model__type=fibre) and FibreChannel elif "Power" in model_name: model_type = ComponentType.power elif "Media" in model_name: model_type = ComponentType.media elif "Cooling" in model_name: model_type = ComponentType.cooling elif "Fan" in model_name: model_type = ComponentType.cooling model, mcreated = ComponentModel.concurrent_get_or_create( type=model_type.id, extra_hash=hashlib.md5(extra).hexdigest(), extra=extra ) model.name = model_name model.save(priority=SAVE_PRIORITY) component, created = GenericComponent.concurrent_get_or_create(device=parent, sn=sn) component.model = model component.label = name firmware = pairs.get("AMM firmware") or pairs.get("FW/BIOS") or pairs.get("Main Application 2") if firmware: component.hard_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"]) else: firmware = pairs.get("Power Module Cooling Device firmware rev.") if firmware: component.hard_firmware = "rev %s" % firmware firmware = pairs.get("Boot ROM") or pairs.get("Main Application 1") or pairs.get("Blade Sys Mgmt Processor") if firmware: component.boot_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"]) firmware = pairs.get("Blade Sys Mgmt Processor") if firmware: component.mgmt_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"]) firmware = pairs.get("Diagnostics") if firmware: component.diag_firmware = "%s %s rev %s" % (firmware["Build ID"], firmware["Rel date"], firmware["Rev"]) component.save(update_last_seen=True, priority=SAVE_PRIORITY) return component
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)
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_memory(pairs, parent, raw, counts, dev_id): try: model = pairs['Mach type/model'] except KeyError: DiscoveryWarning( message="Memory model unknown", plugin=__name__, device=parent, ).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) 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)
def save_storage(storage, dev): mount_points = [] for s in storage: if not s.get('sn'): continue stor, created = Storage.concurrent_get_or_create(device=dev, sn=s.get('sn')) try: stor.size = int(s.get('size')) except ValueError: pass stor.label = s.get('label') model = '{} {}MiB'.format(stor.label, stor.size) stor.mount_point = s.get('mountpoint') mount_points.append(stor.mount_point) extra = '' stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, type=ComponentType.disk.id, speed=0, cores=0, extra=extra, extra_hash=hashlib.md5(extra).hexdigest(), family=model) stor.model.name = model stor.model.save(priority=SAVE_PRIORITY) stor.save(priority=SAVE_PRIORITY) dev.storage_set.exclude(mount_point__in=mount_points).delete()
def handle_hpacu(dev, disks, priority=0): for disk_handle, disk in disks.iteritems(): if not disk.get('serial_number'): continue stor, created = Storage.concurrent_get_or_create(device=dev, sn=disk['serial_number']) stor.device = dev size_value, size_unit = disk['size'].split() stor.size = int(float(size_value) / units.size_divisor[size_unit]) stor.speed = int(disk.get('rotational_speed', 0)) stor.label = '{} {}'.format(' '.join(disk['model'].split()), disk['interface_type']) disk_default = dict( model = 'unknown', firmware_revision = 'unknown', interface_type = 'unknown', size = 'unknown', rotational_speed = 'unknown', status = 'unknown', ) disk_default.update(disk) extra = """Model: {model} Firmware Revision: {firmware_revision} Interface: {interface_type} Size: {size} Rotational Speed: {rotational_speed} Status: {status}""".format(**disk_default) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=priority) stor.save(priority=priority)
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_memory(pairs, parent, raw, counts, dev_id): try: model = pairs['Mach type/model'] except KeyError: DiscoveryWarning( message="Memory model unknown", plugin=__name__, device=parent, ).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) 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)
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()
def handle_lshw_fibre_cards(dev, lshw, is_virtual=False, priority=0): buses = [] for bus in jpath.get_all('..bus', lshw): if not bus: continue if isinstance(bus, list): buses.extend(bus) else: buses.append(bus) buses = filter(lambda item: item['id'].startswith('fiber'), buses) buses.sort(key=lambda item: item['physid']) handled_buses = set() for bus in buses: physid = unicode(bus['physid']) for handled in handled_buses: if physid.startswith(handled): break else: fib, created = FibreChannel.concurrent_get_or_create(device=dev, physical_id=physid) fib.label = "{} {}".format(bus['vendor'], bus['product']) extra = fib.label fib.model, c = ComponentModel.concurrent_get_or_create( type=ComponentType.fibre.id, family=bus['vendor'], extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) fib.model.name = bus['product'] fib.model.save(priority=priority) fib.save(priority=priority) handled_buses.add(physid)
def handle_facts_disks(dev, facts, is_virtual=False): disks = {} _cur_key = None for k, v in facts.iteritems(): if not k.startswith('disk_'): continue k = k[5:] if k.endswith('_product'): _cur_key = 'product' k = k[:-8] elif k.endswith('_revision'): _cur_key = 'revision' k = k[:-9] elif k.endswith('_size'): _cur_key = 'size' k = k[:-5] elif k.endswith('_vendor'): _cur_key = 'vendor' k = k[:-7] elif k.endswith('_serial'): _cur_key = 'serial' k = k[:-7] else: continue disks.setdefault(k, {})[_cur_key] = v.strip() for label, disk in disks.iteritems(): try: if 'size' not in disk or not int(disk['size']): continue except ValueError: continue if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST: continue if disk['product'].lower() in DISK_PRODUCT_BLACKLIST: continue sn = disk.get('serial', '').strip() if sn: stor, created = Storage.concurrent_get_or_create(device=dev, sn=sn) else: stor, created = Storage.concurrent_get_or_create(device=dev, mount_point=label, sn=None) stor.size = disk['size'] = int(int(disk['size']) / 1024 / 1024) stor.label = '{} {} {}'.format(disk['vendor'].strip(), disk['product'].strip(), disk['revision'].strip()) extra = """Vendor: {vendor} Product: {product} Firmware Revision: {revision} Size: {size}""".format(**disk) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=0, type=ComponentType.disk.id, family=disk['vendor'].strip(), extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=SAVE_PRIORITY) stor.save(priority=SAVE_PRIORITY)
def handle_lshw_fibre_cards(dev, lshw, is_virtual=False, priority=0): buses = [] for bus in jpath.get_all('..bus', lshw): if not bus: continue if isinstance(bus, list): buses.extend(bus) else: buses.append(bus) buses = filter(lambda item: item['id'].startswith('fiber'), buses) buses.sort(key=lambda item: item['handle']) handled_buses = set() detected_fc_cards = set() for bus in buses: handle = unicode(bus['handle']) m = re.search(r"([1-9][0-9]*)", handle) if not m: continue physid = m.group(1) if physid in handled_buses: continue fib, created = FibreChannel.concurrent_get_or_create(device=dev, physical_id=physid) fib.label = "{} {}".format(bus['vendor'], bus['product']) extra = fib.label fib.model, c = ComponentModel.concurrent_get_or_create( type=ComponentType.fibre.id, family=bus['vendor'], extra_hash=hashlib.md5(extra).hexdigest()) fib.model.extra = extra fib.model.name = bus['product'] fib.model.save(priority=priority) fib.save(priority=priority) handled_buses.add(physid) detected_fc_cards.add(fib.pk) dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
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 parse_megaraid(facts, dev): disks = {} for k, value in facts.iteritems(): m = MEGARAID_REGEX.match(k) if not m: continue controller = m.group(1) disk = m.group(2) property = m.group(3) disks.setdefault((controller, disk), {})[property] = value.strip() for (controller_handle, disk_handle), disk in disks.iteritems(): disk['vendor'], disk['product'], disk['serial_number'] = \ _handle_inquiry_data(disk.get('inquiry_data', ''), controller_handle, disk_handle) if not disk.get('serial_number') or disk.get('media_type') not in ('Hard Disk Device', 'Solid State Device'): continue if {'coerced_size', 'vendor', 'product', 'pd_type'} - \ set(disk.keys()): # not all required keys present continue if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST: continue if disk['product'].lower() in DISK_PRODUCT_BLACKLIST: continue stor, created = Storage.concurrent_get_or_create(device=dev, sn=disk['serial_number']) stor.device = dev size_value, size_unit, rest = disk['coerced_size'].split(' ', 2) size_value = size_value.replace(',', '') stor.size = int(float(size_value) / units.size_divisor[size_unit]) stor.speed = int(disk.get('rotational_speed', 0)) label_meta = [' '.join(disk['vendor'].split()), disk['product']] if 'pd_type' in disk: label_meta.append(disk['pd_type']) stor.label = ' '.join(label_meta) disk_default = dict( vendor = 'unknown', product = 'unknown', device_firmware_level = 'unknown', pd_type = 'unknown', coerced_size = 'unknown', ) disk_default.update(disk) extra = """Model: {vendor} {product} Firmware Revision: {device_firmware_level} Interface: {pd_type} Size: {coerced_size} """.format(**disk_default) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=SAVE_PRIORITY) stor.save(priority=SAVE_PRIORITY)
def handle_facts_disks(dev, facts, is_virtual=False): disks = {} _cur_key = None for k, v in facts.iteritems(): if not k.startswith('disk_'): continue k = k[5:] if k.endswith('_product'): _cur_key = 'product' k = k[:-8] elif k.endswith('_revision'): _cur_key = 'revision' k = k[:-9] elif k.endswith('_size'): _cur_key = 'size' k = k[:-5] elif k.endswith('_vendor'): _cur_key = 'vendor' k = k[:-7] elif k.endswith('_serial'): _cur_key = 'serial' k = k[:-7] else: continue disks.setdefault(k, {})[_cur_key] = v.strip() for label, disk in disks.iteritems(): try: if 'size' not in disk or not int(disk['size']): continue except ValueError: continue if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST: continue if disk['product'].lower() in DISK_PRODUCT_BLACKLIST: continue sn = disk.get('serial', '').strip() if sn: stor, created = Storage.concurrent_get_or_create( device=dev, sn=sn) else: stor, created = Storage.concurrent_get_or_create( device=dev, mount_point=label, sn=None) stor.size = disk['size'] = int(int(disk['size']) / 1024 / 1024) stor.label = '{} {} {}'.format( disk['vendor'].strip(), disk['product'].strip(), disk['revision'].strip()) extra = """Vendor: {vendor} Product: {product} Firmware Revision: {revision} Size: {size}""".format(**disk) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=0, type=ComponentType.disk.id, family=disk['vendor'].strip(), extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=SAVE_PRIORITY) stor.save(priority=SAVE_PRIORITY)
def handle_megaraid(dev, disks, priority=0): for (controller_handle, disk_handle), disk in disks.iteritems(): disk['vendor'], disk['product'], disk['serial_number'] = \ _handle_inquiry_data( disk.get('inquiry_data', ''), controller_handle, disk_handle ) if not disk.get('serial_number') or disk.get('media_type') not in ( 'Hard Disk Device', 'Solid State Device'): continue if {'coerced_size', 'vendor', 'product', 'pd_type'} - \ set(disk.keys()): # not all required keys present continue if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST: continue if disk['product'].lower() in DISK_PRODUCT_BLACKLIST: continue stor, created = Storage.concurrent_get_or_create( device=dev, sn=disk['serial_number']) stor.device = dev size_value, size_unit, rest = disk['coerced_size'].split(' ', 2) size_value = size_value.replace(',', '') stor.size = int(float(size_value) / units.size_divisor[size_unit]) stor.speed = int(disk.get('rotational_speed', 0)) label_meta = [' '.join(disk['vendor'].split()), disk['product']] if 'pd_type' in disk: label_meta.append(disk['pd_type']) stor.label = ' '.join(label_meta) disk_default = dict( vendor='unknown', product='unknown', device_firmware_level='unknown', pd_type='unknown', coerced_size='unknown', ) disk_default.update(disk) extra = """Model: {vendor} {product} Firmware Revision: {device_firmware_level} Interface: {pd_type} Size: {coerced_size} """.format(**disk_default) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=priority) stor.save(priority=priority)
def parse_smartctl(facts, dev): disks = {} for k, value in facts.iteritems(): m = SMARTCTL_REGEX.match(k) if not m: continue disk = m.group(1) property = m.group(2) disks.setdefault(disk, {})[property] = value.strip() for disk_handle, disk in disks.iteritems(): if not disk.get('serial_number') or disk.get('device_type') != 'disk': continue if {'user_capacity', 'vendor', 'product', 'transport_protocol'} - \ set(disk.keys()): # not all required keys present continue if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST: continue if disk['product'].lower() in DISK_PRODUCT_BLACKLIST: continue stor, created = Storage.concurrent_get_or_create(device=dev, sn=disk['serial_number']) stor.device = dev size_value, size_unit, rest = disk['user_capacity'].split(' ', 2) size_value = size_value.replace(',', '') stor.size = int(int(size_value) / units.size_divisor[size_unit]) stor.speed = int(disk.get('rotational_speed', 0)) label_meta = [' '.join(disk['vendor'].split()), disk['product']] if 'transport_protocol' in disk: label_meta.append(disk['transport_protocol']) stor.label = ' '.join(label_meta) disk_default = dict( vendor = 'unknown', product = 'unknown', revision = 'unknown', transport_protocol = 'unknown', user_capacity = 'unknown', ) disk_default.update(disk) extra = """Model: {vendor} {product} Firmware Revision: {revision} Interface: {transport_protocol} Size: {user_capacity} """.format(**disk_default) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=SAVE_PRIORITY) stor.save(priority=SAVE_PRIORITY)
def _save_shares(dev, volumes): wwns = [] for (label, serial, size, type, speed) in volumes: wwn = normalize_wwn(serial) wwns.append(wwn) model, created = ComponentModel.concurrent_get_or_create( name='MSA %s disk share' % type, type=ComponentType.share.id, family=type, speed=speed) share, created = DiskShare.concurrent_get_or_create(wwn=wwn, device=dev) share.model = model share.label = label share.size = size * 512 / 1024 / 1024 share.snapshot_size = 0 share.save() dev.diskshare_set.exclude(wwn__in=wwns).delete()
def splunk(**options): splunk = Splunk() splunk.start() percent = splunk.progress while percent < 100: if options['verbose']: print(percent) time.sleep(30) percent = splunk.progress hosts = {} total_mb = 0 for item in splunk.results: host = item['host'] mb = float(item['MBytes']) total_mb += mb if host in hosts: hosts[host] += mb else: hosts[host] = mb if options['verbose']: print(len(hosts), 'hosts used', total_mb, ' MiBs total.') for host, usage in hosts.iteritems(): ip = IPAddress.objects.filter(hostname__startswith=host).order_by( '-last_seen') if not ip.count(): if options['verbose']: print('Warning: host', host, 'not found in device database.') continue dev = ip[0].device if not dev: if options['verbose']: print('Warning: host', host, 'not tied to a device in the ' 'database.') continue name = 'Splunk Volume 100 GiB' symbol = 'splunkvolume' model, created = ComponentModel.concurrent_get_or_create( type=ComponentType.unknown.id, speed=0, cores=0, size=0, family=symbol, extra_hash='' ) if created: model.name = name model.save() res, created = SplunkUsage.concurrent_get_or_create( model=model, device=dev, day=date.today()) res.size = usage res.save() return True, 'done.', options
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)
def handle(self, *args, **options): splunk = Splunk() splunk.start() percent = splunk.progress while percent < 100: if options['verbose']: print(percent) time.sleep(30) percent = splunk.progress hosts = {} total_mb = 0 for item in splunk.results: host = item['host'] mb = float(item['MBytes']) total_mb += mb if host in hosts: hosts[host] += mb else: hosts[host] = mb if options['verbose']: print(len(hosts), 'hosts used', total_mb, ' MiBs total.') for host, usage in hosts.iteritems(): ip = IPAddress.objects.filter(hostname__startswith=host).order_by( '-last_seen') if not ip.count(): if options['verbose']: print('Warning: host', host, 'not found in device database.') continue dev = ip[0].device if not dev: if options['verbose']: print('Warning: host', host, 'not tied to a device in the ' 'database.') continue name = 'Splunk Volume 100 GiB' symbol = 'splunkvolume' model, created = ComponentModel.concurrent_get_or_create( type=ComponentType.unknown.id, speed=0, cores=0, size=0, family=symbol, extra_hash='' ) if created: model.name = name model.save() res, created = SplunkUsage.concurrent_get_or_create( model=model, device=dev, day=date.today()) res.size = usage res.save()
def handle_smartctl(dev, disks, priority=0): for disk_handle, disk in disks.iteritems(): if not disk.get('serial_number') or disk.get('device_type') != 'disk': continue if {'user_capacity', 'vendor', 'product', 'transport_protocol'} - \ set(disk.keys()): # not all required keys present continue if disk['vendor'].lower() in DISK_VENDOR_BLACKLIST: continue if disk['product'].lower() in DISK_PRODUCT_BLACKLIST: continue stor, created = Storage.concurrent_get_or_create( device=dev, sn=disk['serial_number']) stor.device = dev size_value, size_unit, rest = disk['user_capacity'].split(' ', 2) size_value = size_value.replace(',', '') stor.size = int(int(size_value) / units.size_divisor[size_unit]) stor.speed = int(disk.get('rotational_speed', 0)) label_meta = [' '.join(disk['vendor'].split()), disk['product']] if 'transport_protocol' in disk: label_meta.append(disk['transport_protocol']) stor.label = ' '.join(label_meta) disk_default = dict( vendor='unknown', product='unknown', revision='unknown', transport_protocol='unknown', user_capacity='unknown', ) disk_default.update(disk) extra = """Model: {vendor} {product} Firmware Revision: {revision} Interface: {transport_protocol} Size: {user_capacity} """.format(**disk_default) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=priority) stor.save(priority=priority)
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)
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)
def _save_shares(dev, shares): wwns = [] for share_id, (label, wwn, snapshot_size, size, type, speed, full) in shares.iteritems(): wwn = normalize_wwn(wwn) wwns.append(wwn) model, created = ComponentModel.concurrent_get_or_create( name='3PAR %s disk share' % type, type=ComponentType.share.id, family=type, speed=speed) share, created = DiskShare.concurrent_get_or_create(wwn=wwn, device=dev) share.model = model share.label = label share.full = full share.size = size share.share_id = share_id share.snapshot_size = snapshot_size share.save() dev.diskshare_set.exclude(wwn__in=wwns).delete()
def parse_hpacu(facts, dev): disks = {} for k, value in facts.iteritems(): m = HPACU_GENERAL_REGEX.match(k) if not m: continue n = HPACU_LOGICAL_PHYSICAL_REGEX.match(m.group(2)) physical_disk = n.group(1) if n else None property = n.group(2) if n else m.group(2) if not physical_disk: continue disks.setdefault(physical_disk, {})[property] = value.strip() for disk_handle, disk in disks.iteritems(): if not disk.get('serial_number'): continue stor, created = Storage.concurrent_get_or_create(device=dev, sn=disk['serial_number']) stor.device = dev size_value, size_unit = disk['size'].split() stor.size = int(float(size_value) / units.size_divisor[size_unit]) stor.speed = int(disk.get('rotational_speed', 0)) stor.label = '{} {}'.format(' '.join(disk['model'].split()), disk['interface_type']) disk_default = dict( model = 'unknown', firmware_revision = 'unknown', interface_type = 'unknown', size = 'unknown', rotational_speed = 'unknown', status = 'unknown', ) disk_default.update(disk) extra = """Model: {model} Firmware Revision: {firmware_revision} Interface: {interface_type} Size: {size} Rotational Speed: {rotational_speed} Status: {status}""".format(**disk_default) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=SAVE_PRIORITY) stor.save(priority=SAVE_PRIORITY)
def splunk(**options): splunk = Splunk() splunk.start() percent = splunk.progress while percent < 100: if options["verbose"]: print(percent) time.sleep(30) percent = splunk.progress hosts = {} total_mb = 0 for item in splunk.results: host = item["host"] mb = float(item["MBytes"]) total_mb += mb if host in hosts: hosts[host] += mb else: hosts[host] = mb if options["verbose"]: print(len(hosts), "hosts used", total_mb, " MiBs total.") for host, usage in hosts.iteritems(): ip = IPAddress.objects.filter(hostname__startswith=host).order_by("-last_seen") if not ip.count(): if options["verbose"]: print("Warning: host", host, "not found in device database.") continue dev = ip[0].device if not dev: if options["verbose"]: print("Warning: host", host, "not tied to a device in the " "database.") continue name = "Splunk Volume 100 GiB" symbol = "splunkvolume" model, created = ComponentModel.concurrent_get_or_create( type=ComponentType.unknown.id, speed=0, cores=0, size=0, family=symbol, extra_hash="" ) if created: model.name = name model.save() res, created = SplunkUsage.concurrent_get_or_create(model=model, device=dev, day=date.today()) res.size = usage res.save() return True, "done.", options
def save_fibre_channel(fcs, dev): detected_fc_cards = [] for f in fcs: pid = f.get('physicalid') model = f.get('model') manufacturer = f.get('manufacturer') fib, created = FibreChannel.concurrent_get_or_create(device=dev, physical_id=pid) fib.label = f.get('label') extra = '%s %s %s %s' % (fib.label, pid, manufacturer, model) fib.model, c = ComponentModel.concurrent_get_or_create( type=ComponentType.fibre.id, family=fib.label, extra_hash=hashlib.md5(extra.encode('utf-8')).hexdigest()) fib.model.extra = extra fib.model.name = model if model else fib.label fib.model.save(priority=SAVE_PRIORITY) fib.save(priority=SAVE_PRIORITY) detected_fc_cards.append(fib.pk) dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
def make_component(name, symbol, key, multiplier, unit): if key not in tenant: return model, created = ComponentModel.concurrent_get_or_create( type=ComponentType.unknown.id, speed=0, cores=0, size=0, family=symbol, extra_hash='' ) if created: model.name = name model.save() res, created = GenericComponent.concurrent_get_or_create( model=model, device=dev, sn='%s-%s' % (symbol, tenant['tenant_id'])) if created: res.label = unit res.save() if model.group and model.group.price: value = tenant[key] / multiplier cost = value * model.group.price / (model.group.size_modifier or 1) total_daily_cost[0] += cost
def save_fibre_channel(fcs, dev): detected_fc_cards = [] for f in fcs: pid = f.get('physicalid') model = f.get('model') manufacturer = f.get('manufacturer') fib, created = FibreChannel.concurrent_get_or_create(device=dev, physical_id=pid) fib.label = f.get('label') extra = '%s %s %s %s' % (fib.label, pid, manufacturer, model) fib.model, c = ComponentModel.concurrent_get_or_create( type=ComponentType.fibre.id, family=fib.label, extra_hash=hashlib.md5(extra.encode('utf-8')).hexdigest()) fib.model.extra = extra fib.model.name = model if model else fib.label fib.model.save(priority=SAVE_PRIORITY) fib.save(priority=SAVE_PRIORITY) detected_fc_cards.append(fib.pk) dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
def cisco_component(dev, inv): extra = '' # '\n'.join('%s: %s' % i for i in inv.iteritems()) comp_type = cisco_type(inv['pid']) model, created = ComponentModel.concurrent_get_or_create( type=comp_type.id, size=0, speed=0, cores=0, family=inv['pid'], extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) name = inv['descr'] if not name.lower().startswith('cisco'): name = 'Cisco %s' % name if created: model.name = name[:50] model.save() elif model.name != name[:50]: model.name = name[:50] model.save() comp, created = GenericComponent.concurrent_get_or_create( sn=inv['sn'], device=dev) comp.model = model comp.label = inv['name'][:255] comp.save() return comp
def handle_lshw_storage(dev, lshw, is_virtual=False, priority=0): mount_points, storages = get_storage_from_lshw(lshw) dev.storage_set.filter(mount_point__in=mount_points).delete() for storage in storages: if storage['sn']: stor, created = Storage.concurrent_get_or_create(sn=storage['sn'], device=dev) stor.mount_point = storage['mount_point'] else: stor, created = Storage.concurrent_get_or_create( sn=None, device=dev, mount_point=storage['mount_point']) stor.size = storage['size'] stor.speed = storage['speed'] stor.label = storage['label'] stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(storage['extra']).hexdigest()) stor.model.extra = storage['extra'] stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=priority) stor.save(priority=priority)
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)
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)
def save_storage(storage, dev): mount_points = [] for s in storage: if not s.get('sn'): continue stor, created = Storage.concurrent_get_or_create(device=dev, sn=s.get('sn')) try: stor.size = int(s.get('size')) except ValueError: pass stor.label = s.get('label') model = '{} {}MiB'.format(stor.label, stor.size) stor.mount_point = s.get('mountpoint') mount_points.append(stor.mount_point) extra = '' stor.model, c = ComponentModel.concurrent_get_or_create(size=stor.size, type=ComponentType.disk.id, speed=0, cores=0, extra=extra, extra_hash=hashlib.md5(extra).hexdigest(), family=model) stor.model.name = model stor.model.save(priority=SAVE_PRIORITY) stor.save(priority=SAVE_PRIORITY) dev.storage_set.exclude(mount_point__in=mount_points).delete()
def make_component(name, symbol, key, multiplier, unit): if key not in tenant: return model, created = ComponentModel.concurrent_get_or_create( type=ComponentType.unknown.id, speed=0, cores=0, size=0, family=symbol, extra_hash='') if created: model.name = name model.save() res, created = GenericComponent.concurrent_get_or_create( model=model, device=dev, sn='%s-%s' % (symbol, tenant['tenant_id'])) if created: res.label = unit res.save() if model.group and model.group.price: value = tenant[key] / multiplier cost = value * model.group.price / (model.group.size_modifier or 1) total_daily_cost[0] += cost
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()
def handle_lshw_fibre_cards(dev, lshw, is_virtual=False, priority=0): buses = [] for bus in jpath.get_all('..bus', lshw): if not bus: continue if isinstance(bus, list): buses.extend(bus) else: buses.append(bus) buses = filter(lambda item: item['id'].startswith('fiber'), buses) buses.sort(key=lambda item: item['handle']) handled_buses = set() detected_fc_cards = set() for bus in buses: handle = unicode(bus['handle']) m = re.search(r"([1-9][0-9]*)", handle) if not m: continue physid = m.group(1) if physid in handled_buses: continue fib, created = FibreChannel.concurrent_get_or_create( device=dev, physical_id=physid) fib.label = "{} {}".format(bus['vendor'], bus['product']) extra = fib.label fib.model, c = ComponentModel.concurrent_get_or_create( type=ComponentType.fibre.id, family=bus['vendor'], extra_hash=hashlib.md5(extra).hexdigest()) fib.model.extra = extra fib.model.name = bus['product'] fib.model.save(priority=priority) fib.save(priority=priority) handled_buses.add(physid) detected_fc_cards.add(fib.pk) dev.fibrechannel_set.exclude(pk__in=detected_fc_cards).delete()
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
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)
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.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=priority) mem.save(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) 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=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)
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
def handle_lshw_storage(dev, lshw, is_virtual=False, priority=0): storages = [] for storage in jpath.get_all('..disk', lshw): if not storage: continue if isinstance(storage, list): storages.extend(storage) else: storages.append(storage) storages.sort(key=get_logical_name) mount_points = set() for stor in storages: if 'logicalname' not in stor: continue ln = stor['logicalname'] if isinstance(ln, list): mount_points.update(ln) else: mount_points.add(ln) dev.storage_set.filter(mount_point__in=mount_points).delete() for storage in storages: if 'size' in storage: size = storage['size'] elif 'capacity' in storage: size = storage['capacity'] else: # empty slot continue sn = unicode(storage.get('serial') or '') or None if not sn or sn.startswith('QM000') or \ storage.get('vendor', '').strip().lower() in DISK_VENDOR_BLACKLIST or \ storage.get('product', '').strip().lower() in DISK_PRODUCT_BLACKLIST: continue if sn: stor, created = Storage.concurrent_get_or_create(sn=sn, device=dev) stor.mount_point = storage.get('logicalname', None) else: stor, created = Storage.concurrent_get_or_create( sn=None, device=dev, mount_point=storage.get('logicalname', None)) stor.size = int(size['value']) stor.size /= units.size_divisor[size['units']] stor.size = int(stor.size) stor.speed = 0 if storage.get('vendor', '').strip(): stor.label = storage['vendor'].strip() + ' ' else: stor.label = '' if storage.get('product', '').strip(): stor.label += storage['product'].strip() elif storage.get('description', '').strip(): stor.label += storage['description'].strip() else: stor.label += 'Generic disk' caps = storage['capabilities'] extra = "\n".join([ ": ".join((unicode(key), unicode(caps[key]) or '')) for key in sorted(caps.keys()) ]) stor.model, c = ComponentModel.concurrent_get_or_create( size=stor.size, speed=stor.speed, type=ComponentType.disk.id, family='', extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) stor.model.name = '{} {}MiB'.format(stor.label, stor.size) stor.model.save(priority=priority) stor.save(priority=priority)
def _add_hp_oa_devices(devices, device_type, parent=None): if devices and not isinstance(devices, list): devices = [devices] for i, device in enumerate(devices): bay = device['BAY']['CONNECTION2']['BLADESYMBOLICNUMBER'] or str(device['BAY']['CONNECTION']) name = device['PN'].strip() or device['SPN'].strip() if not name.startswith('HP'): name = 'HP ' + name firmware = str(device.get('FWRI', '')) sn = device['SN'].strip() if sn in SERIAL_BLACKLIST: sn = None if not sn: sn = device['BSN'].strip() if sn in SERIAL_BLACKLIST: sn = None try: ip = network.validate_ip(device['MGMTIPADDR']) except ValueError: continue ip_address, created = IPAddress.concurrent_get_or_create(address=str(ip)) if created: ip_address.hostname = network.hostname(ip_address.address) ip_address.snmp_name = name ip_address.save(update_last_seen=True) # no priorities for IP addresses if device_type == DeviceType.management: ip_address.is_management = True if parent and not parent.management: parent.management = ip_address parent.save(priority=SAVE_PRIORITY) extra = name model, mcreated = ComponentModel.concurrent_get_or_create( type=ComponentType.management.id, extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) model.name = name model.save(priority=SAVE_PRIORITY) component, created = GenericComponent.concurrent_get_or_create( device=parent, sn=sn) component.model = model component.label = name component.save(priority=SAVE_PRIORITY) if ip: ip_address.is_management = True ip_address.device = parent ip_address.save() # no priorities for IP addresses continue if device_type == DeviceType.switch and 'SAN' in name: device_type = DeviceType.fibre_channel_switch ethernets = list(_get_ethernets(device)) if not (ip and name and (sn or ethernets)): continue dev = None if ip and device_type in (DeviceType.switch, DeviceType.fibre_channel_switch): ip_addr, ip_created = IPAddress.concurrent_get_or_create(address=ip) if ip_addr.device: dev = ip_addr.device dev.parent = parent if dev is None: dev = Device.create(sn=sn, model_name=name, model_type=device_type, ethernets=ethernets, parent=parent, priority=SAVE_PRIORITY) if firmware: dev.hard_firmware = firmware if bay: name = '%s [%s]' % (name, bay) if bay: if 'A' in bay or 'B' in bay: dev.chassis_position = int(bay[:-1]) if bay[-1] == 'A': dev.chassis_position += 1000 elif bay[-1] == 'B': dev.chassis_position += 2000 else: dev.chassis_position = int(bay) dev.position = bay else: dev.chassis_position = i + 1 dev.save(update_last_seen=True, priority=SAVE_PRIORITY) ip_address.device = dev ip_address.save(update_last_seen=True) # no priorities for IP addresses
def _component(model_type, pairs, parent, raw): model_type = ComponentType.unknown if 'Mach type/model' in pairs: model_name = '%s (%s)' % (pairs['Mach type/model'], pairs['Part no.']) elif 'Part no.' in pairs: model_name = pairs['Part no.'] else: raise DeviceError('No model/part no.') if not model_name.startswith('IBM'): model_name = 'IBM ' + model_name sn = pairs.get('Mach serial number') if sn in SERIAL_BLACKLIST: sn = None if not sn: sn = pairs['FRU serial no.'] if sn in SERIAL_BLACKLIST: sn = None if not sn: raise DeviceError('No serial no.') name = pairs.get('Name') or pairs.get('Product Name') or model_name extra = model_name if 'Management' in model_name: model_type = ComponentType.management elif 'Fibre Channel' in model_name: model_type = ComponentType.fibre return None # FIXME: merge GenericComponent(model__type=fibre) and FibreChannel elif 'Power' in model_name: model_type = ComponentType.power elif 'Media' in model_name: model_type = ComponentType.media elif 'Cooling' in model_name: model_type = ComponentType.cooling elif 'Fan' in model_name: model_type = ComponentType.cooling model, mcreated = ComponentModel.concurrent_get_or_create( type=model_type.id, extra_hash=hashlib.md5(extra).hexdigest(), extra=extra) model.name = model_name model.save(priority=SAVE_PRIORITY) component, created = GenericComponent.concurrent_get_or_create( device=parent, sn=sn) component.model = model component.label = name firmware = (pairs.get('AMM firmware') or pairs.get('FW/BIOS') or pairs.get('Main Application 2')) if firmware: component.hard_firmware = '%s %s rev %s' % ( firmware['Build ID'], firmware['Rel date'], firmware['Rev']) else: firmware = pairs.get('Power Module Cooling Device firmware rev.') if firmware: component.hard_firmware = 'rev %s' % firmware firmware = (pairs.get('Boot ROM') or pairs.get('Main Application 1') or pairs.get('Blade Sys Mgmt Processor')) if firmware: component.boot_firmware = '%s %s rev %s' % ( firmware['Build ID'], firmware['Rel date'], firmware['Rev']) firmware = (pairs.get('Blade Sys Mgmt Processor')) if firmware: component.mgmt_firmware = '%s %s rev %s' % ( firmware['Build ID'], firmware['Rel date'], firmware['Rev']) firmware = (pairs.get('Diagnostics')) if firmware: component.diag_firmware = '%s %s rev %s' % ( firmware['Build ID'], firmware['Rel date'], firmware['Rev']) component.save(update_last_seen=True, priority=SAVE_PRIORITY) return component
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