-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathUploadGatewayLogs.ps1
268 lines (183 loc) · 9.18 KB
/
UploadGatewayLogs.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
param(
[psobject]$config
,
[string]$stateFilePath
)
function ProcessLogFiles ($logFiles, $storagePath, $executionDate, $tempPath)
{
Write-Host "Files modified since last run: $($logFiles.Count)"
foreach ($logFile in $logFiles) {
$storagePathTemp = $storagePath
# Try to parse the date out of the file name
if ($executionDate)
{
$dateRegExMatches = ($logFile.Name | Select-String -pattern ".+(\d{8}).\d+").Matches
if ($dateRegExMatches)
{
$fileDateStr = $dateRegExMatches.Groups[1].Value
$fileDate = [datetime]::ParseExact($fileDateStr, "yyyyMMdd", [Globalization.CultureInfo]::InvariantCulture)
}
else
{
$fileDate = $executionDate
}
if ($fileDate)
{
$storagePathTemp = ("$storagePath/{0:yyyy}/{0:MM}/{0:dd}" -f $fileDate)
}
}
if ($tempPath)
{
# Local copy the file, because it could be blocked by Gateway
Write-Host "Copying file: '$($logFile.FullName)' to '$tempPath'"
$fileOutputPath = "$tempPath\$($storagePathTemp.Replace("/", "\"))\$($logFile.Name)"
New-Item -ItemType Directory -Path (Split-Path $fileOutputPath -Parent) -ErrorAction SilentlyContinue | Out-Null
Copy-Item -Path $logFile.FullName -Destination $fileOutputPath -Force
}
else{
$fileOutputPath = $logFile.FullName
}
if ($config.StorageAccountConnStr)
{
Write-Host "Sync '$fileOutputPath' to BlobStorage"
# Send to Storage
Add-FileToBlobStorage -storageRootPath $storagePathTemp -filePath $fileOutputPath -storageAccountConnStr $config.StorageAccountConnStr -storageContainerName $config.StorageAccountContainerName
# If storage account is configured and upload is done the file is deleted from the local file system
if ($tempPath -and $fileOutputPath)
{
Write-Host "Deleting local file copy: '$fileOutputPath'"
# Remove the local copy
Remove-Item $fileOutputPath -Force
}
}
}
}
try {
Write-Host "Upload - GatewayLogs Start"
$stopwatch = [System.Diagnostics.Stopwatch]::new()
$stopwatch.Start()
$runDate = [datetime]::UtcNow
$lastRunDate = $null
$localOutputPath = $config.OutputPath
# Ensure folders
@($localOutputPath) |% {
New-Item -ItemType Directory -Path $_ -ErrorAction SilentlyContinue | Out-Null
}
if (!$stateFilePath) {
$stateFilePath = ".\state.json"
}
if (Test-Path $stateFilePath) {
$state = Get-Content $stateFilePath | ConvertFrom-Json
}
else {
$state = New-Object psobject
}
if ($state.GatewayLogs.LastRun) {
if (!($state.GatewayLogs.LastRun -is [datetime])) {
$state.GatewayLogs.LastRun = [datetime]::Parse($state.GatewayLogs.LastRun).ToUniversalTime()
}
$lastRunDate = $state.GatewayLogs.LastRun
}
else {
$state | Add-Member -NotePropertyName "GatewayLogs" -NotePropertyValue @{"LastRun" = $null } -Force
}
Write-Host "LastRun: '$lastRunDate'"
if (!(Test-Path $config.GatewayLogsPath))
{
throw "Cannot find gateway logs path '$($config.GatewayLogsPath)' - https://docs.microsoft.com/en-us/data-integration/gateway/service-gateway-log-files"
}
foreach ($obj in $config.GatewayLogsPath) {
$gatewayProperties = @{
GatewayObjectId = $null
}
$gatewayClusters = $null
# Manual metadata
if ($obj -is [PSCustomObject])
{
$path = $obj.Path
$gatewayProperties.GatewayObjectId = $obj.GatewayId
$gatewayProperties.GatewayName = $obj.GatewayName
$gatewayProperties.GatewayCluster = $obj.GatewayCluster
$gatewayProperties.Server = $obj.Server
$gatewayProperties.Version = $obj.Version
$gatewayProperties.NumberOfCores = $obj.NumberOfCores
}
else {
$path = $obj
if (Test-Path "$path\GatewayProperties.txt")
{
$gatewayProperties = Get-Content "$path\GatewayProperties.txt" | ConvertFrom-Json
}
if (Test-Path "$path\GatewayClusters.txt")
{
$gatewayClusters = Get-Content "$path\GatewayClusters.txt" | ConvertFrom-Json
}
}
# If GatewayObjectId is not specified try to find it in the logs
if (!$gatewayProperties.GatewayObjectId)
{
$reportFile = Get-ChildItem -path $path -Recurse |? {$_.Name -ilike "*Report_*.log"} | Sort-Object Length | Select -first 1
if (!$reportFile)
{
throw "Cannot find any report ('*Report_*.log') file on '$path' to infer the GatewayId. Please ensure there is at least one report. If its a newly installed Gateway you may need to run a refresh and wait a couple of minutes."
}
$gatewayIdFromCSV = Get-Content -path $reportFile.FullName -First 2 | ConvertFrom-Csv | Select -ExpandProperty GatewayObjectId
$gatewayProperties.GatewayObjectId = $gatewayIdFromCSV
}
# Try to get the gateway core count
if (!$gatewayProperties.NumberOfCores -and !$gatewayProperties.CpuRelatedInfo)
{
try {
$serverCPU = (Get-ComputerInfo -Property CsProcessors).CsProcessors
$gatewayProperties.NumberOfCores = $serverCPU.NumberOfLogicalProcessors
}
catch {
Write-Warning "Error getting the server core count"
}
}
$gatewayId = $gatewayProperties.GatewayObjectId
if (!$gatewayId)
{
throw "Gateway Id is not defined."
}
$outputPathLogs = ("$($config.StorageAccountContainerRootPath)/{0:gatewayid}/logs" -f $gatewayId)
$outputPathMetadata = ("$($config.StorageAccountContainerRootPath)/{0:gatewayid}/metadata" -f $gatewayId)
$outputPathReports = ("$($config.StorageAccountContainerRootPath)/{0:gatewayid}/reports" -f $gatewayId)
# GatewayProperties json file
$gatewayMetadataFilePath = "$localOutputPath\$($config.StorageAccountContainerRootPath)\$gatewayId\metadata\GatewayProperties.json"
New-Item -ItemType Directory -Path (Split-Path $gatewayMetadataFilePath -Parent) -ErrorAction SilentlyContinue | Out-Null
ConvertTo-Json $gatewayProperties -Depth 5 | Out-File $gatewayMetadataFilePath -force -Encoding utf8
ProcessLogFiles -logFiles (Get-ChildItem $gatewayMetadataFilePath) -storagePath $outputPathMetadata
# GatewayClusters json file
if ($gatewayClusters)
{
$gatewayClustersFilePath = "$localOutputPath\$($config.StorageAccountContainerRootPath)\$gatewayId\metadata\GatewayClusters.json"
New-Item -ItemType Directory -Path (Split-Path $gatewayClustersFilePath -Parent) -ErrorAction SilentlyContinue | Out-Null
ConvertTo-Json $gatewayClusters -Depth 5 | Out-File $gatewayClustersFilePath -force -Encoding utf8
ProcessLogFiles -logFiles (Get-ChildItem $gatewayClustersFilePath) -storagePath $outputPathMetadata
}
# Gateway Logs
$logFiles = @(Get-ChildItem -File -Path "$path\*.log" -ErrorAction SilentlyContinue)
Write-Host "Gateway log count: $($logFiles.Count)"
$logFiles = @($logFiles | ? { $_.LastWriteTimeUtc -gt $lastRunDate -or $lastRunDate -eq $null })
ProcessLogFiles -logFiles $logFiles -storagePath $outputPathLogs -executionDate $runDate -tempPath $localOutputPath
# Gateway Reports
$logFiles = @(Get-ChildItem -File -Path "$path\*Report_*.log" -Recurse -ErrorAction SilentlyContinue)
Write-Host "Gateway Report log count: $($logFiles.Count)"
$logFiles = @($logFiles | ? { $_.LastWriteTimeUtc -gt $lastRunDate -or $lastRunDate -eq $null })
ProcessLogFiles -logFiles $logFiles -storagePath $outputPathReports -tempPath $localOutputPath
# Gateway Config Properties
$logFiles = @(Get-ChildItem -File -Path "$path\*ConfigurationProperties.json" -ErrorAction SilentlyContinue)
Write-Host "Gateway Config file count: $($logFiles.Count)"
$logFiles = @($logFiles | ? { $_.LastWriteTimeUtc -gt $lastRunDate -or $lastRunDate -eq $null })
ProcessLogFiles -logFiles $logFiles -storagePath $outputPathMetadata -tempPath $localOutputPath
}
# Save state
$state.GatewayLogs.LastRun = $runDate.ToString("o")
New-Item -Path (Split-Path $stateFilePath -Parent) -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
ConvertTo-Json $state | Out-File $stateFilePath -force -Encoding utf8
}
finally {
$stopwatch.Stop()
Write-Host "Ellapsed: $($stopwatch.Elapsed.TotalSeconds)s"
}