2019-08-05 16:55:37 +02:00
|
|
|
from itertools import chain
|
2019-08-07 10:32:23 +02:00
|
|
|
import logging
|
2019-08-04 14:37:51 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2019-08-04 15:14:36 +02:00
|
|
|
from netaddr import IPAddress
|
2019-08-04 14:37:51 +02:00
|
|
|
import netifaces
|
|
|
|
|
|
|
|
from netbox_agent.config import netbox_instance as nb
|
2019-08-04 21:48:06 +02:00
|
|
|
from netbox_agent.ethtool import Ethtool
|
|
|
|
|
|
|
|
IFACE_TYPE_100ME_FIXED = 800
|
|
|
|
IFACE_TYPE_1GE_FIXED = 1000
|
|
|
|
IFACE_TYPE_1GE_GBIC = 1050
|
|
|
|
IFACE_TYPE_1GE_SFP = 1100
|
|
|
|
IFACE_TYPE_2GE_FIXED = 1120
|
|
|
|
IFACE_TYPE_5GE_FIXED = 1130
|
|
|
|
IFACE_TYPE_10GE_FIXED = 1150
|
|
|
|
IFACE_TYPE_10GE_CX4 = 1170
|
|
|
|
IFACE_TYPE_10GE_SFP_PLUS = 1200
|
|
|
|
IFACE_TYPE_10GE_XFP = 1300
|
|
|
|
IFACE_TYPE_10GE_XENPAK = 1310
|
|
|
|
IFACE_TYPE_10GE_X2 = 1320
|
|
|
|
IFACE_TYPE_25GE_SFP28 = 1350
|
|
|
|
IFACE_TYPE_40GE_QSFP_PLUS = 1400
|
|
|
|
IFACE_TYPE_50GE_QSFP28 = 1420
|
|
|
|
IFACE_TYPE_100GE_CFP = 1500
|
|
|
|
IFACE_TYPE_100GE_CFP2 = 1510
|
|
|
|
IFACE_TYPE_100GE_CFP4 = 1520
|
|
|
|
IFACE_TYPE_100GE_CPAK = 1550
|
|
|
|
IFACE_TYPE_100GE_QSFP28 = 1600
|
|
|
|
IFACE_TYPE_200GE_CFP2 = 1650
|
|
|
|
IFACE_TYPE_200GE_QSFP56 = 1700
|
|
|
|
IFACE_TYPE_400GE_QSFP_DD = 1750
|
|
|
|
IFACE_TYPE_OTHER = 32767
|
2019-08-04 14:37:51 +02:00
|
|
|
|
|
|
|
# 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])$')
|
|
|
|
|
2019-08-04 15:14:36 +02:00
|
|
|
|
2019-08-04 14:37:51 +02:00
|
|
|
class Network():
|
2019-08-04 15:14:36 +02:00
|
|
|
def __init__(self, server, *args, **kwargs):
|
2019-08-04 14:37:51 +02:00
|
|
|
self.nics = []
|
|
|
|
|
2019-08-04 15:14:36 +02:00
|
|
|
self.server = server
|
2019-08-04 14:37:51 +02:00
|
|
|
self.scan()
|
|
|
|
|
|
|
|
def scan(self):
|
|
|
|
for interface in os.listdir('/sys/class/net/'):
|
|
|
|
if re.match(INTERFACE_REGEX, interface):
|
|
|
|
ip_addr = netifaces.ifaddresses(interface).get(netifaces.AF_INET)
|
|
|
|
nic = {
|
|
|
|
'name': interface,
|
|
|
|
'mac': open('/sys/class/net/{}/address'.format(interface), 'r').read().strip(),
|
|
|
|
'ip': [
|
2019-08-04 15:14:36 +02:00
|
|
|
'{}/{}'.format(
|
|
|
|
x['addr'],
|
|
|
|
IPAddress(x['netmask']).netmask_bits()
|
|
|
|
) for x in ip_addr
|
2019-08-04 14:37:51 +02:00
|
|
|
] if ip_addr else None, # FIXME: handle IPv6 addresses
|
2019-08-04 21:48:06 +02:00
|
|
|
'ethtool': Ethtool(interface).parse()
|
2019-08-04 14:37:51 +02:00
|
|
|
}
|
|
|
|
self.nics.append(nic)
|
|
|
|
|
|
|
|
def get_network_cards(self):
|
|
|
|
return self.nics
|
|
|
|
|
2019-08-04 21:48:06 +02:00
|
|
|
def get_netbox_type_for_nic(self, nic):
|
2019-08-04 23:24:51 +02:00
|
|
|
if nic.get('ethtool') is None:
|
|
|
|
return IFACE_TYPE_OTHER
|
2019-08-04 21:48:06 +02:00
|
|
|
if nic['ethtool']['speed'] == '10000Mb/s':
|
|
|
|
if nic['ethtool']['port'] == 'FIBRE':
|
|
|
|
return IFACE_TYPE_10GE_SFP_PLUS
|
|
|
|
return IFACE_TYPE_10GE_FIXED
|
|
|
|
elif nic['ethtool']['speed'] == '1000Mb/s':
|
|
|
|
if nic['ethtool']['port'] == 'FIBRE':
|
|
|
|
return IFACE_TYPE_1GE_SFP
|
|
|
|
return IFACE_TYPE_1GE_FIXED
|
|
|
|
return IFACE_TYPE_OTHER
|
|
|
|
|
|
|
|
def create_netbox_nic(self, device, nic):
|
|
|
|
# TODO: add Optic Vendor, PN and Serial
|
2019-08-06 17:59:46 +02:00
|
|
|
type = self.get_netbox_type_for_nic(nic)
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.info('Creating NIC {name} ({mac}) on {device}'.format(
|
2019-08-06 17:59:46 +02:00
|
|
|
name=nic['name'], mac=nic['mac'], device=device.name))
|
2019-08-04 21:48:06 +02:00
|
|
|
return nb.dcim.interfaces.create(
|
|
|
|
device=device.id,
|
|
|
|
name=nic['name'],
|
|
|
|
mac_address=nic['mac'],
|
2019-08-06 17:59:46 +02:00
|
|
|
type=type,
|
2019-08-04 21:48:06 +02:00
|
|
|
)
|
|
|
|
|
2019-08-05 16:51:01 +02:00
|
|
|
def create_netbox_network_cards(self):
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.debug('Creating NIC...')
|
2019-08-04 15:14:36 +02:00
|
|
|
device = self.server.get_netbox_server()
|
|
|
|
for nic in self.nics:
|
|
|
|
interface = nb.dcim.interfaces.get(
|
|
|
|
mac_address=nic['mac'],
|
|
|
|
)
|
|
|
|
# if network doesn't exist we create it
|
|
|
|
if not interface:
|
2019-08-04 21:48:06 +02:00
|
|
|
new_interface = self.create_netbox_nic(device, nic)
|
2019-08-04 15:14:36 +02:00
|
|
|
if nic['ip']:
|
|
|
|
# for each ip, we try to find it
|
|
|
|
# assign the device's interface to it
|
|
|
|
# or simply create it
|
|
|
|
for ip in nic['ip']:
|
|
|
|
netbox_ip = nb.ipam.ip_addresses.get(
|
|
|
|
address=ip,
|
|
|
|
)
|
|
|
|
if netbox_ip:
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.info('Assigning existing IP {ip} to {interface}'.format(
|
2019-08-06 17:59:46 +02:00
|
|
|
ip=ip, interface=new_interface))
|
2019-08-04 15:14:36 +02:00
|
|
|
netbox_ip.interface = new_interface
|
|
|
|
netbox_ip.save()
|
|
|
|
else:
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.info('Create new IP {ip} on {interface}'.format(
|
2019-08-06 17:59:46 +02:00
|
|
|
ip=ip, interface=new_interface))
|
2019-08-04 15:14:36 +02:00
|
|
|
netbox_ip = nb.ipam.ip_addresses.create(
|
|
|
|
address=ip,
|
|
|
|
interface=new_interface.id,
|
|
|
|
status=1,
|
|
|
|
)
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.debug('Finished creating NIC!')
|
2019-08-05 16:51:01 +02:00
|
|
|
|
|
|
|
def update_netbox_network_cards(self):
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.debug('Updating NIC...')
|
2019-08-05 16:51:01 +02:00
|
|
|
device = self.server.get_netbox_server()
|
|
|
|
|
|
|
|
# delete IP on netbox that are not known on this server
|
|
|
|
netbox_ips = nb.ipam.ip_addresses.filter(
|
|
|
|
device=device
|
|
|
|
)
|
2019-08-05 16:55:37 +02:00
|
|
|
all_local_ips = list(chain.from_iterable([
|
|
|
|
x['ip'] for x in self.nics if x['ip'] is not None
|
|
|
|
]))
|
2019-08-05 16:51:01 +02:00
|
|
|
for netbox_ip in netbox_ips:
|
|
|
|
if netbox_ip.address not in all_local_ips:
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.info('Unassigning IP {ip} from {interface}'.format(
|
2019-08-06 17:59:46 +02:00
|
|
|
ip=netbox_ip.address, interface=netbox_ip.interface))
|
2019-08-05 16:51:01 +02:00
|
|
|
netbox_ip.interface = None
|
|
|
|
netbox_ip.save()
|
|
|
|
|
|
|
|
# update each nic
|
|
|
|
for nic in self.nics:
|
|
|
|
interface = nb.dcim.interfaces.get(
|
|
|
|
mac_address=nic['mac'],
|
|
|
|
)
|
2019-08-07 15:36:24 +02:00
|
|
|
if not interface:
|
|
|
|
logging.info('Interface {} not found, creating..'.format(
|
|
|
|
mac_address=nic['mac'])
|
|
|
|
)
|
|
|
|
interface = self.create_netbox_nic(device, nic)
|
2019-08-05 16:51:01 +02:00
|
|
|
|
|
|
|
nic_update = False
|
|
|
|
if nic['name'] != interface.name:
|
|
|
|
nic_update = True
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.info('Updating interface {interface} name to: {name}'.format(
|
2019-08-06 17:59:46 +02:00
|
|
|
interface=interface, name=nic['name']))
|
2019-08-05 16:51:01 +02:00
|
|
|
interface.name = nic['name']
|
|
|
|
|
|
|
|
if nic['ip']:
|
|
|
|
# sync local IPs
|
|
|
|
for ip in nic['ip']:
|
|
|
|
netbox_ip = nb.ipam.ip_addresses.get(
|
|
|
|
address=ip,
|
|
|
|
)
|
|
|
|
if not netbox_ip:
|
|
|
|
# create netbbox_ip on device
|
|
|
|
netbox_ip = nb.ipam.ip_addresses.create(
|
|
|
|
address=ip,
|
|
|
|
interface=interface.id,
|
|
|
|
status=1,
|
|
|
|
)
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.info('Created new IP {ip} on {interface}'.format(
|
2019-08-06 17:59:46 +02:00
|
|
|
ip=ip, interface=interface))
|
2019-08-05 16:51:01 +02:00
|
|
|
else:
|
2019-08-06 17:59:46 +02:00
|
|
|
if netbox_ip.interface.id != interface.id:
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.info(
|
|
|
|
'Detected interface change: old interface is {old_interface} '
|
2019-08-06 17:59:46 +02:00
|
|
|
'(id: {old_id}), new interface is {new_interface} (id: {new_id})'
|
|
|
|
.format(
|
|
|
|
old_interface=netbox_ip.interface, new_interface=interface,
|
|
|
|
old_id=netbox_ip.id, new_id=interface.id
|
|
|
|
))
|
2019-08-05 17:41:03 +02:00
|
|
|
netbox_ip.interface = interface
|
2019-08-05 16:51:01 +02:00
|
|
|
netbox_ip.save()
|
|
|
|
if nic_update:
|
|
|
|
interface.save()
|
2019-08-07 10:32:23 +02:00
|
|
|
logging.debug('Finished updating NIC!')
|