WIP - create netbox interfaces with IP
This commit is contained in:
parent
1063ec63e0
commit
d5673d4992
4 changed files with 75 additions and 24 deletions
|
@ -1,7 +1,8 @@
|
||||||
# Originally from https://github.com/opencoff/useful-scripts/blob/master/linktest.py
|
# Originally from https://github.com/opencoff/useful-scripts/blob/master/linktest.py
|
||||||
|
|
||||||
# mapping fields from ethtool output to simple names
|
# mapping fields from ethtool output to simple names
|
||||||
field_map = { 'Supported ports': 'ports',
|
field_map = {
|
||||||
|
'Supported ports': 'ports',
|
||||||
'Supported link modes': 'sup_link_modes',
|
'Supported link modes': 'sup_link_modes',
|
||||||
'Supports auto-negotiation': 'sup_autoneg',
|
'Supports auto-negotiation': 'sup_autoneg',
|
||||||
'Advertised link modes': 'adv_link_modes',
|
'Advertised link modes': 'adv_link_modes',
|
||||||
|
@ -13,28 +14,29 @@ field_map = { 'Supported ports': 'ports',
|
||||||
'Link detected': 'link',
|
'Link detected': 'link',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def parse_ethtool_output(data):
|
def parse_ethtool_output(data):
|
||||||
"""parse ethtool output"""
|
"""
|
||||||
|
parse ethtool output
|
||||||
|
"""
|
||||||
|
|
||||||
fields = {}
|
fields = {}
|
||||||
field = ""
|
field = ''
|
||||||
fields['speed'] = "-"
|
fields['speed'] = '-'
|
||||||
fields['link'] = "-"
|
fields['link'] = '-'
|
||||||
fields['duplex'] = '-'
|
fields['duplex'] = '-'
|
||||||
for line in data.split('\n')[1:]:
|
for line in data.split('\n')[1:]:
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
r = line.find(':')
|
r = line.find(':')
|
||||||
#verbose(" line=|%s|", line)
|
|
||||||
if r > 0:
|
if r > 0:
|
||||||
field = line[:r].strip()
|
field = line[:r].strip()
|
||||||
if field not in field_map:
|
if field not in field_map:
|
||||||
continue
|
continue
|
||||||
field = field_map[field]
|
field = field_map[field]
|
||||||
data = line[r+1:].strip()
|
data = line[r+1:].strip()
|
||||||
fields[field] = data
|
fields[field] = data
|
||||||
#verbose(" %s=%s", field, data)
|
|
||||||
else:
|
else:
|
||||||
if len(field) > 0 and \
|
if len(field) > 0 and \
|
||||||
field in field_map.keys():
|
field in field_map.keys():
|
||||||
fields[field] += ' ' + line.strip()
|
fields[field] += ' ' + line.strip()
|
||||||
return fields
|
return fields
|
||||||
|
|
|
@ -1,21 +1,31 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import socket
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from netaddr import IPAddress
|
||||||
import netifaces
|
import netifaces
|
||||||
|
|
||||||
from netbox_agent.ethtool import parse_ethtool_output
|
|
||||||
from netbox_agent.config import netbox_instance as nb
|
from netbox_agent.config import netbox_instance as nb
|
||||||
|
from netbox_agent.ethtool import parse_ethtool_output
|
||||||
|
|
||||||
# Regex to match base interface name
|
# Regex to match base interface name
|
||||||
# Doesn't match vlan interfaces and other loopback etc
|
# 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])$')
|
INTERFACE_REGEX = re.compile('^(eth[0-9]+|ens[0-9]+|enp[0-9]+s[0-9]f[0-9])$')
|
||||||
|
|
||||||
|
# FIXME: finish mapping tabble
|
||||||
|
ETHTOOL_TO_NETBOX_TYPE = {
|
||||||
|
1200: 'SFP+ (10GE)',
|
||||||
|
1150: '10g baseT',
|
||||||
|
1000: '1G cuivre',
|
||||||
|
1400: '40G',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Network():
|
class Network():
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, server, *args, **kwargs):
|
||||||
self.nics = []
|
self.nics = []
|
||||||
|
|
||||||
|
self.server = server
|
||||||
self.scan()
|
self.scan()
|
||||||
|
|
||||||
def _ethtool_for_interface(self, interface):
|
def _ethtool_for_interface(self, interface):
|
||||||
|
@ -30,7 +40,10 @@ class Network():
|
||||||
'name': interface,
|
'name': interface,
|
||||||
'mac': open('/sys/class/net/{}/address'.format(interface), 'r').read().strip(),
|
'mac': open('/sys/class/net/{}/address'.format(interface), 'r').read().strip(),
|
||||||
'ip': [
|
'ip': [
|
||||||
x['addr'] for x in ip_addr
|
'{}/{}'.format(
|
||||||
|
x['addr'],
|
||||||
|
IPAddress(x['netmask']).netmask_bits()
|
||||||
|
) for x in ip_addr
|
||||||
] if ip_addr else None, # FIXME: handle IPv6 addresses
|
] if ip_addr else None, # FIXME: handle IPv6 addresses
|
||||||
'ethtool': self._ethtool_for_interface(interface)
|
'ethtool': self._ethtool_for_interface(interface)
|
||||||
}
|
}
|
||||||
|
@ -39,13 +52,40 @@ class Network():
|
||||||
def get_network_cards(self):
|
def get_network_cards(self):
|
||||||
return self.nics
|
return self.nics
|
||||||
|
|
||||||
def get_netbox_network_cards(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def update_netbox_network_cards(self):
|
def update_netbox_network_cards(self):
|
||||||
# if network doesn't exist we create it
|
device = self.server.get_netbox_server()
|
||||||
if True:
|
for nic in self.nics:
|
||||||
pass
|
interface = nb.dcim.interfaces.get(
|
||||||
# or we check if it needs update
|
device=device,
|
||||||
else:
|
mac_address=nic['mac'],
|
||||||
pass
|
)
|
||||||
|
# if network doesn't exist we create it
|
||||||
|
if not interface:
|
||||||
|
new_interface = nb.dcim.interfaces.create(
|
||||||
|
device=device.id,
|
||||||
|
name=nic['name'],
|
||||||
|
mac_address=nic['mac'],
|
||||||
|
)
|
||||||
|
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:
|
||||||
|
netbox_ip.interface = new_interface
|
||||||
|
netbox_ip.save()
|
||||||
|
else:
|
||||||
|
netbox_ip = nb.ipam.ip_addresses.create(
|
||||||
|
address=ip,
|
||||||
|
interface=new_interface.id,
|
||||||
|
status=1,
|
||||||
|
)
|
||||||
|
# or we check if it needs update
|
||||||
|
else:
|
||||||
|
# FIXME: implement update
|
||||||
|
# update name or ip
|
||||||
|
# see https://github.com/Solvik/netbox_agent/issues/9
|
||||||
|
pass
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
import socket
|
||||||
|
|
||||||
from netbox_agent.config import netbox_instance as nb
|
from netbox_agent.config import netbox_instance as nb
|
||||||
import netbox_agent.dmidecode as dmidecode
|
import netbox_agent.dmidecode as dmidecode
|
||||||
from netbox_agent.network import Network
|
from netbox_agent.network import Network
|
||||||
|
|
||||||
|
|
||||||
class ServerBase():
|
class ServerBase():
|
||||||
def __init__(self, dmi=None):
|
def __init__(self, dmi=None):
|
||||||
if dmi:
|
if dmi:
|
||||||
|
@ -13,7 +15,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(service_tag=self.get_service_tag())
|
self.network = Network(server=self)
|
||||||
|
|
||||||
def get_product_name(self):
|
def get_product_name(self):
|
||||||
"""
|
"""
|
||||||
|
@ -110,6 +112,9 @@ class ServerBase():
|
||||||
)
|
)
|
||||||
return new_server
|
return new_server
|
||||||
|
|
||||||
|
def get_netbox_server(self):
|
||||||
|
return nb.dcim.devices.get(serial=self.get_service_tag())
|
||||||
|
|
||||||
def netbox_create(self):
|
def netbox_create(self):
|
||||||
if self.is_blade():
|
if self.is_blade():
|
||||||
# let's find the blade
|
# let's find the blade
|
||||||
|
@ -139,6 +144,8 @@ class ServerBase():
|
||||||
if not server:
|
if not server:
|
||||||
self._netbox_create_server()
|
self._netbox_create_server()
|
||||||
|
|
||||||
|
self.network.update_netbox_network_cards()
|
||||||
|
|
||||||
def print_debug(self):
|
def print_debug(self):
|
||||||
# FIXME: do something more generic by looping on every get_* methods
|
# FIXME: do something more generic by looping on every get_* methods
|
||||||
print('Datacenter:', self.get_datacenter())
|
print('Datacenter:', self.get_datacenter())
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
pynetbox >= 4.0
|
pynetbox >= 4.0
|
||||||
|
netaddr >= 0.7
|
||||||
|
netifaces >= 0.10
|
||||||
|
|
Loading…
Reference in a new issue