-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathDatadog.psm1
412 lines (349 loc) · 14.6 KB
/
Datadog.psm1
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
function Install-DatadogAgent
{
[CmdletBinding()]
param (
# Parameters that are always valid
[String] $AgentInstallerURL,
[String] $AgentInstallerPath,
[String] $AgentVersion,
[String] $AgentInstallLogPath,
[switch] $ApmInstrumentationEnabled,
[string[]] $APMLibraries,
[switch] $RestartIIS,
[switch] $Preview,
# Parameters that are only valid on first install
[String]$ApiKey,
[String]$Site,
[String]$Tags,
[String]$Hostname,
[String]$DDAgentUsername,
[String]$DDAgentPassword,
[String]$ApplicationDataDirectory,
[String]$ProjectLocation
)
validateAdminPriviledges
validate64BitProcess
enableTSL12
$installableLibs = @("dotnet")
$apmlibs = @{}
[string] $uniqueID = [System.Guid]::NewGuid()
if (($ApmInstrumentationEnabled -eq $true) -or ($APMLibraries))
{
## make sure installableLibs is always all lower case; the comparison below is case
## sensitive, and we'll use all lower as the standard
if (!$APMLibraries)
{
foreach ($avail in $installableLibs)
{
$apmlibs[$avail] = "latest"
}
}
else
{
## break out the comma separated list of libraries
foreach ($lib in $APMLibraries)
{
$lib = $lib.Trim()
## see if there's a version
$l, $v = $lib.Split(":")
if (-not ($installableLibs -contains $l.ToLower()))
{
$exception = [Exception]::new("Unknown Library name $($l).")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
if (!$v)
{
$apmlibs[$l] = "latest"
}
else
{
$apmlibs[$l] = $v
}
}
}
}
installAgent -params $PSBoundParameters -uniqueID $uniqueID -Preview:$Preview
foreach ($l in $apmlibs.Keys)
{
Write-Host -ForegroundColor Green "Installing Library $l version $($apmlibs[$l])"
if ($l -eq "dotnet")
{
installDotnetTracer -uniqueID $uniqueID -version $apmlibs[$l] -Preview:$Preview
}
}
if ($RestartIIS)
{
Write-Host "Restarting IIS"
if(!$Preview)
{
stop-service -force was
Restart-Service -Name W3SVC -Force
}
}
<#
.SYNOPSIS
Installs the Datadog Agent.
.DESCRIPTION
Downloads the latest Datadog Windows Agent installer, validates the installer's signature, and executes the installation.
.PARAMETER AgentInstallerURL
Override the URL which the Agent installer is downloaded from.
.PARAMETER AgentInstallerPath
Path to a local Datadog Windows Agent installer to run. If this option is provided, an Agent installer will not be downloaded.
.PARAMETER AgentVersion
The Agent version to download. Example: 7.52.1
.PARAMETER AgentInstallLogPath
Override the Agent installation log location.
.PARAMETER ApmInstrumentationEnabled
Specify that APM tracers should be installed.
.PARAMETER APMLibraries
Comma-separated list of APM libraries to install, with optional versions. Example: APMLibraries="DotNet:2.52.0". Currently only DotNet is supported.
.PARAMETER Preview
Preview mode. When enabled, the script will not make changes to the system. It will only output the commands that would be run.
.PARAMETER RestartIIS
Restart IIS after installation.
.PARAMETER ApiKey
Adds the Datadog API KEY to the configuration file.
.PARAMETER Site
Set the Datadog intake site. Example: SITE=datadoghq.com
.PARAMETER Tags
Comma-separated list of tags to assign in the configuration file. Example: TAGS="key_1:val_1,key_2:val_2"
.PARAMETER Hostname
Configures the hostname reported by the Agent to Datadog (overrides any hostname calculated at runtime).
.PARAMETER DDAgentUsername
Override the default ddagentuser username used during Agent installation (v6.11.0+).
.PARAMETER DDAgentPassword
Override the cryptographically secure password generated for the ddagentuser user during Agent installation (v6.11.0+). Must be provided for installs on domain servers/domain controllers.
.PARAMETER ApplicationDataDirectory
Override the directory to use for the configuration file directory tree (v6.11.0+). May only be provided on initial install; not valid for upgrades. Default: C:\ProgramData\Datadog
.PARAMETER ProjectLocation
Override the directory to use for the binary file directory tree (v6.11.0+). May only be provided on initial install; not valid for upgrades. Default: %ProgramFiles%\Datadog\Datadog Agent
.INPUTS
None. You cannot pipe objects to Install-DDAgent.
.OUTPUTS
None.
.EXAMPLE
C:\PS> Install-DDAgent -ApiKey "<YOUR_DATADOG_API_KEY>"
.EXAMPLE
C:\PS> Install-DDAgent -ApiKey "<YOUR_DATADOG_API_KEY>" -WithAPMTracers "DotNet"
.LINK
Learn more about the Datadog Windows Agent User:
https://docs.datadoghq.com/agent/guide/windows-agent-ddagent-user/
#>
}
###############################
# Unexported helper functions #
###############################
function validateAdminPriviledges()
{
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
$exception = [Exception]::new("Administrator priviledges required.")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
}
function validate64BitProcess()
{
if (-not [Environment]::Is64BitProcess)
{
$exception = [Exception]::new("This command must be run in a 64-bit environment.")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
}
function enableTSL12()
{
# Powershell does not enabled TLS 1.2 by default, & we want it enabled for faster downloads
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
}
function installAgent
{
param(
[hashtable]$params,
[string] $uniqueID,
[switch] $Preview
)
$installerPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "datadog-agent-$uniqueID.msi"
$downloadInstaller = $true
if ($params.ContainsKey('AgentInstallerPath'))
{
$installerPath = $params.AgentInstallerPath
$downloadInstaller = $false
}
if ($downloadInstaller -eq $true)
{
$version = "datadog-agent-7-latest.amd64"
if ($params.ContainsKey('AgentVersion'))
{
$version = "ddagent-cli-$($params.AgentVersion)"
}
$url = "https://s3.amazonaws.com/ddagent-windows-stable/$version.msi"
if ($params.ContainsKey('AgentInstallerURL'))
{
$url = $params.AgentInstallerURL
}
Write-Host "Downloading Datadog Windows Agent installer"
if($Preview)
{
Write-Host "Preview mode enabled. Skipping download of the Datadog Windows Agent installer."
Write-Host "Download URL: $url"
$downloadInstaller = $false
}
else
{
downloadAsset -url $url -outFile $installerPath
}
}
$logFile = ""
if ($params.ContainsKey('AgentInstallLogPath'))
{
$logFile = $params.AgentInstallLogPath
}
else
{
$logFile = createTemporaryLogFile -prefix "ddagent-msi"
}
$defaultInstallArgs = @("/qn", "/log `"$logFile`"", "/i `"$installerPath`"")
$customInstallArgs = formatAgentInstallerArguments -params $params
Write-Host "Installing Datadog Windows Agent"
if($Preview)
{
Write-Host "Preview mode enabled. Skipping installation of the Datadog Windows Agent."
Write-Host "Installer path: $installerPath"
Write-Host "Log file path: $logFile"
Write-Host "Installer arguments: $($defaultInstallArgs; $customInstallArgs)"
return
}
$installResult = Start-Process -Wait msiexec -ArgumentList $($defaultInstallArgs; $customInstallArgs) -PassThru
if ($downloadInstaller)
{
Remove-Item $installerPath
}
if ($installResult.ExitCode -ne 0)
{
$exception = [Exception]::new("Agent installation failed. For more information, check the installation log file at $logFile.")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
## write the install info file
$installInfo = @"
---
install_method:
tool: powershell-Datadog
tool_version: $($MyInvocation.MyCommand.Module.Version)
installer_version: Datadog_powershell_module
"@
$appDataDir = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Datadog\Datadog Agent").ConfigRoot
Out-File -FilePath $appDataDir\install_info -InputObject $installInfo
}
function installDotnetTracer
{
param(
[string] $uniqueID,
[string] $version,
[switch] $Preview
)
$downloadTag = ""
$downloadVersion = ""
if (!$version -or ($version -eq "latest"))
{
$downloadTag = ((Invoke-WebRequest -UseBasicParsing https://api.github.com/repos/DataDog/dd-trace-dotnet/releases/latest).Content | ConvertFrom-Json).tag_name
$downloadVersion = $downloadTag.TrimStart("v")
}
else
{
## validate that the version exists
$rels = (Invoke-WebRequest -UseBasicParsing https://api.github.com/repos/DataDog/dd-trace-dotnet/releases).content | convertfrom-json
if (-not ($rels.name -contains $version))
{
throw("Version $version not found")
}
$downloadTag = "v$($version)"
$downloadVersion = $version
}
$installerPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "datadog-dotnet-apm-$uniqueID.msi"
Write-Host "Downloading Datadog .NET Tracing Library installer ($downloadTag) ($downloadVersion)"
$dlurl = "https://github.com/DataDog/dd-trace-dotnet/releases/download/$downloadTag/datadog-dotnet-apm-$downloadVersion-x64.msi"
if($Preview)
{
Write-Host "Preview mode enabled. Skipping installation of the Datadog .NET Tracing Library installer."
Write-Host "Download URL: $dlurl"
return
}
downloadAsset -url $dlurl -outFile "$installerPath"
Write-Host "Installing Datadog .NET Tracing Library"
$installResult = Start-Process -Wait msiexec -ArgumentList "/qn /i $installerPath" -PassThru
Remove-Item $installerPath
if ($installResult.ExitCode -ne 0)
{
$exception = [Exception]::new(".NET Tracing Library installation failed.")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
}
function doesDatadogYamlExist()
{
try {
$configRoot = (Get-ItemProperty -ErrorAction Stop -Path 'HKLM:\SOFTWARE\Datadog\Datadog Agent').ConfigRoot
if ($configRoot -ne "")
{
return Test-Path -Path (Join-Path -Path $configRoot -ChildPath "datadog.yaml")
}
return $false
}
catch {
return $false
}
}
function formatAgentInstallerArguments($params)
{
[string[]] $formattedArgs = @()
if ($params.ContainsKey('ApiKey')) { $formattedArgs += "APIKEY=$($params.ApiKey)"}
if ($params.ContainsKey('Site')) { $formattedArgs += "SITE=$($params.Site)"}
if ($params.ContainsKey('Hostname')) { $formattedArgs += "HOSTNAME=$($params.Hostname)" }
if ($params.ContainsKey('DDAgentUsername')) { $formattedArgs += "DDAGENTUSER_NAME=$($params.DDAgentUsername)" }
if ($params.ContainsKey('DDAgentPassword')) { $formattedArgs += "DDAGENTUSER_PASSWORD=$($params.DDAgentPassword)" }
if ($params.ContainsKey('Tags')) { $formattedArgs += "TAGS=`"$($params.Tags)`"" }
if ($params.ContainsKey('ApplicationDataDirectory')) { $formattedArgs += "APPLICATIONDATADIRECTORY=`"$($params.ApplicationDataDirectory)`"" }
if ($params.ContainsKey('ProjectLocation')) { $formattedArgs += "PROJECTLOCATION=`"$($params.ProjectLocation)`"" }
if (($formattedArgs.Count -ne 0) -and (doesDatadogYamlExist -eq $true))
{
Write-Warning "A datadog.yaml file already exists. The contents of that file will take precedence over the following parameters: $formattedArgs"
# We will still pass the parameters along to the installer, and let it decide what to do with them
}
return $formattedArgs
}
function downloadAsset($url, $outFile)
{
(New-Object System.Net.WebClient).DownloadFile($url, $outFile)
if (-not $?)
{
$exception = [Exception]::new("Download failed.")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
if (-not (hasValidDDSignature -asset $outFile))
{
$exception = [Exception]::new("$outFile did not pass the signature check.")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
}
function hasValidDDSignature($asset)
{
if (-not (Test-Path $asset -PathType Leaf)) {
$exception = [Exception]::new("$asset not found")
$PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($exception, "FatalError", [Management.Automation.ErrorCategory]::InvalidOperation, $null))
}
$signature = Get-AuthenticodeSignature $asset
if ($signature.Status -ne "Valid")
{
return $false
}
if ($signature.SignerCertificate.Subject -contains 'Datadog') # case insensitive check
{
return $false
}
return $true
}
function createTemporaryLogFile($prefix)
{
$tempFile = New-TemporaryFile
$renamedTempFile = Rename-Item -Path $tempFile -NewName "$prefix-$((Get-ChildItem $tempFile).BaseName).log" -PassThru
return $renamedTempFile.FullName
}