cms 类网站,中学网站建设 课设,注册大创网,不同风格的网页通过上一篇的简介#xff0c;相信各位对于简单的创建alert#xff0c;以及Azure monitor使用以及大概有个印象了。基础的使用总是非常简单的#xff0c;这里再分享一个常用的alert使用方法实际工作中#xff0c;不管是日常运维还是做项目#xff0c;我们都需要知道VM的实际… 通过上一篇的简介相信各位对于简单的创建alert以及Azure monitor使用以及大概有个印象了。基础的使用总是非常简单的这里再分享一个常用的alert使用方法 实际工作中不管是日常运维还是做项目我们都需要知道VM的实际性能情况避免出现性能瓶颈因此创建alert是一种非常方便的方式我们可以通过alert第一时间知道系统出现了性能的瓶颈以便尽快采取解决措施。 因此也衍生了一个实际的问题单独为一台VM开启alert很简单但是如果我们需要为一个资源组内十几甚至几十上百台VM统一创建alert则会非常麻烦 在这里分享一个自己写的简单脚本可以通过批量的方式为一个资源组内的所有VM或者是某个单独的VM创建alert省去很多不必要的重复性工作以下是代码的内容# .NOTESCreated with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.134Created on: 2019/1/10 13:19Created by: mxyOrganization: Filename: .DESCRIPTIONA 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 StopWrite-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 alertif ($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-NullWrite-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便发邮件给abcabc.com的alert则可以按照以下方式运行 .\Create-AzureAlert.ps1 -RGName mxytest -MailAddress abcabc.com -Metric CPU -Operation GreaterThan -Threshold 80 -TimeAggregationOperator Average -WindowSize 00:10:00创建完成后即可在alert中国看到对应的内容Get-AzureRmAlertRule -ResourceGroupName mxytest -WarningAction SilentlyContinue也可以通过PowerShell获取到信息 转载于:https://blog.51cto.com/mxyit/2345235