-
Notifications
You must be signed in to change notification settings - Fork 9
/
get-tenantProblemFeed.ps1
283 lines (238 loc) · 9.71 KB
/
get-tenantProblemFeed.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
<#
.Synopsis
For viewing or saving a Dynatrace tenant problem feed as Powershell tables and csv files
.Description
This script is to extract the problems for an environment for designated entities and period.
And also present in a powershell or csv format.
todo:
- Add a human readable date as startTimeNice and endTimeNice
- Nicely order the $data prior to returning
- Option to fetch human readable name of the problem (for summarising options)
- support tags for filtering of problem feed
.Notes
Author: Adrian, Michael Ball
Version: 1.0.0 - 16062020
ChangeLog
1.0.0
MVP - Things work
1.0.1
Commenting - adding of todo list,
Removal of stray output
show problem detail default is now false
.Example
./get-tenantProblemFeed.ps1 -dtenv <env> -token <token> -periodType "week" -timePeriod 3 -outfile test.csv
#>
<#
###########################
# Start of scaffold block #
###########################
#>
PARAM (
# The cluster or tenant the HU report should be fetched from
[Parameter()][ValidateNotNullOrEmpty()] $dtenv = $env:dtenv,
# Token must have Smartscape Read access for a tenant
[Alias('dttoken')][ValidateNotNullOrEmpty()][string] $token = $env:dttoken,
# What period length should be reported
[ValidateSet('minute', 'hour', 'day', 'week')][string]$periodType = 'hour',
# How many of these time periods back should be requested
[String]$timePeriod = 2,
# Number of results to pull back
[int]$pageSize = 500,
# Path to output a csv representation of the fetched data.
[ValidateNotNullOrEmpty()][string] $outfile,
# Prints Help output
[Alias('h')][switch] $help,
# use this switch to tell this script to not check token or cluster viability
[switch] $noCheckCompatibility,
# use this switch to tell powershell to ignore ssl concerns
[switch] $noCheckCertificate,
# Flag to expand the details of the problem feed
[switch] $expandDetail,
# String entity for tags to be used as a filter
[string] $tags,
# DO NOT USE - This is set by Script Author
[String[]]$script:tokenPermissionRequirements = @('DataExport')
)
# Help flag checks
if ($h -or $help) {
Get-Help $script:MyInvocation.MyCommand.Path -Detailed
exit 0
}
# Ensure that dtenv and token are both populated
if (!$script:dtenv) {
return Write-Error "dtenv was not populated - unable to continue"
}
elseif (!$script:token) {
return Write-Error "token/dttoken was not populated - unable to continue"
}
# Try to 'fix' a missing https:// in the env
if ($script:dtenv -notlike "https://*" -and $script:dtenv -notlike "http://*") {
Write-Host -ForegroundColor DarkYellow -Object "WARN: Environment URI was missing 'httpx://' prefix"
$script:dtenv = "https://$script:dtenv"
Write-host -ForegroundColor Cyan "New environment URL: $script:dtenv"
}
# Try to 'fix' a trailing '/'
if ($script:dtenv[$script:dtenv.Length - 1] -eq '/') {
$script:dtenv = $script:dtenv.Substring(0, $script:dtenv.Length - 1)
write-host -ForegroundColor DarkYellow -Object "WARNING: Removed trailing '/' from dtenv input"
}
$baseURL = "$script:dtenv/api/v1"
# Setup Network settings to work from less new setups
if ($nocheckcertificate) {
# SSL and other compatability settings
function Disable-SslVerification {
if (-not ([System.Management.Automation.PSTypeName]"TrustEverything").Type) {
Add-Type -TypeDefinition @"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class TrustEverything
{
private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; }
public static void SetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
public static void UnsetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; } }
"@
}
[TrustEverything]::SetCallback()
}
function Enable-SslVerification {
if (([System.Management.Automation.PSTypeName]"TrustEverything").Type) {
[TrustEverything]::UnsetCallback()
}
}
Disable-SslVerification
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocoltype]::Tls12
# Construct the headers for this API request
$headers = @{
Authorization = "Api-Token $script:token";
Accept = "application/json; charset=utf-8";
"Content-Type" = "application/json; charset=utf-8"
}
function confirm-supportedClusterVersion ($minimumVersion = 176, $logmsg = '') {
# Environment version check - cancel out if too old
$uri = "$baseURL/config/clusterversion"
Write-Host -ForegroundColor cyan -Object "Cluster Version Check$logmsg`: GET $uri"
$res = Invoke-RestMethod -Method GET -Headers $headers -Uri $uri
$envVersion = $res.version -split '\.'
if ($envVersion -and (([int]$envVersion[0]) -ne 1 -or ([int]$envVersion[1]) -lt $minimumVersion)) {
Write-Error "Failed Environment version check - Expected: > 1.$minimumVersion - Got: $($res.version)"
exit
}
}
function confirm-requiredTokenPerms ($token, $requirePerms, $logmsg = '') {
# Token has required Perms Check - cancel out if it doesn't have what's required
$uri = "$baseURL/tokens/lookup"
$headers = @{
Authorization = "Api-Token $token";
Accept = "application/json; charset=utf-8";
"Content-Type" = "application/json; charset=utf-8"
}
Write-Host -ForegroundColor cyan -Object "Token Permissions Check$logmsg`: POST $uri"
$res = Invoke-RestMethod -Method POST -Headers $headers -Uri $uri -body "{ `"token`": `"$token`"}"
if (($requirePerms | Where-Object { $_ -notin $res.scopes }).count) {
Write-Error "Failed Token Permission check. Token requires: $($requirePerms -join ',')"
write-host "Token provided only had: $($res.scopes -join ',')"
exit
}
}
if (!$noCheckCompatibility) {
<#
Determine what type environment we have? This script will only work on tenants
SaaS tenant = https://*.live.dynatrace.com
Managed tenant = https://*/e/UUID
Managed Cluster = https://*
#>
$envType = 'cluster'
if ($script:dtenv -like "*.live.dynatrace.com") {
$envType = 'env'
}
elseif ($script:dtenv -like "http*://*/e/*") {
$envType = 'env'
}
# Script won't work on a cluster
if ($envType -eq 'cluster') {
write-error "'$script:dtenv' looks like an invalid URL (and Clusters are not supported by this script)"
exit
}
confirm-supportedClusterVersion 176
confirm-requiredTokenPerms $script:token $script:tokenPermissionRequirements
}
function convertTo-jsDate($date) {
return [Math]::Floor(1000 * (Get-Date ($date) -UFormat %s))
}
$timePeriodHash = @{
minute = 1000 * 60;
hour = 1000 * 60 * 60;
day = 1000 * 60 * 60 * 24;
week = 1000 * 60 * 60 * 24 * 7;
}
<#
###########################
# End of scaffold block #
###########################
#>
## Form the base of the url endpoint to be queried
$baseURL = "$baseURL/problem/feed"
# Add the time and other params
$now = convertTo-jsDate ([datetime]::UtcNow)
$start = $now - $timePeriodHash[$script:periodType] * $script:timePeriod
$baseURL = $baseURL + "?startTimestamp=$start&endTimestamp=$now&expandDetails=$([string] $expandDetail)"
# Add the System.Web type - the lack of this will be a headache otherwise.
Add-Type -AssemblyName System.Web -ErrorAction SilentlyContinue | Out-Null
## TODO ###
# Encode the tags
<# $filterTags = [System.Web.HttpUtility]::UrlEncode($script:USQL) -replace '\+', '%20' #>
# create the end URI with tags as a filter
if ([string]::IsNullOrEmpty($tag)) {
$uri = $baseURL
}
else {
$uri = $baseURL + "&tag="
}
# Output the uri used as information event
write-host $uri -ForegroundColor Cyan
# make the call, being aware of different pwsh versions
try {
$response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers
}
catch {
Write-Error $_
exit 1
}
$response.result.problems
## Create columnName variable to loop through
$columnName = ($response.result.problems | Get-Member -MemberType "NoteProperty").name
# iterate through the returned data to construct a PWSH Object, output and possibly save it
$data = @()
Foreach ($row in $response.result.problems) {
$_row = New-Object PSObject
for ($i = 0; $i -lt $columnName.Length; $i++) {
## Reduce System.Object[] in outputs
## Expand the object into strings.
if (($row.($columnName[$i]).GetType().Name) -ieq "Object[]") {
[string] $objValue = ""
foreach ($objs in $row.($columnName[$i])) {
$objValue += [string] ($objs)
}
$_row | Add-Member -MemberType NoteProperty -Name $columnName[$i] -Value $objValue
}
else {
$_row | Add-Member -MemberType NoteProperty -Name $columnName[$i] -Value $row.($columnName[$i])
}
}
$data += $_row
}
# If we need to output to file
if ($script:outfile) {
## Additional condition to check output of Split-Path
if (Split-Path $script:outfile) {
if (!(Split-Path $script:outfile | Test-Path -PathType Container)) {
New-Item -Path (Split-Path $script:outfile) -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
}
}
$outputFile = New-Item -Path $script:outfile -Force
$data | ConvertTo-Csv -NoTypeInformation | out-file -FilePath $script:outfile
write-host "Written to csv table to $($outputFile.fullname)" -ForegroundColor Green
}
$data