framework/Resources/Scripts/enable_detailed_monitoring.ps1
|
Param( [string]$EnvironmentName, [string]$ScriptsFolderPath = ".\Resources\Scripts" ) . "$ScriptsFolderPath\log_monitoring_common.ps1" . "$ScriptsFolderPath\dashboard_common.ps1" $vms = [System.Collections.ArrayList]@() $script:resource = $null function GetResourceGroup { $script:resource = Get-AzResourceGroup | Where-Object { $_.Tags -and $_.Tags.Contains("EnvironmentName") -and $_.Tags["EnvironmentName"] -eq $EnvironmentName } -ErrorAction Stop if (-not $script:resource) { throw "No such environment exists" } } function GetUploadedVms { param( [string]$ResourceGroup ) $script:vms = @(Get-AzVM -ErrorAction Stop -ResourceGroupName $ResourceGroup | Where-Object { $_.Tags.ContainsKey("InstallMode") } | ForEach-Object { [PSCustomObject]@{ Name = $_.Name Location = $_.Location VmSize = $_.HardwareProfile.VmSize OsType = $_.StorageProfile.OsDisk.OsType Zone = ($_.Zones[0]) Publisher = $_.StorageProfile.ImageReference.Publisher Offer = $_.StorageProfile.ImageReference.Offer Sku = $_.StorageProfile.ImageReference.Sku Version = $_.StorageProfile.ImageReference.Version NIC = $_.NetworkProfile.NetworkInterfaces.id StorageAccountType = $_.StorageProfile.OsDisk.ManagedDisk.StorageAccountType Disk = $_.StorageProfile.OsDisk.Name Tags = $_.Tags } } ) } function AddMonitoringTag { $script:resource.Tags["EnableDetailedMonitoring"] = $true Set-AzResourceGroup -Name $script:resource.ResourceGroupName -Tag $($script:resource.Tags) | Out-Null } function CheckIfDetailedLoggingAlreadyEnabled { if ($script:resource.Tags.ContainsKey("EnableDetailedMonitoring")) { $enableDetailedMonitoring = [System.Convert]::ToBoolean($script:resource.Tags["EnableDetailedMonitoring"]) if ($enableDetailedMonitoring) { throw "Detailed monitoring is already enabled" } } } function EnableDetailedMonitoring { InstallMonitoringResources InstallMonitoringOnVm } function InstallMonitoringResources { Install-NcAzMonitoringResources -ResourceGroupName $($script:resource.ResourceGroupName) } function InstallMonitoringOnVm { $enableDetailedMonitoring = [System.Convert]::ToBoolean($script:resource.Tags["EnableDetailedMonitoring"]) if ($enableDetailedMonitoring) { $caches = @() foreach ($vm in $script:vms) { Install-NcAzMonitoringOnVM -ResourceGroupName $($script:resource.ResourceGroupName) -VmName $vm.Name if ($vm.Tags.ContainsKey("Caches")) { $caches += $vm.Tags["Caches"] -split ',' } } $caches = $caches | Select-Object -Unique foreach ($cache in $caches) { Update-NcAzDashboards -ScriptsFolderPath $ScriptsFolderPath -ResourceGroupName $($script:resource.ResourceGroupName) -CacheName $cache } } } function ExecuteCommands { GetResourceGroup CheckIfDetailedLoggingAlreadyEnabled GetUploadedVms -ResourceGroup $($script:resource.ResourceGroupName) AddMonitoringTag EnableDetailedMonitoring } try { ShowExecutingCommand if (-not (Get-AzContext)) { Connect-AzAccount if (Get-AzContext) { ExecuteCommands } } else { ExecuteCommands } } catch { Write-Error $($_.Exception.Message) } |