-
Notifications
You must be signed in to change notification settings - Fork 49
/
get_set_power_manager_setting.py
352 lines (293 loc) · 16 KB
/
get_set_power_manager_setting.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
#
# _author_ = Mahendran P <Mahendran_P@Dell.com>
#
# Copyright (c) 2022 Dell EMC Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
#### Synopsis
Script to get or set Power Manager Settings applied on OpenManage Enterprise
#### Description
This script exercises the Power Manager REST API to get & set Power Manager Settings.
- For authentication X-Auth is used over Basic Authentication
- Note that the credentials entered are not stored to disk.
#### Python Example
python get_set_power_manager_settings.py --ip <xx> --username <username> --password <pwd>
"""
# Import the modules required for this script
import argparse
import json
import sys
from argparse import RawTextHelpFormatter
# Import the modules required for this script
from requests.packages.urllib3.exceptions import InsecureRequestWarning
try:
import urllib3
import requests
from columnar import columnar
except ModuleNotFoundError:
print("This program requires urllib3, requests, and columnar. To install them on most systems run "
"`pip install requests urllib3`")
sys.exit(0)
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# Temperature Display Unit dictionary to display the output for better reading
temp_disp_unit_dictionary = {
1: "Celsius",
2: "Fahrenheit"}
# Power Display Unit dictionary to display the output for better reading
power_disp_unit_dictionary = {
1: "Watt",
2: "BTU/Hr"}
# Metric Interval dictionary to display the output for better reading
metric_interval_dictionary = {
15: "15 minutes",
30: "30 minutes",
60: "60 minutes"}
# Built-in Report Interval dictionary to display the output for better reading
report_interval_dictionary = {
1: "1 Day",
7: "7 Days",
15: "15 Days",
30: "30 Days",
90: "90 Days",
180: "180 Days",
365: "365 Days"}
# Built-in Report Time Granularity dictionary to display the output for better reading
report_granularity_dictionary = {
1: "1 Hour",
2: "1 Day"}
# Top Energy Consumers dictionary to display the output for better reading
top_energy_interval_dictionary = {
4: "1 Day",
5: "1 Week",
6: "2 Weeks",
7: "1 Month",
8: "3 Months",
9: "6 Months",
10: "1 Year"}
# Delete Metric Data dictionary to display the output for better reading
delete_metric_data_dictionary = {
1: "Delete data",
2: "Keep data"}
# Reset WSMAN Power Metric dictionary to display the output for better reading
reset_metric_dictionary = {
1: "Enabled",
2: "Disable"}
# Power Manager Settings dictionary's dictionary to display the output for better reading
settings_dictionary = {
(1 or 1.0): temp_disp_unit_dictionary,
(2 or 2.0): power_disp_unit_dictionary,
(3 or 3.0): metric_interval_dictionary,
(5 or 5.0): report_interval_dictionary,
(6 or 6.0): report_granularity_dictionary,
(7 or 7.0): top_energy_interval_dictionary,
(8 or 8.0): delete_metric_data_dictionary,
(9 or 9.0): reset_metric_dictionary,
(51 or 51.0): "Any number of days between 30 to 365"}
def get_power_manager_settings(ip_address, user_name, password):
""" Authenticate with OpenManage Enterprise, get power manager settings"""
try:
# Defining Session URL & headers
session_url = 'https://%s/api/SessionService/Sessions' % ip_address
headers = {'content-type': 'application/json'}
# Define Payload for posting session API
user_details = {'UserName': user_name,
'Password': password,
'SessionType': 'API'}
# Define OUTPUT header & data format
output_column_headers = ['Setting_ID', 'Name', 'Default_Value', 'Current_Value', 'Setting_Value_Enum']
output_column_data = []
# Defining Power Manager settings URL
settings_url = "https://%s/api/PowerService/Settings" % ip_address
# Create the session with OpenManage Enterprise
session_info = requests.post(session_url, verify=False,
data=json.dumps(user_details),
headers=headers)
# If session doesn't create, message the user with error
if session_info.status_code != 201 & session_info.status_code != 200:
session_json_data = session_info.json()
if 'error' in session_json_data:
error_content = session_json_data['error']
if '@Message.ExtendedInfo' not in error_content:
print("Unable to create a session with %s" % ip_address)
else:
extended_error_content = error_content['@Message.ExtendedInfo']
print(
"Unable to create a session with %s. See below ExtendedInfo for more information" % ip_address)
print(extended_error_content[0]['Message'])
else:
print("Unable to create a session with %s. Please try again later" % ip_address)
return 0
else:
headers['X-Auth-Token'] = session_info.headers['X-Auth-Token']
# Get Power Manager settings API call with OpenManage Enterprise
settings_response = requests.get(settings_url, headers=headers, verify=False)
settings_json_data = settings_response.json()
# If settings API doesn't respond or failed, message the user with error
if settings_response.status_code != 201 & settings_response.status_code != 200:
if 'error' in settings_json_data:
error_content = settings_json_data['error']
if '@Message.ExtendedInfo' not in error_content:
print("Unable to retrieve Power Manager settings from %s" % ip_address)
else:
extended_error_content = error_content['@Message.ExtendedInfo']
print(
"Unable to retrieve Power Manager settings from %s. See below ExtendedInfo for more information" % ip_address)
print(extended_error_content[0]['Message'])
else:
print("Unable to retrieve Power Manager settings from %s" % ip_address)
return 0
else:
settings_content = json.loads(settings_response.content)
if settings_content:
# For every elements in the Settings response, store the details in the table
for settings_elem in settings_content["value"]:
data_dictionary = settings_dictionary[int(settings_elem["Id"])]
settings_data = [settings_elem["Id"], settings_elem["Name"], settings_elem["DefaultValue"],
settings_elem["Value"], data_dictionary]
output_column_data.append(settings_data)
table = columnar(output_column_data, output_column_headers, no_borders=True)
print("\n ==========================================")
print(" Power Manager Settings ")
print(" ==========================================")
print(table)
return 1
else:
print("No Power Manager settings from %s" % ip_address)
return 0
except Exception as error:
print("Unexpected error:", str(error))
return 0
def set_power_manager_settings(ip_address, user_name, password, settings_id, settings_value):
""" Authenticate with OpenManage Enterprise, set power manager settings"""
try:
# Defining Session URL & its headers
session_url = 'https://%s/api/SessionService/Sessions' % ip_address
headers = {'content-type': 'application/json'}
# Define Payload for posting session API
user_details = {'UserName': user_name,
'Password': password,
'SessionType': 'API'}
# Defining Power Manager settings URL
settings_url = "https://%s/api/PowerService/Actions/PowerService.UpdateSettings" % ip_address
# Payload for posting settings API
settings_payload = {"Settings": [{"Id": int(settings_id), "Value": int(settings_value)}]}
# Create the session with OpenManage Enterprise
session_info = requests.post(session_url, verify=False,
data=json.dumps(user_details),
headers=headers)
# If session doesn't create, message the user with error
if session_info.status_code != 201 & session_info.status_code != 200:
session_json_data = session_info.json()
if 'error' in session_json_data:
error_content = session_json_data['error']
if '@Message.ExtendedInfo' not in error_content:
print("Unable to create a session with %s" % ip_address)
else:
extended_error_content = error_content['@Message.ExtendedInfo']
print(
"Unable to create a session with %s. See below ExtendedInfo for more information" % ip_address)
print(extended_error_content[0]['Message'])
else:
print("Unable to create a session with %s. Please try again later" % ip_address)
else:
headers['X-Auth-Token'] = session_info.headers['X-Auth-Token']
# Set Power Manager settings API call with OpenManage Enterprise
settings_response = requests.post(settings_url, data=json.dumps(settings_payload), headers=headers,
verify=False)
settings_json_data = settings_response.json()
# If settings API doesn't respond or failed, message the user with error
if settings_response.status_code != 201 & settings_response.status_code != 200:
if 'error' in settings_json_data:
error_content = settings_json_data['error']
if '@Message.ExtendedInfo' not in error_content:
print("Unable to set Power Manager Setting on %s" % ip_address)
else:
extended_error_content = error_content['@Message.ExtendedInfo']
print(
"Unable to set Power Manager Setting on %s. See below ExtendedInfo for more information" % ip_address)
print(extended_error_content[0]['Message'])
else:
print("Unable to set Power Manager Setting on %s" % ip_address)
else:
print("Successfully applied Power Manger Setting on %s" % ip_address)
except Exception as error:
print("Unexpected error:", str(error))
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)
parser.add_argument("--ip", "-i", required=True, help="OpenManage Enterprise IP <- Mandatory")
parser.add_argument("--username", "-u", required=False,
help="Username for OpenManage Enterprise <- Optional; default = admin", default="admin")
parser.add_argument("--password", "-p", required=True, help="Password for OpenManage Enterprise <- Mandatory")
args = parser.parse_args()
print("WARNING: THIS SCRIPT IS EXPERIMENTAL.")
print("The Power Manager scripts were originally internal Dell scripts we then published externally. If you see "
"this message and are using one of these scripts it would be very helpful if you open an issue on GitHub "
"at https://github.com/dell/OpenManage-Enterprise/issues and tell us you are using the script. We have not "
"dedicated any resources to optimizing them but are happy to do so if we know the community is using them. "
"Likewise if you find a bug in one of these scripts feel free to open an issue and we will investigate.")
return_value = get_power_manager_settings(args.ip, args.username, args.password)
# Only if get_power_manager_settings returns success, the proceed to do set.
if return_value == 1:
get_inputs = input("Do you want to change any Power Manager settings (Y/N) : ")
# Until user says No to change the settings
while get_inputs in ('Y', 'y', ''):
# Get the respective settings enumeration dictionary basis user input i.e. setting ID.
setting_id_input = input("Please enter Setting_ID : ")
# Define the type basis the input given either in int or float. Error out if otherwise & continue
if "." in setting_id_input:
setting_id_input = float(setting_id_input)
elif setting_id_input.isdigit():
setting_id_input = int(setting_id_input)
else:
print(
"\n !!! ERROR :: Wrong Setting's ID Entered !!! \n Please provide proper setting's ID & try again\n")
continue
# if the setting ID provided doesn't exist, then error out & continue
if settings_dictionary.get(setting_id_input) == None:
print(
"\n !!! ERROR :: Wrong Setting's ID Entered !!! \n Please provide proper setting's ID & try again\n")
continue
else:
define_dictionary = settings_dictionary[setting_id_input]
# Display the supported values basis the user setting input for easy user choice of setting the value
print("Supported key values: \n ")
if setting_id_input != 51 or setting_id_input != 51.0:
for key, value in define_dictionary.items():
print(" ", key, ' : ', value)
else:
print(define_dictionary)
# Get the user input for setting value
settings_value_inputs = input("\nPlease enter Setting_Value : ")
# if the setting value provided is not an integer and doesn't exist in the dictionary, then error out & continue
if not settings_value_inputs.isdigit():
print(
"\n !!! ERROR :: Wrong Setting's Value Entered !!! \n Please provide proper setting's value & try again\n")
continue
else:
settings_value_inputs = int(settings_value_inputs)
if define_dictionary.get(settings_value_inputs) == None:
print(
"\n !!! ERROR :: Wrong Setting's Value Entered !!! \n Please provide proper setting's value & try again\n")
continue
set_power_manager_settings(args.ip, args.username, args.password, setting_id_input, settings_value_inputs)
get_inputs = input("\nDo you want to change any other Power Manager settings (Y/N) : ")
print("WARNING: THIS SCRIPT IS EXPERIMENTAL.")
print("The Power Manager scripts were originally internal Dell scripts we then published externally. If you see "
"this message and are using one of these scripts it would be very helpful if you open an issue on GitHub "
"at https://github.com/dell/OpenManage-Enterprise/issues and tell us you are using the script. We have not "
"dedicated any resources to optimizing them but are happy to do so if we know the community is using them. "
"Likewise if you find a bug in one of these scripts feel free to open an issue and we will investigate.")