Skip to content

Commit

Permalink
chore(mssql): Alias env vars (#925)
Browse files Browse the repository at this point in the history
* chore(mssql): Alias env vars

* fix(mssql): Make compatible with PS v <7
  • Loading branch information
rthorn-nr authored May 26, 2023
1 parent b437f8c commit 681a4f9
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions recipes/newrelic/infrastructure/ohi/sql/ms-sql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ preInstall:
exit 2
}
$NR_CLI_DB_HOSTNAME= "$env:NR_CLI_DB_HOSTNAME"
if ([string]::IsNullOrWhiteSpace($NR_CLI_DB_HOSTNAME)) {$NR_CLI_DB_HOSTNAME = hostname}
$NR_CLI_DB_PORT = "$env:NR_CLI_DB_PORT"
if ([string]::IsNullOrWhiteSpace($NR_CLI_DB_PORT)) {$NR_CLI_DB_PORT = "1433"}
$NEW_RELIC_MSSQL_DB_HOSTNAME = if ($env:NEW_RELIC_MSSQL_DB_HOSTNAME) { $env:NEW_RELIC_MSSQL_DB_HOSTNAME } else { $env:NR_CLI_DB_HOSTNAME };
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_HOSTNAME)) {$NEW_RELIC_MSSQL_DB_HOSTNAME = hostname}
$NEW_RELIC_MSSQL_DB_PORT = if ($env:NEW_RELIC_MSSQL_DB_PORT) { $env:NEW_RELIC_MSSQL_DB_PORT } else { $env:NR_CLI_DB_PORT };
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_PORT)) {$NEW_RELIC_MSSQL_DB_PORT = "1433"}
# Loop on each instance names to see if we can connect
foreach ($instance in $instances) {
$connection = "${NR_CLI_DB_HOSTNAME}\${instance},${NR_CLI_DB_PORT}"
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance},${NEW_RELIC_MSSQL_DB_PORT}"
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
If ($name.Length -ge 3) {
$actualName = $name[2].ToString().Trim();
Expand All @@ -59,7 +59,7 @@ preInstall:
}
}
$connection = "${NR_CLI_DB_HOSTNAME}\${instance}"
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance}"
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
If ($name.Length -ge 3) {
$actualName = $name[2].ToString().Trim();
Expand All @@ -73,7 +73,7 @@ preInstall:
}
}
exit 3
exit 3
'
info: |
To capture data from the Microsoft SqlServer integration, we need to create a new SqlServer user with specific permissions (CONNECT, VIEW SERVER STATE, READ).
Expand Down Expand Up @@ -119,24 +119,24 @@ install:
$MAX_RETRIES=3
# Check Env Vars
$NR_CLI_DB_HOSTNAME= "$env:NR_CLI_DB_HOSTNAME"
if ([string]::IsNullOrWhiteSpace($NR_CLI_DB_HOSTNAME)) {$NR_CLI_DB_HOSTNAME = hostname}
$NEW_RELIC_MSSQL_DB_HOSTNAME = if ($env:NEW_RELIC_MSSQL_DB_HOSTNAME) { $env:NEW_RELIC_MSSQL_DB_HOSTNAME } else { $env:NR_CLI_DB_HOSTNAME };
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_HOSTNAME)) {$NEW_RELIC_MSSQL_DB_HOSTNAME = hostname}
$NR_CLI_DB_PORT = "$env:NR_CLI_DB_PORT"
if ([string]::IsNullOrWhiteSpace($NR_CLI_DB_PORT)) {$NR_CLI_DB_PORT = "1433"}
$NEW_RELIC_MSSQL_DB_PORT = if ($env:NEW_RELIC_MSSQL_DB_PORT) { $env:NEW_RELIC_MSSQL_DB_PORT } else { $env:NR_CLI_DB_PORT };
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_PORT)) {$NEW_RELIC_MSSQL_DB_PORT = "1433"}
$NR_CLI_DB_USERNAME= "$env:NR_CLI_DB_USERNAME"
if ([string]::IsNullOrWhiteSpace($NR_CLI_DB_USERNAME)) {$NR_CLI_DB_USERNAME = "newrelic"}
$NEW_RELIC_MSSQL_DB_USERNAME = if ($env:NEW_RELIC_MSSQL_DB_USERNAME) { $env:NEW_RELIC_MSSQL_DB_USERNAME } else { $env:NR_CLI_DB_USERNAME };
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_USERNAME)) {$NEW_RELIC_MSSQL_DB_USERNAME = "newrelic"}
$NR_CLI_DB_PASSWORD= "$env:NR_CLI_DB_PASSWORD"
if ([string]::IsNullOrWhiteSpace($NR_CLI_DB_PASSWORD)) {
$NEW_RELIC_MSSQL_DB_PASSWORD = if ($env:NEW_RELIC_MSSQL_DB_PASSWORD) { $env:NEW_RELIC_MSSQL_DB_PASSWORD } else { $env:NR_CLI_DB_PASSWORD };
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_PASSWORD)) {
Add-Type -AssemblyName System.Web;
$NR_CLI_DB_PASSWORD = [System.Web.Security.Membership]::GeneratePassword(20,2).replace("-","");
$NEW_RELIC_MSSQL_DB_PASSWORD = [System.Web.Security.Membership]::GeneratePassword(20,2).replace("-","");
}
$NR_SQL_PASSWORD="$env:NR_CLI_SQL_PASSWORD"
$NR_SQL_USERNAME="$env:NR_CLI_SQL_USERNAME"
$NR_ENABLE_BUFFER_METRICS="$env:NR_CLI_ENABLE_BUFFER_METRICS"
$NR_ENABLE_DATABASE_RESERVE_METRICS="$env:NR_CLI_ENABLE_RESERVE_METRICS"
$NEW_RELIC_MSSQL_SQL_PASSWORD = if ($env:NEW_RELIC_MSSQL_SQL_PASSWORD) { $env:NEW_RELIC_MSSQL_SQL_PASSWORD } else { $env:NR_CLI_SQL_PASSWORD };
$NEW_RELIC_MSSQL_SQL_USERNAME = if ($env:NEW_RELIC_MSSQL_SQL_USERNAME) { $env:NEW_RELIC_MSSQL_SQL_USERNAME } else { $env:NR_CLI_SQL_USERNAME };
$NR_ENABLE_BUFFER_METRICS = if ($env:NEW_RELIC_MSSQL_ENABLE_BUFFER_METRICS) { $env:NEW_RELIC_MSSQL_ENABLE_BUFFER_METRICS } else { $env:NR_CLI_ENABLE_BUFFER_METRICS };
$NR_ENABLE_DATABASE_RESERVE_METRICS = if ($env:NEW_RELIC_MSSQL_ENABLE_RESERVE_METRICS) { $env:NEW_RELIC_MSSQL_ENABLE_RESERVE_METRICS } else { $env:NR_CLI_ENABLE_RESERVE_METRICS };
$OhiConfig = "C:\\Program Files\\New Relic\\newrelic-infra\\integrations.d\\mssql-config.yml";
$CreateConfig=1
Expand All @@ -162,9 +162,9 @@ install:
Add-Content -Path $OhiConfig -Value " - name`:` nri-mssql" -Force | Out-Null;
Add-Content -Path $OhiConfig -Value " env`:` " -Force | Out-Null;
Add-Content -Path $OhiConfig -Value " HOSTNAME`:` ${NR_CLI_DB_HOSTNAME}" -Force | Out-Null;
Add-Content -Path $OhiConfig -Value " USERNAME`:` ${NR_CLI_DB_USERNAME}" -Force | Out-Null;
Add-Content -Path $OhiConfig -Value " PASSWORD`:` `"${NR_CLI_DB_PASSWORD}`"" -Force | Out-Null;
Add-Content -Path $OhiConfig -Value " HOSTNAME`:` ${NEW_RELIC_MSSQL_DB_HOSTNAME}" -Force | Out-Null;
Add-Content -Path $OhiConfig -Value " USERNAME`:` ${NEW_RELIC_MSSQL_DB_USERNAME}" -Force | Out-Null;
Add-Content -Path $OhiConfig -Value " PASSWORD`:` `"${NEW_RELIC_MSSQL_DB_PASSWORD}`"" -Force | Out-Null;
if ($NR_ENABLE_BUFFER_METRICS -eq "0") {
Add-Content -Path $OhiConfig -Value " ENABLE_BUFFER_METRICS`:` false" -Force | Out-Null;
Expand Down Expand Up @@ -194,9 +194,9 @@ install:
)
if ([string]::IsNullOrWhiteSpace($Port)) {
$connection = "${NR_CLI_DB_HOSTNAME}\${instance}"
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance}"
} else {
$connection = "${NR_CLI_DB_HOSTNAME}\${instance},${Port}"
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance},${Port}"
}
$SQL=@"
Expand All @@ -209,7 +209,7 @@ install:
FETCH NEXT FROM db_user_cursor INTO @name WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXECUTE(`"USE [`" + @name + `"]; IF EXISTS(select top 1 name from sys.syslogins where name = `"`"${NR_CLI_DB_USERNAME}`"`") DROP USER ${NR_CLI_DB_USERNAME};`" )
EXECUTE(`"USE [`" + @name + `"]; IF EXISTS(select top 1 name from sys.syslogins where name = `"`"${NEW_RELIC_MSSQL_DB_USERNAME}`"`") DROP USER ${NEW_RELIC_MSSQL_DB_USERNAME};`" )
END TRY
BEGIN CATCH
END CATCH
Expand All @@ -218,12 +218,12 @@ install:
CLOSE db_user_cursor
DEALLOCATE db_user_cursor
IF EXISTS(select top 1 name from sys.syslogins where name = `"${NR_CLI_DB_USERNAME}`")
IF EXISTS(select top 1 name from sys.syslogins where name = `"${NEW_RELIC_MSSQL_DB_USERNAME}`")
BEGIN
DECLARE @session_id INT
DECLARE db_login_cursor CURSOR
READ_ONLY FORWARD_ONLY
FOR SELECT session_id FROM sys.dm_exec_sessions WHERE login_name = `"${NR_CLI_DB_USERNAME}`"
FOR SELECT session_id FROM sys.dm_exec_sessions WHERE login_name = `"${NEW_RELIC_MSSQL_DB_USERNAME}`"
OPEN db_login_cursor
FETCH NEXT FROM db_login_cursor INTO @session_id WHILE @@FETCH_STATUS = 0
BEGIN
Expand All @@ -233,20 +233,20 @@ install:
CLOSE db_login_cursor
DEALLOCATE db_login_cursor
DROP LOGIN ${NR_CLI_DB_USERNAME}
DROP LOGIN ${NEW_RELIC_MSSQL_DB_USERNAME}
END
CREATE LOGIN ${NR_CLI_DB_USERNAME} WITH PASSWORD = `"${NR_CLI_DB_PASSWORD}`"
CREATE LOGIN ${NEW_RELIC_MSSQL_DB_USERNAME} WITH PASSWORD = `"${NEW_RELIC_MSSQL_DB_PASSWORD}`"
GRANT CONNECT SQL, VIEW SERVER STATE, VIEW ANY DEFINITION TO ${NR_CLI_DB_USERNAME}
GRANT CONNECT SQL, VIEW SERVER STATE, VIEW ANY DEFINITION TO ${NEW_RELIC_MSSQL_DB_USERNAME}
DECLARE db_create_cursor CURSOR
READ_ONLY FORWARD_ONLY
FOR SELECT NAME FROM master.sys.databases WHERE NAME NOT IN (`"master`",`"msdb`",`"tempdb`",`"model`",`"rdsadmin`",`"distribution`") and state !=6
OPEN db_create_cursor
FETCH NEXT FROM db_create_cursor INTO @name WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXECUTE(`"USE [`" + @name + `"]; IF NOT EXISTS(SELECT name from sys.database_principals where name = `"`"${NR_CLI_DB_USERNAME}`"`") BEGIN CREATE USER ${NR_CLI_DB_USERNAME} FOR LOGIN ${NR_CLI_DB_USERNAME}; END`" )
EXECUTE(`"USE [`" + @name + `"]; IF NOT EXISTS(SELECT name from sys.database_principals where name = `"`"${NEW_RELIC_MSSQL_DB_USERNAME}`"`") BEGIN CREATE USER ${NEW_RELIC_MSSQL_DB_USERNAME} FOR LOGIN ${NEW_RELIC_MSSQL_DB_USERNAME}; END`" )
END TRY
BEGIN CATCH
END CATCH
Expand All @@ -255,7 +255,7 @@ install:
CLOSE db_create_cursor
DEALLOCATE db_create_cursor
select name from sys.syslogins where name = `"${NR_CLI_DB_USERNAME}`"
select name from sys.syslogins where name = `"${NEW_RELIC_MSSQL_DB_USERNAME}`"
"@
$NriSqlFile = "$env:TEMP\nri-sql.sql"
Expand Down Expand Up @@ -283,7 +283,7 @@ install:
If ($SqlOutput.Length -ge 3) {
$userName = $SqlOutput[2].ToString().Trim();
If ($userName -eq ${NR_CLI_DB_USERNAME}) {
If ($userName -eq ${NEW_RELIC_MSSQL_DB_USERNAME}) {
# User created successfully
return $true
}
Expand All @@ -299,25 +299,25 @@ install:
foreach ($instance in $instances) {
$connection = "${NR_CLI_DB_HOSTNAME}\${instance},${NR_CLI_DB_PORT}"
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance},${NEW_RELIC_MSSQL_DB_PORT}"
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
If ($name.Length -ge 3) {
$actualName = $name[2].ToString().Trim();
If ($actualName -eq $instance) {
# Was able to connect with port
$canLogin=($loginModes | Where-Object -Property PSPath -like -Value "*${actualName}*")
if (-Not ($canLogin -eq $null)) {
$isCreated = (Create-SqlUser -InstanceName $instance -Port $NR_CLI_DB_PORT)
$isCreated = (Create-SqlUser -InstanceName $instance -Port $NEW_RELIC_MSSQL_DB_PORT)
if ($isCreated) {
Write-OhiConfig -InstanceName $instance -Port $NR_CLI_DB_PORT
Write-OhiConfig -InstanceName $instance -Port $NEW_RELIC_MSSQL_DB_PORT
$setupCount++
}
}
continue
}
}
$connection = "${NR_CLI_DB_HOSTNAME}\${instance}"
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance}"
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
If ($name.Length -ge 3) {
$actualName = $name[2].ToString().Trim();
Expand Down

0 comments on commit 681a4f9

Please sign in to comment.