-
Notifications
You must be signed in to change notification settings - Fork 282
/
GetDiskOperationREDFISH.py
162 lines (151 loc) · 9.05 KB
/
GetDiskOperationREDFISH.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
#!/usr/bin/python3
#
# GetDiskOperationREDFISH. Python script using Redfish API DMTF to get check a disk if any operations are in progress.
#
# _author_ = Texas Roemer <Texas_Roemer@Dell.com>
# _version_ = 3.0
#
# Copyright (c) 2019, Dell, Inc.
#
# 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 argparse
import getpass
import json
import logging
import re
import requests
import sys
import time
import warnings
from pprint import pprint
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser(description="Python script using Redfish API DMTF to get check a disk if any operations are in progress")
parser.add_argument('-ip',help='iDRAC IP address', required=False)
parser.add_argument('-u', help='iDRAC username', required=False)
parser.add_argument('-p', help='iDRAC password. If you do not pass in argument -p, script will prompt to enter user password which will not be echoed to the screen.', required=False)
parser.add_argument('-x', help='Pass in X-Auth session token for executing Redfish calls. All Redfish calls will use X-Auth token instead of username/password', required=False)
parser.add_argument('--ssl', help='SSL cert verification for all Redfish calls, pass in value \"true\" or \"false\". By default, this argument is not required and script ignores validating SSL cert for all Redfish calls.', required=False)
parser.add_argument('--script-examples', help='Get executing script examples', action="store_true", dest="script_examples", required=False)
parser.add_argument('--get-controllers', help='Get server storage controller FQDDs', action="store_true", dest="get_controllers", required=False)
parser.add_argument('--get-disks', help='Get server storage controller disk FQDDs and their raid status, pass in storage controller FQDD, Example "\RAID.Integrated.1-1\"', dest="get_disks", required=False)
parser.add_argument('--check-disk', help='Pass in disk FQDD string to check if any operations are running on this disk', required=False)
args = vars(parser.parse_args())
logging.basicConfig(format='%(message)s', stream=sys.stdout, level=logging.INFO)
def script_examples():
print("""\n- GetDiskOperationREDFISH.py -ip 192.168.0.120 -u root -p calvin --get-controllers, this example will return all storage controller FQDDs for this server.
\n- GetDiskOperationREDFISH.py -ip 192.168.0.120 -u root -p calvin --get-disks RAID.Integrated.1-1, this example will return only disks behind this storage controller.
\n- GetDiskOperationREDFISH.py -ip 192.168.0.120 -u root -p calvin --check-disk Disk.Bay.1:Enclosure.Internal.0-0:RAID.Mezzanine.1C-1, this example will return any operation information for this drive.""")
sys.exit(0)
def check_supported_idrac_version():
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage' % idrac_ip, verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code == 401:
logging.warning("\n- WARNING, status code %s returned. Incorrect iDRAC username/password or invalid privilege detected." % response.status_code)
sys.exit(0)
elif response.status_code != 200:
logging.warning("\n- WARNING, iDRAC version installed does not support this feature using Redfish API")
sys.exit(0)
def test_valid_controller_FQDD_string(x):
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/%s' % (idrac_ip, x),verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/%s' % (idrac_ip, x),verify=verify_cert,auth=(idrac_username, idrac_password))
if response.status_code != 200:
logging.error("\n- FAIL, either controller FQDD does not exist or typo in FQDD string name (FQDD controller string value is case sensitive)")
sys.exit(0)
def get_storage_controllers():
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage' % idrac_ip,verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage' % idrac_ip,verify=verify_cert,auth=(idrac_username, idrac_password))
data = response.json()
logging.info("\n- Server controller(s) detected -\n")
controller_list = []
for i in data['Members']:
controller_list.append(i['@odata.id'].split("/")[-1])
print(i['@odata.id'].split("/")[-1])
def get_pdisks():
test_valid_controller_FQDD_string(args["get_disks"])
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/%s' % (idrac_ip, args["get_disks"]), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/%s' % (idrac_ip, args["get_disks"]), verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logging.error("\n- FAIL, GET command failed, return code %s" % response.status_code)
logging.error("Extended Info Message: {0}".format(response.json()))
sys.exit(0)
drive_list = []
if data['Drives'] == []:
logging.warning("\n- WARNING, no drives detected for %s" % args["get_disks"])
sys.exit(0)
else:
for i in data['Drives']:
drive_list.append(i['@odata.id'].split("/")[-1])
logging.info("\n- Drives detected for controller \"%s\" and RaidStatus\n" % args["get_disks"])
for i in drive_list:
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/Drives/%s' % (idrac_ip, i), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/Drives/%s' % (idrac_ip, i), verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
logging.info(" - Disk: %s, Raidstatus: %s" % (i, data['Oem']['Dell']['DellPhysicalDisk']['RaidStatus']))
def get_disk_operation_info():
logging.info("\n- %s operation information -\n" % args["check_disk"])
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/Drives/%s' % (idrac_ip, args["check_disk"]), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1/Storage/Drives/%s' % (idrac_ip, args["check_disk"]), verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code == 200:
if data['Operations'] == []:
logging.info("- WARNING, no operations running for %s" % args["check_disk"])
sys.exit()
else:
for i in data['Operations']:
for ii in i.items():
print("%s: %s" % (ii[0],ii[1]))
else:
logging.error("- FAIL, GET command failed to get disk operation information, status code %s returned" % response.status_code)
logging.error(data)
sys.exit(0)
if __name__ == "__main__":
if args["script_examples"]:
script_examples()
if args["ip"] and args["ssl"] or args["u"] or args["p"] or args["x"]:
idrac_ip=args["ip"]
idrac_username=args["u"]
if args["p"]:
idrac_password=args["p"]
if not args["p"] and not args["x"] and args["u"]:
idrac_password = getpass.getpass("\n- Argument -p not detected, pass in iDRAC user %s password: " % args["u"])
if args["ssl"]:
if args["ssl"].lower() == "true":
verify_cert = True
elif args["ssl"].lower() == "false":
verify_cert = False
else:
verify_cert = False
else:
verify_cert = False
check_supported_idrac_version()
else:
logging.error("\n- FAIL, invalid argument values or not all required parameters passed in. See help text or argument --script-examples for more details.")
sys.exit(0)
if args["check_disk"]:
get_disk_operation_info()
elif args["get_disks"]:
get_pdisks()
elif args["get_controllers"]:
get_storage_controllers()
else:
logging.error("\n- FAIL, invalid argument values or not all required parameters passed in. See help text or argument --script-examples for more details.")