diff --git a/netbox_agent/inventory.py b/netbox_agent/inventory.py index f4f8cb6..173393a 100644 --- a/netbox_agent/inventory.py +++ b/netbox_agent/inventory.py @@ -399,16 +399,16 @@ class Inventory(): def create_netbox_memory(self, memory): manufacturer = self.find_or_create_manufacturer(memory['vendor']) - + name = 'Slot {} ({}GB)'.format(memory['slot'], memory['size']) nb_memory = nb.dcim.inventory_items.create( device=self.device_id, discovered=True, manufacturer=manufacturer.id, tags=[INVENTORY_TAG['memory']['name']], - name='{} ({}GB)'.format(memory['description'], memory['size']), + name=name, part_id=memory['product'], serial=memory['serial'], - description='Slot {}'.format(memory['slot']), + description=memory['description'], ) logging.info('Creating Memory {location} {type} {size}GB'.format( diff --git a/netbox_agent/lldp.py b/netbox_agent/lldp.py index ddd8697..af50d1b 100644 --- a/netbox_agent/lldp.py +++ b/netbox_agent/lldp.py @@ -8,35 +8,18 @@ class LLDP(): def parse(self): output_dict = {} - vlans = {} - vid = None for entry in self.output.splitlines(): if '=' not in entry: continue path, value = entry.strip().split("=", 1) - split_path = path.split(".") - interface = split_path[1] - path_components, final = split_path[:-1], split_path[-1] + path = path.split(".") + path_components, final = path[:-1], path[-1] + current_dict = output_dict - - if vlans.get(interface) is None: - vlans[interface] = {} - for path_component in path_components: current_dict[path_component] = current_dict.get(path_component, {}) current_dict = current_dict[path_component] - if 'vlan-id' in path: - vid = value - vlans[interface][value] = vlans[interface].get(vid, {}) - elif path.endswith('vlan'): - vid = value.replace('vlan-', '') - vlans[interface][vid] = vlans[interface].get(vid, {}) - elif 'pvid' in path: - vlans[interface][vid]['pvid'] = True - if 'vlan' not in path: - current_dict[final] = value - for interface, vlan in vlans.items(): - output_dict['lldp'][interface]['vlan'] = vlan + current_dict[final] = value return output_dict def get_switch_ip(self, interface): @@ -55,4 +38,11 @@ class LLDP(): # lldp.eth0.vlan.vlan-id=296 if self.data['lldp'].get(interface) is None: return None - return self.data['lldp'][interface]['vlan'] + + lldp = self.data['lldp'][interface] + if lldp.get('vlan'): + if type(lldp['vlan']) is str: + return int(lldp['vlan'].replace('vlan-', '')) + elif lldp['vlan'].get('vlan-id'): + return int(lldp['vlan'].get('vlan-id')) + return None diff --git a/netbox_agent/network.py b/netbox_agent/network.py index bbb1b12..398f649 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -224,20 +224,17 @@ class Network(): interface.mode = 200 interface.tagged_vlans = [nb_vlan] if nb_vlan else [] interface.untagged_vlan = None - # if lldp reports a vlan-id with pvid - elif lldp_vlan: - pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']] - if len(pvid_vlan) > 0 and ( - interface.mode is None or - interface.mode.value != 100 or - interface.untagged_vlan is None or - interface.untagged_vlan.vid != int(pvid_vlan[0])): - logging.info('Resetting access VLAN on interface {interface}'.format( - interface=interface)) - update = True - nb_vlan = self.get_or_create_vlan(pvid_vlan[0]) - interface.mode = 100 - interface.untagged_vlan = nb_vlan.id + # if lldp reports a vlan-id + elif lldp_vlan and ( + interface.mode is None or interface.mode.value != 100 or + interface.untagged_vlan is None or + interface.untagged_vlan.vid != lldp_vlan): + logging.info('Resetting access VLAN on interface {interface}'.format( + interface=interface)) + update = True + nb_vlan = self.get_or_create_vlan(lldp_vlan) + interface.mode = 100 + interface.untagged_vlan = nb_vlan.id return update, interface def create_or_update_ipmi(self): @@ -298,14 +295,10 @@ class Network(): interface.save() elif config.network.lldp and self.lldp.get_switch_vlan(nic['name']) is not None: # if lldp reports a vlan on an interface, tag the interface in access and set the vlan - # report only the interface which has `pvid=yes` (ie: lldp.eth3.vlan.pvid=yes) - # if pvid is not present, it'll be processed as a vlan tagged interface - vlans = self.lldp.get_switch_vlan(nic['name']) - for vid, vlan_infos in vlans.items(): - nb_vlan = self.get_or_create_vlan(vid) - if vlan_infos.get('vid'): - interface.mode = 100 - interface.untagged_vlan = nb_vlan.id + vlan_id = self.lldp.get_switch_vlan(nic['name']) + nb_vlan = self.get_or_create_vlan(vlan_id) + interface.mode = 100 + interface.untagged_vlan = nb_vlan.id interface.save() # cable the interface