-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create-AzSStorageDashboard.ps1
450 lines (396 loc) · 19 KB
/
Create-AzSStorageDashboard.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
param(
[CmdletBinding(DefaultParameterSetName="relativeTime")]
[Parameter(Position = 1, Mandatory = $false)]
[string]$adminSubscriptionName = "Default Provider Subscription" ,
[Parameter(Position = 2, Mandatory = $false)]
[string]$jsonTemplateLocation = ".\templateJson" ,
[Parameter(Mandatory = $false)]
[System.Object]$DefaultProfile,
[Parameter(ParameterSetName="relativeTime")]
[ValidateSet('PT30M', 'PT4H', 'PT12H', 'P1D', 'P2D', 'P3D', 'P7D', 'P30D')]
[string]$duration = 'P1D',
[Parameter(Mandatory=$True, ParameterSetName="absoluteTime", HelpMessage="Please enter the start time of time range you want to see.")]
[datetime]$startTime,
[Parameter(Mandatory=$True, ParameterSetName="absoluteTime", HelpMessage="Please enter the end time of time range you want to see.")]
[datetime]$endTime,
[Parameter(Mandatory = $false)]
[ValidateSet('Automatic', 'PT1M', 'PT1H', 'P1D', 'PT5M', 'PT15M', 'PT30M', 'PT6H', 'PT12H')]
[string]$timeGrain = 'Automatic',
[Parameter(Mandatory = $false)]
[string]$outputLocation = '.'
)
<#
.SYNOPSIS
Generate json used in Azure Stack portal to show volumes performances.
.Description
This function is used to generate dashboard jsons, which can be uploaded on Azure Stack Dashboard to create related dashboards.
You can use this function without parameters to create jsons, showing last 1 day metrics, at the current folder.
To set time range, you can define duration or pair of startTime and endTime.
.Inputs
Charts' Time range and granularity settings. Json output location setting.
.Outputs
Three jsons represent count, latency and throughput performance respectively.
.Parameter timeGrain
The timespan defines the time granularity in charts, in ISO 8601 duration format.
.Parameter outputLocation
The location to expose generated jsons.
.Parameter duration
The timespan defines the time range of volume metrics, in ISO 8601 duration format.
.Parameter startTime
The start time of time range shown on dashboard.
.Parameter endTime
The start time of time range shown on dashboard.
.Example
# default json save to spedified location
Save-AzureStackVolumesPerformanceDashboardJson -outputLocation 'D:\dashboardJsons'
.Example
# data of last day with 15min interval
Save-AzureStackVolumesPerformanceDashboardJson -duration "P1D" -timeGrain "PT15M"
.Example
# date from 4/1 to 4/8 with 1hr interval
Save-AzureStackVolumesPerformanceDashboardJson -startTime (Get-date("2019-04-01")) -endTime (Get-date("2019-04-08")) -timeGrain "PT1H"
.Notes
Author: Azure Stack Azure Monitor Team
.Link
Source Code: https://github.com/GTMer/AzureStack-VolumesPerformanceDashboard-Generator
#>
function Save-AzureStackVolumesPerformanceDashboardJson {
[CmdletBinding(DefaultParameterSetName="relativeTime")]
param (
[Parameter(Mandatory = $false)]
[System.Object]$DefaultProfile,
[Parameter(ParameterSetName="relativeTime")]
[ValidateSet('PT30M', 'PT4H', 'PT12H', 'P1D', 'P2D', 'P3D', 'P7D', 'P30D')]
[string]$duration = 'P1D',
[Parameter(Mandatory=$True, ParameterSetName="absoluteTime", HelpMessage="Please enter the start time of time range you want to see.")]
[datetime]$startTime,
[Parameter(Mandatory=$True, ParameterSetName="absoluteTime", HelpMessage="Please enter the end time of time range you want to see.")]
[datetime]$endTime,
[Parameter(Mandatory = $false)]
[ValidateSet('Automatic', 'PT1M', 'PT1H', 'P1D', 'PT5M', 'PT15M', 'PT30M', 'PT6H', 'PT12H')]
[string]$timeGrain = 'Automatic',
[Parameter(Mandatory = $false)]
[string]$outputLocation = '.'
)
# If user do not input DefaultProfile
if ($null -eq $DefaultProfile) {
$script:context = Get-AzureRmContext -ListAvailable | Where-Object { $_.Name.Contains($adminSubscriptionName)} | Select-Object -first 1
}
else {
$script:context = $DefaultProfile.Context
}
# if user hadn't added and login to AzureRmEnvironment, exit
if ($null -eq $script:context.Account) {
throw "Please login in AzureRm account first."
}
if ((Test-Path -Path $outputLocation) -eq $false) {
throw "Output location not exist."
}
# $resourceId = Get-AzureStackResourceId
$volumes = Get-AzureStackVolumes
if ($timeGrain -eq 'Automatic') {
if ($PSCmdlet.ParameterSetName -eq "absoluteTime") {
$timeRange = $endTime - $startTime
}
else {
$timeRange = [System.Xml.XmlConvert]::ToTimeSpan($duration)
}
if ($timeRange -le (New-TimeSpan -Hours 4)) {
$timeGrain = "PT1M"
}
if ($timeRange -lt (New-TimeSpan -Days 1)) {
$timeGrain = "PT5M"
}
elseif ($timeRange -le (New-TimeSpan -Days 1)) {
$timeGrain = "PT15M"
}
elseif ($timeRange -le (New-TimeSpan -Days 3)) {
$timeGrain = "PT30M"
}
elseif ($timeRange -le (New-TimeSpan -Days 7)) {
$timeGrain = "PT1H"
}
else {
$timeGrain = "PT6H"
}
}
$description = "timeGrain: $timeGrain; `n"
if ($PSCmdlet.ParameterSetName -eq "absoluteTime") {
if ($startTime -gt $endTime) {
throw ("StartTime should less than EndTime!")
}
if ($startTime -gt $(Get-Date)) {
throw ("StartTime should less than Now!")
}
$description += "startTime: $($startTime.ToString('o')); `nendTime: $($endTime.ToString('o')); `n"
@("ObjStore", "Infrastructure", "VmTemp") | ForEach-Object {
Get-DashboardVolumesJson -volumeType $_ -startTime $startTime.ToString('o') -endTime $endTime.ToString('o') -timeGrain $timeGrain -description $description -volumes $volumes |
ConvertTo-Json -Depth 100 > $($outputLocation.TrimEnd('\') + '\' + "DashboardVolume" + $_ + "_customTime.json")
Write-Host "$($outputLocation.TrimEnd('\') + '\' + "DashboardVolume" + $_ + "_customTime.json") finished."
}
}
else {
$description += "duration: $duration; `n"
$durationTotalMilliseconds = ([System.Xml.XmlConvert]::ToTimeSpan($duration)).TotalMilliseconds
@("ObjStore", "Infrastructure", "VmTemp") | ForEach-Object {
Get-DashboardVolumesJson -duration $durationTotalMilliseconds -timeGrain $timeGrain -description $description -volumes $volumes -volumeType $_ |
ConvertTo-Json -Depth 100 > $($outputLocation.TrimEnd('\') + '\' + "DashboardVolume" + $_ + "_" + $duration + ".json")
Write-Host "$($outputLocation.TrimEnd('\') + '\' + "DashboardVolume" + $_ + "_" + $duration + ".json") finished."
}
}
}
function Get-AzureStackResourceId {
[CmdletBinding()]
param (
)
try {
Write-Host "Getting resource Id from AzureRmSubscription."
$adminSubscription = $script:context.Subscription
$location = Get-AzureRmLocation -DefaultProfile $script:context -ErrorAction Stop -Verbose
}
catch {
Write-Error $_
throw "Please login in AzureRm account. If still happens, check your environment settings in psm1 file."
}
"subscriptions/$($adminSubscription.Id)/resourceGroups/System.$($location.location)/providers/Microsoft.Fabric.Admin/fabricLocations/$($location.location)"
}
function Get-AzureStackVolumes {
[CmdletBinding()]
param (
)
try {
Write-Host "Getting volumes data from ARM."
$location = Get-AzureRmLocation -DefaultProfile $script:context -ErrorAction Stop -Verbose
$location = $location.Location
$scaleUnits = Get-AzureRmResource -DefaultProfile $script:context -ResourceName $location -ResourceType Microsoft.Fabric.Admin/fabricLocations/scaleunits -ResourceGroupName "System.$($location)" -ApiVersion "2016-05-01"
$sotrageSubSystems = Get-AzureRmResource -DefaultProfile $script:context -ResourceName $scaleUnits.Name -ResourceType Microsoft.Fabric.Admin/fabricLocations/scaleunits/storageSubSystems -ResourceGroupName "System.$($location)" -ApiVersion "2018-10-01"
$volumes = Get-AzureRmResource -DefaultProfile $script:context -ResourceName $sotrageSubSystems.Name -ResourceType Microsoft.Fabric.Admin/fabricLocations/scaleunits/storageSubSystems/volumes -ResourceGroupName "System.$($location)" -ApiVersion "2018-10-01"
}
catch {
Write-Error $_.ToString()
Write-Error "Cannot fetch data from ARM."
}
$volumes
}
<#
.SYNOPSIS
Transform volumes into dictionary of list of tuple to show volume name namemaps, eg: $volumesByType["ObjStore"][2] = Tupe["Volume11", "Obj_store2"] .
#>
function Get-volumesByType {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[array]$volumes = ( Get-AzureStackVolumes ),
[Parameter(Mandatory = $false)]
[array]$volumeTypes = @("ObjStore", "Infrastructure", "VmTemp")
)
Write-Host "Analyzing volumes data."
if ($volumes.Count -eq 0) {
return
}
$volumesByType = @{}
$volumeTypes | ForEach-Object {
$volumesByType.$_ = New-Object 'Collections.Generic.List[Tuple[String,String]]'
}
$volumes | ForEach-Object {
$labelPrefix = [regex]::match($_.properties.volumeLabel, '(.*)_.*').Groups[1].Value
if ($volumeTypes.Contains($labelPrefix)) {
$volumeLocalName = [regex]::match($_.properties.volumeLocalName, '.*\/(.*)').Groups[1].Value
$volumesByType.$labelPrefix.add([Tuple]::Create($volumeLocalName, $_.properties.volumeLabel))
}
}
$volumeTypes | ForEach-Object {
$volumesByType.$_ = $volumesByType.$_ | Sort-Object Item2
}
$volumesByType
}
<#
.SYNOPSIS
Transform template json into PsCustomObject.
#>
function Initialize-TilePsCustomObject {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidateSet("Throughput", "Count", "Latency", "Capacity")]
[string]$metricType,
[Parameter(ParameterSetName="relativeTime")]
[Double]$duration = 86400000,
[Parameter(ParameterSetName="absoluteTime")]
[string]$startTime,
[Parameter(ParameterSetName="absoluteTime")]
[string]$endTime,
[Parameter(Mandatory = $false)]
[string]$timeGrain = "PT15M",
[Parameter(Mandatory = $true)]
[string]$resourceId
)
if ($metricType -eq "Capacity") {
$tileTemplate = $Script:capacityTemplate.Replace("<resourceIdToBeReplaced>", '/' + $resourceId) | ConvertFrom-Json
}
else {
$tileTemplate = $Script:tileTemplate.Replace("<resourceIdToBeReplaced>", '/' + $resourceId) | ConvertFrom-Json
}
# set tile size
$tileTemplate.position.colSpan = $tileColSpan
$tileTemplate.position.rowSpan = $tileRowSpan
$templateChart = $tileTemplate.metadata.inputs[0].value.charts[0]
# set time range
$templateChart.timeContext.psobject.properties.remove("relative")
$templateChart.timeContext.psobject.properties.remove("absolute")
if ($PSCmdlet.ParameterSetName -eq "relativeTime") {
$templateChart.timeContext | Add-Member -MemberType NoteProperty -Name "relative" -Value ([psCustomObject]@{'duration'=$duration})
}
else {
$templateChart.timeContext | Add-Member -MemberType NoteProperty -Name "absolute" -Value ([psCustomObject]@{'startTime'=$startTime; 'endTime'=$endTime})
}
# set time granularity
$chartGrainMap = @{'Automatic' = 1; 'PT1M' = 2; 'PT1H' = 3; 'P1D' = 4; 'PT5M' = 7; 'PT15M' = 8; 'PT30M' = 9; 'PT6H' = 10; 'PT12H' = 11}
$templateChart.itemDataModel.appliedISOGrain = $templateChart.timeContext.options.appliedISOGrain = $timeGrain
$templateChart.timeContext.options.grain = $chartGrainMap.$timeGrain
$tileTemplate
}
<#
.SYNOPSIS
Get individual tile PsCustomObjects.
#>
function Get-TilePsCustomObject {
param (
[Parameter(Mandatory = $true)]
[psCustomObject]$tileTemplate,
[Parameter(Mandatory = $true)]
[ValidateSet("Throughput", "Count", "Latency", "Capacity")]
[string]$metricType,
[Parameter(Mandatory = $true)]
[string]$tileName,
[Parameter(Mandatory = $true)]
[string]$filterVolumeName,
[Parameter(Mandatory = $true)]
[int]$positionX,
[Parameter(Mandatory = $true)]
[int]$positionY
)
# deep copy
$tileTemplate = $tileTemplate | ConvertTo-Json -Depth 100 | ConvertFrom-Json
$templateChart = $tileTemplate.metadata.inputs[0].value.charts[0]
#change aggregation type
$aggregationTypeofMetric = @{'Throughput' = 'Sum'; 'Count' = 'Sum'; 'Latency' = 'Avg'; 'Capacity' = 'Sum'}
$aggregationType = $aggregationTypeofMetric.$metricType
if ($aggregationType -eq "Sum") {
$templateChart.metrics | ForEach-Object { $_.aggregationType = 4 }
$templateChart.itemDataModel.metrics | ForEach-Object {$_.metricAggregation = 1 }
}
else {
$templateChart.metrics | ForEach-Object { $_.aggregationType = 1 }
$templateChart.itemDataModel.metrics | ForEach-Object {$_.metricAggregation = 4 }
}
switch ($metricType) {
Capacity {
}
Default {
#change metrics name
$templateChart.metrics[0].name = $templateChart.itemDataModel.metrics[0].id.name.id = "VolumeOperations" + $( if ($metricType -eq "Count") {""} else {$metricType} ) + "Read"
$templateChart.metrics[1].name = $templateChart.itemDataModel.metrics[1].id.name.id = "VolumeOperations" + $( if ($metricType -eq "Count") {""} else {$metricType} ) + "Write"
$templateChart.itemDataModel.metrics[0].id.name.displayName = $metricType + "Read"
$templateChart.itemDataModel.metrics[1].id.name.displayName = $metricType + "Write"
}
}
#change tile name
$templateChart.title = $templateChart.itemDataModel.title = $tileName
#change filter
$templateChart.itemDataModel.filters.OperandFilters[0].OperandSelectedValues[0] = $filterVolumeName
# change position
$tileTemplate.position.x = $positionX
$tileTemplate.position.Y = $positionY
$tileTemplate
}
<#
.SYNOPSIS
Get dashboard json in PsCustomObject format.
#>
function Get-DashboardVolumesJson {
[CmdletBinding(DefaultParameterSetName="relativeTime")]
param (
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[array]$volumes = ( Get-AzureStackVolumes ),
[Parameter(Mandatory = $false)]
[ValidateSet("ObjStore", "Infrastructure", "VmTemp")]
[string]$volumeType = "ObjStore",
[Parameter(ParameterSetName="relativeTime")]
[Double]$duration = 86400000,
[Parameter(ParameterSetName="absoluteTime")]
[string]$startTime,
[Parameter(ParameterSetName="absoluteTime")]
[string]$endTime,
[Parameter(Mandatory = $false)]
[string]$timeGrain = "PT15M",
[Parameter(Mandatory = $false)]
[string]$description = ""
)
if ($volumes.Count -eq 0) {
return
}
$volumesByType = Get-volumesByType -volumes $volumes
Write-Host "Generating dashboard json."
$resourceId = ( Get-AzureStackResourceId )
$dashboardBody = $Script:dashboardBody.Replace("<resourceIdToBeReplaced>", '/' + $resourceId) | ConvertFrom-Json
# change dashboard title/name
$dashboardBody.name = $dashboardBody.tags."hidden-title" = $volumeType + " Volumes Operation Performance"
$Templates = @{}
$metricTypes | ForEach-Object {
if ($PSCmdlet.ParameterSetName -eq "relativeTime") {
$Templates[$_] = Initialize-TilePsCustomObject -metricType $_ -duration $duration -timeGrain $timeGrain -resourceId $resourceId
}
else {
$Templates[$_] = Initialize-TilePsCustomObject -metricType $_ -startTime $startTime -endTime $endTime -timeGrain $timeGrain -resourceId $resourceId
}
}
# deprecated tiles
# set markDown board content
# $dashboardBody.properties.lenses."0".parts."0".metadata.settings.content.settings.content += $aggregationType + " Volume Operations " + $metricType + " by `n" + $description
# the tile of total performance
# $tileJson = $tileTemplate | ConvertTo-Json -depth 100 | ConvertFrom-Json
# $tileJson.metadata.inputs[0].value.charts[0].itemDataModel.psobject.properties.remove("filters")
# $dashboardBody.properties.lenses."0".parts | Add-Member -MemberType NoteProperty -Name "1" -Value $tileJson
$dashboardBody.properties.lenses."0".parts = [PSCustomObject]@{}
# create tiles
for ($($rowNum = 0; $tileNum = 0); $rowNum -lt $volumesByType.$volumeType.Count; $rowNum++) {
$positionY = $tileRowSpan * $rowNum
for ($colNum = 0; $colNum -lt $metricTypes.Count; $colNum++) {
$posotionX = $tileColSpan * $colNum
$thisMetricType = $metricTypes[$colNum]
$tileName = $volumesByType.$volumeType[$rowNum].Item2 + " " + $( if ($thisMetricType -eq "Count") {"Operation"} else {""} ) + $metricTypes[$colNum]
$filterVolumeName = $( if ($thisMetricType -eq "Capacity") { $volumesByType.$volumeType[$rowNum].Item2 } else { $volumesByType.$volumeType[$rowNum].Item1 } )
$tileJsonObj = Get-TilePsCustomObject -tileTemplate $Templates[$metricTypes[$colNum]] -tileName $tileName -positionX $posotionX -positionY $positionY -filterVolumeName $filterVolumeName -metricType $thisMetricType
# $chart.itemDataModel.filters.OperandFilters[0].OperandSelectedValues[0] = $volumesByType.($volumeTypes[$rowNum])[$colNum].Item1
$dashboardBody.properties.lenses."0".parts | Add-Member -MemberType NoteProperty -Name $tileNum -Value $tileJsonObj
$tileNum++
}
}
$dashboardBody
}
#========Module Initalize========#
$context = $null
#size of tile
$tileColSpan = 6
$tileRowSpan = 4
# If you want to add new metrics, adapt function Initialize-TilePsCustomObject and Get-TilePsCustomObject, then register here
$metricTypes = @('Throughput', 'Count', 'Latency', 'Capacity')
if (!((Test-Path -Path ($jsonTemplateLocation.TrimEnd('\') + '\dashboardBody.json')) -and (Test-Path -Path ($jsonTemplateLocation.TrimEnd('\') + '\tileTemplate.json' )))) {
throw "Template location not exist."
}
try {
$dashboardBody = Get-Content ($jsonTemplateLocation.TrimEnd('\') + '\dashboardBody.json') | Out-String
$tileTemplate = Get-Content ($jsonTemplateLocation.TrimEnd('\') + '\tileTemplate.json') | Out-String
$capacityTemplate = Get-Content ($jsonTemplateLocation.TrimEnd('\') + '\capacityTemplate.json') | Out-String
}
catch {
Write-Error $_
throw "Template not exist"
}
if ($PSCmdlet.ParameterSetName -eq "absoluteTime") {
Save-AzureStackVolumesPerformanceDashboardJson -DefaultProfile $DefaultProfile -startTime $startTime -endTime $endTime -timeGrain $timeGrain -outputLocation $outputLocation
}
else {
Save-AzureStackVolumesPerformanceDashboardJson -DefaultProfile $DefaultProfile -duration $duration -timeGrain $timeGrain -outputLocation $outputLocation
}