-
Notifications
You must be signed in to change notification settings - Fork 49
/
Get-Alerts.ps1
543 lines (424 loc) · 16.6 KB
/
Get-Alerts.ps1
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
#Requires -Version 7
<#
_author_ = Grant Curell <grant_curell@dell.com>
_contributor_ = Raajeev Kalyanaraman wrote the method for getting alerts by group
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
Retrieves alerts from a target OME Instance.
.DESCRIPTION
This script provides a large number of ways to get alerts with various filters. With no arguments it will pull all
alerts from the OME instance. The below filters are available:
- top - Pull top records
- skip - Skip N number of records
- orderby - Order by a specific column
- id - Filter by the OME internal event ID
- Alert device ID - Filter by the OME internal ID for the device
- Alert Device Identifier / Service Tag - Filter by the device identifier or service tag of a device
- Device type - Filter by device type (server, chassis, etc)
- Severity type - The severity of the alert - warning, critical, info, etc
- Status type - The status of the device - normal, warning, critical, etc
- Category Name - The type of alert generated. Audit, configuration, storage, system health, etc
- Subcategory ID - Filter by a specific subcategory. The list is long - see the --get-subcategories option for details
- Subcategory name - Same as above except the name of the category instead of the ID
- Message - Filter by the message generated with the alert
- TimeStampBegin - Not currently available. See https://github.com/dell/OpenManage-Enterprise/issues/101
- TimeStampEnd - Not currently available. See https://github.com/dell/OpenManage-Enterprise/issues/101
- Device name - Filter by a specific device name
- Group name - Filter alerts by a group name
- Group description - Filter alerts by a group description
Authentication is done over x-auth with basic authentication. Note: Credentials are not stored on disk.
.PARAMETER IpAddress
This is the IP address of the OME Appliance
.PARAMETER Credentials
Credentials used to talk to the OME Appliance
.PARAMETER Top
Top records to return.
.PARAMETER Pages
You will generally not need to change this unless you are using a large value for top
- typically more than 50 devices. In the UI the results come in pages. Even when
not using the UI the results are still delivered in 'pages'. The 'top' argument
effectively sets the page size to the value you select and will return *everything*
, albeit much slower, by iterating over all pages in OME. To prevent this we tell it
to only return a certain number of pages. By default this value is 1. If you want
more than one page of results you can set this.
.PARAMETER Skip
The number of records, starting at the top, to skip.
.PARAMETER Orderby
Order to apply to the output.
.PARAMETER Id
Filter by the OME internal event ID.
.PARAMETER AlertDeviceId
Filter by OME internal device ID.
.PARAMETER AlertDeviceIdentifier
Filter by the device identifier. For servers this is the service tag.
.PARAMETER AlertDeviceType
Filter by device type.
.PARAMETER SeverityType
Filter by the severity type of the alert.
.PARAMETER StatusType
Filter by status type of the device.
.PARAMETER CategoryName
Filter by category name.
.PARAMETER GetSubcategories
Grabs a list of subcategories from the OME instance.
.PARAMETER SubcategoryId
Filter by subcategory ID. To get a list of subcategory IDs available run this program
with the --get-subcategories option.
.PARAMETER SubcategoryName
Filter by subcategory name. To get a list of subcategory names available run this
program with the --get-subcategories option.
.PARAMETER Message
Filter by message.
.PARAMETER TimeStampBegin
Filter by starting time of alerts. Use format YYYY-MM-DD HH:MM:SS.SS. Ex: 2021-09-07 19:01:28.46
You must surround it with quotes '
.PARAMETER TimeStampEnd
Filter by ending time of alerts. Use format YYYY-MM-DD HH:MM:SS.SS. Ex: 2021-09-07 19:01:28.46
You must surround it with quotes '
.PARAMETER AlertDeviceName
Filter by the OME device name.
.PARAMETER AlertsByGroupName
The name of the group on which you want to filter.
.PARAMETER AlertsByGroupDescription
The description of the group on which you want to filter.
.EXAMPLE
$creds = Get-Credential
Get-Alerts.ps1 -IpAddress 192.168.1.93 -Credentials $creds -CategoryName SYSTEM_HEALTH -Top 10
Get-Alerts.ps1 -IpAddress 192.168.1.93 -Credentials $creds -Top 5 -Skip 3 -Orderby TimeStampAscending -StatusType CRITICAL
Get-Alerts.ps1 -IpAddress 192.168.1.85 -Credentials $creds -TimeStampEnd '2021-09-07 19:01:28.46' -TimeStampBegin '2015-09-07 19:01:28.46' -CategoryName SYSTEM_HEALTH -Top 10
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[System.Net.IPAddress] $IpAddress,
[Parameter(Mandatory)]
[pscredential] $Credentials,
[Parameter(Mandatory = $false)]
[string] $Top,
[Parameter(Mandatory = $false)]
[int] $Pages,
[Parameter(Mandatory = $false)]
[string] $Skip,
[Parameter(Mandatory = $False)]
[ValidateSet('AlertDeviceIdentifier', 'AlertDeviceType', 'SeverityType',
'StatusType', 'SubCategoryName', 'Message', 'TimeStampDescending',
'TimeStampAscending')]
[String] $Orderby,
[Parameter(Mandatory = $false)]
[string] $Id,
[Parameter(Mandatory = $false)]
[string] $AlertDeviceId,
[Parameter(Mandatory = $false)]
[string] $AlertDeviceIdentifier,
[Parameter(Mandatory = $false)]
[ValidateSet('SERVER', 'CHASSIS', 'NETWORK_CONTROLLER', 'NETWORK_IOM', 'STORAGE', 'STORAGE_IOM')]
[string] $AlertDeviceType,
[Parameter(Mandatory = $false)]
[ValidateSet('WARNING', 'CRITICAL', 'INFO', 'NORMAL', 'UNKNOWN')]
[string] $SeverityType,
[Parameter(Mandatory = $false)]
[ValidateSet('NORMAL', 'UNKNOWN', 'WARNING', 'CRITICAL', 'NOSTATUS')]
[string] $StatusType,
[Parameter(Mandatory = $false)]
[ValidateSet('AUDIT', 'CONFIGURATION', 'MISCELLANEOUS', 'STORAGE', 'SYSTEM_HEALTH', 'UPDATES',
'WORK_NOTES')]
[string] $CategoryName,
[Parameter(Mandatory = $false)]
[Switch] $GetSubcategories,
[Parameter(Mandatory = $false)]
[string] $SubcategoryId,
[Parameter(Mandatory = $false)]
[string] $SubcategoryName,
[Parameter(Mandatory = $false)]
[string] $Message,
[Parameter(Mandatory = $false)]
[string] $TimeStampBegin,
[Parameter(Mandatory = $false)]
[string] $TimeStampEnd,
[Parameter(Mandatory = $false)]
[string] $AlertDeviceName,
[Parameter(Mandatory = $false)]
[string] $AlertsByGroupName,
[Parameter(Mandatory = $false)]
[string] $AlertsByGroupDescription
)
function Get-Data {
<#
.SYNOPSIS
Used to interact with API resources
.DESCRIPTION
This function retrieves data from a specified URL. Get requests from OME return paginated data. The code below
handles pagination. This is the equivalent in the UI of a list of results that require you to go to different
pages to get a complete listing.
.PARAMETER Url
The API url against which you would like to make a request
.PARAMETER OdataFilter
An optional parameter for providing an odata filter to run against the API endpoint.
.PARAMETER MaxPages
The maximum number of pages you would like to return
.INPUTS
None. You cannot pipe objects to Get-Data.
.OUTPUTS
dict. A dictionary containing the results of the API call or an empty dictionary in the case of a failure
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Url,
[Parameter(Mandatory = $false)]
[string]
$OdataFilter,
[Parameter(Mandatory = $false)]
[int]
$MaxPages = $null
)
$Data = @()
$NextLinkUrl = $null
try {
if ($PSBoundParameters.ContainsKey('OdataFilter')) {
$CountData = Invoke-RestMethod -Uri $Url"?`$filter=$($OdataFilter)" -Method Get -Credential $Credentials -SkipCertificateCheck
if ($CountData.'@odata.count' -lt 1) {
Write-Error "No results were found for filter $($OdataFilter)."
return @{}
}
}
else {
$CountData = Invoke-RestMethod -Uri $Url -Method Get -Credential $Credentials -ContentType $Type `
-SkipCertificateCheck
}
if ($null -ne $CountData.'value') {
$Data += $CountData.'value'
}
else {
$Data += $CountData
}
if ($CountData.'@odata.nextLink') {
$NextLinkUrl = "https://$($IpAddress)$($CountData.'@odata.nextLink')"
}
$i = 1
while ($NextLinkUrl) {
if ($MaxPages) {
if ($i -ge $MaxPages) {
break
}
$i = $i + 1
}
$NextLinkData = Invoke-RestMethod -Uri "$($NextLinkUrl)" -Method Get -Credential $Credentials `
-ContentType $Type -SkipCertificateCheck
if ($null -ne $NextLinkData.'value') {
$Data += $NextLinkData.'value'
}
else {
$Data += $NextLinkData
}
if ($NextLinkData.'@odata.nextLink') {
$NextLinkUrl = "https://$($IpAddress)$($NextLinkData.'@odata.nextLink')"
}
else {
$NextLinkUrl = $null
}
}
return $Data
}
catch [System.Net.Http.HttpRequestException] {
Write-Error "There was a problem connecting to OME or the URL supplied is invalid. Did it become unavailable?"
return @{}
}
}
$SEVERITYTYPEMAP =
@{
WARNING = '8'
CRITICAL = '16'
INFO = '2'
NORMAL = '4'
UNKNOWN = '1'
}
$STATUSTYPEMAP =
@{
NORMAL = '1000'
UNKNOWN = '2000'
WARNING = '3000'
CRITICAL = '4000'
NOSTATUS = '5000'
}
$ALERTDEVICETYPEMAP =
@{
SERVER = '1000'
CHASSIS = '2000'
NETWORK_CONTROLLER = '9000'
NETWORK_IOM = '4000'
STORAGE = '3000'
STORAGE_IOM = '8000'
}
$CATEGORYIDMAP =
@{
AUDIT = 4
MISCELLANEOUS = 7
STORAGE = 2
SYSTEM_HEALTH = 1
UPDATES = 3
WORK_NOTES = 6
CONFIGURATION = 5
}
try {
if ($PSBoundParameters.ContainsKey('GetSubcategories')) {
Write-Output Get-Data "https://$($IpAddress)/api/AlertService/AlertCategories"
}
if ($PSBoundParameters.ContainsKey('Pages') -and -not $PSBoundParameters.ContainsKey('Top')) {
Write-Error "You cannot provide the pages argument without the top argument."
Exit
}
if ($PSBoundParameters.ContainsKey('Top') -and -not $PSBoundParameters.ContainsKey('Pages')) {
$Pages = 1
}
$AuditLogsUrl = "https://$($IpAddress)/api/AlertService/Alerts"
$UserOdataFilter = @()
if ($PSBoundParameters.ContainsKey('Id')) {
$UserOdataFilter += "Id eq $($Id)"
}
if ($PSBoundParameters.ContainsKey('AlertDeviceId')) {
$UserOdataFilter += "AlertDeviceId eq %$($AlertDeviceId)"
}
if ($PSBoundParameters.ContainsKey('AlertDeviceIdentifier')) {
$UserOdataFilter += "AlertDeviceIdentifier eq '$($AlertDeviceIdentifier)'"
}
if ($PSBoundParameters.ContainsKey('AlertDeviceType')) {
$UserOdataFilter += "AlertDeviceType eq $($ALERTDEVICETYPEMAP[$AlertDeviceType])"
}
if ($PSBoundParameters.ContainsKey('StatusType')) {
$UserOdataFilter += "StatusType eq $($STATUSTYPEMAP[$StatusType])"
}
if ($PSBoundParameters.ContainsKey('SeverityType')) {
$UserOdataFilter += "SeverityType eq $($SEVERITYTYPEMAP[$SeverityType])"
}
if ($PSBoundParameters.ContainsKey('CategoryName')) {
$UserOdataFilter += "CategoryId eq $($CATEGORYIDMAP[$CategoryName])"
}
if ($PSBoundParameters.ContainsKey('SubcategoryId')) {
$UserOdataFilter += "SubCategoryId eq $($SubcategoryId)"
}
if ($PSBoundParameters.ContainsKey('SubcategoryName')) {
$UserOdataFilter += "SubCategoryName eq '$($SubcategoryName)'"
}
if ($PSBoundParameters.ContainsKey('AlertDeviceName')) {
$UserOdataFilter += "AlertDeviceName eq '$($AlertDeviceName)'"
}
if ($PSBoundParameters.ContainsKey('Message')) {
$UserOdataFilter += "Message eq '$($Message)'"
}
if ($PSBoundParameters.ContainsKey('TimeStampBegin')) {
$UserOdataFilter += "TimeStamp ge '$($TimeStampBegin)'"
}
if ($PSBoundParameters.ContainsKey('TimeStampEnd')) {
$UserOdataFilter += "TimeStamp le '$($TimeStampEnd)'"
}
$GroupUrl = "https://$($IpAddress)/api/GroupService/Groups"
$Groups = $null
$GroupId = ""
if ($PSBoundParameters.ContainsKey('AlertsByGroupName')) {
$Groups = Get-Data $GroupUrl "Name eq '$($AlertsByGroupName)'"
if ($Groups.Length -lt 1) {
Write-Error "Error: We were unable to find a group matching the name $($AlertsByGroupName)."
Exit
}
$GroupId = $Groups[0].'Id'
}
elseif ($PSBoundParameters.ContainsKey('AlertsByGroupDescription')) {
$Groups = Get-Data $GroupUrl "Description eq '$($AlertsByGroupDescription)'"
if ($Groups.Length -lt 1) {
Write-Error "Error: We were unable to find a group matching the description $($AlertsByGroupDescription)."
Exit
}
$GroupId = $Groups[0].'Id'
}
if ($PSBoundParameters.ContainsKey('AlertsByGroupDescription') -or $PSBoundParameters.ContainsKey('AlertsByGroupName')) {
$UserOdataFilter += "AlertDeviceGroup eq $($GroupId)"
}
$UrlFilter = $null
if ($UserOdataFilter.Length -gt 0) {
$UrlFilter = ''
ForEach ($Index in (0..($UserOdataFilter.Count - 1))) {
# Do not append and on the last element of the filter
if ($Index -eq $UserOdataFilter.Count - 1) {
$UrlFilter += $UserOdataFilter[$Index]
}
else {
$UrlFilter += "$($UserOdataFilter[$Index]) and "
}
}
}
if ($PSBoundParameters.ContainsKey('Orderby')) {
if ($Orderby -eq "TimeStampAscending") {
$Orderby = "TimeStamp asc"
}
if ($Orderby -eq "TimeStampDescending") {
$Orderby = "TimeStamp desc"
}
}
# These are arguments which aren't filters: top, skip, and orderby
$NonfilterArgs = @()
if ($null -eq $UrlFilter) {
if ($PSBoundParameters.ContainsKey('Top')) {
$NonfilterArgs += "top=$($Top)"
}
if ($PSBoundParameters.ContainsKey('Skip')) {
$NonfilterArgs += "skip=$($Skip)"
}
if ($PSBoundParameters.ContainsKey('Orderby')) {
$NonfilterArgs += "orderby=$($Orderby)"
}
# Create the URL if there is no filter argument
$NonFilterUrl = $null
if ($NonfilterArgs.Length -gt 0) {
$NonFilterUrl = ''
ForEach ($Index in (0..($NonfilterArgs.Count - 1))) {
# Do not append &$ on the last element of the filter
if ($Index -eq 0) {
$NonFilterUrl += "?`$$($NonfilterArgs[$Index])"
}
else {
$NonFilterUrl += "&`$$($NonfilterArgs[$Index])"
}
}
$AuditLogsUrl = $AuditLogsUrl + $NonFilterUrl
}
}
else {
if ($PSBoundParameters.ContainsKey('Top')) {
$UrlFilter += "&`$top=$($Top)"
}
if ($PSBoundParameters.ContainsKey('Skip')) {
$UrlFilter += "&`$skip=$($Skip)"
}
if ($PSBoundParameters.ContainsKey('Orderby')) {
$UrlFilter += "&`$orderby=$($Orderby)"
}
}
if ($null -ne $UrlFilter) {
Write-Output "The URL is $($AuditLogsUrl)?`$filter=$($UrlFilter)"
Write-Output "You can modify this URL in accordance with the odata 4 standard. See http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html for details."
$Output = Get-Data $AuditLogsUrl $UrlFilter $Pages
Write-Output $Output
}
else {
Write-Output "The URL is $($AuditLogsUrl)"
$Output = Get-Data -Url $AuditLogsUrl -MaxPages $Pages
Write-Output $Output
}
}
catch {
Write-Error "Exception occurred at line $($_.InvocationInfo.ScriptLineNumber) - $($_.Exception.Message)"
}
# https://192.168.1.85/api/AlertService/Alerts?$filter=TimeStamp%20ge%20%272021-09-07%2019:01:22.483%27 - working