rework network part by ignoring devices and ip from config file
This commit is contained in:
parent
215e66d62f
commit
79ebcdb323
3 changed files with 76 additions and 25 deletions
|
|
@ -30,3 +30,9 @@ if config.get('rack_location'):
|
||||||
RACK_LOCATION_DRIVER_FILE = rack_location.get('driver_file')
|
RACK_LOCATION_DRIVER_FILE = rack_location.get('driver_file')
|
||||||
RACK_LOCATION = rack_location.get('driver')
|
RACK_LOCATION = rack_location.get('driver')
|
||||||
RACK_LOCATION_REGEX = rack_location.get('regex')
|
RACK_LOCATION_REGEX = rack_location.get('regex')
|
||||||
|
|
||||||
|
NETWORK_IGNORE_INTERFACES = None
|
||||||
|
NETWORK_IGNORE_IPS = None
|
||||||
|
if config.get('network'):
|
||||||
|
NETWORK_IGNORE_INTERFACES = config['network']['ignore_interfaces']
|
||||||
|
NETWORK_IGNORE_IPS = config['network']['ignore_ips']
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ from netaddr import IPAddress
|
||||||
import netifaces
|
import netifaces
|
||||||
|
|
||||||
from netbox_agent.config import netbox_instance as nb
|
from netbox_agent.config import netbox_instance as nb
|
||||||
|
from netbox_agent.config import NETWORK_IGNORE_INTERFACES, NETWORK_IGNORE_IPS
|
||||||
from netbox_agent.ethtool import Ethtool
|
from netbox_agent.ethtool import Ethtool
|
||||||
|
|
||||||
IFACE_TYPE_100ME_FIXED = 800
|
IFACE_TYPE_100ME_FIXED = 800
|
||||||
|
|
@ -33,10 +34,7 @@ IFACE_TYPE_200GE_CFP2 = 1650
|
||||||
IFACE_TYPE_200GE_QSFP56 = 1700
|
IFACE_TYPE_200GE_QSFP56 = 1700
|
||||||
IFACE_TYPE_400GE_QSFP_DD = 1750
|
IFACE_TYPE_400GE_QSFP_DD = 1750
|
||||||
IFACE_TYPE_OTHER = 32767
|
IFACE_TYPE_OTHER = 32767
|
||||||
|
IFACE_TYPE_LAG = 200
|
||||||
# Regex to match base interface name
|
|
||||||
# Doesn't match vlan interfaces and other loopback etc
|
|
||||||
INTERFACE_REGEX = re.compile('^(eth[0-9]+|ens[0-9]+|enp[0-9]+s[0-9]f[0-9])$')
|
|
||||||
|
|
||||||
|
|
||||||
class Network():
|
class Network():
|
||||||
|
|
@ -44,29 +42,69 @@ class Network():
|
||||||
self.nics = []
|
self.nics = []
|
||||||
|
|
||||||
self.server = server
|
self.server = server
|
||||||
|
self.device = self.server.get_netbox_server()
|
||||||
self.scan()
|
self.scan()
|
||||||
|
|
||||||
def scan(self):
|
def scan(self):
|
||||||
for interface in os.listdir('/sys/class/net/'):
|
for interface in os.listdir('/sys/class/net/'):
|
||||||
if re.match(INTERFACE_REGEX, interface):
|
if NETWORK_IGNORE_INTERFACES and \
|
||||||
|
re.match(NETWORK_IGNORE_INTERFACES, interface):
|
||||||
|
logging.debug('Ignore interface {interface}'.format(interface=interface))
|
||||||
|
continue
|
||||||
|
else:
|
||||||
ip_addr = netifaces.ifaddresses(interface).get(netifaces.AF_INET)
|
ip_addr = netifaces.ifaddresses(interface).get(netifaces.AF_INET)
|
||||||
|
mac = open('/sys/class/net/{}/address'.format(interface), 'r').read().strip()
|
||||||
|
vlan = None
|
||||||
|
if len(interface.split('.')) > 1:
|
||||||
|
vlan = int(interface.split('.')[1])
|
||||||
|
bonding = False
|
||||||
|
bonding_slaves = []
|
||||||
|
if os.path.isdir('/sys/class/net/{}/bonding'.format(interface)):
|
||||||
|
bonding = True
|
||||||
|
bonding_slaves = open(
|
||||||
|
'/sys/class/net/{}/bonding/slaves'.format(interface)
|
||||||
|
).read().split()
|
||||||
nic = {
|
nic = {
|
||||||
'name': interface,
|
'name': interface,
|
||||||
'mac': open('/sys/class/net/{}/address'.format(interface), 'r').read().strip(),
|
'mac': mac if mac != '00:00:00:00:00:00' else None,
|
||||||
'ip': [
|
'ip': [
|
||||||
'{}/{}'.format(
|
'{}/{}'.format(
|
||||||
x['addr'],
|
x['addr'],
|
||||||
IPAddress(x['netmask']).netmask_bits()
|
IPAddress(x['netmask']).netmask_bits()
|
||||||
) for x in ip_addr
|
) for x in ip_addr if not re.match(NETWORK_IGNORE_IPS, x['addr'])
|
||||||
] if ip_addr else None, # FIXME: handle IPv6 addresses
|
] if ip_addr else None, # FIXME: handle IPv6 addresses
|
||||||
'ethtool': Ethtool(interface).parse()
|
'ethtool': Ethtool(interface).parse(),
|
||||||
|
'vlan': vlan,
|
||||||
|
'bonding': bonding,
|
||||||
|
'bonding_slaves': bonding_slaves,
|
||||||
}
|
}
|
||||||
self.nics.append(nic)
|
self.nics.append(nic)
|
||||||
|
|
||||||
def get_network_cards(self):
|
def get_network_cards(self):
|
||||||
return self.nics
|
return self.nics
|
||||||
|
|
||||||
|
def get_netbox_network_card(self, nic):
|
||||||
|
if nic['mac'] is None:
|
||||||
|
interface = nb.dcim.interfaces.get(
|
||||||
|
device_id=self.device.id,
|
||||||
|
name=nic['name'],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
interface = nb.dcim.interfaces.get(
|
||||||
|
device_id=self.device.id,
|
||||||
|
mac_address=nic['mac'],
|
||||||
|
name=nic['name'],
|
||||||
|
)
|
||||||
|
return interface
|
||||||
|
|
||||||
|
def get_netbox_network_cards(self):
|
||||||
|
return nb.dcim.interfaces.filter(
|
||||||
|
device_id=self.device.id,
|
||||||
|
)
|
||||||
|
|
||||||
def get_netbox_type_for_nic(self, nic):
|
def get_netbox_type_for_nic(self, nic):
|
||||||
|
if nic['bonding']:
|
||||||
|
return IFACE_TYPE_LAG
|
||||||
if nic.get('ethtool') is None:
|
if nic.get('ethtool') is None:
|
||||||
return IFACE_TYPE_OTHER
|
return IFACE_TYPE_OTHER
|
||||||
if nic['ethtool']['speed'] == '10000Mb/s':
|
if nic['ethtool']['speed'] == '10000Mb/s':
|
||||||
|
|
@ -79,28 +117,26 @@ class Network():
|
||||||
return IFACE_TYPE_1GE_FIXED
|
return IFACE_TYPE_1GE_FIXED
|
||||||
return IFACE_TYPE_OTHER
|
return IFACE_TYPE_OTHER
|
||||||
|
|
||||||
def create_netbox_nic(self, device, nic):
|
def create_netbox_nic(self, nic):
|
||||||
# TODO: add Optic Vendor, PN and Serial
|
# TODO: add Optic Vendor, PN and Serial
|
||||||
type = self.get_netbox_type_for_nic(nic)
|
type = self.get_netbox_type_for_nic(nic)
|
||||||
logging.info('Creating NIC {name} ({mac}) on {device}'.format(
|
logging.info('Creating NIC {name} ({mac}) on {device}'.format(
|
||||||
name=nic['name'], mac=nic['mac'], device=device.name))
|
name=nic['name'], mac=nic['mac'], device=self.device.name))
|
||||||
return nb.dcim.interfaces.create(
|
return nb.dcim.interfaces.create(
|
||||||
device=device.id,
|
device=self.device.id,
|
||||||
name=nic['name'],
|
name=nic['name'],
|
||||||
mac_address=nic['mac'],
|
mac_address=nic['mac'],
|
||||||
type=type,
|
type=type,
|
||||||
|
mode=200 if nic['vlan'] else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_netbox_network_cards(self):
|
def create_netbox_network_cards(self):
|
||||||
logging.debug('Creating NIC...')
|
logging.debug('Creating NIC...')
|
||||||
device = self.server.get_netbox_server()
|
|
||||||
for nic in self.nics:
|
for nic in self.nics:
|
||||||
interface = nb.dcim.interfaces.get(
|
interface = self.get_netbox_network_card(nic)
|
||||||
mac_address=nic['mac'],
|
|
||||||
)
|
|
||||||
# if network doesn't exist we create it
|
# if network doesn't exist we create it
|
||||||
if not interface:
|
if not interface:
|
||||||
new_interface = self.create_netbox_nic(device, nic)
|
new_interface = self.create_netbox_nic(nic)
|
||||||
if nic['ip']:
|
if nic['ip']:
|
||||||
# for each ip, we try to find it
|
# for each ip, we try to find it
|
||||||
# assign the device's interface to it
|
# assign the device's interface to it
|
||||||
|
|
@ -126,11 +162,20 @@ class Network():
|
||||||
|
|
||||||
def update_netbox_network_cards(self):
|
def update_netbox_network_cards(self):
|
||||||
logging.debug('Updating NIC...')
|
logging.debug('Updating NIC...')
|
||||||
device = self.server.get_netbox_server()
|
|
||||||
|
# delete unknown interface
|
||||||
|
nb_nics = self.get_netbox_network_cards()
|
||||||
|
local_nics = [x['name'] for x in self.nics]
|
||||||
|
for nic in nb_nics:
|
||||||
|
if nic.name not in local_nics:
|
||||||
|
logging.info('Deleting netbox interface {name} because not present locally'.format(
|
||||||
|
name=nic.name
|
||||||
|
))
|
||||||
|
nic.delete()
|
||||||
|
|
||||||
# delete IP on netbox that are not known on this server
|
# delete IP on netbox that are not known on this server
|
||||||
netbox_ips = nb.ipam.ip_addresses.filter(
|
netbox_ips = nb.ipam.ip_addresses.filter(
|
||||||
device=device
|
device_id=self.device.id
|
||||||
)
|
)
|
||||||
all_local_ips = list(chain.from_iterable([
|
all_local_ips = list(chain.from_iterable([
|
||||||
x['ip'] for x in self.nics if x['ip'] is not None
|
x['ip'] for x in self.nics if x['ip'] is not None
|
||||||
|
|
@ -144,14 +189,12 @@ class Network():
|
||||||
|
|
||||||
# update each nic
|
# update each nic
|
||||||
for nic in self.nics:
|
for nic in self.nics:
|
||||||
interface = nb.dcim.interfaces.get(
|
interface = self.get_netbox_network_card(nic)
|
||||||
mac_address=nic['mac'],
|
|
||||||
)
|
|
||||||
if not interface:
|
if not interface:
|
||||||
logging.info('Interface {} not found, creating..'.format(
|
logging.info('Interface {mac_address} not found, creating..'.format(
|
||||||
mac_address=nic['mac'])
|
mac_address=nic['mac'])
|
||||||
)
|
)
|
||||||
interface = self.create_netbox_nic(device, nic)
|
interface = self.create_netbox_nic(nic)
|
||||||
|
|
||||||
nic_update = False
|
nic_update = False
|
||||||
if nic['name'] != interface.name:
|
if nic['name'] != interface.name:
|
||||||
|
|
@ -167,7 +210,7 @@ class Network():
|
||||||
address=ip,
|
address=ip,
|
||||||
)
|
)
|
||||||
if not netbox_ip:
|
if not netbox_ip:
|
||||||
# create netbbox_ip on device
|
# create netbox_ip on device
|
||||||
netbox_ip = nb.ipam.ip_addresses.create(
|
netbox_ip = nb.ipam.ip_addresses.create(
|
||||||
address=ip,
|
address=ip,
|
||||||
interface=interface.id,
|
interface=interface.id,
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ class ServerBase():
|
||||||
self.system = self.dmi.get_by_type('System')
|
self.system = self.dmi.get_by_type('System')
|
||||||
self.bios = self.dmi.get_by_type('BIOS')
|
self.bios = self.dmi.get_by_type('BIOS')
|
||||||
|
|
||||||
self.network = Network(server=self)
|
self.network = None
|
||||||
|
|
||||||
def get_datacenter(self):
|
def get_datacenter(self):
|
||||||
dc = Datacenter()
|
dc = Datacenter()
|
||||||
|
|
@ -198,6 +198,7 @@ class ServerBase():
|
||||||
if not server:
|
if not server:
|
||||||
self._netbox_create_server(datacenter)
|
self._netbox_create_server(datacenter)
|
||||||
|
|
||||||
|
self.network = Network(server=self)
|
||||||
self.network.create_netbox_network_cards()
|
self.network.create_netbox_network_cards()
|
||||||
logging.debug('Server created!')
|
logging.debug('Server created!')
|
||||||
|
|
||||||
|
|
@ -264,6 +265,7 @@ class ServerBase():
|
||||||
update = True
|
update = True
|
||||||
server.hostname = self.get_hostname()
|
server.hostname = self.get_hostname()
|
||||||
# check network cards
|
# check network cards
|
||||||
|
self.network = Network(server=self)
|
||||||
self.network.update_netbox_network_cards()
|
self.network.update_netbox_network_cards()
|
||||||
if update:
|
if update:
|
||||||
server.save()
|
server.save()
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue