From c553a5309204ec8d3e8f379de7a6638d6c53def2 Mon Sep 17 00:00:00 2001 From: anosov1960 Date: Tue, 9 Mar 2021 22:31:33 -0800 Subject: [PATCH] Added Linux support for SQL detection --- DiscoverSql.ps1 | 7 +- DiscoverSql.sh | 6 + .../azure-hybrid-benefit/DiscoverSql.ps1 | 11 ++ .../azure-hybrid-benefit/DiscoverSql.sh | 6 + .../sql-license-usage.csv | 2 + .../sql-license-usage.ps1 | 112 +++++++++++++----- 6 files changed, 111 insertions(+), 33 deletions(-) create mode 100644 DiscoverSql.sh create mode 100644 samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 create mode 100644 samples/manage/azure-hybrid-benefit/DiscoverSql.sh create mode 100644 samples/manage/azure-hybrid-benefit/sql-license-usage.csv diff --git a/DiscoverSql.ps1 b/DiscoverSql.ps1 index aadf0058..120e055f 100644 --- a/DiscoverSql.ps1 +++ b/DiscoverSql.ps1 @@ -1,7 +1,8 @@ - - # This function is to run on each VM to detect if SQL server is installed - +# +# This script checks if SQL Server is installed on Windows +# +# [bool] $SqlInstalled = $false $regPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' if (Test-Path $regPath) { diff --git a/DiscoverSql.sh b/DiscoverSql.sh new file mode 100644 index 00000000..5b430e3a --- /dev/null +++ b/DiscoverSql.sh @@ -0,0 +1,6 @@ +if ! systemctl is-active --quiet mssql-server.service; then + echo "False" + exit + else + echo "True" + fi \ No newline at end of file diff --git a/samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 b/samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 new file mode 100644 index 00000000..874a4d5c --- /dev/null +++ b/samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 @@ -0,0 +1,11 @@ + + +# This script checks if SQL Server is installed on Windows + + [bool] $SqlInstalled = $false + $regPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' + if (Test-Path $regPath) { + $inst = (get-itemproperty $regPath).InstalledInstances + $SqlInstalled = ($inst.Count -gt 0) + } + Write-Output $SqlInstalled diff --git a/samples/manage/azure-hybrid-benefit/DiscoverSql.sh b/samples/manage/azure-hybrid-benefit/DiscoverSql.sh new file mode 100644 index 00000000..5b430e3a --- /dev/null +++ b/samples/manage/azure-hybrid-benefit/DiscoverSql.sh @@ -0,0 +1,6 @@ +if ! systemctl is-active --quiet mssql-server.service; then + echo "False" + exit + else + echo "True" + fi \ No newline at end of file diff --git a/samples/manage/azure-hybrid-benefit/sql-license-usage.csv b/samples/manage/azure-hybrid-benefit/sql-license-usage.csv new file mode 100644 index 00000000..20c48433 --- /dev/null +++ b/samples/manage/azure-hybrid-benefit/sql-license-usage.csv @@ -0,0 +1,2 @@ +"Date","Time","Subscription Name","Subscription ID","AHB ECs","PAYG ECs","AHB Std vCores","AHB Ent vCores","PAYG Std vCores","PAYG Ent vCores","HADR Std vCores","HADR Ent vCores","Developer vCores","Express vCores","Unregistered vCores","Unknown vCores" +"2021-03-09","22:22:47","ADS TINA Engineering","a5082b19-8a6e-4bc5-8fdd-8ef39dfebc39","0","0","8","0","13","0","0","0","18","0","0","0" diff --git a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 index 41ce4702..e11b0d83 100644 --- a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 +++ b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 @@ -180,9 +180,9 @@ function AddVCores { } } -function Discovery { +function DiscoveryOnWindows { - # This function is to run on each VM to detect if SQL server is installed +# This script checks if SQL Server is installed on Windows [bool] $SqlInstalled = $false $regPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' @@ -193,6 +193,19 @@ function Discovery { Write-Output $SqlInstalled } +# +# This script checks if SQL Server is installed on Linux +# +# +$DiscoveryOnLinux = + 'if ! systemctl is-active --quiet mssql-server.service; then + echo "False" + exit + else + echo "True" + fi' + + #The following block is required for runbooks only if ($UseInRunbook){ @@ -235,7 +248,8 @@ $GetVCoresDef = $function:GetVCores.ToString() $AddVCoresDef = $function:AddVCores.ToString() # Create a script file with the SQL server discovery logic -New-Item -ItemType file -path DiscoverSql.ps1 -value $function:Discovery.ToString() -Force | Out-Null +New-Item -ItemType file -path DiscoverSql.ps1 -value $function:DiscoveryOnWindows.ToString() -Force | Out-Null +New-Item -ItemType file -path DiscoverSql.sh -value $DiscoveryOnLinux -Force | Out-Null # Subscriptions to scan @@ -385,30 +399,12 @@ foreach ($sub in $subscriptions){ #Scan all VMs with SQL server installed using a parallel loop (up to 10 at a time). For that reason function AddVCores is not used #NOTE: ForEach-Object -Parallel is not supported in Runbooks (requires PS v7.1) - if ($UseInRunbook){ - Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object { - $vCores = GetVCores -type 'virtualMachines' -name $_.HardwareProfile.VmSize - $sql_vm = Get-AzSqlVm -ResourceGroupName $_.ResourceGroupName -Name $_.Name -ErrorAction Ignore - if ($sql_vm) { - AddVCores -Tier $sql_vm.Sku -LicenseType $sql_vm.LicenseType -CoreCount $vCores - } - else { - try { - $out = Invoke-AzVMRunCommand -ResourceGroupName $_.ResourceGroupName -Name $_.Name -CommandId 'RunPowerShellScript' -ScriptPath 'DiscoverSql.ps1' -ErrorAction Stop - if ($out.Value[0].Message -contains 'True'){ - $subtotal.unreg_sqlvm += $vCores - } - } - catch { - } - } - } - } - else { - Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object -ThrottleLimit 10 -Parallel { + if ($PSVersionTable.PSVersion.Major -ge 7){ + $vms = Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object -ThrottleLimit 10 -Parallel { $function:GetVCores = $using:GetVCoresDef $vCores = GetVCores -type 'virtualMachines' -name $_.HardwareProfile.VmSize $sql_vm = Get-AzSqlVm -ResourceGroupName $_.ResourceGroupName -Name $_.Name -ErrorAction Ignore + if ($sql_vm) { switch ($sql_vm.Sku) { "Enterprise" { @@ -427,7 +423,7 @@ foreach ($sub in $subscriptions){ default {$($using:subtotal).payg_std += $vCores} } } - "Developer" { + "Developer" { $($using:subtotal).developer += $vCores } "Express" { @@ -436,16 +432,72 @@ foreach ($sub in $subscriptions){ } } else { - try { - $out = Invoke-AzVMRunCommand -ResourceGroupName $_.ResourceGroupName -Name $_.Name -CommandId 'RunPowerShellScript' -ScriptPath 'DiscoverSql.ps1' -ErrorAction Stop - if ($out.Value[0].Message -contains 'True'){ - $($using:subtotal).unreg_sqlvm += $vCores + if ($_.StorageProfile.OSDisk.OSType -eq "Windows"){ + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunPowerShellScript' + ScriptPath = 'DiscoverSql.ps1' + ErrorAction = 'Stop' } } + else { + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunShellScript' + ScriptPath = 'DiscoverSql.sh' + ErrorAction = 'Stop' + } + } + try { + $out = Invoke-AzVMRunCommand @params + if ($out.Value[0].Message.Contains('True')){ + $($using:subtotal).unreg_sqlvm += $vCores + } + } catch { + write-host $params.Name "No acceaa" } } - } + } + } + else { + Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object { + $vCores = GetVCores -type 'virtualMachines' -name $_.HardwareProfile.VmSize + $sql_vm = Get-AzSqlVm -ResourceGroupName $_.ResourceGroupName -Name $_.Name -ErrorAction Ignore + if ($sql_vm) { + AddVCores -Tier $sql_vm.Sku -LicenseType $sql_vm.LicenseType -CoreCount $vCores + } + else { + if ($_.StorageProfile.OSDisk.OSType -eq "Windows"){ + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunPowerShellScript' + ScriptPath = 'DiscoverSql.ps1' + ErrorAction = 'Stop' + } + } + else { + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunShellScript' + ScriptPath = 'DiscoverSql.sh' + ErrorAction = 'Stop' + } + }try { + $out = Invoke-AzVMRunCommand @params + if ($out.Value[0].Message.Contains('True')){ + $subtotal.unreg_sqlvm += $vCores + } + } + catch { + write-host $params.Name "No acceaa" + } + } + } } [system.gc]::Collect()