-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathconfig.py
180 lines (166 loc) · 6.92 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import logging
import sys
import jsonargparse
import pynetbox
import requests
import urllib3
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",
env_prefix="NETBOX_AGENT_",
default_env=True,
)
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")
p.add_argument("--update-psu", action="store_true", help="Update PSU")
p.add_argument(
"--update-hypervisor",
action="store_true",
help="Update virtualization cluster and virtual machines",
)
p.add_argument(
"--update-old-devices",
action="store_true",
help="Update serial number of existing (old ?) devices having same name but different serial",
)
p.add_argument(
"--purge-old-devices",
action="store_true",
help="Purge existing (old ?) devices having same name but different serial",
)
p.add_argument(
"--expansion-as-device",
action="store_true",
help="Manage blade expansions as external devices",
)
p.add_argument("--log_level", default="debug")
p.add_argument("--netbox.ssl_ca_certs_file", help="SSL CA certificates file")
p.add_argument("--netbox.url", help="Netbox URL")
p.add_argument("--netbox.token", help="Netbox API Token")
p.add_argument(
"--netbox.ssl_verify", default=True, action="store_true", help="Disable SSL verification"
)
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("--virtual.hypervisor", action="store_true", help="Is a hypervisor or not")
p.add_argument(
"--virtual.list_guests_cmd",
default=None,
help="Command to output the list of vrtualization guests in the hypervisor separated by whitespace",
)
p.add_argument(
"--hostname_cmd",
default=None,
help="Command to output hostname, used as Device's name in netbox",
)
p.add_argument(
"--device.platform",
default=None,
help="Override device platform. Here we use OS distribution.",
)
p.add_argument("--device.tags", default=r"", help="tags to use for a host")
p.add_argument(
"--preserve-tags",
action="store_true",
help="Append new unique tags, preserve those already present",
)
p.add_argument(
"--device.custom_fields",
default=r"",
help="custom_fields to use for a host, eg: field1=v1,field2=v2",
)
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")
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.ipmi", default=True, help="Enable gathering IPMI information")
p.add_argument("--network.lldp", help="Enable auto-cabling feature through LLDP infos")
p.add_argument(
"--network.nic_id",
choices=("name", "mac"),
default="name",
help="What property to use as NIC identifier",
)
p.add_argument(
"--network.primary_mac",
choices=("permanent", "temp"),
default="temp",
help="Which MAC address to use as primary. Permanent requires ethtool and fallbacks to temporary",
)
p.add_argument(
"--inventory",
action="store_true",
help="Enable HW inventory (CPU, Memory, RAID Cards, Disks) feature",
)
p.add_argument(
"--process-virtual-drives",
action="store_true",
help="Process virtual drives information from RAID controllers to fill disk custom_fields",
)
p.add_argument(
"--force-disk-refresh", action="store_true", help="Forces disks detection reprocessing"
)
p.add_argument("--dump-disks-map", help="File path to dump physical/virtual disks map")
options = p.parse_args()
return options
config = get_config()
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)
nb = pynetbox.api(
url=get_config().netbox.url,
token=get_config().netbox.token,
)
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:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session = requests.Session()
session.verify = False
nb.http_session = session
return nb
netbox_instance = get_netbox_instance()