This commit is contained in:
Solvik Blum 2019-08-30 17:04:18 +02:00
parent 7d674ea35f
commit 8952c7e1eb
No known key found for this signature in database
GPG key ID: CC12B3DC262B6C47
7 changed files with 34 additions and 19 deletions

View file

@ -1,5 +1,3 @@
import argparse
from netbox_agent.logging import logging # NOQA
from netbox_agent.vendors.dell import DellHost
import netbox_agent.dmidecode as dmidecode

View file

@ -1,6 +1,8 @@
import logging
import pynetbox
import jsonargparse
import sys
def get_config():
p = jsonargparse.ArgumentParser(
@ -22,21 +24,23 @@ def get_config():
p.add_argument('--update-location', action='store_true', help='Update location')
p.add_argument('--log_level', default='debug')
p.add_argument('--netbox.url', help='Netbox URL', required=False)
p.add_argument('--netbox.token', help='Netbox API Token', required=False)
p.add_argument('--datacenter_location.driver', help='Datacenter location driver, ie: cmd, file')
p.add_argument('--datacenter_location.driver_file', help='Datacenter location custom driver file path')
p.add_argument('--netbox.url', help='Netbox URL')
p.add_argument('--netbox.token', help='Netbox API Token')
p.add_argument('--datacenter_location.driver',
help='Datacenter location driver, ie: cmd, file')
p.add_argument('--datacenter_location.driver_file',
help='Datacenter location custom driver file path')
p.add_argument('--datacenter_location.regex',
help='Datacenter location regex to extract Netbox DC slug')
p.add_argument('--rack_location.driver', help='Rack location driver, ie: cmd, file')
p.add_argument('--rack_location.driver_file', help='Rack location custom driver file path')
p.add_argument('--rack_location.regex', help='Rack location regex to extract Rack name')
p.add_argument('--slot_location.driver', help='Slot location driver, ie: cmd, file')
p.add_argument('--slot.driver_file', help='Slotlocation custom driver file path')
p.add_argument('--slot_location.driver_file', help='Slot location custom driver file path')
p.add_argument('--slot_location.regex', help='Slot location regex to extract slot name')
p.add_argument('--network.ignore_interfaces', default='(dummy.*|docker.*)',
p.add_argument('--network.ignore_interfaces', default=r'(dummy.*|docker.*)',
help='Regex to ignore interfaces')
p.add_argument('--network.ignore_ips', default='^(127\.0\.0\..*|fe80.*|::1.*)',
p.add_argument('--network.ignore_ips', default=r'^(127\.0\.0\..*|fe80.*|::1.*)',
help='Regex to ignore IPs')
p.add_argument('--network.lldp', help='Enable auto-cabling feature through LLDP infos')
p.add_argument('--inventory', action='store_true',
@ -45,15 +49,17 @@ def get_config():
options = p.parse_args()
return options
def get_netbox_instance():
config = get_config()
if config.netbox.url is None or config.netbox.token is None:
logging.error('Netbox URL and token are mandatory')
return None
sys.exit(1)
return pynetbox.api(
url=get_config().netbox.url,
token=get_config().netbox.token,
)
config = get_config()
netbox_instance = get_netbox_instance()

View file

@ -135,7 +135,8 @@ def get_by_type(type_id):
def _execute_cmd():
if not is_tool('dmidecode'):
logging.error('Dmidecode does not seem to be present on your system. Add it your path or check the compatibility of this project with your distro.')
logging.error('Dmidecode does not seem to be present on your system. Add it your path or '
'check the compatibility of this project with your distro.')
sys.exit(1)
return _subprocess.check_output(['dmidecode', ], stderr=_subprocess.PIPE)

View file

@ -52,6 +52,8 @@ class Inventory():
"""
def __init__(self, server):
if config.inventory is None or config.update_inventory is None:
return None
self.server = server
netbox_server = self.server.get_netbox_server()
self.device_id = netbox_server.id if netbox_server else None
@ -322,7 +324,7 @@ class Inventory():
return True
def update(self):
if not config.inventory:
if config.inventory is None or config.update_inventory is None:
return False
self.update_netbox_cpus()
self.update_netbox_memory()

View file

@ -51,9 +51,10 @@ class LocationBase():
class Datacenter(LocationBase):
def __init__(self):
driver = config.datacenter_location.driver.split(':')[0] if config.datacenter_location.driver else None
driver_value = ':'.join(config.datacenter_location.driver.split(':')[1:]) if config.datacenter_location.driver \
else None
driver = config.datacenter_location.driver.split(':')[0] if \
config.datacenter_location.driver else None
driver_value = ':'.join(config.datacenter_location.driver.split(':')[1:]) if \
config.datacenter_location.driver else None
driver_file = config.datacenter_location.driver_file
regex = config.datacenter_location.regex
super().__init__(driver, driver_value, driver_file, regex)
@ -61,8 +62,10 @@ class Datacenter(LocationBase):
class Rack(LocationBase):
def __init__(self):
driver = config.rack_location.driver.split(':')[0] if config.rack_location.driver else None
driver_value = ':'.join(config.rack_location.driver.split(':')[1:]) if config.rack_location.driver else None
driver = config.rack_location.driver.split(':')[0] if \
config.rack_location.driver else None
driver_value = ':'.join(config.rack_location.driver.split(':')[1:]) if \
config.rack_location.driver else None
driver_file = config.rack_location.driver_file
regex = config.rack_location.regex
super().__init__(driver, driver_value, driver_file, regex)
@ -70,8 +73,10 @@ class Rack(LocationBase):
class Slot(LocationBase):
def __init__(self):
driver = config.slot_location.driver.split(':')[0] if config.slot_location.driver else None
driver_value = ':'.join(config.slot_location.driver.split(':')[1:]) if config.slot_location.driver else None
driver = config.slot_location.driver.split(':')[0] if \
config.slot_location.driver else None
driver_value = ':'.join(config.slot_location.driver.split(':')[1:]) if \
config.slot_location.driver else None
driver_file = config.slot_location.driver_file
regex = config.slot_location.regex
super().__init__(driver, driver_value, driver_file, regex)

View file

@ -502,6 +502,8 @@ class Network():
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...')
# delete unknown interface

View file

@ -2,3 +2,4 @@ pynetbox==4.0.6
netaddr==0.7.19
netifaces==0.10.9
pyyaml==5.1.2
jsonargparse==2.1.0