Add LLDP auto cabling (#41)

This commit is contained in:
Solvik 2019-08-26 11:05:41 +02:00 committed by GitHub
parent 0ef0e1c698
commit 33c55340b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 226 additions and 20 deletions

48
netbox_agent/lldp.py Normal file
View file

@ -0,0 +1,48 @@
import subprocess
class LLDP():
def __init__(self):
self.output = subprocess.getoutput('lldpctl -f keyvalue')
self.data = self.parse()
def parse(self):
output_dict = {}
for entry in self.output.splitlines():
if '=' not in entry:
continue
path, value = entry.strip().split("=", 1)
path = path.split(".")
path_components, final = path[:-1], path[-1]
current_dict = output_dict
for path_component in path_components:
current_dict[path_component] = current_dict.get(path_component, {})
current_dict = current_dict[path_component]
current_dict[final] = value
return output_dict
def get_switch_ip(self, interface):
# lldp.eth0.chassis.mgmt-ip=100.66.7.222
if self.data['lldp'].get(interface) is None:
return None
return self.data['lldp'][interface]['chassis']['mgmt-ip']
def get_switch_port(self, interface):
# lldp.eth0.port.descr=GigabitEthernet1/0/1
if self.data['lldp'].get(interface) is None:
return None
return self.data['lldp'][interface]['port']['descr']
def get_switch_vlan(self, interface):
# lldp.eth0.vlan.vlan-id=296
if self.data['lldp'].get(interface) is None:
return None
lldp = self.data['lldp'][interface]
if lldp.get('vlan'):
if lldp['vlan'].get('vlan-id'):
return int(lldp['vlan'].get('vlan-id'))
elif type(lldp['vlan']) is str:
return int(lldp['vlan'].replace('vlan-', ''))
return None