-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredfish_collector.py
931 lines (779 loc) · 33.3 KB
/
redfish_collector.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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
"""
handles the redfish inventory collection requests
"""
import logging
import time
import socket
import requests
class CollectorException(Exception):
"""
exception class for the collector
"""
class RedfishIventoryCollector:
"""
collects the inventory from a server using Redfish
"""
def __init__(self, timeout, target, usr, pwd):
self.target = target
self.ip_address = self.get_bmc_ip_address(target)
self._username = usr
self._password = pwd
self.timeout = timeout
self._response_time = 0
self.last_http_code = 0
self._urls = {
"Systems_Root": "/redfish/v1/Systems",
"Chassis_Root": "/redfish/v1/Chassis",
}
self._inventory = {}
self._start_time = time.time()
self.session_url = None
self._auth_token = None
self._basic_auth = False
self._session = None
def get_bmc_ip_address(self, target):
"""
Get the IP address of the target.
"""
try:
return socket.gethostbyname(target)
except socket.gaierror as err:
raise CollectorException(f"DNS lookup failed for Remote Board {target}: {err}") from err
def get_session(self):
"""
get a session from the server's remote management interface using Redfish
"""
# Get the url for the server info and messure the response time
logging.info(" Target %s: Connecting to server %s", self.target, self.ip_address)
start_time = time.time()
server_response = self.connect_server("/redfish/v1", noauth=True)
if not server_response:
logging.error(
" Target %s: No data received from server %s!",
self.target,
self.ip_address
)
self._session = None
return
self._response_time = round(time.time() - start_time,2)
logging.info(" Target %s: Response time: %s seconds.", self.target, self._response_time)
session_service = self.connect_server(
server_response['SessionService']['@odata.id'],
basic_auth=True
)
if not self.last_http_code == 200:
logging.warning(" Target %s: Failed to get a session from server %s!",
self.target,
self.ip_address
)
return
sessions_url = (
f"https://{self.ip_address}"
f"{session_service['Sessions']['@odata.id']}"
)
session_data = {"UserName": self._username, "Password": self._password}
self._session.auth = None
result = ""
# Try to get a session
try:
result = self._session.post(
sessions_url,
json=session_data,
verify=False,
timeout=self.timeout
)
result.raise_for_status()
except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout) as err:
logging.warning(
" Target %s: A timeout occured %s: %s",
self.target,
self.ip_address,
err
)
except requests.exceptions.ConnectionError:
logging.warning(
" Target %s: Failed to get an auth token from server %s. Retrying ...",
self.target,
self.ip_address
)
try:
result = self._session.post(
sessions_url, json=session_data, verify=False, timeout=self.timeout
)
result.raise_for_status()
except requests.exceptions.ConnectionError as excptn:
logging.error(
" Target %s: Error getting an auth token from server %s: %s",
self.target,
self.ip_address,
excptn
)
self._basic_auth = True
except requests.exceptions.HTTPError as err:
logging.warning(
" Target %s: No session received from server %s: %s",
self.target,
self.ip_address,
err
)
logging.warning(" Target %s: Switching to basic authentication.", self.target)
self._basic_auth = True
if result:
if result.status_code in [200,201]:
self._auth_token = result.headers['X-Auth-Token']
self.session_url = result.json()['@odata.id']
logging.info(
" Target %s: Got an auth token from server %s!",
self.target,
self.ip_address
)
def connect_server(self, command, noauth = False, basic_auth = False, fields = None):
"""
connect to the server and get the data
"""
logging.captureWarnings(True)
server_req = ""
server_response = ""
self.last_http_code = 0
request_start = time.time()
if fields:
logging.debug(" Target %s: Getting fields %s", self.target, fields)
command += "?$select=" + ",".join(fields)
url = f"https://{self.ip_address}{command}"
# check if we already established a session with the server
if not self._session:
self._session = requests.Session()
else:
logging.debug(" Target %s: Using existing session.", self.target)
self._session.verify = False
self._session.headers.update({'charset': 'utf-8'})
self._session.headers.update({'content-type': 'application/json'})
if noauth:
logging.debug(" Target %s: Using no auth", self.target)
elif basic_auth or self._basic_auth:
self._session.auth = (self._username, self._password)
logging.debug(
" Target %s: Using basic auth with user %s",
self.target,
self._username
)
else:
logging.debug(" Target %s: Using auth token", self.target)
self._session.auth = None
self._session.headers.update({'X-Auth-Token': self._auth_token})
logging.debug(" Target %s: Using URL %s", self.target, url)
try:
server_req = self._session.get(url, timeout = self.timeout)
server_req.raise_for_status()
except requests.exceptions.ConnectionError:
logging.warning(
" Target %s: Connection Error with IP %s. Retrying ...",
self.target,
self.ip_address
)
try:
server_req = self._session.get(url, timeout = self.timeout)
server_req.raise_for_status()
except requests.exceptions.ConnectionError as e:
logging.error(
" Target %s: Unable to connect to IP %s: %s",
self.target,
self.ip_address,
e
)
except requests.exceptions.RequestException as err:
if err.response:
logging.error(
" Target %s: Unable to connect to %s: Status Code %s",
self.target,
err.response.url,
err.response.status_code
)
logging.error(" Target %s: %s", self.target, err.response.text)
else:
logging.error(" Target %s: Unable to connect to URL %s", self.target, url)
if server_req != "":
self.last_http_code = server_req.status_code
server_response = self._check_req_text(server_req)
request_duration = round(time.time() - request_start,2)
logging.debug(
" Target %s: Request duration: %s",
self.target,
request_duration
)
return server_response
def _check_req_text(self, req):
"""extract the text from the request"""
req_text = ""
try:
req_text = req.json()
except ValueError:
logging.debug(" Target %s: No json data received.", self.target)
# req evaluates to True if the status code was between 200 and 400, False otherwise.
# workaround for Cisco UCSC-C480-M5 returning 503 but still delivering the data
if req or req.status_code == 503:
return req_text
# if the request fails the server might give a hint in the ExtendedInfo field
if req_text:
if "error" in req_text and '@Message.ExtendedInfo' in req_text['error']:
logging.debug(
" Target %s: %s: %s",
self.target, req_text['error']['code'],
req_text['error']['message']
)
if isinstance(req_text['error']['@Message.ExtendedInfo'], list):
if 'Message' in req_text['error']['@Message.ExtendedInfo'][0]:
logging.debug(
" Target %s: %s",
self.target,
req_text['error']['@Message.ExtendedInfo'][0]['Message']
)
elif isinstance(req_text['error']['@Message.ExtendedInfo'], dict):
if 'Message' in req_text['error']['@Message.ExtendedInfo']:
logging.debug(
" Target %s: %s",
self.target,
req_text['error']['@Message.ExtendedInfo']['Message']
)
else:
pass
return req_text
def _get_system_urls(self):
systems = self.connect_server(self._urls['Systems_Root']+'?$expand=*')
if not systems:
logging.error(
" Target %s: No Systems Info could be retrieved!",
self.target
)
return
if not systems.get('Members'):
logging.error(
" Target %s: No Systems Members found!",
self.target
)
return
if len(systems['Members']) > 1:
logging.warning(
" Target %s: More than one System found!",
self.target
)
server_info = systems['Members'][0]
if not server_info:
logging.warning(" Target %s: No Server Info could be retrieved!", self.target)
return
# Get the server info for the labels
self._urls.update({'Systems': server_info['@odata.id']})
fields = (
'SKU',
'SerialNumber',
'Manufacturer',
'Model',
'PowerState',
'MemorySummary',
'ProcessorSummary'
)
for field in fields:
self._inventory.update({field: server_info.get(field)})
logging.info(" Target %s: Server powerstate: %s",
self.target,
self._inventory['PowerState']
)
# get the links of the parts for later
urls = (
'Memory',
'EthernetInterfaces',
'NetworkInterfaces',
'Processors',
'Storage',
'SimpleStorage',
'BaseNetworkAdapters'
)
for url in urls:
if url in server_info:
self._urls.update({url: server_info[url]['@odata.id']})
def _get_chassis_urls(self):
logging.debug(" Target %s: Get the Chassis URLs.", self.target)
chassis = self.connect_server(self._urls['Chassis_Root'])
if not chassis:
logging.warning(" Target %s: No Chassis could be retrieved!", self.target)
return
for chassi in chassis['Members']:
chassi_info = self.connect_server(chassi['@odata.id'])
if not chassi_info:
logging.warning(" Target %s: Chassis has no info! %s",
self.target,
chassi['@odata.id']
)
continue
urls = ('Power', 'PCIeDevices', 'NetworkAdapters')
for url in urls:
if not self._urls.get(url):
self._urls.update({url: []})
if url in chassi_info:
self._urls[url].append(chassi_info[url]['@odata.id'])
# Dell iDRAC has some of the URLs in the links section, e.g. PCIeDevices
elif (
'Links' in chassi_info and
url in chassi_info['Links'] and
chassi_info['Links'][url] != []
):
for entry in chassi_info['Links'][url]:
if isinstance(entry, str):
link = entry
else:
link = entry['@odata.id']
self._urls[url].append(link)
else:
logging.warning(
" Target %s: No %s URL found in Chassi info for Chassis %s!",
self.target,
url,
chassi_info['Name']
)
def _get_urls(self, url):
urls= []
logging.debug(" Target %s: Get the %s URLs.", self.target, url)
if isinstance(self._urls[url], list):
device_urls = self._urls[url]
else:
device_urls = [self._urls[url]]
for device_url in device_urls:
collection = self.connect_server(device_url)
if collection and collection.get('Members'):
for member in collection['Members']:
urls.append(member['@odata.id'])
return urls
def _get_storage_info(self, fields):
logging.info(" Target %s: Get the storage data.", self.target)
self._inventory.update({'Controllers': []})
self._urls.update({'Drives': []})
storage_collection = self.connect_server(self._urls['Storage'])
if storage_collection:
self._get_storage_details(storage_collection, fields)
if self._urls.get('SimpleStorage') and not self._inventory['Controllers']:
# Some Cisco and SuperMicro servers have the storage info in SimpleStorage
simple_storage = self.connect_server(self._urls['SimpleStorage'])
if simple_storage:
pass
# Get the storage details in case the Storage url has no proper infos (Supermicro)
# Need to write a function similar to _get_storage_details for this case.
def _get_storage_details(self, storage_collection, fields):
# From the storage collection we get the URL for every single storage controller
for controller in storage_collection['Members']:
# get the actual controller data
controller_data = self.connect_server(controller['@odata.id'])
if not controller_data:
continue
if controller_data.get('StorageControllers'):
# Cisco sometimes uses a list instead of a dict
if isinstance(controller_data['StorageControllers'], list):
controller_details = controller_data['StorageControllers'][0]
else:
controller_details = controller_data['StorageControllers']
else:
controller_details = controller_data
controller_info = self._get_device_info(controller_details, fields, value_check=False)
if controller_info:
# HPE ILO5 is missing the Name in the details of the controllers
controller_name = controller_details.get('Name', controller_data.get('Name'))
if controller_name:
controller_info.update({'Name': controller_name.rstrip()})
# Get the amount of drives attached to the controller
if controller_data.get('[email protected]'):
controller_info.update(
{'DrivesAttached': controller_data['[email protected]']}
)
controller_info['NetboxName'] = "RAID"
self._inventory['Controllers'].append(controller_info)
# Get the drive URLs for later gathering the info
for drive in controller_data['Drives']:
self._urls['Drives'].append(drive['@odata.id'])
def _get_power_info(self, fields):
logging.info(" Target %s: Get the PSU data.", self.target)
power_data = self.connect_server(self._urls['Power'])
if power_data:
self._inventory.update({'PSU': []})
for psu in power_data['PowerSupplies']:
psu_info = self._get_device_info(psu, fields)
self._inventory['PSU'].append(psu_info)
def _get_memory_info(self, fields):
logging.info(" Target %s: Get the Memory data.", self.target)
dimm_urls = self._get_urls('Memory')
if not dimm_urls:
logging.warning(" Target %s: No DIMM URLs found!", self.target)
return
self._inventory.update({'Memory': []})
for dimm_url in dimm_urls:
dimm = self.connect_server(dimm_url)
if not dimm:
continue
dimm_info = self._get_device_info(dimm, fields)
if not dimm_info:
continue
ram_rounded = round(self._inventory['MemorySummary']['TotalSystemMemoryGiB'])
dimm_info['NetboxName'] = f"RAM {ram_rounded}GB"
# HPE has the DIMM Manufacturer in the OEM data
oem_data = dimm.get('Oem', {}).get('Hpe', {})
if oem_data.get('DIMMStatus') != 'NotPresent':
dimm_info['Manufacturer'] = oem_data.get('VendorName')
self._inventory['Memory'].append(dimm_info)
def _get_info_from_urls(self, urls, fields=None):
devices = []
for url in urls:
device_info = self.connect_server(url)
if not device_info:
continue
device = self._get_device_info(device_info, fields)
if not device:
continue
devices.append(device)
return devices
def _get_device_info(self, device_info, fields, value_check=True):
'''extract certain fields from the data'''
current_device = {}
if fields:
for field in fields:
field_value = device_info.get(field)
# HPE and Cisco: some field values filled with unnecessary spaces in the end
if isinstance(field_value, str):
field_value = field_value.rstrip()
current_device.update({field: field_value})
else:
current_device = device_info
if value_check:
has_values = [
v for k, v in current_device.items()
if v != "" and v is not None and k != "Name"
]
if not has_values:
return None
return current_device
def _get_port_info(self, port_info):
'''get the port speed of the NIC'''
port = (
{
'PortSpeed': 0,
'MAC': None
}
)
# Get the MAC address
# HPE Gen11, Lenovo v3
if 'Ethernet' in port_info:
macs = port_info['Ethernet']['AssociatedMACAddresses']
if isinstance(macs, list):
port['MAC'] = macs[0]
else:
port['MAC'] = macs
# Dell R750xd, XE9680
else:
port['MAC'] = port_info.get('AssociatedNetworkAddresses')
if 'SupportedLinkCapabilities' not in port_info and 'CurrentSpeedGbps' not in port_info:
logging.warning(
" Target %s: No CurrentSpeedGbps and SupportedLinkCapabilities found for Port %s!",
self.target,
port_info['@odata.id']
)
return port
current_port_speed_gbps = 0
if 'CurrentSpeedGbps' in port_info:
current_port_speed_gbps = (
port_info.get('CurrentSpeedGbps') or
port_info.get('MaxSpeedGbps') or
0
)
else:
speed = 0
capabilities = port_info.get('SupportedLinkCapabilities', [])
if isinstance(capabilities, list) and capabilities:
speed = (
capabilities[0].get('CapableLinkSpeedMbps') or
capabilities[0].get('LinkSpeedMbps') or
0
)
elif isinstance(capabilities, dict):
speed = (
capabilities.get('CapableLinkSpeedMbps') or
capabilities.get('LinkSpeedMbps') or
0
)
if isinstance(speed, list):
speed = speed[-1]
speed = int(speed)
# Even though the name is CapableLinkSpeedMbps,
# the speed is sometimes in bits/second (seen on HPE Gen11)
if speed > 1048576:
current_port_speed_gbps = round(speed / 1024 / 1024 / 1024)
else:
current_port_speed_gbps = round(speed / 1000)
port['PortSpeed'] = current_port_speed_gbps
return port
def _get_drive_info(self, fields):
"""
get the drive info
"""
logging.info(" Target %s: Get the drive data.", self.target)
drives = self._get_info_from_urls(self._urls['Drives'], fields)
drives_updated = []
for drive in drives:
if not drive['PartNumber']:
drive['PartNumber'] = drive['Model']
if drive['CapacityBytes'] > 0:
if (drive['Protocol'] in ["PCIe", "NVMe"] and
drive['MediaType'] == "SSD") or "NVMe" in drive['Name']:
drive['NetboxName'] = (
f"NVMe {round(drive['CapacityBytes']/1024/1024/1024)}GB"
)
elif (drive['Protocol'] in ["SATA", "SAS", None] and
drive['MediaType'] in ["SSD", "HDD"]):
drive['NetboxName'] = (
f"{drive['MediaType']} {round(drive['CapacityBytes']/1024/1024/1024)}GB"
)
# Supermicro is missing the Protocol for SSDs
drive['Protocol'] = getattr(drive, 'Protocol', 'SATA')
else:
logging.warning(
" Target %s: Unknown Drive Type! Protocol = %s, MediaType = %s",
self.target, drive['Protocol'],
drive['MediaType']
)
drives_updated.append(drive)
self._inventory.update({'Drives': drives_updated})
def _get_processor_info(self, fields):
logging.info(" Target %s: Get the CPU data.", self.target)
proc_urls = self._get_urls('Processors')
if not proc_urls:
logging.warning(" Target %s: No Processors found!", self.target)
return
processors = self._get_info_from_urls(proc_urls, fields)
processors_updated = []
for processor in processors:
if processor['ProcessorType'] == 'CPU':
processor['NetboxName'] = f"CPU {processor['TotalCores']}C"
elif processor['ProcessorType'] == 'GPU':
# The NVIDIA GPUs might appear as well here as
# CPUs with ProcessorType == 'GPU'.
# We need to filter them out to avoid duplicate entries.
continue
else:
logging.warning(
" Target %s: Unknown Processor Type for %s: %s.",
self.target,
processor['Name'],
processor['ProcessorType']
)
if processor['Model']:
processor['Description'] = processor['Model']
processors_updated.append(processor)
self._inventory.update({'Processors': processors_updated})
def _get_pci_devices_info(self, fields):
"""
get the PCIe devices info
"""
logging.info(" Target %s: Get the PCIeDevices data.", self.target)
if isinstance(self._urls['PCIeDevices'], list):
pcie_urls = self._urls['PCIeDevices']
else:
pcie_urls = self._get_urls('PCIeDevices')
if not pcie_urls:
logging.warning(" Target %s: No PCIe URLs found!", self.target)
return
pcie_device_urls = []
for pcie_url in pcie_urls:
collection = self.connect_server(pcie_url)
if collection and collection.get('Members'):
for member in collection['Members']:
pcie_device_urls.append(member['@odata.id'])
if not pcie_device_urls:
logging.warning(" Target %s: No PCIe Device URLs found!", self.target)
return
pcie_devices = self._get_info_from_urls(pcie_device_urls, fields=fields)
pcie_devices_updated = []
# Get the DeviceClass
for pcie_device in pcie_devices:
pcie_device_functions_urls = []
pcie_functions = ""
# Lenovo and Dell
if pcie_device.get('Links'):
pcie_functions = pcie_device['Links'].get('PCIeFunctions')
pcie_device.pop("Links")
# HPE
if pcie_device.get('PCIeFunctions'):
# Did we get the pcie_functions already above?
# Lenovo has both entries (Links and PCIeFunctions)
if not pcie_functions:
pcie_functions_link = [pcie_device['PCIeFunctions']['@odata.id']]
pcie_function_result = self._get_info_from_urls(
pcie_functions_link, fields=None)
if pcie_function_result:
pcie_functions = pcie_function_result[0]['Members']
pcie_device.pop("PCIeFunctions")
if pcie_functions:
for member in pcie_functions:
pcie_device_functions_urls.append(member['@odata.id'])
pcie_device_functions = self._get_info_from_urls(
pcie_device_functions_urls, fields=None)
if pcie_device_functions:
try:
pcie_device['DeviceClass'] = pcie_device_functions[0]['DeviceClass']
except (KeyError, AttributeError):
pcie_device['DeviceClass'] = ""
device_class_to_name = {
"NetworkController": "NIC",
"DisplayController": "GPU",
"MassStorageController": "RAID"
}
pcie_device['NetboxName'] = device_class_to_name.get(pcie_device['DeviceClass'], "")
pcie_devices_updated.append(pcie_device)
self._inventory.update({'PCIeDevices': pcie_devices_updated})
def _get_network_info(self, fields):
logging.info(" Target %s: Get the NetworkAdapters data.", self.target)
nic_urls = self._get_urls('NetworkAdapters')
if not nic_urls:
logging.warning(" Target %s: No NIC URLs found!", self.target)
return
network_cards = self._get_info_from_urls(nic_urls, fields)
network_cards_updated = []
for nic in network_cards:
port_speed_gbps = 0
# HPE Gen11 Calls it 'Ports'
nic_ports_url = nic['NetworkPorts'] if nic['NetworkPorts'] else nic.get('Ports')
if nic_ports_url:
self._urls['NetworkPorts'] = nic_ports_url['@odata.id']
ports = self._get_urls('NetworkPorts')
ports_info = self._get_info_from_urls(ports)
nic['Ports'] = []
for port_info in ports_info:
nic['Ports'].append(self._get_port_info(port_info))
port_speed_gbps = max((port.get('PortSpeed') or 0) for port in nic['Ports'])
nic['NetboxName'] = f"NIC {port_speed_gbps}Gb" if port_speed_gbps else "NIC"
nic.pop('NetworkPorts')
network_cards_updated.append(nic)
self._inventory.update({'NetworkAdapters': network_cards_updated})
def _get_tpm_info(self):
logging.info(" Target %s: Get the TPM data.", self.target)
systeminfo = self.connect_server(self._urls['Systems'], fields=['TrustedModules'])
tpm_modules = []
if systeminfo and systeminfo.get('TrustedModules'):
for tpm in systeminfo['TrustedModules']:
module_type = tpm.get('InterfaceType')
module_state = tpm.get('Status', {}).get('State', 'Unknown')
tpm_info = {
'Description': f'Interface Type: {module_type} - State: {module_state}',
'Manufacturer': self._inventory['Manufacturer'],
'NetboxName': 'TPM'
}
tpm_modules.append(tpm_info)
self._inventory.update({'TrustedModules': tpm_modules})
def _collect_component_data(self, component, method, component_name, fields=None):
if component in self._urls and self._urls[component]:
if fields:
method(fields)
else:
method()
else:
logging.warning(
" Target %s: No %s URL provided! Cannot get %s data!",
self.target,
component,
component_name
)
def collect(self):
"""
collect the inventory from the server
"""
logging.info(" Target %s: Collecting data ...", self.target)
# Get the System URLs
self._get_system_urls()
if not self._urls.get('Systems'):
return None
# Collect data for different components
self._collect_component_data('Systems', self._get_tpm_info, "TPM")
self._collect_component_data('Chassis_Root', self._get_chassis_urls, "Chassis")
self._collect_component_data(
'Storage', self._get_storage_info, "Storage",
('Name', 'Model', 'Manufacturer', 'SerialNumber', 'PartNumber', 'SKU')
)
self._collect_component_data(
'Drives', self._get_drive_info, "drive",
(
'Name', 'Model', 'Manufacturer', 'SerialNumber', 'PartNumber', 'SKU',
'MediaType', 'CapacityBytes', 'Protocol'
)
)
self._collect_component_data(
'Power', self._get_power_info, "PSU",
('Name', 'Manufacturer', 'Model', 'SerialNumber', 'PartNumber', 'SKU')
)
self._collect_component_data(
'Memory', self._get_memory_info, "memory",
(
'Name', 'Model', 'Manufacturer', 'SerialNumber', 'PartNumber', 'SKU',
'CapacityMiB', 'OperatingSpeedMhz', 'MemoryDeviceType'
)
)
self._collect_component_data(
'Processors', self._get_processor_info, "Processors",
(
'Name', 'Manufacturer', 'Model', 'SerialNumber', 'PartNumber', 'SKU',
'ProcessorType', 'TotalCores', 'TotalThreads', 'Description'
)
)
self._collect_component_data(
'PCIeDevices', self._get_pci_devices_info, "PCIeDevices",
(
'Name', 'Model', 'Manufacturer', 'SerialNumber', 'PartNumber', 'SKU',
'Links', 'PCIeFunctions', 'Id'
)
)
self._collect_component_data(
'NetworkAdapters', self._get_network_info, "NetworkAdapters",
(
'Name', 'Model', 'Manufacturer', 'SerialNumber', 'PartNumber', 'SKU',
'NetworkPorts', 'Ports'
)
)
duration = round(time.time() - self._start_time, 2)
logging.info(" Target %s: Scrape duration: %s seconds", self.target, duration)
return self._inventory
def close_session(self):
"""
close the session with the server
"""
if self._auth_token:
logging.debug(
" Target %s: Deleting Redfish session with server %s",
self.target,
self.ip_address
)
session_url = f"https://{self.ip_address}{self.session_url}"
headers = {'x-auth-token': self._auth_token}
logging.debug(" Target %s: Using URL %s", self.target, session_url)
try:
response = requests.delete(
session_url,
verify=False,
timeout=self.timeout,
headers=headers
)
except requests.exceptions.ReadTimeout as err:
logging.warning(
" Target %s: Failed to delete session with server %s: %s",
self.target,
self.ip_address,
err
)
else:
response.close()
logging.info(
" Target %s: Redfish Session deleted successfully.",
self.target
)
else:
logging.debug(
" Target %s: No Redfish session existing with server %s",
self.target,
self.ip_address
)
if self._session:
logging.info(" Target %s: Closing requests session.", self.target)
self._session.close()