def save_device_data(data, remote_ip): device = data['device'] ethernets = [ Eth(e.get('label'), MACAddressField.normalize(e.get('mac')), str_to_ethspeed(e.get('speed'))) for e in data['ethernets'] if MACAddressField.normalize(e.get('mac')) not in MAC_PREFIX_BLACKLIST] sn = device.get('sn') vendor = device.get('vendor', '') if not ethernets and not sn: raise NoRequiredDataError('No MAC addresses and no device SN.') ip_addresses = [ e['ipaddress'] for e in data['ethernets'] if e.get('ipaddress') ] if not ip_addresses: raise NoRequiredIPAddressError( "Couldn't find any IP address for this device." ) try: dev = Device.create( sn=sn, ethernets=ethernets, model_name='%s %s %s' % ( device.get('caption'), vendor, device.get('version'), ), model_type=DeviceType.unknown, priority=SAVE_PRIORITY ) except ValueError as e: DiscoveryWarning( message="Failed to create device: " + str(e), plugin=__name__, ip=str(remote_ip), ).save() return None dev.save(priority=SAVE_PRIORITY) os = data['operating_system'] o = OperatingSystem.create(dev, os_name=os.get('label'), family='Windows', priority=SAVE_PRIORITY) o.memory = int(os['memory']) o.storage = int(os['storage']) o.cores_count = int(os['corescount']) o.save(priority=SAVE_PRIORITY) for ip in ip_addresses: ip_address, _ = IPAddress.concurrent_get_or_create(address=str(ip)) ip_address.device = dev ip_address.is_management = False ip_address.save() vendor = vendor.lower() is_virtual = any(virtual in vendor for virtual in CPU_VIRTUAL_LIST) save_processors(data['processors'], dev, is_virtual) save_memory(data['memory'], dev) save_storage(data['storage'], dev) save_shares(data['shares'], dev, ip_address) save_fibre_channel(data['fcs'], dev) save_software(data.get('software', []), dev) return dev
def save_device_data(data, remote_ip): device = data['device'] ethernets = [ Eth(e.get('label'), MACAddressField.normalize(e.get('mac')), str_to_ethspeed(e.get('speed'))) for e in data['ethernets'] if MACAddressField.normalize(e.get('mac')) not in MAC_PREFIX_BLACKLIST ] sn = device.get('sn') vendor = device.get('vendor', '') if not ethernets and not sn: raise NoRequiredDataError('No MAC addresses and no device SN.') ip_addresses = [ e['ipaddress'] for e in data['ethernets'] if e.get('ipaddress') ] if not ip_addresses: raise NoRequiredIPAddressError( "Couldn't find any IP address for this device.") try: dev = Device.create(sn=sn, ethernets=ethernets, model_name='%s %s %s' % ( device.get('caption'), vendor, device.get('version'), ), model_type=DeviceType.unknown, priority=SAVE_PRIORITY) except ValueError as e: DiscoveryWarning( message="Failed to create device: " + str(e), plugin=__name__, ip=str(remote_ip), ).save() return None dev.save(priority=SAVE_PRIORITY) os = data['operating_system'] o = OperatingSystem.create(dev, os_name=os.get('label'), family='Windows', priority=SAVE_PRIORITY) o.memory = int(os['memory']) o.storage = int(os['storage']) o.cores_count = int(os['corescount']) o.save(priority=SAVE_PRIORITY) for ip in ip_addresses: ip_address, _ = IPAddress.concurrent_get_or_create(address=str(ip)) ip_address.device = dev ip_address.is_management = False ip_address.save() vendor = vendor.lower() is_virtual = any(virtual in vendor for virtual in CPU_VIRTUAL_LIST) save_processors(data['processors'], dev, is_virtual) save_memory(data['memory'], dev) save_storage(data['storage'], dev) save_shares(data['shares'], dev, ip_address) save_fibre_channel(data['fcs'], dev) save_software(data.get('software', []), dev) return dev
def _create_device(data): ethernets = [Eth( 'DEPLOYMENT MAC', MACAddressField.normalize(data['mac']), EthernetSpeed.unknown )] dev = Device.create( ethernets=ethernets, model_type=DeviceType.unknown, model_name='Unknown', verified=True, ) dev.name = data['hostname'] try: dev.parent = Device.objects.get(sn=data['rack_sn']) except Device.DoesNotExist: pass dev.save() IPAddress.objects.create( address=data['ip'], device=dev, hostname=data['hostname'], ) if management_ip_unique(data['management_ip']): IPAddress.objects.create( address=data['management_ip'], device=dev, is_management=True ) return dev
def _ssh_ssg(ip_address, user, password): ssh = _connect_ssh(ip_address, user, password) lines = ssh.ssg_command('get system') pairs = parse.pairs(lines=lines[:10]) name = pairs['Product Name'] version = pairs['Hardware Version'].split(',', 1)[0] model = '%s %s' % (name, version) mac = pairs['Base Mac'].replace('.', '').upper() sn = pairs['Serial Number'].split(',', 1)[0] result = { 'type': DeviceType.firewall.raw, 'model_name': model, 'mac_addresses': [MACAddressField.normalize(mac)], 'hostname': name, 'management_ip_addresses': [ip_address], 'parts': [{ 'boot_firmware': pairs['Software Version'].split(',', 1)[0], 'type': ComponentType.power.raw, }], } if sn not in SERIAL_BLACKLIST: result['serial_number'] = sn return result
def _create_device(data): ethernets = [ Eth('DEPLOYMENT MAC', MACAddressField.normalize(data['mac']), EthernetSpeed.unknown) ] dev = Device.create( ethernets=ethernets, model_type=DeviceType.unknown, model_name='Unknown', verified=True, ) dev.name = data['hostname'] try: dev.parent = Device.objects.get(sn=data['rack_sn']) except Device.DoesNotExist: pass dev.save() IPAddress.objects.create( address=data['ip'], device=dev, hostname=data['hostname'], ) if management_ip_unique(data['management_ip']): IPAddress.objects.create(address=data['management_ip'], device=dev, is_management=True) return dev
def _add_dev_blade(ip, pairs, parent, raw, counts, dev_id): if '[' in dev_id: pos = int(dev_id[dev_id.find('[') + 1:dev_id.find(']')]) else: pos = None dev = _dev(DeviceType.blade_server, pairs, parent, raw) dev.chassis_position = pos if pos: dev.position = '%02d' % pos else: dev.position = '' counts.blade += 1 dev.save(update_last_seen=True, priority=SAVE_PRIORITY) for i in range(1, 9): name = 'MAC Address %d' % i mac = pairs.get(name) if mac == 'Not Available' or mac is None: continue eth, created = Ethernet.concurrent_get_or_create( mac=MACAddressField.normalize(mac), defaults=dict(device=dev), ) eth.label = name eth.save(priority=SAVE_PRIORITY) return dev
def create_deployments(data, user, mass_deployment): for item in data: mac = MACAddressField.normalize(item['mac']) dev = None try: dev = Device.objects.get(ethernet__mac=mac) except Device.DoesNotExist: if 'ralph_assets' in settings.INSTALLED_APPS and item[ 'asset_identity' ]: from ralph_assets.api_ralph import get_asset_by_sn_or_barcode asset = get_asset_by_sn_or_barcode(item['asset_identity']) if asset: try: dev = Device.objects.get(pk=asset['device_id']) except Device.DoesNotExist: pass if not dev: dev = _create_device(item) Deployment.objects.create( user=user, device=dev, mac=mac, ip=item['ip'], hostname=item['hostname'], preboot=item['preboot'], venture=item['venture'], venture_role=item['venture_role'], mass_deployment=mass_deployment, )
def _validate_hostname(hostname, mac, parsed_hostnames, row_number): mac = MACAddressField.normalize(mac) try: dev = Device.admin_objects.get(ethernet__mac=mac) except Device.DoesNotExist: if hostname_exists(hostname): raise forms.ValidationError( "Row %s: Hostname already exists." % row_number ) else: ip_addresses = list( dev.ipaddress_set.values_list('address', flat=True) ) ip_addresses_in_dns = find_addresses_for_hostname(hostname) for ip in ip_addresses_in_dns: if ip not in ip_addresses: raise forms.ValidationError( "Row %s: Using an old device %s failed. " "Exists A or PTR records in DNS which are not assigned " "to device IP addresses." % (row_number, dev) ) if Deployment.objects.filter(hostname=hostname).exists(): raise forms.ValidationError( "Row %s: Running deployment with hostname: %s already " "exists." % (row_number, hostname) ) if hostname in parsed_hostnames: raise forms.ValidationError( "Row %s: Duplicated hostname. " "Please check previous rows..." % row_number )
def _get_address_candidates(address): try: ip_address = str(ipaddr.IPAddress(address)) except ValueError: ip_address = None try: mac = MACAddressField.normalize(address) except ValueError: mac = None if not mac: hostname = address if ip_address: candidates = IPAddress.objects.filter(address=ip_address, ) elif mac: ips = { str(ip) for ip in DHCPEntry.objects.filter( mac=mac).values_list('ip', flat=True) } candidates = IPAddress.objects.filter(address__in=ips) else: candidates = IPAddress.objects.filter( Q(hostname=hostname) | Q(address__in=find_addresses_for_hostname(hostname))) return candidates.filter(device__deleted=False, device__model__type__in={ DeviceType.rack_server, DeviceType.blade_server, DeviceType.virtual_server, DeviceType.unknown, })
def _validate_deploy_children(mac, row_number): mac = MACAddressField.normalize(mac) try: device = Device.admin_objects.get(ethernet__mac=mac) except Device.DoesNotExist: return if device.deleted: return children = device.child_set.filter(deleted=False) if children.exists(): raise forms.ValidationError( "Row %d: Device with MAC %s exists and has child devices " "[%s]. Delete the child devices first." % ( row_number, mac, ', '.join(str(d) for d in children.all()), )) if device.servermount_set.filter(device__deleted=False).exists(): raise forms.ValidationError( "Row %d: Device with MAC %s exists and exports shares." % (row_number, mac)) for share in device.diskshare_set.all(): if any(( share.disksharemount_set.filter( device__deleted=False).exists(), share.disksharemount_set.filter( server__deleted=False).exists(), )): raise forms.ValidationError( "Row %d: Device with MAC %s exists and exports disks." % (row_number, mac))
def _validate_hostname(hostname, mac, parsed_hostnames, row_number): mac = MACAddressField.normalize(mac) try: dev = Device.admin_objects.get(ethernet__mac=mac) except Device.DoesNotExist: if hostname_exists(hostname): raise forms.ValidationError("Row %s: Hostname already exists." % row_number) else: ip_addresses = list(dev.ipaddress_set.values_list('address', flat=True)) ip_addresses_in_dns = find_addresses_for_hostname(hostname) for ip in ip_addresses_in_dns: if ip not in ip_addresses: raise forms.ValidationError( "Row %s: Using an old device %s failed. " "Exists A or PTR records in DNS which are not assigned " "to device IP addresses." % (row_number, dev)) if Deployment.objects.filter(hostname=hostname).exists(): raise forms.ValidationError( "Row %s: Running deployment with hostname: %s already " "exists." % (row_number, hostname)) if hostname in parsed_hostnames: raise forms.ValidationError("Row %s: Duplicated hostname. " "Please check previous rows..." % row_number)
def _snmp_modular_macs(ip_address, ip_address_is_management, snmp_community): oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 12, 0) # Max blades message = snmp_command( ip_address, snmp_community, oid, attempts=1, timeout=0.5, ) max_blades = int(message[0][1]) blades_macs = {} for blade_no in range(1, max_blades + 1): oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 202, 3, 1, 1, blade_no) blades_macs[blade_no] = set( snmp_macs( ip_address, snmp_community, oid, attempts=1, timeout=0.5, ), ) results = [] management_ip_addresses = [] if ip_address_is_management: management_ip_addresses.append(ip_address) for i, macs in blades_macs.iteritems(): unique_macs = macs for j, other_macs in blades_macs.iteritems(): if i == j: continue unique_macs -= other_macs if unique_macs: results.append({ 'type': unicode(DeviceType.blade_server), 'model_name': 'Intel Modular Blade', 'mac_addresses': [ MACAddressField.normalize(mac) for mac in unique_macs ], 'management_ip_addresses': management_ip_addresses, 'chassis_position': i, }) return results
def _get_mac_addresses(ssh): mac_addresses = [] for line in _ssh_lines(ssh, "show chassis mac-addresses"): line = line.lower() if line.startswith("public base address"): mac_addresses.append(MACAddressField.normalize(line.replace("public base address", "").strip())) return mac_addresses
def _validate_deploy_children(mac, row_number): mac = MACAddressField.normalize(mac) try: device = Device.admin_objects.get(ethernet__mac=mac) except Device.DoesNotExist: return if device.deleted: return children = device.child_set.filter(deleted=False) if children.exists(): raise forms.ValidationError( "Row %d: Device with MAC %s exists and has child devices " "[%s]. Delete the child devices first." % ( row_number, mac, ', '.join(str(d) for d in children.all()), ) ) if device.servermount_set.filter(device__deleted=False).exists(): raise forms.ValidationError( "Row %d: Device with MAC %s exists and exports shares." % (row_number, mac) ) for share in device.diskshare_set.all(): if any(( share.disksharemount_set.filter(device__deleted=False).exists(), share.disksharemount_set.filter(server__deleted=False).exists(), )): raise forms.ValidationError( "Row %d: Device with MAC %s exists and exports disks." % (row_number, mac) )
def create_deployments(data, user, mass_deployment): for item in data: mac = MACAddressField.normalize(item['mac']) dev = None try: dev = Device.objects.get(ethernet__mac=mac) except Device.DoesNotExist: if 'ralph_assets' in settings.INSTALLED_APPS and item[ 'asset_identity']: from ralph_assets.api_ralph import get_asset_by_sn_or_barcode asset = get_asset_by_sn_or_barcode(item['asset_identity']) if asset: try: dev = Device.objects.get(pk=asset['device_id']) except Device.DoesNotExist: pass if not dev: dev = _create_device(item) Deployment.objects.create( user=user, device=dev, mac=mac, ip=item['ip'], hostname=item['hostname'], preboot=item['preboot'], venture=item['venture'], venture_role=item['venture_role'], mass_deployment=mass_deployment, service=ServiceCatalog.objects.get(name=item['service']), device_environment=DeviceEnvironment.objects.get( name=item['device_environment'], ), )
def _get_mac_addresses(ssh): mac_addresses = [] for line in _ssh_lines(ssh, 'show chassis mac-addresses'): compar = re.match('.*(base|public) address[ ]*(.*)', line.lower()) if compar is not None: mac_addresses.append(MACAddressField.normalize(compar.groups()[1])) return mac_addresses
def _get_mac_addresses(ip_address, user, password): opener = urllib2.build_opener() request = urllib2.Request( LOGIN_URL_TEMPLATE.format(ip_address=ip_address), 'WEBVAR_USERNAME={user}&WEBVAR_PASSWORD={password}'.format( user=user, password=password, ), ) session = _get_code( opener.open(request, timeout=5), re.compile(r"'SESSION_COOKIE'\s*:\s*'([^']*)'"), ) request = urllib2.Request( MAC_URL_TEMPLATE.format(ip_address=ip_address), headers={'Cookie': 'SessionCookie=%s' % session}, ) json_data = _get_code( opener.open(request, timeout=5), re.compile(r"WEBVAR_STRUCTNAME_GETMBMAC\s*:\s*\[({[^\}]*})"), ).replace("'", '"') macs = json.loads(json_data).values() request = urllib2.Request( MGMT_MAC_URL_TEMPLATE.format(ip_address=ip_address), headers={'Cookie': 'SessionCookie=%s' % session}, ) json_data = _get_code( opener.open(request, timeout=5), re.compile(r"WEBVAR_STRUCTNAME_HL_GETLANCONFIG\s*:\s*\[({[^\}]*})"), ).replace("'", '"') macs.append(json.loads(json_data)['MAC']) return [ MACAddressField.normalize(mac) for mac in macs if mac.replace(':', '').upper()[:6] not in MAC_PREFIX_BLACKLIST ]
def _get_address_candidates(address): try: ip_address = str(ipaddr.IPAddress(address)) except ValueError: ip_address = None try: mac = MACAddressField.normalize(address) except ValueError: mac = None if not mac: hostname = address if ip_address: candidates = IPAddress.objects.filter( address=ip_address, ) elif mac: ips = { str(ip) for ip in DHCPEntry.objects.filter(mac=mac).values_list('ip', flat=True) } candidates = IPAddress.objects.filter(address__in=ips) else: candidates = IPAddress.objects.filter( Q(hostname=hostname) | Q(address__in=find_addresses_for_hostname(hostname)) ) return candidates.filter( device__deleted=False, device__model__type__in={ DeviceType.rack_server, DeviceType.blade_server, DeviceType.virtual_server, DeviceType.unknown, } )
def _get_lldp_info(ssh, messages=[]): """ Parse LLDPCTL output and return informations about connected devices. """ stdin, stdout, stderr = ssh.exec_command( "/usr/bin/sudo /usr/sbin/lldpctl", ) network_connections = [] connection = {} for line in stdout.readlines(): if not connection: connection = { 'connection_type': ConnectionType.network.name, 'details': {} } if "Interface:" in line: connection['details']['outbound_port'] = line.strip().split( ":")[1].split(",")[0].strip() if "ChassisID:" in line: connection[ 'connected_device_mac_addresses'] = MACAddressField.normalize( line.strip().split(':', 1)[1].replace('mac', '').strip()) if "MgmtIP:" in line: connection['connected_device_ip_addresses'] = line.strip().split( ":")[1].strip() if "PortID:" in line: connection['details']['inbound_port'] = line.strip().split( ":")[1].strip() network_connections.append(connection) connection = {} return network_connections
def save_device_data(data, remote_ip): device = data["device"] ethernets = [ Eth(e.get("label"), MACAddressField.normalize(e.get("mac")), str_to_ethspeed(e.get("speed"))) for e in data["ethernets"] if MACAddressField.normalize(e.get("mac")) not in MAC_PREFIX_BLACKLIST ] sn = device.get("sn") vendor = device.get("vendor", "") if not ethernets and not sn: raise NoRequiredDataError("No MAC addresses and no device SN.") ip_addresses = [e["ipaddress"] for e in data["ethernets"] if e.get("ipaddress")] if not ip_addresses: raise NoRequiredIPAddressError("Couldn't find any IP address for this device.") try: dev = Device.create( sn=sn, ethernets=ethernets, model_name="%s %s %s" % (device.get("caption"), vendor, device.get("version")), model_type=DeviceType.unknown, priority=SAVE_PRIORITY, ) except ValueError as e: DiscoveryWarning(message="Failed to create device: " + str(e), plugin=__name__, ip=str(remote_ip)).save() return None dev.save(priority=SAVE_PRIORITY) os = data["operating_system"] o = OperatingSystem.create(dev, os_name=os.get("label"), family="Windows", priority=SAVE_PRIORITY) o.memory = int(os["memory"]) o.storage = int(os["storage"]) o.cores_count = int(os["corescount"]) o.save(priority=SAVE_PRIORITY) for ip in ip_addresses: ip_address, _ = IPAddress.concurrent_get_or_create(address=str(ip)) ip_address.device = dev ip_address.is_management = False ip_address.save() vendor = vendor.lower() is_virtual = any(virtual in vendor for virtual in CPU_VIRTUAL_LIST) save_processors(data["processors"], dev, is_virtual) save_memory(data["memory"], dev) save_storage(data["storage"], dev) save_shares(data["shares"], dev, ip_address) save_fibre_channel(data["fcs"], dev) save_software(data.get("software", []), dev) return dev
def handle_facts_mac_addresses(facts): mac_addresses = set() for interface in facts['interfaces'].split(','): mac_address = facts.get('macaddress_{}'.format(interface)) if not mac_address: continue mac_addresses.add(MACAddressField.normalize(mac_address)) return list(mac_addresses)
class DHCPEntry(TimeTrackable): mac = MACAddressField(verbose_name=_("MAC address"), unique=False) ip = db.CharField(verbose_name=_("IP address"), blank=True, unique=False, default="", max_length=len('xxx.xxx.xxx.xxx')) class Meta: verbose_name = _("DHCP entry") verbose_name_plural = _("DHCP entries")
def get_default_mac(facts): for suffix in ('', '_eth0', '_igb0', '_bnx0', '_bge0', '_nfo0', '_nge0'): mac = facts.get('macaddress{}'.format(suffix)) if mac: result = MACAddressField.normalize(mac) if result[:6] in MAC_PREFIX_BLACKLIST: continue return result return None
def _get_mac_addresses(ssh): mac_addresses = [] for line in _ssh_lines(ssh, 'show chassis mac-addresses'): line = line.lower() if line.startswith('public base address'): mac_addresses.append( MACAddressField.normalize( line.replace('public base address', '').strip())) return mac_addresses
def _find_device(mac): """Find the device to be re-used for this deployment. Return None if no matching device is found. """ mac = MACAddressField.normalize(mac) try: return Device.admin_objects.get(ethernet__mac=mac) except Device.DoesNotExist: return None
def _get_cluster_member(ssh, ip_address): stdin, stdout, stderr = ssh.exec_command("ifconfig eth0 | head -n 1") mac = stdout.readline().split()[-1] return { "model_name": "Proxmox", "mac_addresses": [MACAddressField.normalize(mac)], "installed_software": [{"model_name": "Proxmox", "path": "proxmox"}], "system_ip_addresses": [ip_address], }
def clean_mac(self): mac = self.cleaned_data['mac'] try: mac = MACAddressField.normalize(mac) if not mac: raise ValueError() except ValueError: raise forms.ValidationError("Invalid MAC address") return mac
def test_archivization(self): id = self.deployment.id data = {} for field in self.deployment._meta.fields: data[field.name] = getattr(self.deployment, field.name) if field.name == 'mac': data[field.name] = MACAddressField.normalize(data[field.name]) self.deployment.archive() archive = ArchivedDeployment.objects.get(pk=id) archive_data = {} for field in archive._meta.fields: archive_data[field.name] = getattr(archive, field.name) if field.name == 'mac': archive_data[field.name] = MACAddressField.normalize( archive_data[field.name]) self.assertEqual(data, archive_data) with self.assertRaises(Deployment.DoesNotExist): Deployment.objects.get(pk=id)
def is_mac_valid(eth): try: mac = MACAddressField.normalize(eth.mac) for black in MAC_PREFIX_BLACKLIST: if mac.startswith(black): return False return True except ValueError: return False
def scan_address(ip_address, **kwargs): if 'nx-os' in (kwargs.get('snmp_name', '') or '').lower(): raise NoMatchError('Incompatible Nexus found.') if kwargs.get('http_family') not in ('Unspecified', 'Cisco'): raise NoMatchError('It is not Cisco.') ssh = _connect_ssh(ip_address) hostname = network.hostname(ip_address) try: ssh.cisco_command('terminal length 500') mac = '\n'.join(ssh.cisco_command( "show version | include Base ethernet MAC Address", )) raw = '\n'.join(ssh.cisco_command("show inventory")) subswitches = get_subswitches( ssh.cisco_command("show version"), hostname, ip_address ) finally: ssh.close() matches = re.match( 'Base ethernet MAC Address\s+:\s*([0-9aA-Z:]+)', mac) if matches.groups(): mac = matches.groups()[0] inventory = list(cisco_inventory(raw)) dev_inv, parts = inventory[0], inventory[1:] sn = dev_inv['sn'] result = get_base_result_template('ssh_cisco_catalyst') if subswitches: # virtual switch doesn't have own unique id, reuse first from stack sn += '-virtual' model_name = 'Virtual Cisco Catalyst %s' % dev_inv['pid'] model_type = DeviceType.switch_stack else: model_name = 'Cisco Catalyst %s' % dev_inv['pid'] model_type = DeviceType.switch result.update({ 'status': 'success', 'device': { 'hostname': hostname if hostname else ip_address, 'model_name': model_name, 'type': model_type.raw, 'management_ip_addresses': [ip_address], 'parts': [{ 'serial_number': part['sn'], 'name': part['name'], 'label': part['descr'], } for part in parts], }, }) if sn not in SERIAL_BLACKLIST: result['device']['serial_number'] = sn if subswitches: result['device']['subdevices'] = subswitches else: result['device']['mac_addresses'] = [MACAddressField.normalize(mac)] return result
def validate_mac(mac): if not mac: return try: mac = MACAddressField.normalize(mac) if not mac: raise ValueError() except ValueError: raise forms.ValidationError("Invalid MAC address") return mac
def _get_ethernets(data): for mezz in data['PORTMAP']['MEZZ']: name = mezz['DEVICE']['NAME'] ports = mezz['DEVICE']['PORT'] if isinstance(ports, dict): ports = [ports] for port in ports: if port['TYPE'] == 'INTERCONNECT_TYPE_ETH': mac=MACAddressField.normalize(port['WWPN']) yield Eth(name, mac, speed=None)
def test_archivization(self): id = self.deployment.id data = {} for field in self.deployment._meta.fields: data[field.name] = getattr(self.deployment, field.name) if field.name == 'mac': data[field.name] = MACAddressField.normalize(data[field.name]) self.deployment.archive() archive = ArchivedDeployment.objects.get(pk=id) archive_data = {} for field in archive._meta.fields: archive_data[field.name] = getattr(archive, field.name) if field.name == 'mac': archive_data[field.name] = MACAddressField.normalize( archive_data[field.name] ) self.assertEqual(data, archive_data) with self.assertRaises(Deployment.DoesNotExist): Deployment.objects.get(pk=id)
def save_device_data(data, remote_ip): device = data['device'] shares = data['shares'] fcs = data['fcs'] storage = data['storage'] memory = data['memory'] processors = data['processors'] os = data['operating_system'] device = data['device'] ethernets = [Eth(e.get('label'), MACAddressField.normalize(e.get('mac')), str_to_ethspeed(e.get('speed'))) for e in data['ethernets'] if MACAddressField.normalize(e.get('mac')) not in MAC_PREFIX_BLACKLIST] if not ethernets: raise NoMACError('No MAC addresses.') dev = Device.create( sn=device.get('sn'), ethernets=ethernets, model_name='%s %s %s' % (device.get('caption'), device.get('vendor'), device.get('version')), model_type=DeviceType.unknown, priority=SAVE_PRIORITY ) dev.save(priority=SAVE_PRIORITY) if not dev.operatingsystem_set.exists(): o = OperatingSystem.create(dev, os_name=os.get('label'), family='Windows', ) o.memory = int(os['memory']) o.storage = int(os['storage']) o.cores_count = int(os['corescount']) o.save() ip_address, _ = IPAddress.concurrent_get_or_create(address=str(remote_ip)) ip_address.device = dev ip_address.is_management = False ip_address.save() save_processors(processors, dev) save_memory(memory, dev) save_storage(storage, dev) save_shares(shares, dev, ip_address) save_fibre_channel(fcs, dev) return dev
def get_instances(ssh): stdin, stdout, stderr = ssh.exec_command("/usr/sbin/gnt-instance list -o name,pnode,snodes,ip,mac --no-headers") for line in stdout: line = line.strip() if not line: continue hostname, primary_node, secondary_nodes, ip, mac = line.split() if ip == "-": ip = None mac = MACAddressField.normalize(mac) yield hostname, primary_node, ip, mac
def _device(ip, name, model_name, sn, macs, shares): device = { 'management_ip_addresses': [ip], 'hostname': name, 'model_name': model_name, 'mac_addresses': [MACAddressField.normalize(mac) for mac in macs], 'disk_exports': shares, } if sn not in SERIAL_BLACKLIST: device['serial_number'] = sn return device
def _add_dev_switch(ip, pairs, parent, raw, counts, dev_id): dev_type = DeviceType.switch if pairs["Mach type/model"].startswith("Fibre Channel SM"): dev_type = DeviceType.fibre_channel_switch dev = _dev(dev_type, pairs, parent, raw) mac = pairs.get("MAC Address") if mac: eth, created = Ethernet.concurrent_get_or_create(mac=MACAddressField.normalize(mac), defaults=dict(device=dev)) eth.label = "Ethernet" eth.save(priority=SAVE_PRIORITY) return dev
def handle_default_mac_address(facts): for suffix in ('', '_eth0', '_igb0', '_bnx0', '_bge0', '_nfo0', '_nge0'): mac = facts.get('macaddress{}'.format(suffix)) if mac: try: result = MACAddressField.normalize(mac) except ValueError: continue if result[:6] in MAC_PREFIX_BLACKLIST: continue return result
def _get_mac_addresses(ssh): """Get the MAC addresses""" stdin, stdout, stderr = ssh.exec_command( "/sbin/ip addr show | /bin/grep 'link/ether'", ) mac_addresses = set() for line in stdout: mac_address = line.split(None, 3)[1] if is_mac_valid(Eth(label='', mac=mac_address, speed=0)): mac_addresses.add(MACAddressField.normalize(mac_address)) return list(mac_addresses)
def _get_cluster_member(ssh, ip_address): stdin, stdout, stderr = ssh.exec_command("ifconfig eth0 | head -n 1") mac = stdout.readline().split()[-1] return { 'model_name': 'Proxmox', 'mac_addresses': [MACAddressField.normalize(mac)], 'installed_software': [{ 'model_name': 'Proxmox', 'path': 'proxmox', }], 'system_ip_addresses': [ip_address], }
def _add_dev_switch(pairs, parent, raw, counts, dev_id): dev_type = DeviceType.switch if pairs['Mach type/model'].startswith('Fibre Channel SM'): dev_type = DeviceType.fibre_channel_switch dev = _dev(dev_type, pairs, parent, raw) mac = pairs.get('MAC Address') if mac: eth, created = Ethernet.concurrent_get_or_create(device=dev, mac=MACAddressField.normalize(mac)) eth.label = 'Ethernet' eth.save(priority=SAVE_PRIORITY) return dev
def clean(self): if any(self.errors): return for form in self.forms: cleaned_data = getattr(form, 'cleaned_data') if not cleaned_data: continue mac_address = cleaned_data.get('mac') if Ethernet.objects.exclude(device=self.device, ).filter( mac=MACAddressField.normalize(mac_address), ).exists(): raise forms.ValidationError( "%s is assigned to another device." % mac_address, )
def _add_dev_switch(pairs, parent, raw, counts, dev_id): dev_type = DeviceType.switch if pairs['Mach type/model'].startswith('Fibre Channel SM'): dev_type = DeviceType.fibre_channel_switch dev = _dev(dev_type, pairs, parent, raw) mac = pairs.get('MAC Address') if mac: eth, created = Ethernet.concurrent_get_or_create( device=dev, mac=MACAddressField.normalize(mac)) eth.label = 'Ethernet' eth.save(priority=SAVE_PRIORITY) return dev
def post(self, request, *args, **kwargs): from ralph.urls import LATEST_API actor = User.objects.get( username=ApiKeyAuthentication().get_identifier(request) ) if not actor.has_perm('create_devices'): raise HttpResponse(_('You cannot create new devices'), status=401) data = Serializer().deserialize( request.body, format=request.META.get('CONTENT_TYPE', 'application/json') ) mac = MACAddressField.normalize(data['mac']) parent = self.get_parent_device(data) ip_addr = get_first_free_ip(data['network']) hostname = self.get_hostname(parent) venture = get_object_or_404( Venture, symbol=data['venture'] ) role = get_object_or_404( VentureRole, venture=venture, name=data['venture-role'] ) ethernets = [Eth( 'DEPLOYMENT MAC', mac, EthernetSpeed.unknown, )] device = Device.create( ethernets=ethernets, model_type=DeviceType.unknown, model_name='Unknown', verified=True, ) device.name = hostname device.parent = parent device.venture = venture device.venture_role = role device.save() IPAddress.objects.create( address=ip_addr, device=device, hostname=hostname, ) reset_dns(hostname, ip_addr) reset_dhcp(ip_addr, data['mac']) resp = HttpResponse('', status=201) resp['Location'] = LATEST_API.canonical_resource_for( 'dev' ).get_resource_uri(device) return resp
def _get_ethernets(data): for mezz in data["PORTMAP"]["MEZZ"]: name = mezz["DEVICE"]["NAME"] ports = mezz["DEVICE"]["PORT"] if isinstance(ports, dict): ports = [ports] for port in ports: if port["TYPE"] == "INTERCONNECT_TYPE_ETH": try: mac = MACAddressField.normalize(port["WWPN"]) except ValueError: continue yield Eth(name, mac, speed=None)
def _snmp_mac_from_ipv6IfPhysicalAddress(ip_address, snmp_name, snmp_community, snmp_version): mac_addresses = set() for mac in snmp_macs( ip_address, snmp_community, (1, 3, 6, 1, 2, 1, 55, 1, 5, 1, 8), # ipv6IfPhysicalAddress attempts=2, timeout=3, snmp_version=snmp_version, ): mac_addresses.add(MACAddressField.normalize(mac)) return mac_addresses
def _get_ethernets(data): for mezz in data['PORTMAP']['MEZZ']: name = mezz['DEVICE']['NAME'] ports = mezz['DEVICE']['PORT'] if isinstance(ports, dict): ports = [ports] for port in ports: if port['TYPE'] == 'INTERCONNECT_TYPE_ETH': try: mac = MACAddressField.normalize(port['WWPN']) except ValueError: continue yield Eth(name, mac, speed=None)
def _add_dev_switch(ip, pairs, parent, raw, counts, dev_id): dev_type = DeviceType.switch if pairs['Mach type/model'].startswith('Fibre Channel SM'): dev_type = DeviceType.fibre_channel_switch dev = _dev(dev_type, pairs, parent, raw) mac = pairs.get('MAC Address') if mac: if 'mac_addresses' not in dev: dev['mac_addresses'] = [] if mac.replace(':', '').upper()[:6] not in MAC_PREFIX_BLACKLIST: dev['mac_addresses'].append(MACAddressField.normalize(mac)) dev['mac_addresses'] = list(set(dev['mac_addresses'])) return dev
def get_instances(ssh): stdin, stdout, stderr = ssh.exec_command( '/usr/sbin/gnt-instance list -o name,pnode,snodes,ip,mac --no-headers', ) for line in stdout: line = line.strip() if not line: continue hostname, primary_node, secondary_nodes, ip, mac = line.split() if ip == '-': ip = None mac = MACAddressField.normalize(mac) yield hostname, primary_node, ip, mac