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.logging import logging # NOQA
from netbox_agent.vendors.dell import DellHost from netbox_agent.vendors.dell import DellHost
import netbox_agent.dmidecode as dmidecode import netbox_agent.dmidecode as dmidecode

View file

@ -1,6 +1,8 @@
import logging import logging
import pynetbox import pynetbox
import jsonargparse import jsonargparse
import sys
def get_config(): def get_config():
p = jsonargparse.ArgumentParser( p = jsonargparse.ArgumentParser(
@ -22,21 +24,23 @@ def get_config():
p.add_argument('--update-location', action='store_true', help='Update location') p.add_argument('--update-location', action='store_true', help='Update location')
p.add_argument('--log_level', default='debug') p.add_argument('--log_level', default='debug')
p.add_argument('--netbox.url', help='Netbox URL', required=False) p.add_argument('--netbox.url', help='Netbox URL')
p.add_argument('--netbox.token', help='Netbox API Token', required=False) 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',
p.add_argument('--datacenter_location.driver_file', help='Datacenter location custom driver file path') 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', p.add_argument('--datacenter_location.regex',
help='Datacenter location regex to extract Netbox DC slug') 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', 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.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('--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_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('--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') 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') help='Regex to ignore IPs')
p.add_argument('--network.lldp', help='Enable auto-cabling feature through LLDP infos') p.add_argument('--network.lldp', help='Enable auto-cabling feature through LLDP infos')
p.add_argument('--inventory', action='store_true', p.add_argument('--inventory', action='store_true',
@ -45,15 +49,17 @@ def get_config():
options = p.parse_args() options = p.parse_args()
return options return options
def get_netbox_instance(): def get_netbox_instance():
config = get_config() config = get_config()
if config.netbox.url is None or config.netbox.token is None: if config.netbox.url is None or config.netbox.token is None:
logging.error('Netbox URL and token are mandatory') logging.error('Netbox URL and token are mandatory')
return None sys.exit(1)
return pynetbox.api( return pynetbox.api(
url=get_config().netbox.url, url=get_config().netbox.url,
token=get_config().netbox.token, token=get_config().netbox.token,
) )
config = get_config() config = get_config()
netbox_instance = get_netbox_instance() netbox_instance = get_netbox_instance()

View file

@ -135,7 +135,8 @@ def get_by_type(type_id):
def _execute_cmd(): def _execute_cmd():
if not is_tool('dmidecode'): 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) sys.exit(1)
return _subprocess.check_output(['dmidecode', ], stderr=_subprocess.PIPE) return _subprocess.check_output(['dmidecode', ], stderr=_subprocess.PIPE)

View file

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

View file

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

View file

@ -502,6 +502,8 @@ class Network():
logging.debug('Finished creating NIC!') logging.debug('Finished creating NIC!')
def update_netbox_network_cards(self): def update_netbox_network_cards(self):
if config.update_all is None or config.update_network is None:
return None
logging.debug('Updating NIC...') logging.debug('Updating NIC...')
# delete unknown interface # delete unknown interface

View file

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