ps/Modules/Alkami.DevOps.Installation/Public/Set-NewRelicConfigurationValues.ps1

215 lines
7.6 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Set-NewRelicConfigurationValues {
<#
.SYNOPSIS
Sets the LogLevel and SlowSql Configuration for the New Relic agent to recommended defaults
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[Alias("LogLevel")]
[string]$loggingLevel = "error",
[Parameter(Mandatory = $false)]
[Alias("NewRelicConfigPath")]
[string]$nrConfigPath = "C:\ProgramData\New Relic\.NET Agent\newrelic.config"
)
$logLead = Get-LogLeadName
try {
Write-Verbose ("$logLead : Looking for New Relic configuration in {0}" -f $nrConfigPath)
[Xml]$newRelicConfig = Read-XmlFile $nrConfigPath -ErrorAction SilentlyContinue
if ([String]::IsNullOrEmpty($newRelicConfig)) {
Write-Warning ("$logLead : The New Relic Config File Could Not be Found")
return
}
}
catch {
Write-Warning "$logLead : The New Relic Config File Could Not be Read Or Is Invalid"
return
}
$xmlNameSpace = @{ nr = 'urn:newrelic-config'; }
Write-Verbose "$logLead : Looking for NewRelic Configuration Node"
$configurationNode = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
if ($null -eq $configurationNode) {
Write-Warning "$logLead : The New Relic configuration node could not be found. Check the New Relic Agent installation"
return
}
Write-Verbose "$logLead : Looking for New Relic Service SSL attr"
$serviceNode = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:service' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
$sslAttr = $null
if ($null -eq $serviceNode) {
Write-Warning "$logLead : Service Node was not found -- this should never be the case. Check the New Relic Agent installation"
} else {
$sslAttr = $serviceNode.Node.Attributes["ssl"]
}
Write-Verbose "$logLead : Looking for NewRelic Log Level Node"
$loggingNode = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:log/@level' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
Write-Verbose "$logLead : Looking for NewRelic SlowSql Node"
$slowSqlNode = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:slowSql/@enabled' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
Write-Verbose "$logLead : Looking for NewRelic transactionTracer Node"
$transactionTracer = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:transactionTracer/@explainEnabled' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
Write-Verbose "$logLead : Looking for NewRelic applicationLogging Node"
$applicationLogging = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:applicationLogging' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
$applicationLoggingEnabled = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:applicationLogging/@enabled' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
$applicationLoggingForwardingEnabled = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:applicationLogging/nr:forwarding/@enabled' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
$applicationLoggingLocalDecorating = Select-Xml `
-Xml $newRelicConfig `
-Xpath '//nr:configuration/nr:applicationLogging/nr:localDecorating/@enabled' `
-Namespace $xmlNamespace `
-ErrorAction SilentlyContinue
$configFileIsDirty = $false
if ($null -eq $loggingNode) {
Write-Warning "$logLead : Log Node was not found -- this should never be the case. Check the New Relic Agent installation"
return
} ElseIf ($loggingNode.ToString() -notmatch $loggingLevel) {
Write-Host "$logLead : Updating the New Relic Logging Configuration"
$configFileIsDirty = $true
$loggingNode.Node.Value = $loggingLevel;
}
if ($null -ne $sslAttr) {
Write-Host "$logLead : The Service/Ssl Attr Exists and Will Be Removed"
$serviceNode.Node.Attributes.Remove($sslAttr)
$configFileIsDirty = $true
}
if ($null -eq $slowSqlNode) {
Write-Host "$logLead : The SlowSQL Node Does Not Exist and Will be Created"
$configFileIsDirty = $true
$tempDoc = New-Object System.Xml.XmlDocument
$tempDoc.LoadXml('<slowSql enabled="false" />')
$newSqlNode = $newRelicConfig.ImportNode($tempDoc.DocumentElement, $true)
$newRelicConfig.DocumentElement.AppendChild($newSqlNode) | Out-Null
} Elseif ($slowSqlNode.Node.Value -notmatch "false") {
Write-Host "$logLead : Updating the New Relic SlowSQL Node Configuration"
$configFileIsDirty = $true
$slowSqlNode.Node.Value = "false"
}
if ($null -eq $transactionTracer) {
Write-Host "$logLead : The transactionTracer Node Does Not Exist and Will be Created"
$configFileIsDirty = $true
$tempDoc = New-Object System.Xml.XmlDocument
$tempDoc.LoadXml(' <transactionTracer enabled="true" transactionThreshold="apdex_f" stackTraceThreshold="500" recordSql="obfuscated" explainEnabled="false" explainThreshold="500" />')
$newSqlNode = $newRelicConfig.ImportNode($tempDoc.DocumentElement, $true)
$newRelicConfig.DocumentElement.AppendChild($newSqlNode) | Out-Null
} ElseIf ($transactionTracer.Node.Value -notmatch "false") {
Write-Host "$logLead : Updating the New Relic transactionTracer Node Configuration"
$configFileIsDirty = $true
$transactionTracer.Node.Value = "false"
}
if ($null -eq $applicationLogging) {
Write-Host "$logLead : The applicationLogging Node Does Not Exist and Will be Created"
$configFileIsDirty = $true
$tempDoc = New-Object System.Xml.XmlDocument
$tempDoc.LoadXml('<applicationLogging enabled="false"><forwarding enabled="false" maxSamplesStored="10000"/><localDecorating enabled="false"/></applicationLogging>')
$newSqlNode = $newRelicConfig.ImportNode($tempDoc.DocumentElement, $true)
$newRelicConfig.DocumentElement.AppendChild($newSqlNode) | Out-Null
} else {
if ($applicationLoggingEnabled.Node.Value -notmatch "false") {
Write-Host "$logLead : Updating the New Relic applicationLogging Node Configuration"
$configFileIsDirty = $true
$applicationLoggingEnabled.Node.Value = "false"
}
if ($applicationLoggingForwardingEnabled.Node.Value -notmatch "false") {
Write-Host "$logLead : Updating the New Relic applicationLoggingforwarding Node Configuration"
$configFileIsDirty = $true
$applicationLoggingForwardingEnabled.Node.Value = "false"
}
if ($applicationLoggingLocalDecorating.Node.Value -notmatch "false") {
Write-Host "$logLead : Updating the New Relic applicationLogginglocalDecorating Node Configuration"
$configFileIsDirty = $true
$applicationLoggingLocalDecorating.Node.Value = "false"
}
}
if ($configFileIsDirty) {
Write-Host ("$logLead : Saving the Modified New Relic Configuration File" -f $nrConfigPath)
$utfNoBOM = New-Object System.Text.UTF8Encoding($false)
Save-XMLFile $nrConfigPath $newRelicConfig.OuterXml.Replace('xmlns=""', [String]::Empty) $utfNoBOM
} else {
Write-Host "$logLead : No Changes Required to the New Relic Config"
}
}