-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathinventory.py
More file actions
605 lines (520 loc) · 21.6 KB
/
Copy pathinventory.py
File metadata and controls
605 lines (520 loc) · 21.6 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
from netbox_agent.config import config
from netbox_agent.config import netbox_instance as nb
from netbox_agent.ethtool import Ethtool
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
import traceback
import pynetbox
import logging
import json
import re
INVENTORY_TAG = {
"cpu": {"name": "hw:cpu", "slug": "hw-cpu"},
"gpu": {"name": "hw:gpu", "slug": "hw-gpu"},
"disk": {"name": "hw:disk", "slug": "hw-disk"},
"interface": {"name": "hw:interface", "slug": "hw-interface"},
"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:
"""
Better Inventory items coming, see:
- https://github.com/netbox-community/netbox/issues/3087
- https://github.com/netbox-community/netbox/issues/3333
This class implements for:
* memory
* cpu
* raid cards
* disks
* gpus
methods that:
* get local item
* get netbox item
* create netbox item
* update netbox item
Known issues:
- no scan of non-raid devices
- no scan of NVMe devices
"""
def __init__(self, server, update_expansion=False):
self.create_netbox_tags()
self.server = server
self.update_expansion = update_expansion
netbox_server = self.server.get_netbox_server(update_expansion)
self.device_id = netbox_server.id if netbox_server else None
self.raid = None
self.disks = []
self.lshw = LSHW()
def create_netbox_tags(self):
ret = []
for key, tag in INVENTORY_TAG.items():
nb_tag = nb.extras.tags.get(name=tag["name"])
if not nb_tag:
nb_tag = nb.extras.tags.create(
name=tag["name"],
slug=tag["slug"],
comments=tag["name"],
)
ret.append(nb_tag)
return ret
def find_or_create_manufacturer(self, name):
if name is None:
return None
manufacturer = nb.dcim.manufacturers.get(
name=name,
)
if not manufacturer:
logging.info("Creating missing manufacturer {name}".format(name=name))
manufacturer = nb.dcim.manufacturers.create(
name=name,
slug=re.sub("[^A-Za-z0-9]+", "-", name).lower(),
)
logging.info("Creating missing manufacturer {name}".format(name=name))
return manufacturer
def get_netbox_inventory(self, device_id, tag):
try:
items = nb.dcim.inventory_items.filter(device_id=device_id, tag=tag)
except pynetbox.core.query.RequestError:
logging.info("Tag {tag} is missing, returning empty array.".format(tag=tag))
items = []
return list(items)
def create_netbox_inventory_item(self, device_id, tags, vendor, name, serial, description):
manufacturer = self.find_or_create_manufacturer(vendor)
_ = nb.dcim.inventory_items.create(
device=device_id,
manufacturer=manufacturer.id,
discovered=True,
tags=tags,
name="{}".format(name),
serial="{}".format(serial),
description=description,
)
logging.info(
"Creating inventory item {} {}/{} {} ".format(vendor, name, serial, description)
)
def get_hw_motherboards(self):
motherboards = []
m = {}
m["serial"] = self.lshw.motherboard_serial
m["vendor"] = self.lshw.vendor
m["name"] = "{} {}".format(self.lshw.vendor, self.lshw.motherboard)
m["description"] = "{} Motherboard".format(self.lshw.motherboard)
motherboards.append(m)
return motherboards
def do_netbox_motherboard(self):
motherboards = self.get_hw_motherboards()
nb_motherboards = self.get_netbox_inventory(
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]:
logging.info(
"Deleting unknown motherboard {motherboard}/{serial}".format(
motherboard=self.lshw.motherboard,
serial=nb_motherboard.serial,
)
)
nb_motherboard.delete()
# create interfaces that are not in netbox
for motherboard in motherboards:
if motherboard.get("serial") not in [x.serial for x in nb_motherboards]:
self.create_netbox_inventory_item(
device_id=self.device_id,
tags=[{"name": INVENTORY_TAG["motherboard"]["name"]}],
vendor="{}".format(motherboard.get("vendor", "N/A")),
serial="{}".format(motherboard.get("serial", "No SN")),
name="{}".format(motherboard.get("name")),
description="{}".format(motherboard.get("description")),
)
def create_netbox_interface(self, iface, component_type=None, component_id=None):
component_args = {}
if component_type and component_id:
component_args["component_type"] = component_type
component_args["component_id"] = component_id
manufacturer = self.find_or_create_manufacturer(iface["vendor"])
_ = nb.dcim.inventory_items.create(
device=self.device_id,
manufacturer=manufacturer.id,
discovered=True,
tags=[{"name": INVENTORY_TAG["interface"]["name"]}],
name="{}".format(iface["product"]),
serial="{}".format(iface["serial"]),
description="{}".format(iface["description"]),
**component_args,
)
def do_netbox_interfaces(self):
nb_interfaces = self.get_netbox_inventory(
device_id=self.device_id, tag=INVENTORY_TAG["interface"]["slug"]
)
interfaces = self.lshw.interfaces
for iface in interfaces:
ethtool = Ethtool(iface["name"]).parse()
# Override interface serial number from lshw with permanent MAC address from ethtool, if available.
if ethtool and "mac" in ethtool and ethtool.get("mac") != "00:00:00:00:00:00":
iface["serial"] = ethtool["mac"]
# delete interfaces that are in netbox but not locally
# use the serial_number has the comparison element
for nb_interface in nb_interfaces:
if nb_interface.serial not in [x["serial"] for x in interfaces]:
logging.info(
"Deleting unknown interface {serial}".format(
serial=nb_interface.serial,
)
)
nb_interface.delete()
for iface in interfaces:
# Override interface description from lshw.
iface["description"] = "{} {}".format(iface["description"], iface["name"])
# Try to find the network interface component id.
component_type = None
component_id = None
nb_interface_component = nb.dcim.interfaces.get(name=iface["name"], device_id=self.device_id)
if hasattr(nb_interface_component, "id"):
component_type = "dcim.interface"
component_id = nb_interface_component.id
if iface.get("serial") not in [x.serial for x in nb_interfaces]:
# create interfaces that are not in netbox
self.create_netbox_interface(iface, component_type, component_id)
else:
# update interfaces that are in netbox if necessary
update = False
nb_interface = [x for x in nb_interfaces if x.serial == iface.get("serial")][0]
if nb_interface.description != iface["description"]:
logging.info(
"Updating interface {} description from {} to {}".format(
nb_interface.serial,
nb_interface.description,
iface["description"],
)
)
nb_interface.description = iface["description"]
update = True
if (
nb_interface.component_type != component_type
or nb_interface.component_id != component_id
):
logging.info(
"Updating interface {} component_type/component_id from {}/{} to {}/{}".format(
nb_interface.serial,
nb_interface.component_type,
nb_interface.component_id,
component_type,
component_id,
)
)
nb_interface.component_type = component_type
nb_interface.component_id = component_id
update = True
if update:
nb_interface.save()
def create_netbox_cpus(self):
for cpu in self.lshw.get_hw_linux("cpu"):
manufacturer = self.find_or_create_manufacturer(cpu["vendor"])
_ = nb.dcim.inventory_items.create(
device=self.device_id,
manufacturer=manufacturer.id,
discovered=True,
tags=[{"name": INVENTORY_TAG["cpu"]["name"]}],
name=cpu["product"],
description="CPU {}".format(cpu["location"]),
# asset_tag=cpu['location']
)
logging.info("Creating CPU model {}".format(cpu["product"]))
def do_netbox_cpus(self):
cpus = self.lshw.get_hw_linux("cpu")
nb_cpus = self.get_netbox_inventory(
device_id=self.device_id,
tag=INVENTORY_TAG["cpu"]["slug"],
)
if not len(nb_cpus) or len(nb_cpus) and len(cpus) != len(nb_cpus):
for x in nb_cpus:
x.delete()
self.create_netbox_cpus()
def get_raid_cards(self, filter_cards=False):
raid_class = None
if self.server.manufacturer in ("Dell", "Huawei"):
if is_tool("omreport"):
raid_class = OmreportRaid
if is_tool("storcli"):
raid_class = StorcliRaid
elif self.server.manufacturer in ("HP", "HPE"):
if is_tool("ssacli"):
raid_class = HPRaid
if not raid_class:
return []
self.raid = raid_class()
if filter_cards and config.expansion_as_device and self.server.own_expansion_slot():
return [
c for c in self.raid.get_controllers() if c.is_external() is self.update_expansion
]
else:
return self.raid.get_controllers()
def create_netbox_raid_card(self, raid_card):
manufacturer = self.find_or_create_manufacturer(raid_card.get_manufacturer())
name = raid_card.get_product_name()
serial = raid_card.get_serial_number()
nb_raid_card = nb.dcim.inventory_items.create(
device=self.device_id,
discovered=True,
manufacturer=manufacturer.id if manufacturer else None,
tags=[{"name": INVENTORY_TAG["raid_card"]["name"]}],
name="{}".format(name),
serial="{}".format(serial),
description="RAID Card",
)
logging.info(
"Creating RAID Card {name} (SN: {serial})".format(
name=name,
serial=serial,
)
)
return nb_raid_card
def do_netbox_raid_cards(self):
"""
Update raid cards in netbobx
Since we only push:
* Name
* Manufacturer
* Serial
We only need to handle destroy and new cards
"""
nb_raid_cards = self.get_netbox_inventory(
device_id=self.device_id, tag=[INVENTORY_TAG["raid_card"]["slug"]]
)
raid_cards = self.get_raid_cards(filter_cards=True)
# delete cards that are in netbox but not locally
# use the serial_number has the comparison element
for nb_raid_card in nb_raid_cards:
if nb_raid_card.serial not in [x.get_serial_number() for x in raid_cards]:
logging.info(
"Deleting unknown locally RAID Card {serial}".format(
serial=nb_raid_card.serial,
)
)
nb_raid_card.delete()
# create card that are not in netbox
for raid_card in raid_cards:
if raid_card.get_serial_number() not in [x.serial for x in nb_raid_cards]:
self.create_netbox_raid_card(raid_card)
def is_virtual_disk(self, disk, raid_devices):
disk_type = disk.get("type")
logicalname = disk.get("logicalname")
description = disk.get("description")
size = disk.get("size")
product = disk.get("product")
if (
logicalname in raid_devices
or disk_type is None
or product is None
or description is None
):
return True
non_raid_disks = [
"MR9361-8i",
]
if (
logicalname in raid_devices
or product in non_raid_disks
or "virtual" in product.lower()
or "logical" in product.lower()
or "volume" in description.lower()
or "dvd-ram" in description.lower()
or description == "SCSI Enclosure"
or (size is None and logicalname is None)
):
return True
return False
def get_hw_disks(self):
disks = []
for raid_card in self.get_raid_cards(filter_cards=True):
disks.extend(raid_card.get_physical_disks())
raid_devices = [
d.get("custom_fields", {}).get("vd_device")
for d in disks
if d.get("custom_fields", {}).get("vd_device")
]
for disk in self.lshw.get_hw_linux("storage"):
if self.is_virtual_disk(disk, raid_devices):
continue
size = round(int(disk.get("size", 0)) / 1073741824, 1)
d = {
"name": "",
"Size": "{} GB".format(size),
"logicalname": disk.get("logicalname"),
"description": disk.get("description"),
"SN": disk.get("serial"),
"Model": disk.get("product"),
"Type": disk.get("type"),
}
if disk.get("vendor"):
d["Vendor"] = disk["vendor"]
else:
d["Vendor"] = get_vendor(disk["product"])
disks.append(d)
# remove duplicate serials
seen = set()
uniq = [x for x in disks if x["SN"] not in seen and not seen.add(x["SN"])]
return uniq
def create_netbox_disk(self, disk):
manufacturer = None
if "Vendor" in disk:
manufacturer = self.find_or_create_manufacturer(disk["Vendor"])
logicalname = disk.get("logicalname")
desc = disk.get("description")
name = "{} ({})".format(disk["Model"], disk["Size"])
description = disk["Type"]
sn = disk.get("SN", "unknown")
parms = {
"device": self.device_id,
"discovered": True,
"tags": [{"name": INVENTORY_TAG["disk"]["name"]}],
"name": name,
"serial": sn,
"part_id": disk["Model"],
"description": description,
"manufacturer": getattr(manufacturer, "id", None),
}
if config.process_virtual_drives:
parms["custom_fields"] = disk.get("custom_fields", {})
_ = nb.dcim.inventory_items.create(**parms)
logging.info(
"Creating Disk {model} {serial}".format(
model=disk["Model"],
serial=sn,
)
)
def dump_disks_map(self, disks):
disk_map = [d["custom_fields"] for d in disks if "custom_fields" in d]
if config.dump_disks_map == "-":
f = sys.stdout
else:
f = open(config.dump_disks_map, "w")
f.write(json.dumps(disk_map, separators=(",", ":"), indent=4, sort_keys=True))
if config.dump_disks_map != "-":
f.close()
def do_netbox_disks(self):
nb_disks = self.get_netbox_inventory(
device_id=self.device_id, tag=INVENTORY_TAG["disk"]["slug"]
)
disks = self.get_hw_disks()
if config.dump_disks_map:
try:
self.dump_disks_map(disks)
except Exception as e:
logging.error("Failed to dump disks map: {}".format(e))
logging.debug(traceback.format_exc())
disk_serials = [d["SN"] for d in disks if "SN" in d]
# delete disks that are in netbox but not locally
# use the serial_number has the comparison element
for nb_disk in nb_disks:
if nb_disk.serial not in disk_serials or config.force_disk_refresh:
logging.info(
"Deleting unknown locally Disk {serial}".format(
serial=nb_disk.serial,
)
)
nb_disk.delete()
if config.force_disk_refresh:
nb_disks = self.get_netbox_inventory(
device_id=self.device_id, tag=INVENTORY_TAG["disk"]["slug"]
)
# create disks that are not in netbox
for disk in disks:
if disk.get("SN") not in [d.serial for d in nb_disks]:
self.create_netbox_disk(disk)
def create_netbox_memory(self, memory):
manufacturer = self.find_or_create_manufacturer(memory["vendor"])
name = "Slot {} ({}GB)".format(memory["slot"], memory["size"])
nb_memory = nb.dcim.inventory_items.create(
device=self.device_id,
discovered=True,
manufacturer=manufacturer.id,
tags=[{"name": INVENTORY_TAG["memory"]["name"]}],
name=name,
part_id=memory["product"],
serial=memory["serial"],
description=memory["description"],
)
logging.info(
"Creating Memory {location} {type} {size}GB".format(
location=memory["slot"],
type=memory["product"],
size=memory["size"],
)
)
return nb_memory
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"]
)
for nb_memory in nb_memories:
if nb_memory.serial not in [x["serial"] for x in memories]:
logging.info(
"Deleting unknown locally Memory {serial}".format(
serial=nb_memory.serial,
)
)
nb_memory.delete()
for memory in memories:
if memory.get("serial") not in [x.serial for x in nb_memories]:
self.create_netbox_memory(memory)
def create_netbox_gpus(self, gpus):
for gpu in gpus:
if "product" in gpu and len(gpu["product"]) > 50:
gpu["product"] = gpu["product"][:48] + ".."
manufacturer = self.find_or_create_manufacturer(gpu["vendor"])
_ = nb.dcim.inventory_items.create(
device=self.device_id,
manufacturer=manufacturer.id,
discovered=True,
tags=[{"name": INVENTORY_TAG["gpu"]["name"]}],
name=gpu["product"],
description=gpu["description"],
)
logging.info("Creating GPU model {}".format(gpu["product"]))
def is_external_gpu(self, gpu):
is_3d_gpu = gpu["description"].startswith("3D")
return self.server.is_blade() and self.server.own_gpu_expansion_slot() and is_3d_gpu
def do_netbox_gpus(self):
gpus = []
gpu_models = {}
for gpu in self.lshw.get_hw_linux("gpu"):
# Filters GPU if an expansion bay is detected:
# The internal (VGA) GPU only goes into the blade inventory,
# the external (3D) GPU goes into the expansion blade.
if config.expansion_as_device and self.update_expansion ^ self.is_external_gpu(gpu):
continue
gpus.append(gpu)
gpu_models.setdefault(gpu["product"], 0)
gpu_models[gpu["product"]] += 1
nb_gpus = self.get_netbox_inventory(
device_id=self.device_id,
tag=INVENTORY_TAG["gpu"]["slug"],
)
nb_gpu_models = {}
for gpu in nb_gpus:
nb_gpu_models.setdefault(str(gpu), 0)
nb_gpu_models[str(gpu)] += 1
up_to_date = set(gpu_models) == set(nb_gpu_models)
if not gpus or not up_to_date:
for x in nb_gpus:
x.delete()
if gpus and not up_to_date:
self.create_netbox_gpus(gpus)
def create_or_update(self):
if config.inventory is None or config.update_inventory is None:
return False
if self.update_expansion is False:
self.do_netbox_cpus()
self.do_netbox_memories()
self.do_netbox_interfaces()
self.do_netbox_motherboard()
self.do_netbox_gpus()
self.do_netbox_disks()
self.do_netbox_raid_cards()
return True