better ethool and device type guessing
This commit is contained in:
parent
4ddbb89c18
commit
15faf59182
3 changed files with 99 additions and 44 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
# 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
|
||||||
|
@ -15,28 +18,53 @@ field_map = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def parse_ethtool_output(data):
|
class Ethtool():
|
||||||
"""
|
"""
|
||||||
parse ethtool output
|
This class aims to parse ethtool output
|
||||||
|
There is several bindings to have something proper, but it requires
|
||||||
|
compilation and other requirements.
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, interface, *args, **kwargs):
|
||||||
|
self.interface = interface
|
||||||
|
|
||||||
fields = {}
|
def _parse_ethtool_output(self):
|
||||||
field = ''
|
"""
|
||||||
fields['speed'] = '-'
|
parse ethtool output
|
||||||
fields['link'] = '-'
|
"""
|
||||||
fields['duplex'] = '-'
|
|
||||||
for line in data.split('\n')[1:]:
|
output = subprocess.getoutput('ethtool {}'.format(self.interface))
|
||||||
line = line.rstrip()
|
|
||||||
r = line.find(':')
|
fields = {}
|
||||||
if r > 0:
|
field = ''
|
||||||
field = line[:r].strip()
|
fields['speed'] = '-'
|
||||||
if field not in field_map:
|
fields['link'] = '-'
|
||||||
continue
|
fields['duplex'] = '-'
|
||||||
field = field_map[field]
|
for line in output.split('\n')[1:]:
|
||||||
data = line[r+1:].strip()
|
line = line.rstrip()
|
||||||
fields[field] = data
|
r = line.find(':')
|
||||||
else:
|
if r > 0:
|
||||||
if len(field) > 0 and \
|
field = line[:r].strip()
|
||||||
field in field_map.keys():
|
if field not in field_map:
|
||||||
fields[field] += ' ' + line.strip()
|
continue
|
||||||
return fields
|
field = field_map[field]
|
||||||
|
output = line[r+1:].strip()
|
||||||
|
fields[field] = output
|
||||||
|
else:
|
||||||
|
if len(field) > 0 and \
|
||||||
|
field in field_map:
|
||||||
|
fields[field] += ' ' + line.strip()
|
||||||
|
return fields
|
||||||
|
|
||||||
|
def _parse_ethtool_module_output(self):
|
||||||
|
status, output = subprocess.getstatusoutput('ethtool -m {}'.format(self.interface))
|
||||||
|
if status != 0:
|
||||||
|
return {}
|
||||||
|
r = re.search(r'Identifier.*\((\w+)\)', output)
|
||||||
|
if r and r.groups() > 0:
|
||||||
|
return {'form_factor': r.groups()[0]}
|
||||||
|
|
||||||
|
def parse(self):
|
||||||
|
return {
|
||||||
|
**self._parse_ethtool_output(),
|
||||||
|
**self._parse_ethtool_module_output(),
|
||||||
|
}
|
||||||
|
|
|
@ -1,25 +1,41 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
|
||||||
|
|
||||||
from netaddr import IPAddress
|
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.ethtool import parse_ethtool_output
|
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
|
||||||
|
|
||||||
# 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, server, *args, **kwargs):
|
def __init__(self, server, *args, **kwargs):
|
||||||
|
@ -28,10 +44,6 @@ class Network():
|
||||||
self.server = server
|
self.server = server
|
||||||
self.scan()
|
self.scan()
|
||||||
|
|
||||||
def _ethtool_for_interface(self, interface):
|
|
||||||
output = subprocess.getoutput('ethtool {}'.format(interface))
|
|
||||||
return parse_ethtool_output(output)
|
|
||||||
|
|
||||||
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 re.match(INTERFACE_REGEX, interface):
|
||||||
|
@ -45,27 +57,42 @@ class Network():
|
||||||
IPAddress(x['netmask']).netmask_bits()
|
IPAddress(x['netmask']).netmask_bits()
|
||||||
) for x in ip_addr
|
) 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': Ethtool(interface).parse()
|
||||||
}
|
}
|
||||||
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_type_for_nic(self, nic):
|
||||||
|
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
|
||||||
|
return nb.dcim.interfaces.create(
|
||||||
|
device=device.id,
|
||||||
|
name=nic['name'],
|
||||||
|
mac_address=nic['mac'],
|
||||||
|
type=self.get_netbox_type_for_nic(nic),
|
||||||
|
)
|
||||||
|
|
||||||
def update_netbox_network_cards(self):
|
def update_netbox_network_cards(self):
|
||||||
device = self.server.get_netbox_server()
|
device = self.server.get_netbox_server()
|
||||||
for nic in self.nics:
|
for nic in self.nics:
|
||||||
interface = nb.dcim.interfaces.get(
|
interface = nb.dcim.interfaces.get(
|
||||||
device=device,
|
|
||||||
mac_address=nic['mac'],
|
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 = nb.dcim.interfaces.create(
|
new_interface = self.create_netbox_nic(device, nic)
|
||||||
device=device.id,
|
|
||||||
name=nic['name'],
|
|
||||||
mac_address=nic['mac'],
|
|
||||||
)
|
|
||||||
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
|
||||||
|
|
|
@ -61,7 +61,7 @@ class ServerBase():
|
||||||
def get_bios_release_date(self):
|
def get_bios_release_date(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _netbox_create_blade_chassis(self):
|
def _netbox_create_blade_chassis(self, datacenter):
|
||||||
device_type = nb.dcim.device_types.get(
|
device_type = nb.dcim.device_types.get(
|
||||||
model=self.get_chassis(),
|
model=self.get_chassis(),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue