frameworkResources/Scripts/enable_detailed_monitoring.ps1
|
Param( [string]$EnvironmentName, [string]$ScriptsFolderPath = ".\Resources\Scripts" ) . "$ScriptsFolderPath\log_monitoring_common.ps1" . "$ScriptsFolderPath\dashboard_common.ps1" . "$ScriptsFolderPath\common.ps1" $vms = [System.Collections.ArrayList]@() $script:resource = $null function GetResourceGroup { $script:resource = CheckIfEnvironmentExits -Environment $EnvironmentName } 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 ModifyMonitoringTag { param( [bool]$TagValue = $true ) $script:resource.Tags["EnableDetailedMonitoring"] = $TagValue 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) ModifyMonitoringTag -TagValue $true EnableDetailedMonitoring } try { ShowExecutingCommand if (-not (Get-AzContext)) { NoContext Connect-AzAccount if (Get-AzContext) { ExecuteCommands } } else { ExecuteCommands } } catch { ModifyMonitoringTag -TagValue $false Write-Error $($_.Exception.Message) } |