-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrabbitmq-collect-env.ps1
378 lines (328 loc) · 13.9 KB
/
rabbitmq-collect-env.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
param(
[switch]$Debug = $false,
[switch]$Verbose = $false,
[int]$DebugLevel = 0,
[int]$VerboseLevel = 0,
[int]$OutputWidth = 8192
)
## -------------------------------------------------------------------
##
## rabbitmq-collect-env.ps1:
## Collect artifacts for RabbitMQ troubleshooting on Windows systems
##
## Based on `riak-debug' as developed by Basho Technologies, Inc
##
## Copyright (c) 2017 Basho Technologies, Inc. All Rights Reserved.
## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
##
## This file is provided to you 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
##
## https://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.
##
## -------------------------------------------------------------------
$ErrorActionPreference = 'Stop'
if ($Debug)
{
$DebugPreference = 'Continue'
Write-Debug -Message 'Enabling debug output'
if ($DebugLevel -gt 0)
{
Set-PSDebug -Strict -Trace 1
}
}
else
{
$DebugPreference = 'SilentlyContinue'
Set-PSDebug -Off
}
if ($Verbose)
{
$VerbosePreference = 'Continue'
Write-Verbose -Message 'Enabling verbose output'
}
else
{
$VerbosePreference = 'SilentlyContinue'
}
Set-StrictMode -Version Latest -ErrorAction 'Stop'
function New-TemporaryDirectory
{
New-Variable -Name parent -Option Constant -Value ([System.IO.Path]::GetTempPath())
Do
{
$tmpDir = New-Item -ItemType Directory -Path (Join-Path -Path $parent -ChildPath (New-Guid))
}
While (-Not $tmpDir)
return $tmpDir
}
function Get-Times
{
New-Variable -Name now -Option Constant -Value (Get-Date)
# Local Time
New-Variable -Name nowLocalStr -Option Constant -Value ($now.ToString("yyyy-MM-ddTHH:mm:ss.fffK"))
# UTC
New-Variable -Name nowUTC -Option Constant -Value ($now.ToUniversalTime())
New-Variable -Name nowUTCStr -Option Constant -Value ($nowUTC.ToString("yyyyMMddTHHmmssZ"))
New-Variable -Name nowUTCFileName -Option Constant -Value ($nowUTC.ToString("yyyyMMddTHHmmssZ"))
$rv = New-Object PsObject -Property @{
Now = $now;
NowLocal = $nowLocalStr;
NowUTC = $nowUTCStr;
NowFileName = $nowUTCFileName;
}
return $rv
}
function Run-Init
{
New-Variable -Name t -Option Constant -Value (Get-Times)
$startMsg = "START TIME UTC: $($t.NowUTC) LOCAL: $($t.NowLocal)"
Write-Verbose -Message $startMsg
New-Variable -Name curdir -Scope Script -Option Constant -Value $PSScriptRoot
Write-Verbose -Message "curdir: $curdir"
New-Variable -Name outputFile -Scope Script -Option Constant -Value `
$(Join-Path -Path $curdir -ChildPath "rabbitmq-collect-env-output-$($t.NowFileName).txt")
Write-Verbose -Message "outputFile: $outputFile"
New-Variable -Name logsArchiveFile -Scope Script -Option Constant -Value `
$(Join-Path -Path $curdir -ChildPath "rabbitmq-collect-env-logs-$($t.NowFileName).zip")
Write-Verbose -Message "logsArchiveFile: $logsArchiveFile"
$initOutFileArgs = @{
Append = $true
FilePath = $outputFile
Width = $OutputWidth
Encoding = 'UTF8'
}
if ($VerboseLevel -gt 0) {
$initOutFileArgs += @{
Verbose = $true
}
} else {
$initOutFileArgs += @{
Verbose = $false
}
}
New-Variable -Name outFileArgs -Scope Script -Option Constant -Value $initOutFileArgs
$initAddContentArgs = @{
Path = $outputFile
}
if ($VerboseLevel -gt 0) {
$initAddContentArgs += @{
Verbose = $true
}
} else {
$initAddContentArgs += @{
Verbose = $false
}
}
New-Variable -Name addContentArgs -Scope Script -Option Constant -Value $initAddContentArgs
New-Variable -Name sep -Scope Script -Option Constant -Value '------------------------------------------------------------------------------------------------------------------------------------'
Remove-Item -ErrorAction 'SilentlyContinue' -Force -LiteralPath $outputFile
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$startMsg"
}
function Get-ErlangInfo
{
$msg = 'Erlang information'
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg$([Environment]::NewLine)"
New-Variable -Name erts_version -Option Constant `
-Value (Get-ChildItem -LiteralPath HKLM:\SOFTWARE\WOW6432Node\Ericsson\Erlang | Select-Object -Last 1).PSChildName
New-Variable -Name erlangProgramFilesPath -Option Constant `
-Value ((Get-ItemProperty -LiteralPath HKLM:\SOFTWARE\WOW6432Node\Ericsson\Erlang\$erts_version).'(default)')
New-Variable -Name erl_exe -Option Constant `
-Value (Join-Path -Path $erlangProgramFilesPath -ChildPath 'bin' | Join-Path -ChildPath 'erl.exe')
New-Variable -Name otp_version -Option Constant `
-Value $(& $erl_exe -boot no_dot_erlang -noshell -eval "{ok,Version}=file:read_file(filename:join([code:root_dir(),'releases',erlang:system_info(otp_release),'OTP_VERSION'])),io:fwrite(Version),halt().")
$msg = "otp_version: $otp_version"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
$msg = "erts_version: $erts_version"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
$msg = "erlangProgramFilesPath: $erlangProgramFilesPath"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
$msg = "erl_exe: $erl_exe"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
}
function Get-RabbitMQInfo
{
$msg = 'RabbitMQ information'
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg$([Environment]::NewLine)"
New-Variable -Name rmqInstallDir -Option Constant `
-Value (Resolve-Path -LiteralPath (Get-ItemProperty -Name Install_Dir -LiteralPath 'HKLM:\SOFTWARE\WOW6432Node\VMware, Inc.\RabbitMQ Server').Install_Dir)
$msg = "RabbitMQ installation directory: $rmqInstallDir"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
New-Variable -Name rmqServerDir -Option Constant `
-Value (Resolve-Path -LiteralPath (Get-ChildItem -LiteralPath $rmqInstallDir -Filter 'rabbitmq_server-*' | Select-Object -First 1).FullName)
$msg = "RabbitMQ server installation directory: $rmqServerDir"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
New-Variable -Name rmqServerSbinDir -Option Constant `
-Value (Resolve-Path -LiteralPath (Join-Path -Path $rmqServerDir.Path -ChildPath 'sbin'))
$msg = "RabbitMQ server sbin directory: $rmqServerSbinDir"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
New-Variable -Name rmqEnvBat -Option Constant `
-Value (Resolve-Path -LiteralPath (Join-Path -Path $rmqServerSbinDir.Path -ChildPath 'rabbitmq-env.bat'))
$msg = "RabbitMQ environment batch file: $rmqEnvBat"
Add-Content @addContentArgs -Value $msg
New-Variable -Name rabbitmqctl -Option Constant `
-Value (Resolve-Path -LiteralPath (Join-Path -Path $rmqServerSbinDir.Path -ChildPath 'rabbitmqctl.bat'))
$msg = "RabbitMQ rabbitmqctl.bat command: $($rabbitmqctl.Path)"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value $msg
########################################################################
# Get RabbitMQ configuration from rabbit_prelaunch
#
$tmpFile = New-TemporaryFile
$tmpFileName = $tmpFile.FullName -replace '\\','/'
try
{
$msg = 'COMMAND / CONFIG: rabbitmqctl.bat eval rabbit_prelaunch:get_context/0'
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg"
$evalString = '"C=rabbit_prelaunch:get_context(),Keys=[advanced_config_file,conf_env_file,config_base_dir,data_base_dir,data_dir,enabled_plugins_file,home_dir,log_base_dir,main_config_file,main_log_file,quorum_queue_dir,rabbitmq_base,rabbitmq_home,stream_queue_dir],file:write_file(""' + $tmpFileName + '"",rabbit_json:encode([{K,unicode:characters_to_binary(maps:get(K,C))} || K <- Keys]))."'
& $rabbitmqctl eval $evalString | Out-Null
New-Variable -Name rmqConfigJson -Option Constant -Value (Get-Content -LiteralPath $tmpFile | ConvertFrom-Json)
$rmqConfigJson | Out-File @outFileArgs
Write-Verbose -Message $rmqConfigJson
}
finally
{
Remove-Item -ErrorAction 'SilentlyContinue' -Force $tmpFile
}
########################################################################
# Get RabbitMQ log files
#
$msg = 'COMMAND: getting RabbitMQ log files'
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg"
New-Variable -Name rmqLogBaseDir -Option Constant -Value $rmqConfigJson.log_base_dir
New-Variable -Name rmqMainLogFileFullName -Option Constant -Value $rmqConfigJson.main_log_file
New-Variable -Name rmqMainLogFileShortName -Option Constant -Value (Split-Path -Leaf -Resolve $rmqMainLogFileFullName)
New-Variable -Name rmqMainLogFileFullNameTempParent -Option Constant -Value (New-TemporaryDirectory)
New-Variable -Name rmqMainLogFileFullNameTemp -Option Constant -Value (Join-Path -Path $rmqMainLogFileFullNameTempParent -ChildPath $rmqMainLogFileShortName)
if (Test-Path -Path $rmqLogBaseDir)
{
try
{
# Note:
# The combination of -Exclude and -LiteralPath does NOT work correctly using Powershell 5.1
Get-ChildItem -Recurse -Path $rmqLogBaseDir -Exclude $rmqMainLogFileShortName | Compress-Archive -Force -DestinationPath $logsArchiveFile
Copy-Item -Force -LiteralPath $rmqMainLogFileFullName -Destination $rmqMainLogFileFullNameTemp
Compress-Archive -LiteralPath $rmqMainLogFileFullNameTemp -Update -DestinationPath $logsArchiveFile
}
finally
{
Remove-Item -Recurse -Force -ErrorAction 'SilentlyContinue' -LiteralPath $rmqMainLogFileFullNameTempParent
}
}
else
{
$msg = "[ERROR] RabbitMQ log base dir does not exist: $rmqLogBaseDir"
Write-Error -ErrorAction 'Continue' -Message $msg
Add-Content @addContentArgs -Value $msg
}
########################################################################
# rabbitmq-env.bat (runs rabbitmq-env-conf.bat if it exists)
#
if (Test-Path -Path $rmqEnvBat)
{
New-Variable -Name rabbitmqEnvRunnerTmpl -Option Constant -Value @"
@echo off
setlocal
setlocal enabledelayedexpansion
setlocal enableextensions
if ERRORLEVEL 1 (
echo "Failed to enable command extensions!"
exit /B 1
)
call "@@PATH@@"
set
"@
$tmpFile = New-TemporaryFile
$rabbitmqEnvRunner = "$tmpFile.bat"
Move-Item -Force -LiteralPath $tmpFile -Destination $rabbitmqEnvRunner
try
{
Set-Content -LiteralPath $rabbitmqEnvRunner -Value $($rabbitmqEnvRunnerTmpl -replace '@@PATH@@', $rmqEnvBat.Path)
$msg = "COMMAND: rabbitmq-env.bat"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg$([Environment]::NewLine)"
& $rabbitmqEnvRunner | Out-File @outFileArgs
}
finally
{
Remove-Item -ErrorAction 'SilentlyContinue' -Force $tmpFile
Remove-Item -ErrorAction 'SilentlyContinue' -Force $rabbitmqEnvRunner
}
}
else
{
$msg = "[ERROR] could not find expected RabbitMQ environment batch file: $rmqEnvBatFullName"
Write-Error -ErrorAction 'Continue' -Message $msg
Add-Content @addContentArgs -Value $msg
}
$msg = "COMMAND: rabbitmqctl.bat report"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg$([Environment]::NewLine)"
& $rabbitmqctl report | ForEach-Object { $_ -replace '\x1b\[[0-9;]*m','' } | Out-File @outFileArgs
}
function Get-Win32_OperatingSystem
{
$msg = 'COMMAND: "Get-CimInstance Win32_OperatingSystem"'
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg"
Get-CIMInstance Win32_OperatingSystem | Format-List -Property '*' | Out-File @outFileArgs
}
function Get-Win32_Process
{
$msg = 'COMMAND: "Get-CimInstance Win32_Process | Select-Object -Property Name,WorkingSetSize,Path,CommandLine"'
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg"
Get-CimInstance Win32_Process | Select-Object -Property Name,WorkingSetSize,CommandLine | Sort-Object -Property Name | Format-List | Out-File @outFileArgs
}
function Get-Win32_Services
{
$msg = 'COMMAND: "Get-Service -ErrorAction ''SilentlyContinue'' | Where-Object { $_.Status -eq ''Running'' } | Select-Object -Property Name,Description,BinaryPathName"'
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg"
Get-Service -ErrorAction 'SilentlyContinue' | Where-Object { $_.Status -eq 'Running' } | Select-Object -Property Name,Description,BinaryPathName | Format-List | Out-File @outFileArgs
}
function Run-Main
{
Get-ErlangInfo
Get-RabbitMQInfo
Get-Win32_OperatingSystem
Get-Win32_Process
Get-Win32_Services
}
function Run-End
{
New-Variable -Name t -Option Constant -Value (Get-Times)
$msg = "STOP TIME UTC: $($t.NowUTC) LOCAL: $($t.NowLocal)"
Write-Verbose -Message $msg
Add-Content @addContentArgs -Value "$sep$([Environment]::NewLine)$msg"
}
try
{
Run-Init
Run-Main
Run-End
}
finally
{
Set-PSDebug -Off
}