温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Azure实践之如何批量为资源组虚拟机创建alert

发布时间:2020-02-14 11:07:52 来源:网络 阅读:2622 作者:mxy00000 栏目:云计算

    通过上一篇的简介,相信各位对于简单的创建alert,以及Azure monitor使用以及大概有个印象了。基础的使用总是非常简单的,这里再分享一个常用的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"


Azure实践之如何批量为资源组虚拟机创建alert


创建完成后即可在alert中国看到对应的内容

Get-AzureRmAlertRule -ResourceGroupName mxytest -WarningAction SilentlyContinue

Azure实践之如何批量为资源组虚拟机创建alert


也可以通过PowerShell获取到信息

Azure实践之如何批量为资源组虚拟机创建alert


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI