2019-09-03 13:16:37 +02:00
|
|
|
import logging
|
2020-07-08 10:02:45 +02:00
|
|
|
import sys
|
2020-07-08 22:19:39 +02:00
|
|
|
|
2020-02-02 20:08:56 +01:00
|
|
|
import jsonargparse
|
|
|
|
import pynetbox
|
2020-07-11 15:14:33 +02:00
|
|
|
import requests
|
|
|
|
import urllib3
|
2020-02-02 20:08:56 +01:00
|
|
|
|
2019-09-03 13:16:37 +02:00
|
|
|
|
|
|
|
def get_config():
|
|
|
|
p = jsonargparse.ArgumentParser(
|
|
|
|
default_config_files=[
|
|
|
|
'/etc/netbox_agent.yaml',
|
|
|
|
'~/.config/netbox_agent.yaml',
|
|
|
|
'~/.netbox_agent.yaml',
|
|
|
|
],
|
|
|
|
prog='netbox_agent',
|
|
|
|
description="Netbox agent to run on your infrastructure's servers",
|
2020-02-02 20:08:56 +01:00
|
|
|
env_prefix='NETBOX_AGENT_',
|
|
|
|
default_env=True
|
2019-09-03 13:16:37 +02:00
|
|
|
)
|
|
|
|
p.add_argument('-c', '--config', action=jsonargparse.ActionConfigFile)
|
|
|
|
|
|
|
|
p.add_argument('-r', '--register', action='store_true', help='Register server to Netbox')
|
|
|
|
p.add_argument('-u', '--update-all', action='store_true', help='Update all infos in Netbox')
|
|
|
|
p.add_argument('-d', '--debug', action='store_true', help='Print debug infos')
|
|
|
|
p.add_argument('--update-network', action='store_true', help='Update network')
|
|
|
|
p.add_argument('--update-inventory', action='store_true', help='Update inventory')
|
|
|
|
p.add_argument('--update-location', action='store_true', help='Update location')
|
2019-09-05 13:47:10 +02:00
|
|
|
p.add_argument('--update-psu', action='store_true', help='Update PSU')
|
2022-02-08 18:45:44 +01:00
|
|
|
p.add_argument('--purge-old-devices', action='store_true',
|
|
|
|
help='Purge existing (old ?) devices having same name but different serial')
|
Manage blade expansions as independent devices
This patch adds the ability to detect and manage GPU and Disk expansion
bays, and either add their internal components into the device
corresponding to the blade server, or into a dedicated device.
It takes advantage of the work made by @cyrinux on GPU bays management, and
applies the same principle to the external disk bays, but harmonize the
inventory management:
- If no argument is specified on the command line, the GPU cards, RAID
controllers and their attached disks are added in the blade device,
and the device corresponding to an expansion device is deleted.
- If the `--expansion-as-device` option is specified on the command
line, a dedicated device corresponding to the expansion bay is
created, and the GPUs, RAID card and attached disks are removed from
the blade device and added to the expansion device.
2022-02-11 18:22:13 +01:00
|
|
|
p.add_argument('--expansion-as-device', action='store_true',
|
|
|
|
help='Manage blade expansions as external devices')
|
2019-09-03 13:16:37 +02:00
|
|
|
|
|
|
|
p.add_argument('--log_level', default='debug')
|
2022-02-21 18:12:16 +01:00
|
|
|
p.add_argument('--netbox.ssl_ca_certs_file', help='SSL CA certificates file')
|
2019-09-03 13:16:37 +02:00
|
|
|
p.add_argument('--netbox.url', help='Netbox URL')
|
|
|
|
p.add_argument('--netbox.token', help='Netbox API Token')
|
2020-07-11 15:27:32 +02:00
|
|
|
p.add_argument('--netbox.ssl_verify', default=True, action='store_true',
|
|
|
|
help='Disable SSL verification')
|
2020-04-19 12:28:49 +02:00
|
|
|
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')
|
2019-09-12 11:15:12 +02:00
|
|
|
p.add_argument('--hostname_cmd', default=None,
|
|
|
|
help="Command to output hostname, used as Device's name in netbox")
|
2020-07-01 18:54:58 +02:00
|
|
|
p.add_argument('--device.tags', default=r'',
|
|
|
|
help='tags to use for a host')
|
|
|
|
p.add_argument('--device.blade_role', default=r'Blade',
|
|
|
|
help='role to use for a blade server')
|
|
|
|
p.add_argument('--device.chassis_role', default=r'Server Chassis',
|
|
|
|
help='role to use for a chassis')
|
|
|
|
p.add_argument('--device.server_role', default=r'Server',
|
|
|
|
help='role to use for a server')
|
|
|
|
p.add_argument('--tenant.driver',
|
|
|
|
help='tenant driver, ie cmd, file')
|
|
|
|
p.add_argument('--tenant.driver_file',
|
|
|
|
help='tenant driver custom driver file path')
|
|
|
|
p.add_argument('--tenant.regex',
|
|
|
|
help='tenant regex to extract Netbox tenant slug')
|
2019-09-03 13:16:37 +02:00
|
|
|
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_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=r'(dummy.*|docker.*)',
|
|
|
|
help='Regex to ignore interfaces')
|
|
|
|
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',
|
|
|
|
help='Enable HW inventory (CPU, Memory, RAID Cards, Disks) feature')
|
|
|
|
|
|
|
|
options = p.parse_args()
|
|
|
|
return options
|
|
|
|
|
|
|
|
|
2022-02-21 18:12:16 +01:00
|
|
|
config = get_config()
|
|
|
|
|
|
|
|
|
2019-09-03 13:16:37 +02:00
|
|
|
def get_netbox_instance():
|
|
|
|
if config.netbox.url is None or config.netbox.token is None:
|
|
|
|
logging.error('Netbox URL and token are mandatory')
|
|
|
|
sys.exit(1)
|
2020-07-08 09:47:48 +02:00
|
|
|
|
2020-07-08 09:36:13 +02:00
|
|
|
nb = pynetbox.api(
|
2019-09-03 13:16:37 +02:00
|
|
|
url=get_config().netbox.url,
|
|
|
|
token=get_config().netbox.token,
|
|
|
|
)
|
2022-02-21 18:12:16 +01:00
|
|
|
ca_certs_file = config.netbox.ssl_ca_certs_file
|
|
|
|
if ca_certs_file is not None:
|
|
|
|
session = requests.Session()
|
|
|
|
session.verify = ca_certs_file
|
|
|
|
nb.http_session = session
|
|
|
|
elif config.netbox.ssl_verify is False:
|
2020-07-08 09:36:13 +02:00
|
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
session = requests.Session()
|
|
|
|
session.verify = False
|
|
|
|
nb.http_session = session
|
2019-09-03 13:16:37 +02:00
|
|
|
|
2020-07-08 09:36:13 +02:00
|
|
|
return nb
|
2019-09-03 13:16:37 +02:00
|
|
|
|
2020-07-08 09:47:48 +02:00
|
|
|
|
2019-09-03 13:16:37 +02:00
|
|
|
netbox_instance = get_netbox_instance()
|