通過上一篇的簡介,相信各位對于簡單的創建alert,以及Azure monitor使用以及大概有個印象了?;A的使用總是非常簡單的,這里再分享一個常用的alert使用方法
實際工作中,不管是日常運維還是做項目,我們都需要知道VM的實際性能情況,避免出現性能瓶頸,因此創建alert是一種非常方便的方式,我們可以通過alert第一時間知道系統出現了性能的瓶頸,以便盡快采取解決措施。
因此,也衍生了一個實際的問題,單獨為一臺VM開啟alert很簡單,但是如果我們需要為一個資源組內十幾甚至幾十上百臺VM統一創建alert,則會非常麻煩
在這里分享一個自己寫的簡單腳本,可以通過批量的方式為一個資源組內的所有VM,或者是某個單獨的VM創建alert,省去很多不必要的重復性工作,以下是代碼的內容
<# .NOTES =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.134 Created on: 2019/1/10 13:19 Created by: mxy Organization: Filename: =========================================================================== .DESCRIPTION A description of the file. #> param ( [parameter(Mandatory = $true)] [string]$RGName,#資源組名稱 [parameter(Mandatory = $false)] [string]$VmName,#VM名稱 [parameter(Mandatory = $true)] [string]$MailAddress,#郵件地址 [parameter(Mandatory = $false)] [ValidateSet("CPU", "Memory")] [string]$Metric = "CPU",#需要針對哪個metric創建alert,方便起見這里目前只是設置了CPU和內存兩種 [parameter(Mandatory = $false)] [ValidateSet("GreaterThan", "GreaterThanOrEqual", "LessThan", "LessThanOrEqual")] [string]$Operation = "GreaterThan",#操作條件 [parameter(Mandatory = $false)] [int]$Threshold = 50,#閾值 [parameter(Mandatory = $false)] [ValidateSet("Average", "Last", "Maximum", "Minimum", "Total")]#計算方式,是平均還是最大等 [string]$TimeAggregationOperator = "Average", [parameter(Mandatory = $false)] [TimeSpan]$WindowSize = "00:05:00"#時間戳 ) function Write-DateTimeMessage { param ( [parameter(Mandatory = $false)] [switch]$Warning, [parameter(Mandatory = $true)] [string]$Message, [parameter(Mandatory = $false)] [string]$ForegroundColor ) if ($Warning) { Write-Warning ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) } else { if ($ForegroundColor) { Write-Host ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) -ForegroundColor $ForegroundColor } else { Write-Host ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) } } } #Get metric name switch ($Metric) { Memory { $MetricName = "\Memory\% Committed Bytes In Use" } CPU { $MetricName = "\Processor Information(_Total)\% Processor Time" } default { #<code> } } #Find the vm if vmname parameter specified try { $Error.Clear() if ($VmName) { Write-DateTimeMessage "Trying to find vm $VmName in resource group $RGName" $vms = Get-AzureRmVM -ResourceGroupName $RGName -Name $VmName -ErrorAction Stop Write-DateTimeMessage "vm $VmName Found in resource group $RGName" } else { $vms = Get-AzureRmVM -ResourceGroupName $RGName -ErrorAction Stop } # Create action email $actionEmail = New-AzureRmAlertRuleEmail -CustomEmail $MailAddress -WarningAction SilentlyContinue # Get resource id and add alert if ($vms -ne $null) { foreach ($vm in $vms) { $vmID = $vm.id $AlertName = $vm.Name + "_Alert_" + $Metric + "_" + $Operation + "_" + $Threshold + "_" + $actionEmail.CustomEmails $Error.Clear() Write-DateTimeMessage "Trying to add alert for vm $($vm.Name) ..." Add-AzureRmMetricAlertRule -Name $AlertName -Location "ChinaEast" -ResourceGroup $RGName -TargetResourceId $vmID -MetricName $MetricName -Operator $Operation -Threshold $Threshold -WindowSize $WindowSize -TimeAggregationOperator $TimeAggregationOperator -Action $actionEmail -ErrorAction 'Stop' -WarningAction 'SilentlyContinue' | Out-Null Write-DateTimeMessage "Add alert for vm $($vm.Name) successfully!" } } else { Write-DateTimeMessage "No vm in resource group $RGName" } } catch { Write-DateTimeMessage $Error[0].Exception.Message }
可以看到腳本很簡單,運行方法這里舉個例子,比如要為mxytest這個資源組下的所有VM創建CPU10分鐘之內大于80便發郵件給abc@abc.com的alert,則可以按照以下方式運行
.\Create-AzureAlert.ps1 -RGName mxytest -MailAddress "abc@abc.com" -Metric CPU -Operation GreaterThan -Threshold 80 -TimeAggregationOperator Average -WindowSize "00:10:00"
創建完成后即可在alert中國看到對應的內容
Get-AzureRmAlertRule -ResourceGroupName mxytest -WarningAction SilentlyContinue
也可以通過PowerShell獲取到信息
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。