-
Notifications
You must be signed in to change notification settings - Fork 687
/
collectBlockchainWorkbenchTroubleshooting.ps1
743 lines (573 loc) · 28.4 KB
/
collectBlockchainWorkbenchTroubleshooting.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<#
.SYNOPSIS
Collects logs and metrics from an Azure Blockchain Workbench instance for
troubleshooting
.DESCRIPTION
Collects logs and metrics from an Azure Blockchain Workbench instance for
troubleshooting. This script will prompt for Azure authentication if you
are not already logged in.
NOTE: If you don't have the latest Azure Powershell installed on your machine,
we recommend that you use the MSI installer to get the latest at
https://github.com/Azure/azure-powershell/releases. Or, run this script using
Azure Cloud shell at https://shell.azure.com.
.PARAMETER SubscriptionID
SubscriptionID to create or locate all resources.
.PARAMETER ResourceGroupName
Name of the Azure Resource Group where Blockchain Workbench has been deployed to.
.PARAMETER OutputDirectory
Path to create the output .ZIP file. If not specified, defaults to the current
directory
.PARAMETER LookbackHours
Number of hours to use when pulling telemetry. Default to 24 hours. Maximum
value is 90 hours.
.PARAMETER OmsSubscriptionId
The subscription id where OMS is deployed. Only pass this parameter if the OMS
for the blockchain network is deployed outside of Blockchain Workbench's resource
group.
.PARAMETER OmsResourceGroup
The resource group where OMS is deployed. Only pass this parameter if the OMS
for the blockchain network is deployed outside of Blockchain Workbench's resource
group.
.PARAMETER OmsWorkspaceName
The OMS workspace name. Only pass this parameter if the OMS for the blockchain
network is deployed outside of Blockchain Workbench's resource group.
.INPUTS
None. You cannot pipe objects to this script.
.OUTPUTS
None. This script does not generate any output. It creates a .ZIP file which
contains a summary file with a "top errors" report, last timestamp
information for each Workbench microservice, and recommended actions.
Following that, it creates two subfolders, "details" and "metrics" for futher
troubleshooting.
.EXAMPLE
C:\tmp> .\collectBlockchainWorkbenchTroubleshooting.ps1 -SubscriptionID "<subscription_id>" -ResourceGroupName "<workbench-resource-group-name>"
#>
param(
[Parameter(Mandatory=$true)][string]$SubscriptionID,
[Parameter(Mandatory=$true)][string]$ResourceGroupName,
[Parameter(Mandatory=$false)][string]$OutputDirectory,
[Parameter(Mandatory=$false)][int]$LookbackHours = 24,
[Parameter(Mandatory=$false)][string]$OmsSubscriptionId,
[Parameter(Mandatory=$false)][string]$OmsResourceGroup,
[Parameter(Mandatory=$false)][string]$OmsWorkspaceName
)
##############################################
## AppInsights query - Helper Functions
##############################################
function Get-AzureRmCachedAccessToken()
{
$ErrorActionPreference = 'Stop'
if(-not (Get-Module AzureRm.Profile)) {
Import-Module AzureRm.Profile
}
$azureRmProfileModuleVersion = (Get-Module AzureRm.Profile).Version
# refactoring performed in AzureRm.Profile v3.0 or later
if($azureRmProfileModuleVersion.Major -ge 3) {
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Accounts.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
} else {
# AzureRm.Profile < v3.0
$azureRmProfile = [Microsoft.WindowsAzure.Commands.Common.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Context.Account.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
}
$currentAzureContext = Get-AzureRmContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
Write-Debug ("Getting access token for tenant" + $currentAzureContext.Subscription.TenantId)
$token = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId)
$token.AccessToken
}
function Execute-InsightsQuery([string]$subscription, [string]$resourceGroup, [string]$aiName, [string]$query)
{
$queryUrlEscaped = [uri]::EscapeDataString($query)
$requestUrl = "https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/microsoft.insights/components/{2}/api/query?query={3}&api-version=2015-05-01" -f $subscription, $resourceGroup, $aiName, $queryUrlEscaped
$bearer = Get-AzureRmCachedAccessToken
Invoke-RestMethod -Uri $requestUrl -Headers @{'Authorization' = 'Bearer ' + $bearer }
}
function RestQueryResultsToObjectView($restResponse){
$table = $restResponse.Tables[0]
$i = 0;
$objectView = New-Object object[] $table.Rows.Count
foreach ($row in $table.Rows)
{
$properties = @{}
for ($columnNum=0; $columnNum -lt $table.Columns.Count; $columnNum++) {
$properties[$table.Columns[$columnNum].ColumnName] = $row[$columnNum]
}
$objectView[$i] = (New-Object PSObject -Property $properties)
$null = $i++
}
$objectView
}
function ZipFiles( $zipfilename, $sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
$zipfilename, $compressionLevel, $false)
}
##############################################
## Log anaylitcs query - Helper Functions
##############################################
$apiVersion = "2017-01-01-preview"
<#
.DESCRIPTION
Invokes a query against the Log Analtyics Query API.
.EXAMPLE
Invoke-LogAnaltyicsQuery -WorkspaceName my-workspace -SubscriptionId 0f991b9d-ab0e-4827-9cc7-984d7319017d -ResourceGroup my-resourcegroup
-Query "union * | limit 1" -CreateObjectView
.PARAMETER WorkspaceName
The name of the Workspace to query against.
.PARAMETER SubscriptionId
The ID of the Subscription this Workspace belongs to.
.PARAMETER ResourceGroup
The name of the Resource Group this Workspace belongs to.
.PARAMETER Query
The query to execute.
.PARAMETER Timespan
The timespan to execute the query against. This should be an ISO 8601 timespan.
.PARAMETER IncludeTabularView
If specified, the raw tabular view from the API will be included in the response.
.PARAMETER IncludeStatistics
If specified, query statistics will be included in the response.
.PARAMETER IncludeRender
If specified, rendering statistics will be included (useful when querying metrics).
.PARAMETER ServerTimeout
Specifies the amount of time (in seconds) for the server to wait while executing the query.
.PARAMETER Environment
Internal use only.
#>
function Invoke-LogAnalyticsQuery {
param(
[string]
[Parameter(Mandatory=$true)]
$WorkspaceName,
[guid]
[Parameter(Mandatory=$true)]
$SubscriptionId,
[string]
[Parameter(Mandatory=$true)]
$ResourceGroup,
[string]
[Parameter(Mandatory=$true)]
$Query,
[string]
$Timespan,
[switch]
$IncludeTabularView,
[switch]
$IncludeStatistics,
[switch]
$IncludeRender,
[int]
$ServerTimeout,
[string]
[ValidateSet("", "int", "aimon")]
$Environment = ""
)
$ErrorActionPreference = "Stop"
$accessToken = GetAccessToken
$armhost = GetArmHost $environment
$queryParams = @("api-version=$apiVersion")
$queryParamString = [string]::Join("&", $queryParams)
$uri = BuildUri $armHost $subscriptionId $resourceGroup $workspaceName $queryParamString
$body = @{
"query" = $query;
"timespan" = $Timespan
} | ConvertTo-Json
$headers = GetHeaders $accessToken -IncludeStatistics:$IncludeStatistics -IncludeRender:$IncludeRender -ServerTimeout $ServerTimeout
$response = Invoke-WebRequest -UseBasicParsing -Uri $uri -Body $body -ContentType "application/json" -Headers $headers -Method Post
if ($response.StatusCode -ne 200 -and $response.StatusCode -ne 204) {
$statusCode = $response.StatusCode
$reasonPhrase = $response.StatusDescription
$message = $response.Content
throw "Failed to execute query.`nStatus Code: $statusCode`nReason: $reasonPhrase`nMessage: $message"
}
$data = $response.Content | ConvertFrom-Json
$result = New-Object PSObject
$result | Add-Member -MemberType NoteProperty -Name Response -Value $response
# In this case, we only need the response member set and we can bail out
if ($response.StatusCode -eq 204) {
$result
return
}
$objectView = CreateObjectView $data
$result | Add-Member -MemberType NoteProperty -Name Results -Value $objectView
if ($IncludeTabularView) {
$result | Add-Member -MemberType NoteProperty -Name Tables -Value $data.tables
}
if ($IncludeStatistics) {
$result | Add-Member -MemberType NoteProperty -Name Statistics -Value $data.statistics
}
if ($IncludeRender) {
$result | Add-Member -MemberType NoteProperty -Name Render -Value $data.render
}
$result
}
function GetAccessToken {
$azureCmdlet = get-command -Name Get-AzureRMContext -ErrorAction SilentlyContinue
if ($azureCmdlet -eq $null)
{
$null = Import-Module AzureRM -ErrorAction Stop;
}
$AzureContext = & "Get-AzureRmContext" -ErrorAction Stop;
$authenticationFactory = New-Object -TypeName Microsoft.Azure.Commands.Common.Authentication.Factories.AuthenticationFactory
if ((Get-Variable -Name PSEdition -ErrorAction Ignore) -and ('Core' -eq $PSEdition)) {
[Action[string]]$stringAction = {param($s)}
$serviceCredentials = $authenticationFactory.GetServiceClientCredentials($AzureContext, $stringAction)
} else {
$serviceCredentials = $authenticationFactory.GetServiceClientCredentials($AzureContext)
}
# We can't get a token directly from the service credentials. Instead, we need to make a dummy message which we will ask
# the serviceCredentials to add an auth token to, then we can take the token from this message.
$message = New-Object System.Net.Http.HttpRequestMessage -ArgumentList @([System.Net.Http.HttpMethod]::Get, "http://foobar/")
$cancellationToken = New-Object System.Threading.CancellationToken
$null = $serviceCredentials.ProcessHttpRequestAsync($message, $cancellationToken).GetAwaiter().GetResult()
$accessToken = $message.Headers.GetValues("Authorization").Split(" ")[1] # This comes out in the form "Bearer <token>"
$accessToken
}
function GetArmHost {
param(
[string]
$environment
)
switch ($environment) {
"" {
$armHost = "management.azure.com"
}
"aimon" {
$armHost = "management.azure.com"
}
"int" {
$armHost = "api-dogfood.resources.windows-int.net"
}
}
$armHost
}
function BuildUri {
param(
[string]
$armHost,
[string]
$subscriptionId,
[string]
$resourceGroup,
[string]
$workspaceName,
[string]
$queryParams
)
"https://$armHost/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/" + `
"microsoft.operationalinsights/workspaces/$workspaceName/api/query?$queryParamString"
}
function GetHeaders {
param(
[string]
$AccessToken,
[switch]
$IncludeStatistics,
[switch]
$IncludeRender,
[int]
$ServerTimeout
)
$preferString = "response-v1=true"
if ($IncludeStatistics) {
$preferString += ",include-statistics=true"
}
if ($IncludeRender) {
$preferString += ",include-render=true"
}
if ($ServerTimeout -ne $null) {
$preferString += ",wait=$ServerTimeout"
}
$headers = @{
"Authorization" = "Bearer $accessToken";
"prefer" = $preferString;
"x-ms-app" = "LogAnalyticsQuery.psm1";
"x-ms-client-request-id" = [Guid]::NewGuid().ToString();
}
$headers
}
function CreateObjectView {
param(
$data
)
# Find the number of entries we'll need in this array
$count = 0
foreach ($table in $data.Tables) {
$count += $table.Rows.Count
}
$objectView = New-Object object[] $count
$i = 0;
foreach ($table in $data.Tables) {
foreach ($row in $table.Rows) {
# Create a dictionary of properties
$properties = @{}
for ($columnNum=0; $columnNum -lt $table.Columns.Count; $columnNum++) {
$properties[$table.Columns[$columnNum].name] = $row[$columnNum]
}
# Then create a PSObject from it. This seems to be *much* faster than using Add-Member
$objectView[$i] = (New-Object PSObject -Property $properties)
$null = $i++
}
}
$objectView
}
$logId = 0
#############################################
# Script Initialization
#############################################
Write-Progress -Id $logId -Activity "Login & Setup" -Status "Login to Azure" -PercentComplete 0
if (($LookbackHours -lt 1) -or ($LookbackHours -gt 90))
{
throw "Please limit the number of hours of telemetry to pull to a value between 1 and 90."
}
$LookbackHoursStr = "$LookbackHours" + "h"
if ((Get-Command "Login-AzureRmAccount" -errorAction SilentlyContinue) -eq $null)
{
throw "Azure Powershell cmdlets were not detected. We recommend that you use the MSI installer to get the latest at
https://github.com/Azure/azure-powershell/releases. Or, run this script using
Azure Cloud shell at https://shell.azure.com."
}
$context = Get-AzureRmContext
if ($context -eq $null -or ($context.Name -eq "Default"))
{
$account = Login-AzureRmAccount -SubscriptionId $SubscriptionID
if ($account -eq $null)
{
throw "Failed to login to Azure. Please try to login again."
}
}
Write-Progress -Id $logId -Activity "Login & Setup" -Status "Loading Azure Resources" -PercentComplete 35
$rg = Get-AzureRmResourceGroup -Name $ResourceGroupName
if ($rg -eq $null)
{
throw "We couldn't locate the resource group $ResourceGroupName. Please check the name and try again"
}
# Locate Resources in Blockchain Workbench deployment
$appInsightsResource = Get-AzureRmApplicationInsights -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue
if ($appInsightsResource -eq $null)
{
throw "Could not locate App Insights within the resource group $ResourceGroupName. Is this a Blockchain Workbench deployment?"
}
$appInsightsResource = $appInsightsResource[0] # Select the App Insights
$websites = Get-AzureRmWebApp -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue
if ($websites -eq $null)
{
throw "Could not locate App Service within the resource group $ResourceGroupName. Is this a Blockchain Workbench deployment?"
}
$apiWebsite = ($websites | Where-Object { $_.SiteName -like "*-api" })[0] # Select the Workbench API
if ($apiWebsite -eq $null)
{
throw "Could not locate API App Service within the resource group $ResourceGroupName. Is this a Blockchain Workbench deployment?"
}
$serviceBus = Get-AzureRmServiceBusNamespace -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue
if ($serviceBus -eq $null)
{
throw "Could not locate Service Bus within the resource group $ResourceGroupName. Is this a Blockchain Workbench deployment?"
}
$serviceBus = $serviceBus[0]
$logAnalytics = Find-AzureRmResource -ResourceGroupNameEquals $ResourceGroupName -ResourceType "Microsoft.OperationalInsights/workspaces" -ErrorAction SilentlyContinue
if ($logAnalytics -ne $null)
{
$logAnalytics = $logAnalytics[0]
$OmsSubscriptionId = $SubscriptionID
$OmsResourceGroup = $ResourceGroupName
$OmsWorkspaceName = $logAnalytics.Name
}
else
{
# Fall back to manually specified OMS if it's not in the same resource group
if ($OmsSubscriptionId -ne $null -and $OmsResourceGroup -ne $null -and $OmsWorkspaceName -ne $null)
{
$logAnalytics = Find-AzureRmResource -ResourceGroupNameEquals $OmsResourceGroup -ResourceNameEquals $OmsWorkspaceName -ResourceType "Microsoft.OperationalInsights/workspaces" -ErrorAction SilentlyContinue
if ($logAnalytics -ne $null)
{
$logAnalytics = $logAnalytics[0]
}
}
}
Write-Progress -Id $logId -Activity "Login & Setup" -Status "Login to Azure" -PercentComplete 100
#############################################
# Create Temporary Directory
#############################################
$logId++
Write-Progress -Id $logId -Activity "Create Temporary Directory" -Status "Creating Temporary Directories" -PercentComplete 0
$tempPath = [System.IO.Path]::GetTempPath()
$guidDir = [System.Guid]::NewGuid()
$outputPath = Join-Path $tempPath $guidDir
New-Item -ItemType Directory -Path $outputPath | Out-Null
New-Item -ItemType Directory -Path $outputPath\details\blockchain | Out-Null
New-Item -ItemType Directory -Path $outputPath\details\workbench | Out-Null
New-Item -ItemType Directory -Path $outputPath\metrics\workbench | Out-Null
New-Item -ItemType Directory -Path $outputPath\metrics\blockchain | Out-Null
if ([string]::IsNullOrEmpty($OutputDirectory))
{
$OutputDirectory = (Get-Item -Path ".\" -Verbose).FullName
}
Write-Progress -Id $logId -Activity "Create Temporary Directory" -Status "Created Temporary Directories" -PercentComplete 100
#############################################
# Create Summary File
#############################################
$logId++
Write-Progress -Id $logId -Activity "Summary Report" -Status "Generating summary report" -PercentComplete 0
$summaryFile = Join-Path $outputPath "summary.txt"
$lastEvent = Execute-InsightsQuery -subscription $SubscriptionID -resourceGroup $ResourceGroupName -aiName $appInsightsResource.Name -query "traces
| where timestamp > ago($LookbackHoursStr)
| evaluate bag_unpack(customDimensions)
| summarize LastLog = max(timestamp) by ServiceName"
$topErrors = Execute-InsightsQuery -subscription $SubscriptionID -resourceGroup $ResourceGroupName -aiName $appInsightsResource.Name -query "exceptions
| where timestamp > ago($LookbackHoursStr)
| reduce by outerMessage"
Add-Content $summaryFile "Summary Report Output"
Add-Content $summaryFile "====================="
Add-Content $summaryFile ""
$runningServices = RestQueryResultsToObjectView($lastEvent) | select ServiceName | foreach { $_.ServiceName }
$services = @("sql-consumer", "dlt-consumer", "appbuilder.api", "dlt-native-api", "key-service", "dlt-api", "dlt-watcher")
Add-Content $summaryFile "[1] Recommended Actions"
if($services.Count -ne $runningServices.Count)
{
Add-Content $summaryFile (" * Did not detect heartbeat from the following services: " + ((Compare-Object $runningServices $services) | select -ExpandProperty InputObject) -join ",")
}
Add-Content $summaryFile ""
Add-Content $summaryFile "[2] Last Telemetry Timestamp, Per Component"
RestQueryResultsToObjectView($lastEvent) | format-table | out-file $summaryFile -Append utf8
Add-Content $summaryFile ""
Add-Content $summaryFile ""
Add-Content $summaryFile "[3] Top Errors, Last 18 hours"
RestQueryResultsToObjectView($topErrors) | format-table -AutoSize | out-file $summaryFile -Append utf8
Add-Content $summaryFile ""
Add-Content $summaryFile ""
Write-Progress -Id $logId -Activity "Summary reporting" -Status "Generated summary report" -PercentComplete 100
#############################################
# Collect Exceptions
#############################################
$logId++
Write-Progress -Id $logId -Activity "Workbench Log Collection" -Status "Collecing Exceptions" -PercentComplete 0
$exceptionsPath = Join-Path $outputPath "\details\workbench\exceptions.csv"
$lastExceptions = Execute-InsightsQuery -subscription $SubscriptionID -resourceGroup $ResourceGroupName -aiName $appInsightsResource.Name -query "exceptions
| order by timestamp desc
| take 500"
RestQueryResultsToObjectView($lastExceptions) | ConvertTo-Csv | out-file $exceptionsPath -Append utf8
#############################################
# Collect API Metrics
#############################################
Write-Progress -Id $logId -Activity "Workbench Log Collection" -Status "Collecting API Metrics" -PercentComplete 25
$apiMetrics = Join-Path $outputPath "\metrics\workbench\apiMetrics.txt"
$http5xx = Get-AzureRmMetric -ResourceId $apiWebsite.Id -MetricName Http5xx -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$http4xx = Get-AzureRmMetric -ResourceId $apiWebsite.Id -MetricName Http4xx -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$http3xx = Get-AzureRmMetric -ResourceId $apiWebsite.Id -MetricName Http3xx -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$http2xx = Get-AzureRmMetric -ResourceId $apiWebsite.Id -MetricName Http2xx -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$requests = Get-AzureRmMetric -ResourceId $apiWebsite.Id -MetricName Requests -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$avgRespTime = Get-AzureRmMetric -ResourceId $apiWebsite.Id -MetricName AverageResponseTime -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 00:01:00 -WarningAction Ignore
Add-Content $apiMetrics "Web API Metrics"
Add-Content $apiMetrics "====================="
Add-Content $apiMetrics ""
Add-Content $apiMetrics "[1] Requests 5xx (total/hour)"
$http5xx.Data | format-table | out-file $apiMetrics -Append utf8
Add-Content $apiMetrics ""
Add-Content $apiMetrics "[2] Requests 4xx (total/hour)"
$http4xx.Data | format-table | out-file $apiMetrics -Append utf8
Add-Content $apiMetrics ""
Add-Content $apiMetrics "[3] Requests 3xx (total/hour)"
$http3xx.Data | format-table | out-file $apiMetrics -Append utf8
Add-Content $apiMetrics ""
Add-Content $apiMetrics "[4] Requests 2xx (total/hour)"
$http2xx.Data | format-table | out-file $apiMetrics -Append utf8
Add-Content $apiMetrics ""
Add-Content $apiMetrics "[5] Requests - Total (total/hour)"
$requests.Data | format-table | out-file $apiMetrics -Append utf8
Add-Content $apiMetrics ""
Add-Content $apiMetrics "[6] Average Response Time (avg/min)"
$avgRespTime.Data | format-table | out-file $apiMetrics -Append utf8
Add-Content $apiMetrics ""
#############################################
# Collect Service Bus Metrics
#############################################
Write-Progress -Id $logId -Activity "Workbench Log Collection" -Status "Collecting Service Bus Metrics" -PercentComplete 45
$sbMetrics = Join-Path $outputPath "\metrics\workbench\sbMetrics.txt"
$incomingMessages = Get-AzureRmMetric -ResourceId $serviceBus.Id -MetricName IncomingMessages -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$outgoingMeesages = Get-AzureRmMetric -ResourceId $serviceBus.Id -MetricName OutgoingMessages -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$serverErrors = Get-AzureRmMetric -ResourceId $serviceBus.Id -MetricName ServerErrors -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
$requests = Get-AzureRmMetric -ResourceId $serviceBus.Id -MetricName IncomingRequests -StartTime (get-date).AddHours(0 - $LookbackHours) -TimeGrain 01:00:00 -WarningAction Ignore
Add-Content $sbMetrics "Service Bus Metrics"
Add-Content $sbMetrics "====================="
Add-Content $sbMetrics ""
Add-Content $sbMetrics "[1] Incoming Messages"
$incomingMessages.Data | format-table | out-file $sbMetrics -Append utf8
Add-Content $sbMetrics "[2] Outgoing Messages"
$outgoingMeesages.Data | format-table | out-file $sbMetrics -Append utf8
Add-Content $sbMetrics "[3] Server Errors"
$serverErrors.Data | format-table | out-file $sbMetrics -Append utf8
Add-Content $sbMetrics "[4] Incoming Requests"
$requests.Data | format-table | out-file $sbMetrics -Append utf8
#############################################
# Collect Availability Results
#############################################
Write-Progress -Id $logId -Activity "Workbench Log Collection" -Status "Collecting Availability Metrics" -PercentComplete 55
$availabilityTest = Join-Path $outputPath "\details\workbench\availabilityTest.csv"
$availability = Execute-InsightsQuery -subscription $SubscriptionID -resourceGroup $ResourceGroupName -aiName $appInsightsResource.Name -query "availabilityResults
| where timestamp > ago($LookbackHoursStr)"
RestQueryResultsToObjectView($availability) | ConvertTo-Csv | out-file $availabilityTest -Append utf8
#############################################
# Collect microservices logs
#############################################
Write-Progress -Id $logId -Activity "Workbench Log Collection" -Status "Collecting Services Logs" -PercentComplete 75
foreach ($service in $services)
{
$microserviceFile = Join-Path $outputPath "\details\workbench\servicelogs_$service.csv"
$logs = Execute-InsightsQuery -subscription $SubscriptionID -resourceGroup $ResourceGroupName -aiName $appInsightsResource.Name -query "traces
| where timestamp > ago($LookbackHoursStr)
| where customDimensions contains '$service'
| take 1500"
RestQueryResultsToObjectView($logs) | ConvertTo-Csv | out-file $microserviceFile -Append utf8
}
Write-Progress -Id $logId -Activity "Workbench Log Collection" -Status "Collected Workbench logs" -PercentComplete 100
if ($logAnalytics -ne $null)
{
$logId++
Write-Progress -Id $logId -Activity "Blockchain Network Log Collection" -Status "Collecting Blockchain Network Logs" -PercentComplete 0
#############################################
# Collect Blockchain network - Mined Transactions
#############################################
$networkMinedTxns = Join-Path $outputPath "\details\blockchain\network_mined_transaction.csv"
$networkMinedTxnsResult = Invoke-LogAnalyticsQuery -SubscriptionId $logAnalytics.SubscriptionId -ResourceGroup $logAnalytics.ResourceGroupName -WorkspaceName $logAnalytics.ResourceName -Query "MinedTransaction_CL
| take 500"
$networkMinedTxnsResult.Results | ConvertTo-Csv | out-file $networkMinedTxns -Append utf8
Write-Progress -Id $logId -Activity "Blockchain Network Log Collection" -Status "Collecting Blockchain Network Logs" -PercentComplete 25
#############################################
# Collect Blockchain network - Pending Transactions
#############################################
$networkPendingTxns = Join-Path $outputPath "\details\blockchain\blockchainnetwork_pending_transaction.csv"
$networkpendingTxnsResult = Invoke-LogAnalyticsQuery -SubscriptionId $logAnalytics.SubscriptionId -ResourceGroup $logAnalytics.ResourceGroupName -WorkspaceName $logAnalytics.ResourceName -Query "PendingTransaction_CL
| take 500"
$networkpendingTxnsResult.Results | ConvertTo-Csv | out-file $networkPendingTxns -Append utf8
Write-Progress -Id $logId -Activity "Blockchain Network Log Collection" -Status "Collecting Blockchain Network Logs" -PercentComplete 50
#############################################
# Collect Blockchain network - Mined Blocks
#############################################
$networkMinedBlocks = Join-Path $outputPath "\details\blockchain\blockchainnetwork_mined_blocks.csv"
$networkMinedblocksResult = Invoke-LogAnalyticsQuery -SubscriptionId $logAnalytics.SubscriptionId -ResourceGroup $logAnalytics.ResourceGroupName -WorkspaceName $logAnalytics.ResourceName -Query "MinedBlock_CL
| take 500"
$networkMinedblocksResult.Results | ConvertTo-Csv | out-file $networkMinedBlocks -Append utf8
Write-Progress -Id $logId -Activity "Blockchain Network Log Collection" -Status "Collecting Blockchain Network Logs" -PercentComplete 75
#############################################
# Collect Blockchain network - Perf Counters
#############################################
$networkPerf = Join-Path $outputPath "\metrics\blockchain\network_perf_counters.csv"
$networkPerfResult = Invoke-LogAnalyticsQuery -SubscriptionId $logAnalytics.SubscriptionId -ResourceGroup $logAnalytics.ResourceGroupName -WorkspaceName $logAnalytics.ResourceName -Query "Perf
| where TimeGenerated > ago($LookbackHoursStr)
| summarize min(CounterValue), max(CounterValue), avg(CounterValue), percentiles_array(CounterValue, 50, 90, 95), stdev(CounterValue) by Computer, CounterPath, bin(TimeGenerated, 5m)
| order by Computer, CounterPath, TimeGenerated"
Write-Progress -Id $logId -Activity "Blockchain Network Log Collection" -Status "Collecting Blockchain Network Logs" -PercentComplete 100
}
#############################################
# Create final zip
#############################################
$timestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
$finalPath = Join-Path $OutputDirectory "\workbench-logs-$timestamp.zip"
ZipFiles $finalPath $outputPath