-
Notifications
You must be signed in to change notification settings - Fork 49
/
get_power_manager_alerts.py
239 lines (197 loc) · 13.4 KB
/
get_power_manager_alerts.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
#
# _author_ = Mahendran P <[email protected]>
#
# 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 the list of Power Manager Specific Alerts in OpenManage Enterprise
#### Description
This script exercises the OpenManage Enterprise REST API to get the list of Power Manager specific alerts
- For authentication, X-Auth is used over Basic Authentication
- Note that the credentials entered are not stored to disk.
#### Python Example
python get_power_manager_alerts.py --ip <xx> --username <username> --password <pwd>
Output:
=======================================
Power Manager - Metrics - Alerts
=======================================
SEVERITY SOURCE_NAME TIME CATEGORY SUB_CATEGORY MESSAGE_ID MESSAGE
Warning linux-0j8n 2020-03-20 16:31:20.484 System Health Metrics CMET0004 POWER on Group_R740s has exceeded its threshold.
Critical linux-0j8n 2020-03-20 16:16:21.252 System Health Metrics CMET0008 TEMPERATURE on Group_R740s has exceeded its lower threshold.
========================================================
Power Manager - Power Configuration - Alerts
========================================================
SEVERITY SOURCE_NAME TIME CATEGORY SUB_CATEGORY MESSAGE_ID MESSAGE
Normal linux-0j8n 2020-01-23 13:30:00.399 System Health Power Configuration CPWR0014 Violation of power policy Policy_on_R740s on group Group_R740s got rectified.
Critical linux-0j8n 2020-01-23 12:30:00.417 System Health Power Configuration CPWR0013 Power policy Policy_on_R740s on group Group_R740s got violated.
"""
# 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)
def get_power_manager_alerts(ip_address, user_name, password):
""" Authenticate with OpenManage Enterprise, enumerate power manager alerts"""
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'}
# Define the power manager alerts URL
alerts_metrics_url = "https://%s/api/AlertService/Alerts?$top=10000000&$filter=contains(SubCategoryName,'Metrics')" % ip_address
alerts_power_config_url = "https://%s/api/AlertService/Alerts?$top=10000000&$filter=contains(SubCategoryName,'Power Configuration')" % ip_address
# Defining OUTPUT format
output_column_headers = ['Severity', 'Source_Name', 'Time', 'Category', 'Sub_Category', 'Message_ID', 'Message']
alerts_metrics_output_column_data = []
alerts_power_config_output_column_data = []
# 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']
# Get Metrics Alerts API call with OpenManage Enterprise
alerts_metrics_response = requests.get(alerts_metrics_url, headers=headers, verify=False)
alerts_metrics_json_data = alerts_metrics_response.json()
# If Metrics Alerts API doesn't respond or failed, message the user with error
if alerts_metrics_response.status_code != 201 & alerts_metrics_response.status_code != 200:
if 'error' in alerts_metrics_json_data:
error_content = alerts_metrics_json_data['error']
if '@Message.ExtendedInfo' not in error_content:
print("Unable to retrieve Power Manager Metrics Alerts from %s" % ip_address)
else:
extended_error_content = error_content['@Message.ExtendedInfo']
print(
"Unable to retrieve Power Manager Metrics Alerts from %s. See below ExtendedInfo for more information" % ip_address)
print(extended_error_content[0]['Message'])
else:
print("Unable to retrieve Power Manager Metrics Alerts from %s" % ip_address)
else:
alerts_metrics_count = alerts_metrics_json_data['@odata.count']
# If the Metrics Alerts count is 0, then error out immediately
if alerts_metrics_count <= 0:
print("No Power Manager Metric Alerts found in %s" % ip_address)
else:
alerts_metrics_content = json.loads(alerts_metrics_response.content)
if alerts_metrics_content:
# For every elements in the Power Manager Metrics Alerts response, store the details in the table
for alerts_metrics_elem in alerts_metrics_content["value"]:
alerts_metrics_data = [alerts_metrics_elem["SeverityName"],
alerts_metrics_elem["AlertDeviceName"],
alerts_metrics_elem["TimeStamp"],
alerts_metrics_elem["CategoryName"],
alerts_metrics_elem["SubCategoryName"],
alerts_metrics_elem["AlertMessageId"],
alerts_metrics_elem["Message"]]
alerts_metrics_output_column_data.append(alerts_metrics_data)
table = columnar(alerts_metrics_output_column_data, output_column_headers, no_borders=True)
print("\n =======================================")
print(" Power Manager - Metrics - Alerts ")
print(" =======================================")
print(table)
else:
print("No Power Manager Metric Alerts found in %s" % ip_address)
# Get Power Configuration Alerts API call with OpenManage Enterprise
alerts_power_config_response = requests.get(alerts_power_config_url, headers=headers, verify=False)
alerts_power_config_json_data = alerts_power_config_response.json()
# If Group Reports API doesn't respond or failed, message the user with error
if alerts_power_config_response.status_code != 201 & alerts_power_config_response.status_code != 200:
if 'error' in alerts_power_config_json_data:
error_content = alerts_power_config_json_data['error']
if '@Message.ExtendedInfo' not in error_content:
print("Unable to retrieve Power Manager - Power Configuration Alerts from %s" % ip_address)
else:
extended_error_content = error_content['@Message.ExtendedInfo']
print(
"Unable to retrieve Power Manager - Power Configuration Alerts from %s. See below ExtendedInfo for more information" % ip_address)
print(extended_error_content[0]['Message'])
else:
print("Unable to retrieve Power Manager - Power Configuration Alerts from %s" % ip_address)
else:
alerts_power_config_count = alerts_power_config_json_data['@odata.count']
# If the Power Configuration Alerts count is 0, then error out immediately
if alerts_power_config_count <= 0:
print("No Power Manager - Power Configuration Alerts found in %s" % ip_address)
else:
alerts_power_config_content = json.loads(alerts_power_config_response.content)
if alerts_power_config_content:
# For every elements in the Power Manager - Power Configuration Alerts response, store the details in the table
for alerts_power_config_elem in alerts_power_config_content["value"]:
alerts_power_config_data = [alerts_power_config_elem["SeverityName"],
alerts_power_config_elem["AlertDeviceName"],
alerts_power_config_elem["TimeStamp"],
alerts_power_config_elem["CategoryName"],
alerts_power_config_elem["SubCategoryName"],
alerts_power_config_elem["AlertMessageId"],
alerts_power_config_elem["Message"]]
alerts_power_config_output_column_data.append(alerts_power_config_data)
table = columnar(alerts_power_config_output_column_data, output_column_headers, no_borders=True)
print("\n ========================================================")
print(" Power Manager - Power Configuration - Alerts ")
print(" ========================================================")
print(table)
else:
print("No Power Manager - Power Configuration Alerts found in %s" % ip_address)
except Exception as error:
print("Unexpected error:", str(error))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)
parser.add_argument("--ip", "-i", required=True, help="OpenManage Enterprise IP")
parser.add_argument("--username", "-u", required=False, help="Username for OpenManage Enterprise ", default="admin")
parser.add_argument("--password", "-p", required=True, help="Password for OpenManage Enterprise ")
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.")
get_power_manager_alerts(args.ip, args.username, args.password)
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.")