-
Notifications
You must be signed in to change notification settings - Fork 12
/
racadm.py
executable file
·678 lines (584 loc) · 25.3 KB
/
racadm.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
#!/usr/bin/env python
import requests
import logging
import re
import datetime
import xml.etree.ElementTree as ET
import sys
import time
class RacStatus(object):
'''Rac Status codes'''
RAC_STATUS_SUCCESS = 0x0
RAC_STATUS_FAILED = 0x1
RAC_STATUS_INVALID_PARAMETER = 0x2
RAC_STATUS_BUFFER_TOO_SMALL = 0x3
RAC_STATUS_FW_OPERATION_FAILED = 0x4
RAC_STATUS_NOT_FOUND = 0x5
RAC_STATUS_ALREADY_EXISTS = 0x6
RAC_STATUS_NOT_ALLOWED = 0x7
RAC_STATUS_REQUIRED_PARAMETER_MISSING = 0x8
RAC_STATUS_INPUT_PARAM_TOO_BIG = 0x9
RAC_STATUS_INPUT_BUFFER_TOO_SMALL = 0xA
RAC_STATUS_SYS_OPERATION_FAILED = 0xB
RAC_STATUS_MEM_ALLOC_FAILED = 0xC
RAC_STATUS_TIME_FUNC_FAILED = 0xD
RAC_STATUS_DATA_CONVERSION_FAILED = 0xE
RAC_STATUS_UNSUPPORTED_CFG = 0xF
RAC_STATUS_INVALID_FILE = 0x11
RAC_STATUS_FILE_OPEN_FAILED = 0x12
RAC_STATUS_FILE_READ_FAILED = 0x13
RAC_STATUS_FILE_WRITE_FAILED = 0x14
RAC_STATUS_RAC_NOT_PRESENT = 0x15
RAC_STATUS_RAC_NOT_READY = 0x16
RAC_STATUS_IPMI_NOT_READY = 0x17
RAC_STATUS_LOAD_LIB_FAILED = 0x18
RAC_STATUS_BUSY = 0x19
class RacadmBase(object):
'''Object used to interact with iDRAC servers'''
def __init__(self, hostname='localhost', username='root', password='calvin', port=443,
verify=False, session=requests.Session()):
'''
initialise variables
@hostname = the iDrac hostname to connect to
@username = usernem to use for login
@password = usernem to use for login
@port = server port to connect to
@log_level = looggin level to use as implmented by the loggin module
@verify = whether to verify ssl certificates
'''
self.last_message = None
self.session_id = None
self.login_state = None
self.hostname = hostname
self.username = username
self.password = password
self.port = port
self.verify = verify
self.logger = logging.getLogger('racadm.RacadmBase')
self.session = session
self.cgiuri = 'https://{}:{}/cgi-bin/'.format(self.hostname, self.port)
def _get_response(self, uri, payload):
'''
get a payload from uri
@uri = the uri to talk to
@payload = the payload to send (as a post)
'''
self.logger.debug('>{}'.format(payload))
#response = self.session.post(uri, data=payload)
response = self.session.post(uri, data=payload, verify=self.verify)
self.logger.debug('<{}'.format(response.content))
return response.content
def _search_xml(self, xml_message, element):
'''
search an xml message for a specific element
use the ElementTree libary to get an element matchin .\\${element}
@xml_message = the xml payload to search
@element = the element to find
'''
root_element = ET.fromstring(xml_message)
return root_element.find('.//{}'.format(element)).text
def _parse_login(self, response):
'''
check if we logged in successfuly and set the session cookie if we did
@response = the respons we need to parse to see if we where successful
'''
sid = self._search_xml(response, 'SID')
self.login_state = self._search_xml(response, 'STATENAME')
self.logger.debug('SID:{}, STATE: {}'.format(sid, self.login_state))
if int(sid) == 0:
#This will probably break something
self.logger.warn('got invalid session try again')
time.sleep(1)
return self.login()
if self.login_state == 'OK':
self.logger.debug('Login Successful')
cookie = { 'sid': sid }
self.session.cookies.update(cookie)
return True
self.logger.warn('Login Failed')
return False
def set_session_id(self):
'''
We need to get the session ID which is different to the one we get back in the xml
we dont call this because we cant close it
'''
session_id = None
message = self.basic_command('getssninfo -A -u {}'.format(self.username))
#There is noway to ask for your session so we will assume we are the last racadm session
for line in message.split("\n"):
session = line.split()
if session[1] == '"RACADM"':
session_id = session[0].translate(None,'"')
self.session_id = session_id
def logout(self):
'''
Close the session so we are not stealling spaces
'''
#this dosn't work because 'Closing the executing session is not permitted.'
#return self.basic_command('closessn -i {}'.format(self.session_id))
def login(self):
'''
Login to the drac server and get a cookie. Use details pased during initialisation
'''
if 'sid' in self.session.cookies and self.session.cookies['sid'] > 0:
self.logger.debug('We already have a session')
self.login_state = 'OK'
return True
uri = '{}login'.format(self.cgiuri)
payload = '<?xml version=\'1.0\'?><LOGIN><REQ>'\
'<USERNAME>{}</USERNAME><PASSWORD>{}</PASSWORD>'\
'</REQ></LOGIN>'.format(self.username, self.password)
content = self._get_response(uri, payload)
return self._parse_login(content)
def _parse_command(self, response, command):
'''
check response and decide if we where successfull
@response = response to check
@command = command that was executed, used to add info to log messages
'''
command_status = int(self._search_xml(response, 'CMDRC'),16)
command_output = self._search_xml(response, 'CMDOUTPUT').strip()
self.logger.debug('STATUS: {}, OUTPUT: {}'.format(command_status, command_output))
if command_status == RacStatus.RAC_STATUS_SUCCESS:
self.logger.info('racadm {} Successful'.format(command))
elif command_status == RacStatus.RAC_STATUS_FAILED:
self.logger.warn('racadm {} failed: {}'.format(command, command_output))
elif command_status == RacStatus.RAC_STATUS_INVALID_PARAMETER:
self.logger.warn('racadm {} invalid command: {}'.format(command, command_output))
else:
self.logger.warn('racadm {} failed with status {}: {}'.format(command, command_status,
command_output))
self.last_message = command_output
return command_status, command_output
def _convert_rac_time(self, date_str):
'''
convert string date of the form Thu Feb 6 06:38:14 2014 to a datetime object
@date_str = string of the form "Thu Feb 6 06:38:14 2014"
'''
months = { 'Jan': 1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6,
'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12}
date_tmp = date_str.split()
time_tmp = date_tmp[3].split(':')
return datetime.datetime(int(date_tmp[4]), int(months[date_tmp[1]]), int(date_tmp[2]),
int(time_tmp[0]), int(time_tmp[1]), int(time_tmp[2]))
def _raw_command(self, command):
'''
run a raw racadm command
@command = command to run see the following for details
ftp://ftp.dell.com/Manuals/all-products/esuprt_electronics/esuprt_software/esuprt_remote_ent_sys_mgmt/integrated-dell-remote-access-cntrllr-6-for-monolithic-srvr-v1.7_Reference%20Guide_en-us.pdf
'''
if self.login_state != 'OK':
self.logger.debug('No valid session attempting login')
del(self.session.cookies['sid'])
if not self.login():
self.logger.error('login failed')
return False
uri = '{}exec'.format(self.cgiuri)
payload = '<?xml version=\'1.0\'?><EXEC><REQ>'\
'<CMDINPUT>racadm {}</CMDINPUT><MAXOUTPUTLEN>0x0fff</MAXOUTPUTLEN>'\
'</REQ></EXEC>'.format(command)
content = self._get_response(uri, payload)
return self._parse_command(content, command)
def basic_command(self, command, retry=1):
'''
command wrapper to preform simple checks
@command = command to run
@retry = Number of time to retry the login
'''
tries=0
status, message = self._raw_command(command)
if status == RacStatus.RAC_STATUS_FAILED and tries < retry:
while tries < retry:
self.session.cookies['sid'] = 0
self.login_state = None
status, message = self._raw_command(command)
if status == RacStatus.RAC_STATUS_SUCCESS:
break
tries += 1
if status == RacStatus.RAC_STATUS_SUCCESS:
return message
return False
class Racadm(RacadmBase):
'''base clase for an object'''
def __init__(self, hostname='localhost', username='root', password='calvin', port=443,
verify=False, session=requests.Session()):
'''
initialise variables
@hostname = the iDrac hostname to connect to
@username = usernem t use for login
@password = usernem to use for login
@port = server port to connect to
@log_level = looggin level to use as implmented by the loggin module
@verify = whether to verify ssl certificates
'''
super(Racadm, self).__init__(hostname, username, password, port, verify, session)
self.logger = logging.getLogger('racadm.Racadm')
def set_led(self, state):
'''
set the led to a specific state
@state = the state you want the led to be in
'''
action = { 'off': 0, 'no_blink': 0, 'no_blinking': 0, 'on': 1, 'blink': 1, 'blinking': 1,
}.get(state, state)
if action not in [0, 1]:
self.logger.warn('set_let not support action {}'.format(state))
return False
return self.basic_command('setled -l {}'.format(action))
def server_action(self, action='powerstatus'):
action = {
'status': 'powerstatus',
'cycle': 'powercycle',
'up': 'powerup',
'down': 'powerdown',
'reset': 'hardreset',
}.get(action.lower(), action.lower())
valid_actions = ['powerstatus', 'powercycle', 'powerup', 'powerdown', 'hardreset' ]
if action not in valid_actions:
self.logger.warn('action {} not supported'.format(action))
return False
return self.basic_command('serveraction {}'.format(action))
def ping(self, address, address_family=4):
'''
ping an ip from the drac interface
@address = the address to ping
@address_family = the ip version to use
'''
if int(address_family) not in [4,6]:
self.logger.warn('address_family {} not supported'.format(address_family))
return False
command = 'ping {}'.format(address)
if int(address_family) == 6:
command = 'ping6 {}'.format(address)
return self.basic_command(command)
def traceroute(self, address, address_family=4):
'''
traceroute an ip from the drac interface
@address = the address to ping
@address_family = the ip version to use
'''
#didn't work for me i think it is timeing out
if int(address_family) not in [4,6]:
self.logger.warn('address_family {} not supported'.format(address_family))
return False
command = 'traceroute {}'.format(address)
if int(address_family) == 6:
command = 'traceroute6 {}'.format(address)
return self.basic_command(command)
def vmdisconnect(self):
'''run racadm vmdisconnect'''
status, message = self._raw_command('vmdisconnect')
if status == RacStatus.RAC_STATUS_FAILED or \
(status == RacStatus.RAC_STATUS_FAILED and \
message == 'No Virtual Media devices are currently connected.'):
return True;
else:
return False
def _vflashd(self, action):
raise NotImplementedError('Dont have a system to test this on')
def _vflashd(self, action):
if action not in ['status', 'initialize']:
self.logger.warn('action {} not supported'.format(action))
return False
return self.basic_command('vflashsd {}'.format(action))
def vflash_status(self):
return self._vflashd('status')
def vflash_initialise(self):
return self._vflashd('initialize')
def vflash_initialize(self):
'''american spelling'''
return self._vflashd('initialize')
def get_arp_table(self):
'''
parse the arp table of the drac interface
'''
status, message = self._raw_command('arp')
arp_table = []
if status == RacStatus.RAC_STATUS_SUCCESS:
self.logger.info('reviced arp table:\n{}'.format(message))
arp_raw = message.split('\n')
for arp in arp_raw:
match_group = re.search('\(([0-9a-fA-F\.]+)\)\s+at\s+([0-9a-fA-F\:]+)', arp)
ip = match_group.group(1)
mac = match_group.group(2)
self.logger.debug('{} -> {}'.format(mac, ip))
arp_table.append({ 'ip': ip, 'mac': mac })
return arp_table
def clear_log(self, log_type='rac'):
'''
clear a drac log
@log_type = the log you want to clear. allowed values are rac and sel
'''
if log_type.lower() == 'rac':
command = 'clrraclog'
elif log_type.lower() == 'sel':
command = 'clrsel'
else:
self.logger.warn('log type \'{}\' not supported')
return False
return self.basic_command(command)
def get_asset_tag(self, module='chassis'):
'''
retrive the asset tag of a specific module
@module = the module of the asset tag you want
WARN: untested
'''
#i dont have anything to test this on
self.logger.warn('This method is untested')
return self.basic_command('getassettag -m {}'.format(module))
def get_chassis_name(self):
'''
retrive the chassis name of a specific module
WARN: untested
'''
#i dont have anything to test this on
self.logger.warn('This method is untested')
return self.basic_command('chassisname')
def get_sensor_info(self):
'''
Retrive the snesor details from drac card
'''
#im not parsing this output. its to fucked
return self.basic_command('getsensorinfo')
def get_service_tag(self):
'''get the Dell service tag'''
return self.basic_command('getsvctag')
def get_usc_version(self):
'''get the usc versiob'''
return self.basic_command('getuscversion')
def rac_reset(self):
'''Reset the drac card'''
return self.basic_command('racreset')
def get_software_inventory(self):
'''return a dictionary containing the drac sowftare inventory'''
inventory = []
item = {}
result = self.basic_command('swinventory')
for line in result.split('\n'):
if '--------------------------------------' in line:
inventory.append(item)
item = {}
elif '=' in line:
key, value = line.split('=')
item.update({key.strip().replace(' ', '_') : value.strip()})
return inventory
def get_version(self):
'''
return a dictionary containing the software versions of the various componets
e.g. bios, drac & usc
'''
versions = {}
result = self.basic_command('getversion')
for line in result.split('\n'):
if '=' in line:
version = line.split('=')
version_type = version[0].strip().replace(' ','_').lower()
versions[version_type] = version[1].strip()
return versions
def get_session_info(self):
'''
rturn a dictionary containing information about the active sessions
'''
sessions = []
results = self.basic_command('getssninfo')
if results:
for line in results.split('\n'):
#Currently the title splits to 7 elements so this should work
if len(colums) == 6:
#Not sure what D stands for here but may as well keep it
date_str = '{}{}'.format(colums[4].replace('/',''),colums[5].replace(':',''))
sessions.append({
'd': colums[0],
'type': colums[1],
'user': colums[2],
'ip': colums[3],
'date': datetime.datetime.strptime(date_str,'%m%d%Y%H%M%S' )
})
return sessions
def get_log(self, log_type):
''' get the Dell specified log type'''
self.logger.warn('This method is untested')
#getraclog returns no data
#getsel and gettracelog both responded with bad xml and
#ERROR: Unable to allocate memory for operation.
#It also sets CMDRC to 0x0 which is anoying
return self.basic_command({
'rac' : 'getraclog',
'sel' : 'getsel',
'trace' : 'gettracelog',
'lc' : 'lclog',
}.get(log_type.lower(), 'getraclog'))
def _basic_table_command(self, command):
'''
very generic table parser. tries to parse the output and return more
sendible python object
@command to run
'''
parsed_table ={}
parsed_items = {}
section = None
status, message = self._raw_command(command)
if status == RacStatus.RAC_STATUS_SUCCESS:
for line in message.split('\n'):
if ':' in line and '=' not in line:
#we are in a new section
sections = line.split(':')
#this is a hack for the HW inventory
if sections[0] == '[InstanceID':
section = sections[1].strip().replace(' ','_').replace(']',' ')
else:
section = sections[0].strip().replace(' ','_')
parsed_table[section] = {}
elif '=' in line:
key, value = line.split('=')
parsed_table[section].update({
key.strip().replace(' ','_') :
value.strip()})
self.logger.debug('parsed {}: {}'.format(command, parsed_table))
return parsed_table
def get_system_info(self):
'''return a dictionory of the drac system information'''
return self._basic_table_command('getsysinfo')
def get_network_config(self):
'''return a dictionory of the drac network config'''
return self._basic_table_command('getniccfg')
def get_hardware_inventory(self):
'''return a dictionory of the drac hardware information'''
return self._basic_table_command('hwinventory')
def get_led_state(self):
'''return the state of the server status led'''
#i dont have anything to test this on
status, message = self._raw_command('getled')
led_state = False
if status == RacStatus.RAC_STATUS_SUCCESS:
led_state = message.split(':')[1].strip()
self.logger.debug('led state: {}'.format(led_state))
return led_state
def get_rac_time(self):
'''
return a datetime object representing the rac system time
'''
#i dont have anything to test this on
status, message = self._raw_command('getractime')
rac_datetime = False
if status == RacStatus.RAC_STATUS_SUCCESS:
rac_datetime = self._convert_rac_time(message)
self.logger.info('rac date: {}'.format(rac_datetime.ctime()))
return rac_datetime
class RacadmConfig(RacadmBase):
'''base clase for an object'''
def __init__(self, group, hostname='localhost', username='root', password='calvin', port=443,
verify=False, has_index=False, session=requests.Session()):
'''
initialise variables
@hostname = the iDrac hostname to connect to
@username = usernem to use for login
@password = usernem to use for login
@port = server port to connect to
@log_level = looggin level to use as implmented by the loggin module
@verify = whether to verify ssl certificates
'''
super(RacadmConfig, self).__init__(hostname, username, password, port, verify, session)
self.logger = logging.getLogger('racadm.RacadmConfig')
self.group = group
self.has_index = has_index
#these systems need an index
if self.group in ['cfgUserAdmin', 'cfgEmailAlert', 'cfgLdapRoleGroup', 'cfgStandardSchema',
'cfgIpmiPef', 'cfgServerPowerSupply', 'cfgVFlashPartition', 'cfgUserDomain', 'cfgSensorRedundancy']:
self.has_index = True
def _parse_config_group(self, message):
'''
used to parse the date from _get_config. which is mainly of the form
key=value
however readonly valuse are prefixed with '#' so we chop that as well
'''
config = {}
for line in message.split('\n'):
if '=' in line:
key, value = line.lstrip('#').split('=')
config[key.strip()] = value.strip()
return config
def _get_config(self, conf_group, conf_object=None, conf_index=None):
'''
generic function to fetch config of the form
racadm getconfig -g ${conf_group} -o ${conf_object} -i ${conf_index}
@conf_group = the config group to query
@conf_object = the config object to query
@conf_index = the config index to query
'''
command = 'getconfig -g {}'.format(conf_group)
if conf_object:
command += ' -o {}'.format(conf_object)
if conf_index:
command += ' -i {}'.format(conf_index)
return self.basic_command(command)
def get_index(self, conf_index, conf_object=None):
'''
genreic function to get a config item that requiers an index
@conf_group = group to query
@conf_index = index to query
@conf_object = object to query
'''
if not self.has_index:
raise TypeError('{} does not support Indexes use get() instead'.format(self.group))
if conf_object:
return self._get_config(self.group, conf_object, conf_index)
return self._parse_config_group(self._get_config(self.group, conf_object, conf_index))
def get(self, conf_object=None):
'''
genreic function to get a config item that requiers no index
@conf_object = object to query
'''
if self.has_index:
raise TypeError('{} must specifiy index use get_index() instead'.format(self.group))
if conf_object:
return self._get_config(self.group, conf_object)
return self._parse_config_group(self._get_config(self.group))
def _set_config(self, conf_value, conf_object=None, conf_index=None):
'''
generic function to fetch config of the form
racadm setconfig -g ${conf_group} -o ${conf_object} -i ${conf_index}
@conf_group = the config group to query
@conf_object = the config object to query
@conf_index = the config index to query
'''
command = 'config -g {} -o {}'.format(self.group, conf_object)
if conf_index:
command += ' -i {}'.format(conf_index)
return self.basic_command('{} {}'.format(command, conf_value))
def set_index(self, conf_object, conf_index, conf_value):
'''
genreic function to set a config item that requiers an index
@conf_group = group to query
@conf_index = index to query
@conf_object = object to query
'''
if not self.has_index:
raise TypeError('{} does not support Indexes use set() instead'.format(self.group))
return self._set_config(conf_value, conf_object, conf_index)
def set(self, conf_object, conf_value):
'''
genreic function to set a config item that requiers no index
@conf_object = object to query
'''
if self.has_index:
raise TypeError('{} must specifiy index use set_index() instead'.format(self.group))
return self._set_config(conf_value, conf_object)
def arg_parse():
'''argument parsing function'''
import argparse #we only need this if we are running in cli mode
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-u', '--username', default='root' )
parser.add_argument('-p', '--password', default='calvin' )
parser.add_argument('-H', '--hostname', default='localhost' )
parser.add_argument('-P', '--port', default=443 )
parser.add_argument('-v', '--verbose', action='count' )
return parser.parse_args()
def main():
'''cli component of racadm'''
args = arg_parse()
logging.basicConfig(level=logging.DEBUG)
racadm = Racadm(args.hostname, args.username, args.password,
args.port )
print racadm.get_session_info()
if __name__ == '__main__':
main()