-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdell_hw_health.py
executable file
·539 lines (493 loc) · 20.5 KB
/
dell_hw_health.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
#! /usr/bin/env python
#
# dell_hw_health.py, python script using Redfish API to get system hardware
# health based on GetSystemHWInventoryREDFISH.py by Texas Roemer
# <Texas_Roemer@Dell.com>
#
# _author_ = Lorenzo Gaggini <lorenzo.gaggini@dada.eu>,
# Texas Roemer <Texas_Roemer@Dell.com>
# _version_ = 1.0
#
# Copyright (c) 2018, Dell, Inc., 2019, Lorenzo Gaggini
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
import requests
import sys
import re
import argparse
from datetime import datetime
import logging
requests.packages.urllib3.disable_warnings()
logger = logging.getLogger('dell_hw_health')
logging.basicConfig()
ENDPOINT = '/redfish/v1/Systems/System.Embedded.1'
def check_supported_idrac_version():
response = requests.get('https://%s%s' % (idrac_ip, ENDPOINT),
verify=False,
auth=(idrac_username, idrac_password))
if response.status_code != 200:
msg = 'WARNING, iDRAC version installed does not support ' +\
'this feature using Redfish API'
logger.warning(msg)
sys.exit(3)
else:
pass
def get_status(data):
status = data[u'Status']['Health']
if status is None:
return 'Unknown'
else:
return status
def is_healthy(status):
if status == 'OK' or status is None:
return True
else:
return False
def is_warning(status):
if status == 'Warning':
return True
else:
return False
def is_unknown(status):
if status == 'Unknown':
return True
else:
return False
def get_nagios_output(status, msg):
print('%s;%s;%s - %s' % (status, HostName, datetime.now(), msg))
sys.exit(status)
def get_report_output(msg):
print(msg)
f = open('hw_inventory.txt', 'a')
f.writelines(msg)
f.writelines('\n')
f.close()
def get_system_information():
global serverSN
global HostName
response = requests.get('https://%s%s' % (idrac_ip, ENDPOINT),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
serverSN = data[u'SerialNumber']
HostName = data[u'HostName']
def get_memory_information():
nagios_status = 0
nagios_msg = ''
response = requests.get('https://%s%s/Memory' % (idrac_ip, ENDPOINT),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
for i in data[u'Members']:
dimm = i[u'@odata.id'].split('/')[-1]
try:
dimm_slot = re.search('DIMM.+', dimm).group()
except:
logger.error('FAIL, unable to get dimm slot info')
sys.exit(2)
response = requests.get('https://%s%s' % (idrac_ip, i[u'@odata.id']),
verify=False,
auth=(idrac_username, idrac_password))
sub_data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % sub_data)
sys.exit(2)
else:
status = get_status(sub_data)
if (is_healthy(status) and args['critical']):
continue
message = 'Server %s %s %s %s PN %s: %s ' % (serverSN, dimm_slot,
sub_data[u'Manufacturer'],
sub_data[u'CapacityMiB'],
sub_data[u'PartNumber'],
status)
if args['nagios'] and not is_healthy(status):
if status == 'Unknown' and nagios_status != 2:
nagios_status = 3
else:
nagios_status = 2
nagios_msg += message
elif not args['nagios']:
get_report_output(message)
if args['nagios']:
if nagios_status == 0:
nagios_msg = 'Memory is OK'
get_nagios_output(nagios_status, nagios_msg)
def get_cpu_information():
nagios_status = 0
nagios_msg = ''
response = requests.get('https://%s%s/Processors' % (idrac_ip, ENDPOINT),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
for i in data[u'Members']:
cpu = i[u'@odata.id'].split('/')[-1]
response = requests.get('https://%s%s' % (idrac_ip, i[u'@odata.id']),
verify=False,
auth=(idrac_username, idrac_password))
sub_data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % sub_data)
sys.exit(2)
else:
status = get_status(sub_data)
if (is_healthy(status) and args['critical']):
continue
message = 'Server %s %s %s: %s ' % (serverSN, cpu,
sub_data['Model'],
status)
if args['nagios'] and not is_healthy(status):
if status == 'Unknown' and nagios_status != 2:
nagios_status = 3
else:
nagios_status = 2
nagios_msg += message
elif not args['nagios']:
get_report_output(message)
if args['nagios']:
if nagios_status == 0:
nagios_msg = 'CPU is OK'
get_nagios_output(nagios_status, nagios_msg)
def get_fan_information():
nagios_status = 0
nagios_msg = ''
response = requests.get('https://%s%s' % (idrac_ip, ENDPOINT),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
if data[u'Links'][u'CooledBy'] == []:
logger.warning('WARNING, no fans detected for system')
else:
for i in data[u'Links'][u'CooledBy']:
response = requests.get('https://%s%s' % (idrac_ip,
i[u'@odata.id']),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
else:
fan = i[u'@odata.id'].split('/')[-1]
try:
fan_slot = re.search('\|\|.+', fan).group().strip('|')
except:
pass
try:
fan_slot = re.search('7CF.+', fan).group().strip('7C')
except:
pass
status = get_status(data)
if (is_healthy(status) and args['critical']):
continue
message = 'Server %s %s %s: %s ' % (serverSN, fan_slot,
data[u'FanName'],
status)
if args['nagios'] and not is_healthy(status):
if status == 'Unknown' and nagios_status != 2:
nagios_status = 3
else:
nagios_status = 2
nagios_msg += message
elif not args['nagios']:
get_report_output(message)
if args['nagios']:
if nagios_status == 0:
nagios_msg = 'FANS are OK'
get_nagios_output(nagios_status, nagios_msg)
def get_ps_information():
nagios_status = 0
nagios_msg = ''
response = requests.get('https://%s%s' % (idrac_ip, ENDPOINT),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
if data[u'Links'][u'PoweredBy'] == []:
logger.warning('WARNING, no power supplies detected for system')
else:
for i in data[u'Links'][u'PoweredBy']:
response = requests.get('https://%s%s' % (idrac_ip,
i[u'@odata.id']),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
else:
ps = i[u'@odata.id'].split('/')[-1]
status = get_status(data)
if (is_healthy(status) and args['critical']):
continue
message = 'Server %s %s %s %s PN %s: %s ' % (serverSN, ps,
data[u'Manufacturer'],
data[u'Model'],
data[u'PartNumber'],
status)
if args['nagios'] and not is_healthy(status):
if status == 'Unknown' and nagios_status != 2:
nagios_status = 3
else:
nagios_status = 2
nagios_msg += message
elif not args['nagios']:
get_report_output(message)
if args['nagios']:
if nagios_status == 0:
nagios_msg = 'PSU are OK'
get_nagios_output(nagios_status, nagios_msg)
def get_storage_controller_information(quiet=False):
nagios_status = 0
nagios_msg = ''
global controller_list
response = requests.get('https://%s%s/Storage' % (idrac_ip, ENDPOINT),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
controller_list = []
for i in data[u'Members']:
controller_list.append(i[u'@odata.id'][46:])
for i in controller_list:
response = requests.get('https://%s%s/Storage/%s' % (idrac_ip,
ENDPOINT, i),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
if u'StorageControllers' not in data:
continue
status = get_status(data[u'StorageControllers'][0])
if (is_healthy(status) and args['critical']):
continue
message = 'Server %s %s: %s ' % (serverSN, i, status)
if args['nagios'] and not is_healthy(status):
if status == 'Unknown' and nagios_status != 2:
nagios_status = 3
else:
nagios_status = 2
nagios_msg += message
elif not args['nagios'] and not quiet:
get_report_output(message)
if args['nagios'] and not quiet:
if nagios_status == 0:
nagios_msg = 'CONTROLLERS are OK'
get_nagios_output(nagios_status, nagios_msg)
def get_storage_disks_information():
nagios_status = 0
nagios_msg = ''
nagios_warning = 0
for i in controller_list:
response = requests.get('https://%s%s/Storage/%s' % (idrac_ip,
ENDPOINT, i),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
drive_list = []
if data[u'Drives'] == []:
message = 'WARNING, no drives detected for %s' % i
else:
pass
for ii in data[u'Drives']:
drive_list.append(ii[u'@odata.id'][53:])
for iii in drive_list:
response = requests.get('https://%s%s/Storage/Drives/%s' % (idrac_ip,
ENDPOINT,
iii),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('\n- FAIL, get command failed, error is: %s' %
data)
sys.exit(2)
status = get_status(data)
if (is_healthy(status) and args['critical']):
continue
message = 'Server %s %s %s %s PN %s: %s ' % (serverSN, iii,
data[u'Manufacturer'],
data[u'Description'],
data[u'PartNumber'],
status)
if args['nagios'] and not is_healthy(status):
if is_warning(status):
nagios_warning += 1
nagios_status = 1
elif is_unknown(status) and nagios_status == 0:
nagios_status = 3
else:
nagios_status = 2
nagios_msg += message
elif not args['nagios']:
get_report_output(message)
if nagios_warning > 1:
nagios_status = 2
if args['nagios']:
if nagios_status == 0:
nagios_msg = 'DISKS are OK'
get_nagios_output(nagios_status, nagios_msg)
def get_backplane_information():
nagios_status = 0
nagios_msg = ''
response = requests.get('https://%s/redfish/v1/Chassis' % (idrac_ip),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
backplane_URI_list = []
for i in data[u'Members']:
backplane = i[u'@odata.id']
if 'Enclosure' in backplane:
backplane_URI_list.append(backplane)
if backplane_URI_list == []:
message = '- WARNING, no backplane information detected for system\n'
print(message)
sys.exit(2)
for i in backplane_URI_list:
response = requests.get('https://%s%s' % (idrac_ip, i), verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
status = get_status(data)
if (is_healthy(status) and args['critical']):
continue
message = '%s %s %s: %s ' % (serverSN, data[u'Id'], data[u'Name'],
status)
if args['nagios'] and not is_healthy(status):
if status == 'Unknown' and nagios_status != 2:
nagios_status = 3
else:
nagios_status = 2
nagios_msg += message
elif not args['nagios']:
get_report_output(message)
if args['nagios']:
if nagios_status == 0:
nagios_msg = 'BACKPLANE is OK'
get_nagios_output(nagios_status, nagios_msg)
def get_temperature_information():
nagios_status = 0
nagios_msg = ''
response = requests.get('https://%s/redfish/v1/Chassis/System.Embedded.1/Thermal' % (idrac_ip),
verify=False,
auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logger.error('FAIL, get command failed, error is: %s' % data)
sys.exit(2)
for i in data[u'Temperatures']:
status = get_status(i)
if (is_healthy(status) and args['critical']):
continue
message = '%s %s %s: %s' % (i[u'PhysicalContext'], i[u'MemberId'], i[u'Name'],
status)
if args['nagios'] and not is_healthy(status):
nagios_status = 2
nagios_msg += message
elif not args['nagios']:
get_report_output(message)
if args['nagios']:
if nagios_status == 0:
nagios_msg = 'TEMPERATURE is OK'
get_nagios_output(nagios_status, nagios_msg)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Python script using ' +
'Redfish API to get system hardware' +
'Health')
parser.add_argument('-ip', help='iDRAC IP address', required=True)
parser.add_argument('-u', help='iDRAC username', required=True)
parser.add_argument('-p', help='iDRAC password', required=True)
parser.add_argument('-m', help='Get memory information', required=False,
action='store_true')
parser.add_argument('-c', help='Get processor information', required=False,
action='store_true')
parser.add_argument('-f', help='Get fan information', required=False,
action='store_true')
parser.add_argument('-ps', help='Get power supply information',
required=False,
action='store_true')
parser.add_argument('-s', help='Get storage controllers information only',
required=False,
action='store_true')
parser.add_argument('-d', help='Get disks information only',
required=False,
action='store_true')
parser.add_argument('-b', help='Get backplane information only',
required=False,
action='store_true')
parser.add_argument('-t', help='Get temperature information only',
required=False,
action='store_true')
parser.add_argument('-a', help='Get all information',
required=False,
action='store_true')
parser.add_argument('-critical', help='Retrieve only failure for report',
required=False,
action='store_true')
parser.add_argument('-nagios', help='Nagios output check mode' +
', only the first option is used',
required=False,
action='store_true')
args = vars(parser.parse_args())
idrac_ip = args['ip']
idrac_username = args['u']
idrac_password = args['p']
check_supported_idrac_version()
get_system_information()
if args['m']:
get_memory_information()
if args['c']:
get_cpu_information()
if args['f']:
get_fan_information()
if args['ps']:
get_ps_information()
if args['s']:
get_storage_controller_information()
if args['d']:
get_storage_controller_information(quiet=True)
get_storage_disks_information()
if args['b']:
get_backplane_information()
if args['t']:
get_temperature_information()
if args['a'] and not args['nagios']:
get_memory_information()
get_cpu_information()
get_fan_information()
get_ps_information()
get_storage_controller_information()
get_storage_disks_information()
get_backplane_information()
get_temperature_information()
else:
logger.warning('-a option is not enabled in nagios mode')