Merge branch 'master' into use-sudo
This commit is contained in:
commit
32bfbe1867
51 changed files with 12763 additions and 493 deletions
|
|
@ -1,31 +1,42 @@
|
|||
from netbox_agent.logging import logging # NOQA
|
||||
from netbox_agent.vendors.dell import DellHost
|
||||
import netbox_agent.dmidecode as dmidecode
|
||||
from netbox_agent.config import config
|
||||
from netbox_agent.logging import logging # NOQA
|
||||
from netbox_agent.vendors.dell import DellHost
|
||||
from netbox_agent.vendors.generic import GenericHost
|
||||
from netbox_agent.vendors.hp import HPHost
|
||||
from netbox_agent.vendors.qct import QCTHost
|
||||
from netbox_agent.vendors.supermicro import SupermicroHost
|
||||
from netbox_agent.virtualmachine import VirtualMachine, is_vm
|
||||
|
||||
MANUFACTURERS = {
|
||||
'Dell Inc.': DellHost,
|
||||
'HP': HPHost,
|
||||
'HPE': HPHost,
|
||||
'Supermicro': SupermicroHost,
|
||||
'Quanta Cloud Technology Inc.': QCTHost,
|
||||
}
|
||||
'Dell Inc.': DellHost,
|
||||
'HP': HPHost,
|
||||
'HPE': HPHost,
|
||||
'Supermicro': SupermicroHost,
|
||||
'Quanta Cloud Technology Inc.': QCTHost,
|
||||
'Generic': GenericHost,
|
||||
}
|
||||
|
||||
|
||||
def run(config):
|
||||
manufacturer = dmidecode.get_by_type('Chassis')[0].get('Manufacturer')
|
||||
server = MANUFACTURERS[manufacturer](dmi=dmidecode)
|
||||
dmi = dmidecode.parse()
|
||||
|
||||
if config.virtual.enabled or is_vm(dmi):
|
||||
if not config.virtual.cluster_name:
|
||||
raise Exception('virtual.cluster_name parameter is mandatory because it\'s a VM')
|
||||
server = VirtualMachine(dmi=dmi)
|
||||
else:
|
||||
manufacturer = dmidecode.get_by_type(dmi, 'Chassis')[0].get('Manufacturer')
|
||||
try:
|
||||
server = MANUFACTURERS[manufacturer](dmi=dmi)
|
||||
except KeyError:
|
||||
server = GenericHost(dmi=dmi)
|
||||
|
||||
if config.debug:
|
||||
server.print_debug()
|
||||
if config.register:
|
||||
server.netbox_create(config)
|
||||
if config.update_all or config.update_network or config.update_location or \
|
||||
if config.register or config.update_all or config.update_network or config.update_location or \
|
||||
config.update_inventory or config.update_psu:
|
||||
server.netbox_update(config)
|
||||
server.netbox_create_or_update(config)
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import logging
|
||||
import pynetbox
|
||||
import jsonargparse
|
||||
import sys
|
||||
|
||||
import jsonargparse
|
||||
import pynetbox
|
||||
|
||||
|
||||
def get_config():
|
||||
p = jsonargparse.ArgumentParser(
|
||||
|
|
@ -13,6 +14,8 @@ def get_config():
|
|||
],
|
||||
prog='netbox_agent',
|
||||
description="Netbox agent to run on your infrastructure's servers",
|
||||
env_prefix='NETBOX_AGENT_',
|
||||
default_env=True
|
||||
)
|
||||
p.add_argument('-c', '--config', action=jsonargparse.ActionConfigFile)
|
||||
|
||||
|
|
@ -27,6 +30,8 @@ def get_config():
|
|||
p.add_argument('--log_level', default='debug')
|
||||
p.add_argument('--netbox.url', help='Netbox URL')
|
||||
p.add_argument('--netbox.token', help='Netbox API Token')
|
||||
p.add_argument('--virtual.enabled', action='store_true', help='Is a virtual machine or not')
|
||||
p.add_argument('--virtual.cluster_name', help='Cluster name of VM')
|
||||
p.add_argument('--hostname_cmd', default=None,
|
||||
help="Command to output hostname, used as Device's name in netbox")
|
||||
p.add_argument('--datacenter_location.driver',
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import logging
|
||||
import re as _re
|
||||
import subprocess as _subprocess
|
||||
import sys
|
||||
|
||||
from netbox_agent.misc import is_tool
|
||||
import logging
|
||||
|
||||
_handle_re = _re.compile('^Handle\\s+(.+),\\s+DMI\\s+type\\s+(\\d+),\\s+(\\d+)\\s+bytes$')
|
||||
_in_block_re = _re.compile('^\\t\\t(.+)$')
|
||||
|
|
@ -11,7 +11,7 @@ _record_re = _re.compile('\\t(.+):\\s+(.+)$')
|
|||
_record2_re = _re.compile('\\t(.+):$')
|
||||
|
||||
_type2str = {
|
||||
0: 'BIOS',
|
||||
0: 'BIOS',
|
||||
1: 'System',
|
||||
2: 'Baseboard',
|
||||
3: 'Chassis',
|
||||
|
|
@ -60,19 +60,22 @@ for type_id, type_str in _type2str.items():
|
|||
_str2type[type_str] = type_id
|
||||
|
||||
|
||||
def parse():
|
||||
def parse(output=None):
|
||||
"""
|
||||
parse the full output of the dmidecode
|
||||
command and return a dic containing the parsed information
|
||||
"""
|
||||
buffer = _execute_cmd()
|
||||
if output:
|
||||
buffer = output
|
||||
else:
|
||||
buffer = _execute_cmd()
|
||||
if isinstance(buffer, bytes):
|
||||
buffer = buffer.decode('utf-8')
|
||||
_data = _parse(buffer)
|
||||
return _data
|
||||
|
||||
|
||||
def get_by_type(type_id):
|
||||
def get_by_type(data, type_id):
|
||||
"""
|
||||
filter the output of dmidecode per type
|
||||
0 BIOS
|
||||
|
|
@ -124,7 +127,6 @@ def get_by_type(type_id):
|
|||
if type_id is None:
|
||||
return None
|
||||
|
||||
data = parse()
|
||||
result = []
|
||||
for entry in data.values():
|
||||
if entry['DMIType'] == type_id:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import re
|
||||
from shutil import which
|
||||
import subprocess
|
||||
from shutil import which
|
||||
|
||||
# Originally from https://github.com/opencoff/useful-scripts/blob/master/linktest.py
|
||||
|
||||
|
|
@ -9,7 +9,7 @@ field_map = {
|
|||
'Supported ports': 'ports',
|
||||
'Supported link modes': 'sup_link_modes',
|
||||
'Supports auto-negotiation': 'sup_autoneg',
|
||||
'Advertised link modes': 'adv_link_modes',
|
||||
'Advertised link modes': 'adv_link_modes',
|
||||
'Advertised auto-negotiation': 'adv_autoneg',
|
||||
'Speed': 'speed',
|
||||
'Duplex': 'duplex',
|
||||
|
|
@ -31,6 +31,7 @@ class Ethtool():
|
|||
There is several bindings to have something proper, but it requires
|
||||
compilation and other requirements.
|
||||
"""
|
||||
|
||||
def __init__(self, interface, *args, **kwargs):
|
||||
self.interface = interface
|
||||
|
||||
|
|
@ -54,7 +55,7 @@ class Ethtool():
|
|||
if field not in field_map:
|
||||
continue
|
||||
field = field_map[field]
|
||||
output = line[r+1:].strip()
|
||||
output = line[r + 1:].strip()
|
||||
fields[field] = output
|
||||
else:
|
||||
if len(field) > 0 and \
|
||||
|
|
@ -63,14 +64,12 @@ class Ethtool():
|
|||
return fields
|
||||
|
||||
def _parse_ethtool_module_output(self):
|
||||
status, output = subprocess.getstatusoutput(
|
||||
'sudo /usr/sbin/ethtool -m {}'.format(self.interface)
|
||||
)
|
||||
if status != 0:
|
||||
return {}
|
||||
r = re.search(r'Identifier.*\((\w+)\)', output)
|
||||
if r and len(r.groups()) > 0:
|
||||
return {'form_factor': r.groups()[0]}
|
||||
status, output = subprocess.getstatusoutput('sudo ethtool -m {}'.format(self.interface))
|
||||
if status == 0:
|
||||
r = re.search(r'Identifier.*\((\w+)\)', output)
|
||||
if r and len(r.groups()) > 0:
|
||||
return {'form_factor': r.groups()[0]}
|
||||
return {}
|
||||
|
||||
def parse(self):
|
||||
if which('ethtool') is None:
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
import logging
|
||||
import pynetbox
|
||||
import re
|
||||
|
||||
from netbox_agent.config import netbox_instance as nb, config
|
||||
from netbox_agent.misc import is_tool, get_vendor
|
||||
import pynetbox
|
||||
|
||||
from netbox_agent.config import config
|
||||
from netbox_agent.config import netbox_instance as nb
|
||||
from netbox_agent.lshw import LSHW
|
||||
from netbox_agent.misc import get_vendor, is_tool
|
||||
from netbox_agent.raid.hp import HPRaid
|
||||
from netbox_agent.raid.omreport import OmreportRaid
|
||||
from netbox_agent.raid.storcli import StorcliRaid
|
||||
from netbox_agent.lshw import LSHW
|
||||
|
||||
INVENTORY_TAG = {
|
||||
'cpu': {'name': 'hw:cpu', 'slug': 'hw-cpu'},
|
||||
|
|
@ -16,7 +18,7 @@ INVENTORY_TAG = {
|
|||
'memory': {'name': 'hw:memory', 'slug': 'hw-memory'},
|
||||
'motherboard': {'name': 'hw:motherboard', 'slug': 'hw-motherboard'},
|
||||
'raid_card': {'name': 'hw:raid_card', 'slug': 'hw-raid-card'},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Inventory():
|
||||
|
|
@ -132,8 +134,8 @@ class Inventory():
|
|||
|
||||
motherboards = self.get_hw_motherboards()
|
||||
nb_motherboards = self.get_netbox_inventory(
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['motherboard']['slug'])
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['motherboard']['slug'])
|
||||
|
||||
for nb_motherboard in nb_motherboards:
|
||||
if nb_motherboard.serial not in [x['serial'] for x in motherboards]:
|
||||
|
|
@ -169,8 +171,8 @@ class Inventory():
|
|||
|
||||
def do_netbox_interfaces(self):
|
||||
nb_interfaces = self.get_netbox_inventory(
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['interface']['slug'])
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['interface']['slug'])
|
||||
interfaces = self.lshw.interfaces
|
||||
|
||||
# delete interfaces that are in netbox but not locally
|
||||
|
|
@ -269,9 +271,9 @@ class Inventory():
|
|||
"""
|
||||
|
||||
nb_raid_cards = self.get_netbox_inventory(
|
||||
device_id=self.device_id,
|
||||
tag=[INVENTORY_TAG['raid_card']['slug']]
|
||||
)
|
||||
device_id=self.device_id,
|
||||
tag=[INVENTORY_TAG['raid_card']['slug']]
|
||||
)
|
||||
raid_cards = self.get_raid_cards()
|
||||
|
||||
# delete cards that are in netbox but not locally
|
||||
|
|
@ -296,7 +298,7 @@ class Inventory():
|
|||
|
||||
non_raid_disks = [
|
||||
'MR9361-8i',
|
||||
]
|
||||
]
|
||||
|
||||
if size is None and logicalname is None or \
|
||||
'virtual' in product.lower() or 'logical' in product.lower() or \
|
||||
|
|
@ -321,7 +323,7 @@ class Inventory():
|
|||
|
||||
d = {}
|
||||
d["name"] = ""
|
||||
d['Size'] = '{} GB'.format(int(size/1024/1024/1024))
|
||||
d['Size'] = '{} GB'.format(int(size / 1024 / 1024 / 1024))
|
||||
d['logicalname'] = logicalname
|
||||
d['description'] = description
|
||||
d['SN'] = serial
|
||||
|
|
@ -378,8 +380,8 @@ class Inventory():
|
|||
|
||||
def do_netbox_disks(self):
|
||||
nb_disks = self.get_netbox_inventory(
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['disk']['slug'])
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['disk']['slug'])
|
||||
disks = self.get_hw_disks()
|
||||
|
||||
# delete disks that are in netbox but not locally
|
||||
|
|
@ -421,9 +423,9 @@ class Inventory():
|
|||
def do_netbox_memories(self):
|
||||
memories = self.lshw.memories
|
||||
nb_memories = self.get_netbox_inventory(
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['memory']['slug']
|
||||
)
|
||||
device_id=self.device_id,
|
||||
tag=INVENTORY_TAG['memory']['slug']
|
||||
)
|
||||
|
||||
for nb_memory in nb_memories:
|
||||
if nb_memory.serial not in [x['serial'] for x in memories]:
|
||||
|
|
@ -436,18 +438,7 @@ class Inventory():
|
|||
if memory.get('serial') not in [x.serial for x in nb_memories]:
|
||||
self.create_netbox_memory(memory)
|
||||
|
||||
def create(self):
|
||||
if config.inventory is None:
|
||||
return False
|
||||
self.do_netbox_cpus()
|
||||
self.do_netbox_memories()
|
||||
self.do_netbox_raid_cards()
|
||||
self.do_netbox_disks()
|
||||
self.do_netbox_interfaces()
|
||||
self.do_netbox_motherboard()
|
||||
return True
|
||||
|
||||
def update(self):
|
||||
def create_or_update(self):
|
||||
if config.inventory is None or config.update_inventory is None:
|
||||
return False
|
||||
self.do_netbox_cpus()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import logging
|
||||
import subprocess
|
||||
|
||||
from netaddr import IPNetwork
|
||||
|
||||
|
||||
class IPMI():
|
||||
"""
|
||||
|
|
@ -33,17 +35,34 @@ class IPMI():
|
|||
: O=OEM
|
||||
Bad Password Threshold : Not Available
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.ret, self.output = subprocess.getstatusoutput('sudo /usr/bin/ipmitool lan print')
|
||||
if self.ret != 0:
|
||||
logging.error('Cannot get ipmi info: {}'.format(self.output))
|
||||
|
||||
def parse(self):
|
||||
ret = {}
|
||||
_ipmi = {}
|
||||
if self.ret != 0:
|
||||
return ret
|
||||
return _ipmi
|
||||
|
||||
for line in self.output.splitlines():
|
||||
key = line.split(':')[0].strip()
|
||||
if key not in ['802.1q VLAN ID', 'IP Address', 'Subnet Mask', 'MAC Address']:
|
||||
continue
|
||||
value = ':'.join(line.split(':')[1:]).strip()
|
||||
ret[key] = value
|
||||
_ipmi[key] = value
|
||||
|
||||
ret = {}
|
||||
ret['name'] = 'IPMI'
|
||||
ret['bonding'] = False
|
||||
ret['mac'] = _ipmi['MAC Address']
|
||||
ret['vlan'] = int(_ipmi['802.1q VLAN ID']) \
|
||||
if _ipmi['802.1q VLAN ID'] != 'Disabled' else None
|
||||
ip = _ipmi['IP Address']
|
||||
netmask = _ipmi['Subnet Mask']
|
||||
address = str(IPNetwork('{}/{}'.format(ip, netmask)))
|
||||
|
||||
ret['ip'] = [address]
|
||||
ret['ipmi'] = True
|
||||
return ret
|
||||
|
|
|
|||
|
|
@ -2,8 +2,11 @@ import subprocess
|
|||
|
||||
|
||||
class LLDP():
|
||||
def __init__(self):
|
||||
self.output = subprocess.getoutput('sudo /usr/sbin/lldpctl -f keyvalue')
|
||||
def __init__(self, output=None):
|
||||
if output:
|
||||
self.output = output
|
||||
else:
|
||||
self.output = subprocess.getoutput('sudo /usr/sbin/lldpctl -f keyvalue')
|
||||
self.data = self.parse()
|
||||
|
||||
def parse(self):
|
||||
|
|
@ -49,6 +52,8 @@ class LLDP():
|
|||
# lldp.eth0.port.descr=GigabitEthernet1/0/1
|
||||
if self.data['lldp'].get(interface) is None:
|
||||
return None
|
||||
if self.data['lldp'][interface]['port'].get('ifname'):
|
||||
return self.data['lldp'][interface]['port']['ifname']
|
||||
return self.data['lldp'][interface]['port']['descr']
|
||||
|
||||
def get_switch_vlan(self, interface):
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ class LocationBase():
|
|||
There's also a support for an external driver file outside of this project in case
|
||||
the logic isn't supported here.
|
||||
"""
|
||||
|
||||
def __init__(self, driver, driver_value, driver_file, regex, *args, **kwargs):
|
||||
self.driver = driver
|
||||
self.driver_value = driver_value
|
||||
|
|
|
|||
|
|
@ -2,10 +2,8 @@ import logging
|
|||
|
||||
from netbox_agent.config import config
|
||||
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
if config.log_level == 'debug':
|
||||
if config.log_level.lower() == 'debug':
|
||||
logger.setLevel(logging.DEBUG)
|
||||
else:
|
||||
logger.setLevel(logging.INFO)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import subprocess
|
||||
import json
|
||||
import logging
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from netbox_agent.misc import is_tool
|
||||
|
|
@ -105,13 +105,14 @@ class LSHW():
|
|||
self.disks.append(d)
|
||||
|
||||
def find_cpus(self, obj):
|
||||
c = {}
|
||||
c["product"] = obj["product"]
|
||||
c["vendor"] = obj["vendor"]
|
||||
c["description"] = obj["description"]
|
||||
c["location"] = obj["slot"]
|
||||
if "product" in obj:
|
||||
c = {}
|
||||
c["product"] = obj["product"]
|
||||
c["vendor"] = obj["vendor"]
|
||||
c["description"] = obj["description"]
|
||||
c["location"] = obj["slot"]
|
||||
|
||||
self.cpus.append(c)
|
||||
self.cpus.append(c)
|
||||
|
||||
def find_memories(self, obj):
|
||||
if "children" not in obj:
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import socket
|
||||
import subprocess
|
||||
from shutil import which
|
||||
|
||||
|
||||
|
|
@ -24,8 +26,14 @@ def get_vendor(name):
|
|||
'MD': 'Toshiba',
|
||||
'MG': 'Toshiba',
|
||||
'WD': 'WDC'
|
||||
}
|
||||
}
|
||||
for key, value in vendors.items():
|
||||
if name.upper().startswith(key):
|
||||
return value
|
||||
return name
|
||||
|
||||
|
||||
def get_hostname(config):
|
||||
if config.hostname_cmd is None:
|
||||
return '{}'.format(socket.gethostname())
|
||||
return subprocess.getoutput(config.hostname_cmd)
|
||||
|
|
|
|||
|
|
@ -1,55 +1,46 @@
|
|||
from itertools import chain
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from itertools import chain
|
||||
|
||||
from netaddr import IPAddress, IPNetwork
|
||||
import netifaces
|
||||
from netaddr import IPAddress
|
||||
|
||||
from netbox_agent.config import netbox_instance as nb, config
|
||||
from netbox_agent.config import config
|
||||
from netbox_agent.config import netbox_instance as nb
|
||||
from netbox_agent.ethtool import Ethtool
|
||||
from netbox_agent.ipmi import IPMI
|
||||
from netbox_agent.lldp import LLDP
|
||||
|
||||
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
|
||||
IFACE_TYPE_LAG = 200
|
||||
|
||||
IPADDRESS_ROLE_ANYCAST = 30
|
||||
|
||||
|
||||
class Network():
|
||||
class Network(object):
|
||||
def __init__(self, server, *args, **kwargs):
|
||||
self.nics = []
|
||||
|
||||
self.server = server
|
||||
self.device = self.server.get_netbox_server()
|
||||
self.lldp = LLDP() if config.network.lldp else None
|
||||
self.scan()
|
||||
self.nics = self.scan()
|
||||
self.ipmi = None
|
||||
self.dcim_choices = {}
|
||||
dcim_c = nb.dcim.choices()
|
||||
|
||||
for choice in dcim_c:
|
||||
self.dcim_choices[choice] = {}
|
||||
for c in dcim_c[choice]:
|
||||
self.dcim_choices[choice][c['label']] = c['value']
|
||||
|
||||
self.ipam_choices = {}
|
||||
ipam_c = nb.ipam.choices()
|
||||
|
||||
for choice in ipam_c:
|
||||
self.ipam_choices[choice] = {}
|
||||
for c in ipam_c[choice]:
|
||||
self.ipam_choices[choice][c['label']] = c['value']
|
||||
|
||||
def get_network_type():
|
||||
return NotImplementedError
|
||||
|
||||
def scan(self):
|
||||
nics = []
|
||||
for interface in os.listdir('/sys/class/net/'):
|
||||
# ignore if it's not a link (ie: bonding_masters etc)
|
||||
if not os.path.islink('/sys/class/net/{}'.format(interface)):
|
||||
|
|
@ -107,13 +98,14 @@ class Network():
|
|||
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': Ethtool(interface).parse(),
|
||||
'vlan': vlan,
|
||||
'bonding': bonding,
|
||||
'bonding_slaves': bonding_slaves,
|
||||
}
|
||||
self.nics.append(nic)
|
||||
nics.append(nic)
|
||||
return nics
|
||||
|
||||
def _set_bonding_interfaces(self):
|
||||
bonding_nics = (x for x in self.nics if x['bonding'])
|
||||
|
|
@ -141,49 +133,45 @@ class Network():
|
|||
|
||||
def get_netbox_network_card(self, nic):
|
||||
if nic['mac'] is None:
|
||||
interface = nb.dcim.interfaces.get(
|
||||
device_id=self.device.id,
|
||||
interface = self.nb_net.interfaces.get(
|
||||
name=nic['name'],
|
||||
**self.custom_arg_id,
|
||||
)
|
||||
else:
|
||||
interface = nb.dcim.interfaces.get(
|
||||
device_id=self.device.id,
|
||||
interface = self.nb_net.interfaces.get(
|
||||
mac_address=nic['mac'],
|
||||
name=nic['name'],
|
||||
**self.custom_arg_id,
|
||||
)
|
||||
return interface
|
||||
|
||||
def get_netbox_network_cards(self):
|
||||
return nb.dcim.interfaces.filter(
|
||||
device_id=self.device.id,
|
||||
mgmt_only=False,
|
||||
return self.nb_net.interfaces.filter(
|
||||
**self.custom_arg_id,
|
||||
)
|
||||
|
||||
def get_netbox_type_for_nic(self, nic):
|
||||
if self.get_network_type() == 'virtual':
|
||||
return self.dcim_choices['interface:type']['Virtual']
|
||||
|
||||
if nic.get('bonding'):
|
||||
return IFACE_TYPE_LAG
|
||||
return self.dcim_choices['interface:type']['Link Aggregation Group (LAG)']
|
||||
|
||||
if nic.get('bonding'):
|
||||
return self.dcim_choices['interface:type']['Link Aggregation Group (LAG)']
|
||||
if nic.get('ethtool') is None:
|
||||
return IFACE_TYPE_OTHER
|
||||
return self.dcim_choices['interface:type']['Other']
|
||||
|
||||
if nic['ethtool']['speed'] == '10000Mb/s':
|
||||
if nic['ethtool']['port'] == 'FIBRE':
|
||||
return IFACE_TYPE_10GE_SFP_PLUS
|
||||
return IFACE_TYPE_10GE_FIXED
|
||||
return self.dcim_choices['interface:type']['SFP+ (10GE)']
|
||||
return self.dcim_choices['interface:type']['10GBASE-T (10GE)']
|
||||
|
||||
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 get_ipmi(self):
|
||||
ipmi = IPMI().parse()
|
||||
return ipmi
|
||||
|
||||
def get_netbox_ipmi(self):
|
||||
ipmi = self.get_ipmi()
|
||||
mac = ipmi['MAC Address']
|
||||
return nb.dcim.interfaces.get(
|
||||
mac=mac
|
||||
)
|
||||
return self.dcim_choices['interface:type']['SFP (1GE)']
|
||||
return self.dcim_choices['interface:type']['1000BASE-T (1GE)']
|
||||
return self.dcim_choices['interface:type']['Other']
|
||||
|
||||
def get_or_create_vlan(self, vlan_id):
|
||||
# FIXME: we may need to specify the datacenter
|
||||
|
|
@ -214,14 +202,16 @@ class Network():
|
|||
interface.untagged_vlan = None
|
||||
# if it's a vlan interface
|
||||
elif vlan_id and (
|
||||
interface.mode is None or interface.mode.value != 200 or
|
||||
len(interface.tagged_vlans) != 1 or
|
||||
interface.tagged_vlans[0].vid != vlan_id):
|
||||
interface.mode is None or
|
||||
type(interface.mode) is not int and (
|
||||
interface.mode.value == self.dcim_choices['interface:mode']['Access'] or
|
||||
len(interface.tagged_vlans) != 1 or
|
||||
interface.tagged_vlans[0].vid != vlan_id)):
|
||||
logging.info('Resetting tagged VLAN(s) on interface {interface}'.format(
|
||||
interface=interface))
|
||||
update = True
|
||||
nb_vlan = self.get_or_create_vlan(vlan_id)
|
||||
interface.mode = 200
|
||||
interface.mode = self.dcim_choices['interface:mode']['Tagged']
|
||||
interface.tagged_vlans = [nb_vlan] if nb_vlan else []
|
||||
interface.untagged_vlan = None
|
||||
# if lldp reports a vlan-id with pvid
|
||||
|
|
@ -229,53 +219,17 @@ class Network():
|
|||
pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']]
|
||||
if len(pvid_vlan) > 0 and (
|
||||
interface.mode is None or
|
||||
interface.mode.value != 100 or
|
||||
interface.mode.value != self.dcim_choices['interface:mode']['Access'] or
|
||||
interface.untagged_vlan is None or
|
||||
interface.untagged_vlan.vid != int(pvid_vlan[0])):
|
||||
logging.info('Resetting access VLAN on interface {interface}'.format(
|
||||
interface=interface))
|
||||
update = True
|
||||
nb_vlan = self.get_or_create_vlan(pvid_vlan[0])
|
||||
interface.mode = 100
|
||||
interface.mode = self.dcim_choices['interface:mode']['Access']
|
||||
interface.untagged_vlan = nb_vlan.id
|
||||
return update, interface
|
||||
|
||||
def create_or_update_ipmi(self):
|
||||
ipmi = self.get_ipmi()
|
||||
mac = ipmi['MAC Address']
|
||||
ip = ipmi['IP Address']
|
||||
netmask = ipmi['Subnet Mask']
|
||||
vlan = int(ipmi['802.1q VLAN ID']) if ipmi['802.1q VLAN ID'] != 'Disabled' else None
|
||||
address = str(IPNetwork('{}/{}'.format(ip, netmask)))
|
||||
|
||||
interface = nb.dcim.interfaces.get(
|
||||
device_id=self.device.id,
|
||||
mgmt_only=True,
|
||||
)
|
||||
nic = {
|
||||
'name': 'IPMI',
|
||||
'mac': mac,
|
||||
'vlan': vlan,
|
||||
'ip': [address],
|
||||
}
|
||||
if interface is None:
|
||||
interface = self.create_netbox_nic(nic, mgmt=True)
|
||||
self.create_or_update_netbox_ip_on_interface(address, interface)
|
||||
else:
|
||||
# let the user chose the name of mgmt ?
|
||||
# guess it with manufacturer (IDRAC, ILO, ...) ?
|
||||
update = False
|
||||
self.create_or_update_netbox_ip_on_interface(address, interface)
|
||||
update, interface = self.reset_vlan_on_interface(nic, interface)
|
||||
if mac.upper() != interface.mac_address:
|
||||
logging.info('IPMI mac changed from {old_mac} to {new_mac}'.format(
|
||||
old_mac=interface.mac_address, new_mac=mac.upper()))
|
||||
interface.mac_address = mac
|
||||
update = True
|
||||
if update:
|
||||
interface.save()
|
||||
return interface
|
||||
|
||||
def create_netbox_nic(self, nic, mgmt=False):
|
||||
# TODO: add Optic Vendor, PN and Serial
|
||||
type = self.get_netbox_type_for_nic(nic)
|
||||
|
|
@ -283,12 +237,12 @@ class Network():
|
|||
name=nic['name'], mac=nic['mac'], device=self.device.name))
|
||||
|
||||
nb_vlan = None
|
||||
interface = nb.dcim.interfaces.create(
|
||||
device=self.device.id,
|
||||
interface = self.nb_net.interfaces.create(
|
||||
name=nic['name'],
|
||||
mac_address=nic['mac'],
|
||||
type=type,
|
||||
mgmt_only=mgmt,
|
||||
**self.custom_arg,
|
||||
)
|
||||
|
||||
if nic['vlan']:
|
||||
|
|
@ -304,7 +258,7 @@ class Network():
|
|||
for vid, vlan_infos in vlans.items():
|
||||
nb_vlan = self.get_or_create_vlan(vid)
|
||||
if vlan_infos.get('vid'):
|
||||
interface.mode = 100
|
||||
interface.mode = self.dcim_choices['interface:mode']['Access']
|
||||
interface.untagged_vlan = nb_vlan.id
|
||||
interface.save()
|
||||
|
||||
|
|
@ -366,7 +320,7 @@ class Network():
|
|||
address=ip,
|
||||
interface=interface.id,
|
||||
status=1,
|
||||
role=IPADDRESS_ROLE_ANYCAST,
|
||||
role=self.ipam_choices['ip-address:role']['Anycast'],
|
||||
)
|
||||
return netbox_ip
|
||||
else:
|
||||
|
|
@ -388,6 +342,112 @@ class Network():
|
|||
netbox_ip.save()
|
||||
return netbox_ip
|
||||
|
||||
def create_or_update_netbox_network_cards(self):
|
||||
if config.update_all is None or config.update_network is None:
|
||||
return None
|
||||
logging.debug('Creating/Updating NIC...')
|
||||
|
||||
# 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
|
||||
))
|
||||
nb_nics.remove(nic)
|
||||
nic.delete()
|
||||
|
||||
# delete IP on netbox that are not known on this server
|
||||
if len(nb_nics):
|
||||
netbox_ips = nb.ipam.ip_addresses.filter(
|
||||
interface_id=[x.id for x in nb_nics],
|
||||
)
|
||||
all_local_ips = list(chain.from_iterable([
|
||||
x['ip'] for x in self.nics if x['ip'] is not None
|
||||
]))
|
||||
for netbox_ip in netbox_ips:
|
||||
if netbox_ip.address not in all_local_ips:
|
||||
logging.info('Unassigning IP {ip} from {interface}'.format(
|
||||
ip=netbox_ip.address, interface=netbox_ip.interface))
|
||||
netbox_ip.interface = None
|
||||
netbox_ip.save()
|
||||
|
||||
# update each nic
|
||||
for nic in self.nics:
|
||||
interface = self.get_netbox_network_card(nic)
|
||||
if not interface:
|
||||
logging.info('Interface {mac_address} not found, creating..'.format(
|
||||
mac_address=nic['mac'])
|
||||
)
|
||||
interface = self.create_netbox_nic(nic)
|
||||
|
||||
nic_update = 0
|
||||
if nic['name'] != interface.name:
|
||||
logging.info('Updating interface {interface} name to: {name}'.format(
|
||||
interface=interface, name=nic['name']))
|
||||
interface.name = nic['name']
|
||||
nic_update += 1
|
||||
|
||||
ret, interface = self.reset_vlan_on_interface(nic, interface)
|
||||
nic_update += ret
|
||||
|
||||
_type = self.get_netbox_type_for_nic(nic)
|
||||
if not interface.type or \
|
||||
_type != interface.type.value:
|
||||
logging.info('Interface type is wrong, resetting')
|
||||
interface.type = _type
|
||||
nic_update += 1
|
||||
|
||||
if hasattr(interface, 'lag') and interface.lag is not None:
|
||||
local_lag_int = next(
|
||||
item for item in self.nics if item['name'] == interface.lag.name
|
||||
)
|
||||
if nic['name'] not in local_lag_int['bonding_slaves']:
|
||||
logging.info('Interface has no LAG, resetting')
|
||||
nic_update += 1
|
||||
interface.lag = None
|
||||
|
||||
# cable the interface
|
||||
if config.network.lldp:
|
||||
switch_ip = self.lldp.get_switch_ip(interface.name)
|
||||
switch_interface = self.lldp.get_switch_port(interface.name)
|
||||
if switch_ip and switch_interface:
|
||||
ret, interface = self.create_or_update_cable(
|
||||
switch_ip, switch_interface, interface
|
||||
)
|
||||
nic_update += ret
|
||||
|
||||
if nic['ip']:
|
||||
# sync local IPs
|
||||
for ip in nic['ip']:
|
||||
self.create_or_update_netbox_ip_on_interface(ip, interface)
|
||||
if nic_update > 0:
|
||||
interface.save()
|
||||
|
||||
self._set_bonding_interfaces()
|
||||
logging.debug('Finished updating NIC!')
|
||||
|
||||
|
||||
class ServerNetwork(Network):
|
||||
def __init__(self, server, *args, **kwargs):
|
||||
super(ServerNetwork, self).__init__(server, args, kwargs)
|
||||
self.ipmi = self.get_ipmi()
|
||||
if self.ipmi:
|
||||
self.nics.append(self.ipmi)
|
||||
self.server = server
|
||||
self.device = self.server.get_netbox_server()
|
||||
self.nb_net = nb.dcim
|
||||
self.custom_arg = {'device': self.device.id}
|
||||
self.custom_arg_id = {'device_id': self.device.id}
|
||||
|
||||
def get_network_type(self):
|
||||
return 'server'
|
||||
|
||||
def get_ipmi(self):
|
||||
ipmi = IPMI().parse()
|
||||
return ipmi
|
||||
|
||||
def connect_interface_to_switch(self, switch_ip, switch_interface, nb_server_interface):
|
||||
logging.info('Interface {} is not connected to switch, trying to connect..'.format(
|
||||
nb_server_interface.name
|
||||
|
|
@ -491,105 +551,22 @@ class Network():
|
|||
)
|
||||
return update, nb_server_interface
|
||||
|
||||
def create_netbox_network_cards(self):
|
||||
logging.debug('Creating NIC...')
|
||||
for nic in self.nics:
|
||||
interface = self.get_netbox_network_card(nic)
|
||||
# if network doesn't exist we create it
|
||||
if not interface:
|
||||
new_interface = self.create_netbox_nic(nic)
|
||||
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']:
|
||||
self.create_or_update_netbox_ip_on_interface(ip, new_interface)
|
||||
self._set_bonding_interfaces()
|
||||
self.create_or_update_ipmi()
|
||||
logging.debug('Finished creating NIC!')
|
||||
|
||||
def update_netbox_network_cards(self):
|
||||
if config.update_all is None or config.update_network is None:
|
||||
return None
|
||||
logging.debug('Updating NIC...')
|
||||
class VirtualNetwork(Network):
|
||||
def __init__(self, server, *args, **kwargs):
|
||||
super(VirtualNetwork, self).__init__(server, args, kwargs)
|
||||
self.server = server
|
||||
self.device = self.server.get_netbox_vm()
|
||||
self.nb_net = nb.virtualization
|
||||
self.custom_arg = {'virtual_machine': self.device.id}
|
||||
self.custom_arg_id = {'virtual_machine_id': self.device.id}
|
||||
|
||||
# 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()
|
||||
dcim_c = nb.virtualization.choices()
|
||||
|
||||
# delete IP on netbox that are not known on this server
|
||||
netbox_ips = nb.ipam.ip_addresses.filter(
|
||||
device_id=self.device.id,
|
||||
interface_id=[x.id for x in nb_nics],
|
||||
)
|
||||
all_local_ips = list(chain.from_iterable([
|
||||
x['ip'] for x in self.nics if x['ip'] is not None
|
||||
]))
|
||||
for netbox_ip in netbox_ips:
|
||||
if netbox_ip.address not in all_local_ips:
|
||||
logging.info('Unassigning IP {ip} from {interface}'.format(
|
||||
ip=netbox_ip.address, interface=netbox_ip.interface))
|
||||
netbox_ip.interface = None
|
||||
netbox_ip.save()
|
||||
for choice in dcim_c:
|
||||
self.dcim_choices[choice] = {}
|
||||
for c in dcim_c[choice]:
|
||||
self.dcim_choices[choice][c['label']] = c['value']
|
||||
|
||||
# update each nic
|
||||
for nic in self.nics:
|
||||
interface = self.get_netbox_network_card(nic)
|
||||
if not interface:
|
||||
logging.info('Interface {mac_address} not found, creating..'.format(
|
||||
mac_address=nic['mac'])
|
||||
)
|
||||
interface = self.create_netbox_nic(nic)
|
||||
|
||||
nic_update = 0
|
||||
if nic['name'] != interface.name:
|
||||
logging.info('Updating interface {interface} name to: {name}'.format(
|
||||
interface=interface, name=nic['name']))
|
||||
interface.name = nic['name']
|
||||
nic_update += 1
|
||||
|
||||
ret, interface = self.reset_vlan_on_interface(nic, interface)
|
||||
nic_update += ret
|
||||
|
||||
type = self.get_netbox_type_for_nic(nic)
|
||||
if not interface.type or \
|
||||
type != interface.type.value:
|
||||
logging.info('Interface type is wrong, resetting')
|
||||
interface.type = type
|
||||
nic_update += 1
|
||||
|
||||
if interface.lag is not None:
|
||||
local_lag_int = next(
|
||||
item for item in self.nics if item['name'] == interface.lag.name
|
||||
)
|
||||
if nic['name'] not in local_lag_int['bonding_slaves']:
|
||||
logging.info('Interface has no LAG, resetting')
|
||||
nic_update += 1
|
||||
interface.lag = None
|
||||
|
||||
# cable the interface
|
||||
if config.network.lldp:
|
||||
switch_ip = self.lldp.get_switch_ip(interface.name)
|
||||
switch_interface = self.lldp.get_switch_port(interface.name)
|
||||
if switch_ip and switch_interface:
|
||||
ret, interface = self.create_or_update_cable(
|
||||
switch_ip, switch_interface, interface
|
||||
)
|
||||
nic_update += ret
|
||||
|
||||
if nic['ip']:
|
||||
# sync local IPs
|
||||
for ip in nic['ip']:
|
||||
self.create_or_update_netbox_ip_on_interface(ip, interface)
|
||||
if nic_update > 0:
|
||||
interface.save()
|
||||
|
||||
self._set_bonding_interfaces()
|
||||
self.create_or_update_ipmi()
|
||||
logging.debug('Finished updating NIC!')
|
||||
def get_network_type(self):
|
||||
return 'virtual'
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import logging
|
||||
|
||||
import netbox_agent.dmidecode as dmidecode
|
||||
from netbox_agent.config import netbox_instance as nb
|
||||
|
||||
PSU_DMI_TYPE = 39
|
||||
|
|
@ -16,7 +17,7 @@ class PowerSupply():
|
|||
|
||||
def get_power_supply(self):
|
||||
power_supply = []
|
||||
for psu in self.server.dmi.get_by_type(PSU_DMI_TYPE):
|
||||
for psu in dmidecode.get_by_type(self.server.dmi, PSU_DMI_TYPE):
|
||||
if 'Present' not in psu['Status'] or psu['Status'] == 'Not Present':
|
||||
continue
|
||||
|
||||
|
|
@ -34,13 +35,13 @@ class PowerSupply():
|
|||
'allocated_draw': None,
|
||||
'maximum_draw': max_power,
|
||||
'device': self.device_id,
|
||||
})
|
||||
})
|
||||
return power_supply
|
||||
|
||||
def get_netbox_power_supply(self):
|
||||
return nb.dcim.power_ports.filter(
|
||||
device_id=self.device_id
|
||||
)
|
||||
)
|
||||
|
||||
def create_or_update_power_supply(self):
|
||||
nb_psus = self.get_netbox_power_supply()
|
||||
|
|
@ -78,10 +79,10 @@ class PowerSupply():
|
|||
if psu['name'] not in [x.name for x in nb_psus]:
|
||||
logging.info('Creating PSU {name} ({description}), {maximum_draw}W'.format(
|
||||
**psu
|
||||
))
|
||||
))
|
||||
nb_psu = nb.dcim.power_ports.create(
|
||||
**psu
|
||||
)
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import re
|
||||
import subprocess
|
||||
|
||||
from netbox_agent.raid.base import Raid, RaidController
|
||||
from netbox_agent.misc import get_vendor
|
||||
from netbox_agent.raid.base import Raid, RaidController
|
||||
|
||||
REGEXP_CONTROLLER_HP = re.compile(r'Smart Array ([a-zA-Z0-9- ]+) in Slot ([0-9]+)')
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import re
|
||||
import subprocess
|
||||
import xml.etree.ElementTree as ET # NOQA
|
||||
import xml.etree.ElementTree as ET # NOQA
|
||||
|
||||
from netbox_agent.misc import get_vendor
|
||||
from netbox_agent.raid.base import Raid, RaidController
|
||||
|
|
@ -43,7 +43,7 @@ class OmreportController(RaidController):
|
|||
ret = []
|
||||
output = subprocess.getoutput(
|
||||
'omreport storage controller controller={} -fmt xml'.format(self.controller_index)
|
||||
)
|
||||
)
|
||||
root = ET.fromstring(output)
|
||||
et_array_disks = root.find('ArrayDisks')
|
||||
if et_array_disks is not None:
|
||||
|
|
@ -54,7 +54,7 @@ class OmreportController(RaidController):
|
|||
'SN': get_field(obj, 'DeviceSerialNumber'),
|
||||
'Size': '{:.0f}GB'.format(
|
||||
int(get_field(obj, 'Length')) / 1024 / 1024 / 1024
|
||||
),
|
||||
),
|
||||
'Type': 'HDD' if int(get_field(obj, 'MediaType')) == 1 else 'SSD',
|
||||
'_src': self.__class__.__name__,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import subprocess
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
from netbox_agent.misc import get_vendor
|
||||
from netbox_agent.raid.base import Raid, RaidController
|
||||
|
|
@ -47,7 +47,7 @@ class StorcliController(RaidController):
|
|||
'Size': size,
|
||||
'Type': media_type,
|
||||
'_src': self.__class__.__name__,
|
||||
})
|
||||
})
|
||||
return ret
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
import logging
|
||||
from pprint import pprint
|
||||
import socket
|
||||
import subprocess
|
||||
from pprint import pprint
|
||||
|
||||
from netbox_agent.config import netbox_instance as nb, config
|
||||
import netbox_agent.dmidecode as dmidecode
|
||||
from netbox_agent.location import Datacenter, Rack
|
||||
from netbox_agent.config import config
|
||||
from netbox_agent.config import netbox_instance as nb
|
||||
from netbox_agent.inventory import Inventory
|
||||
from netbox_agent.network import Network
|
||||
from netbox_agent.location import Datacenter, Rack
|
||||
from netbox_agent.network import ServerNetwork
|
||||
from netbox_agent.power import PowerSupply
|
||||
|
||||
|
||||
|
|
@ -36,10 +37,10 @@ class ServerBase():
|
|||
else:
|
||||
self.dmi = dmidecode.parse()
|
||||
|
||||
self.baseboard = self.dmi.get_by_type('Baseboard')
|
||||
self.bios = self.dmi.get_by_type('BIOS')
|
||||
self.chassis = self.dmi.get_by_type('Chassis')
|
||||
self.system = self.dmi.get_by_type('System')
|
||||
self.baseboard = dmidecode.get_by_type(self.dmi, 'Baseboard')
|
||||
self.bios = dmidecode.get_by_type(self.dmi, 'BIOS')
|
||||
self.chassis = dmidecode.get_by_type(self.dmi, 'Chassis')
|
||||
self.system = dmidecode.get_by_type(self.dmi, 'System')
|
||||
|
||||
self.network = None
|
||||
|
||||
|
|
@ -135,7 +136,7 @@ class ServerBase():
|
|||
def get_power_consumption(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def _netbox_create_blade_chassis(self, datacenter, rack):
|
||||
def _netbox_create_chassis(self, datacenter, rack):
|
||||
device_type = get_device_type(self.get_chassis())
|
||||
device_role = get_device_role('Server Chassis')
|
||||
serial = self.get_chassis_service_tag()
|
||||
|
|
@ -171,27 +172,6 @@ class ServerBase():
|
|||
)
|
||||
return new_blade
|
||||
|
||||
def _netbox_set_blade_slot(self, chassis, server):
|
||||
slot = self.get_blade_slot()
|
||||
# Find the slot and update it with our blade
|
||||
device_bays = nb.dcim.device_bays.filter(
|
||||
device_id=chassis.id,
|
||||
name=slot,
|
||||
)
|
||||
if len(device_bays) > 0:
|
||||
logging.info(
|
||||
'Setting device ({serial}) new slot on {slot} '
|
||||
'(Chassis {chassis_serial})..'.format(
|
||||
serial=server.serial, slot=slot, chassis_serial=chassis.serial
|
||||
))
|
||||
device_bay = device_bays[0]
|
||||
device_bay.installed_device = server
|
||||
device_bay.save()
|
||||
else:
|
||||
logging.error('Could not find slot {slot} for chassis'.format(
|
||||
slot=slot
|
||||
))
|
||||
|
||||
def _netbox_create_server(self, datacenter, rack):
|
||||
device_role = get_device_role('Server')
|
||||
device_type = get_device_type(self.get_product_name())
|
||||
|
|
@ -214,103 +194,89 @@ class ServerBase():
|
|||
def get_netbox_server(self):
|
||||
return nb.dcim.devices.get(serial=self.get_service_tag())
|
||||
|
||||
def netbox_create(self, config):
|
||||
logging.debug('Creating Server..')
|
||||
def _netbox_set_or_update_blade_slot(self, server, chassis, datacenter):
|
||||
# before everything check if right chassis
|
||||
actual_device_bay = server.parent_device.device_bay if server.parent_device else None
|
||||
actual_chassis = actual_device_bay.device if actual_device_bay else None
|
||||
slot = self.get_blade_slot()
|
||||
if actual_chassis and \
|
||||
actual_chassis.serial == chassis.serial and \
|
||||
actual_device_bay.name == slot:
|
||||
return
|
||||
|
||||
real_device_bays = nb.dcim.device_bays.filter(
|
||||
device_id=chassis.id,
|
||||
name=slot,
|
||||
)
|
||||
if len(real_device_bays) > 0:
|
||||
logging.info(
|
||||
'Setting device ({serial}) new slot on {slot} '
|
||||
'(Chassis {chassis_serial})..'.format(
|
||||
serial=server.serial, slot=slot, chassis_serial=chassis.serial
|
||||
))
|
||||
# reset actual device bay if set
|
||||
if actual_device_bay:
|
||||
actual_device_bay.installed_device = None
|
||||
actual_device_bay.save()
|
||||
# setup new device bay
|
||||
real_device_bay = real_device_bays[0]
|
||||
real_device_bay.installed_device = server
|
||||
real_device_bay.save()
|
||||
else:
|
||||
logging.error('Could not find slot {slot} for chassis'.format(
|
||||
slot=slot
|
||||
))
|
||||
|
||||
def netbox_create_or_update(self, config):
|
||||
"""
|
||||
Netbox method to create or update info about our server/blade
|
||||
|
||||
Handle:
|
||||
* new chassis for a blade
|
||||
* new slot for a blade
|
||||
* hostname update
|
||||
* Network infos
|
||||
* Inventory management
|
||||
* PSU management
|
||||
"""
|
||||
datacenter = self.get_netbox_datacenter()
|
||||
rack = self.get_netbox_rack()
|
||||
if self.is_blade():
|
||||
# let's find the blade
|
||||
serial = self.get_service_tag()
|
||||
blade = nb.dcim.devices.get(serial=serial)
|
||||
chassis = nb.dcim.devices.get(serial=self.get_chassis_service_tag())
|
||||
# if it doesn't exist, create it
|
||||
if not blade:
|
||||
# check if the chassis exist before
|
||||
# if it doesn't exist, create it
|
||||
chassis = nb.dcim.devices.get(
|
||||
serial=self.get_chassis_service_tag()
|
||||
)
|
||||
if not chassis:
|
||||
chassis = self._netbox_create_blade_chassis(datacenter, rack)
|
||||
|
||||
blade = self._netbox_create_blade(chassis, datacenter, rack)
|
||||
if self.is_blade():
|
||||
chassis = nb.dcim.devices.get(
|
||||
serial=self.get_chassis_service_tag()
|
||||
)
|
||||
# Chassis does not exist
|
||||
if not chassis:
|
||||
chassis = self._netbox_create_chassis(datacenter, rack)
|
||||
|
||||
server = nb.dcim.devices.get(serial=self.get_service_tag())
|
||||
if not server:
|
||||
server = self._netbox_create_blade(chassis, datacenter, rack)
|
||||
|
||||
# Set slot for blade
|
||||
self._netbox_set_blade_slot(chassis, blade)
|
||||
self._netbox_set_or_update_blade_slot(server, chassis, datacenter)
|
||||
else:
|
||||
server = nb.dcim.devices.get(serial=self.get_service_tag())
|
||||
if not server:
|
||||
self._netbox_create_server(datacenter, rack)
|
||||
|
||||
self.network = Network(server=self)
|
||||
self.network.create_netbox_network_cards()
|
||||
|
||||
self.power = PowerSupply(server=self)
|
||||
self.power.create_or_update_power_supply()
|
||||
|
||||
if config.inventory:
|
||||
self.inventory = Inventory(server=self)
|
||||
self.inventory.create()
|
||||
logging.debug('Server created!')
|
||||
|
||||
def _netbox_update_chassis_for_blade(self, server, datacenter):
|
||||
chassis = server.parent_device.device_bay.device
|
||||
device_bay = nb.dcim.device_bays.get(
|
||||
server.parent_device.device_bay.id
|
||||
)
|
||||
netbox_chassis_serial = server.parent_device.device_bay.device.serial
|
||||
move_device_bay = False
|
||||
|
||||
# check chassis serial with dmidecode
|
||||
if netbox_chassis_serial != self.get_chassis_service_tag():
|
||||
move_device_bay = True
|
||||
# try to find the new netbox chassis
|
||||
chassis = nb.dcim.devices.get(
|
||||
serial=self.get_chassis_service_tag()
|
||||
)
|
||||
if not chassis:
|
||||
chassis = self._netbox_create_blade_chassis(datacenter)
|
||||
if move_device_bay or device_bay.name != self.get_blade_slot():
|
||||
logging.info('Device ({serial}) seems to have moved, reseting old slot..'.format(
|
||||
serial=server.serial))
|
||||
device_bay.installed_device = None
|
||||
device_bay.save()
|
||||
|
||||
# Set slot for blade
|
||||
self._netbox_set_blade_slot(chassis, server)
|
||||
|
||||
def netbox_update(self, config):
|
||||
"""
|
||||
Netbox method to update info about our server/blade
|
||||
|
||||
Handle:
|
||||
* new chasis for a blade
|
||||
* new slot for a bblade
|
||||
* hostname update
|
||||
* new network infos
|
||||
"""
|
||||
logging.debug('Updating Server...')
|
||||
# check network cards
|
||||
if config.register or config.update_all or config.update_network:
|
||||
self.network = ServerNetwork(server=self)
|
||||
self.network.create_or_update_netbox_network_cards()
|
||||
# update inventory if feature is enabled
|
||||
if config.inventory and (config.register or config.update_all or config.update_inventory):
|
||||
self.inventory = Inventory(server=self)
|
||||
self.inventory.create_or_update()
|
||||
# update psu
|
||||
if config.register or config.update_all or config.update_psu:
|
||||
self.power = PowerSupply(server=self)
|
||||
self.power.create_or_update_power_supply()
|
||||
self.power.report_power_consumption()
|
||||
|
||||
server = nb.dcim.devices.get(serial=self.get_service_tag())
|
||||
if not server:
|
||||
raise Exception("The server (Serial: {}) isn't yet registered in Netbox, register"
|
||||
'it before updating it'.format(self.get_service_tag()))
|
||||
update = 0
|
||||
if self.is_blade():
|
||||
datacenter = self.get_netbox_datacenter()
|
||||
# if it's already linked to a chassis
|
||||
if server.parent_device:
|
||||
self._netbox_update_chassis_for_blade(server, datacenter)
|
||||
else:
|
||||
logging.info('Blade is not in a chassis, fixing...')
|
||||
chassis = nb.dcim.devices.get(
|
||||
serial=self.get_chassis_service_tag()
|
||||
)
|
||||
if not chassis:
|
||||
chassis = self._netbox_create_blade_chassis(datacenter)
|
||||
# Set slot for blade
|
||||
self._netbox_set_blade_slot(chassis, server)
|
||||
|
||||
# for every other specs
|
||||
# check hostname
|
||||
if server.name != self.get_hostname():
|
||||
|
|
@ -321,25 +287,12 @@ class ServerBase():
|
|||
ret, server = self.update_netbox_location(server)
|
||||
update += ret
|
||||
|
||||
# check network cards
|
||||
if config.update_all or config.update_network:
|
||||
self.network = Network(server=self)
|
||||
self.network.update_netbox_network_cards()
|
||||
# update inventory
|
||||
if config.update_all or config.update_inventory:
|
||||
self.inventory = Inventory(server=self)
|
||||
self.inventory.update()
|
||||
# update psu
|
||||
if config.update_all or config.update_psu:
|
||||
self.power = PowerSupply(server=self)
|
||||
self.power.create_or_update_power_supply()
|
||||
self.power.report_power_consumption()
|
||||
if update:
|
||||
server.save()
|
||||
logging.debug('Finished updating Server!')
|
||||
|
||||
def print_debug(self):
|
||||
self.network = Network(server=self)
|
||||
self.network = ServerNetwork(server=self)
|
||||
print('Datacenter:', self.get_datacenter())
|
||||
print('Netbox Datacenter:', self.get_netbox_datacenter())
|
||||
print('Rack:', self.get_rack())
|
||||
|
|
|
|||
8
netbox_agent/vendors/dell.py
vendored
8
netbox_agent/vendors/dell.py
vendored
|
|
@ -1,8 +1,8 @@
|
|||
import logging
|
||||
import subprocess
|
||||
|
||||
from netbox_agent.server import ServerBase
|
||||
from netbox_agent.misc import is_tool
|
||||
from netbox_agent.server import ServerBase
|
||||
|
||||
|
||||
class DellHost(ServerBase):
|
||||
|
|
@ -20,7 +20,7 @@ class DellHost(ServerBase):
|
|||
` Location In Chassis: Slot 03`
|
||||
"""
|
||||
if self.is_blade():
|
||||
return self.dmi.get_by_type('Baseboard')[0].get('Location In Chassis').strip()
|
||||
return self.baseboard[0].get('Location In Chassis').strip()
|
||||
return None
|
||||
|
||||
def get_chassis_name(self):
|
||||
|
|
@ -30,12 +30,12 @@ class DellHost(ServerBase):
|
|||
|
||||
def get_chassis(self):
|
||||
if self.is_blade():
|
||||
return self.dmi.get_by_type('Chassis')[0]['Version'].strip()
|
||||
return self.chassis[0]['Version'].strip()
|
||||
return self.get_product_name()
|
||||
|
||||
def get_chassis_service_tag(self):
|
||||
if self.is_blade():
|
||||
return self.dmi.get_by_type('Chassis')[0]['Serial Number'].strip()
|
||||
return self.chassis[0]['Serial Number'].strip()
|
||||
return self.get_service_tag()
|
||||
|
||||
def get_power_consumption(self):
|
||||
|
|
|
|||
23
netbox_agent/vendors/generic.py
vendored
Normal file
23
netbox_agent/vendors/generic.py
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import netbox_agent.dmidecode as dmidecode
|
||||
from netbox_agent.server import ServerBase
|
||||
|
||||
|
||||
class GenericHost(ServerBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(GenericHost, self).__init__(*args, **kwargs)
|
||||
self.manufacturer = dmidecode.get_by_type(self.dmi, 'Baseboard')[0].get('Manufacturer')
|
||||
|
||||
def is_blade(self):
|
||||
return None
|
||||
|
||||
def get_blade_slot(self):
|
||||
return None
|
||||
|
||||
def get_chassis_name(self):
|
||||
return None
|
||||
|
||||
def get_chassis(self):
|
||||
return self.get_product_name()
|
||||
|
||||
def get_chassis_service_tag(self):
|
||||
return self.get_service_tag()
|
||||
7
netbox_agent/vendors/hp.py
vendored
7
netbox_agent/vendors/hp.py
vendored
|
|
@ -1,3 +1,4 @@
|
|||
import netbox_agent.dmidecode as dmidecode
|
||||
from netbox_agent.server import ServerBase
|
||||
|
||||
|
||||
|
|
@ -19,7 +20,7 @@ class HPHost(ServerBase):
|
|||
"""
|
||||
# FIXME: make a dmidecode function get_by_dminame() ?
|
||||
if self.is_blade():
|
||||
locator = self.dmi.get_by_type(204)
|
||||
locator = dmidecode.get_by_type(self.dmi, 204)
|
||||
if self.get_product_name() == 'ProLiant BL460c Gen10':
|
||||
locator = locator[0]['Strings']
|
||||
return {
|
||||
|
|
@ -27,14 +28,14 @@ class HPHost(ServerBase):
|
|||
'Enclosure Name': locator[0].strip(),
|
||||
'Server Bay': locator[3].strip(),
|
||||
'Enclosure Serial': locator[4].strip(),
|
||||
}
|
||||
}
|
||||
return locator[0]
|
||||
|
||||
def get_blade_slot(self):
|
||||
if self.is_blade():
|
||||
return 'Bay {}'.format(
|
||||
int(self.hp_rack_locator['Server Bay'].strip())
|
||||
)
|
||||
)
|
||||
return None
|
||||
|
||||
def get_chassis(self):
|
||||
|
|
|
|||
8
netbox_agent/vendors/qct.py
vendored
8
netbox_agent/vendors/qct.py
vendored
|
|
@ -7,12 +7,12 @@ class QCTHost(ServerBase):
|
|||
self.manufacturer = 'QCT'
|
||||
|
||||
def is_blade(self):
|
||||
return 'Location In Chassis' in self.dmi.get_by_type('Baseboard')[0].keys()
|
||||
return 'Location In Chassis' in self.baseboard[0].keys()
|
||||
|
||||
def get_blade_slot(self):
|
||||
if self.is_blade():
|
||||
return 'Slot {}'.format(
|
||||
self.dmi.get_by_type('Baseboard')[0].get('Location In Chassis').strip()
|
||||
self.baseboard[0].get('Location In Chassis').strip()
|
||||
)
|
||||
return None
|
||||
|
||||
|
|
@ -23,10 +23,10 @@ class QCTHost(ServerBase):
|
|||
|
||||
def get_chassis(self):
|
||||
if self.is_blade():
|
||||
return self.dmi.get_by_type('Chassis')[0]['Version'].strip()
|
||||
return self.chassis[0]['Version'].strip()
|
||||
return self.get_product_name()
|
||||
|
||||
def get_chassis_service_tag(self):
|
||||
if self.is_blade():
|
||||
return self.dmi.get_by_type('Chassis')[0]['Serial Number'].strip()
|
||||
return self.chassis[0]['Serial Number'].strip()
|
||||
return self.get_service_tag()
|
||||
|
|
|
|||
1
netbox_agent/vendors/supermicro.py
vendored
1
netbox_agent/vendors/supermicro.py
vendored
|
|
@ -2,6 +2,7 @@
|
|||
from netbox_agent.location import Slot
|
||||
from netbox_agent.server import ServerBase
|
||||
|
||||
|
||||
"""
|
||||
Supermicro DMI can be messed up. They depend on the vendor
|
||||
to set the correct values. The endusers cannot
|
||||
|
|
|
|||
86
netbox_agent/virtualmachine.py
Normal file
86
netbox_agent/virtualmachine.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import os
|
||||
|
||||
import netbox_agent.dmidecode as dmidecode
|
||||
from netbox_agent.config import config
|
||||
from netbox_agent.config import netbox_instance as nb
|
||||
from netbox_agent.logging import logging # NOQA
|
||||
from netbox_agent.misc import get_hostname
|
||||
from netbox_agent.network import VirtualNetwork
|
||||
|
||||
|
||||
def is_vm(dmi):
|
||||
bios = dmidecode.get_by_type(dmi, 'BIOS')
|
||||
system = dmidecode.get_by_type(dmi, 'System')
|
||||
|
||||
if 'Hyper-V' in bios[0]['Version'] or \
|
||||
'Xen' in bios[0]['Version'] or \
|
||||
'Google Compute Engine' in system[0]['Product Name'] or \
|
||||
'VirtualBox' in bios[0]['Version'] or \
|
||||
'VMware' in system[0]['Manufacturer']:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class VirtualMachine(object):
|
||||
def __init__(self, dmi=None):
|
||||
if dmi:
|
||||
self.dmi = dmi
|
||||
else:
|
||||
self.dmi = dmidecode.parse()
|
||||
self.network = None
|
||||
|
||||
def get_memory(self):
|
||||
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') # e.g. 4015976448
|
||||
mem_gib = mem_bytes / (1024.**2) # e.g. 3.74
|
||||
return int(mem_gib)
|
||||
|
||||
def get_vcpus(self):
|
||||
return os.cpu_count()
|
||||
|
||||
def get_netbox_vm(self):
|
||||
hostname = get_hostname(config)
|
||||
vm = nb.virtualization.virtual_machines.get(
|
||||
name=hostname
|
||||
)
|
||||
return vm
|
||||
|
||||
def get_netbox_cluster(self, name):
|
||||
cluster = nb.virtualization.clusters.get(
|
||||
name=name,
|
||||
)
|
||||
return cluster
|
||||
|
||||
def netbox_create_or_update(self, config):
|
||||
logging.debug('It\'s a virtual machine')
|
||||
created = False
|
||||
updated = 0
|
||||
|
||||
hostname = get_hostname(config)
|
||||
vm = self.get_netbox_vm()
|
||||
|
||||
vcpus = self.get_vcpus()
|
||||
memory = self.get_memory()
|
||||
if not vm:
|
||||
logging.debug('Creating Virtual machine..')
|
||||
cluster = self.get_netbox_cluster(config.virtual.cluster_name)
|
||||
|
||||
vm = nb.virtualization.virtual_machines.create(
|
||||
name=hostname,
|
||||
cluster=cluster.id,
|
||||
vcpus=vcpus,
|
||||
memory=memory,
|
||||
)
|
||||
created = True
|
||||
|
||||
self.network = VirtualNetwork(server=self)
|
||||
self.network.create_or_update_netbox_network_cards()
|
||||
|
||||
if not created and vm.vcpus != vcpus:
|
||||
vm.vcpus = vcpus
|
||||
updated += 1
|
||||
elif not created and vm.memory != memory:
|
||||
vm.memory = memory
|
||||
updated += 1
|
||||
|
||||
if updated:
|
||||
vm.save()
|
||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue