Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to batch create alert for resource group virtual machines in Azure practice

2024-05-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Through the introduction to the previous article, I believe you have an impression of the simple creation of alert, and the use of Azure monitor. The basic usage is always very simple, so here is a common way to use alert.

In practical work, whether it is daily operation and maintenance or doing projects, we all need to know the actual performance of VM to avoid performance bottlenecks, so creating alert is a very convenient way, we can know the performance bottleneck of the system at the first time through alert, so as to take solutions as soon as possible.

Therefore, a practical problem arises. It is easy to enable alert for a single VM, but it will be very troublesome if we need to create an alert for a dozen or even dozens of VM in a resource group.

To share a simple script written by yourself, you can create alert for all VM in a resource group, or for a single VM in batch, saving a lot of unnecessary repetitive work. Here is the content of the code.

Param ([parameter (Mandatory = $true)] [string] $RGName,# resource group name [parameter (Mandatory = $false)] [string] $VmName,#VM name [parameter (Mandatory = $true)] [string] $MailAddress,# email address [parameter (Mandatory = $false)] [ValidateSet ("CPU", "Memory")] [string] $Metric = "CPU", # for which metric to create alert For convenience, there are only two kinds of CPU and memory: [parameter (Mandatory = $false)] [ValidateSet ("GreaterThan", "GreaterThanOrEqual", "LessThan", "LessThanOrEqual")] [string] $Operation = "GreaterThan", # operating conditions [parameter (Mandatory = $false)] [int] $Threshold = 50 # threshold [parameter (Mandatory = $false)] [ValidateSet ("Average", "Last", "Maximum", "Minimum", "Total")] # calculation method Average or maximum [string] $TimeAggregationOperator = "Average", [parameter (Mandatory = $false)] [TimeSpan] $WindowSize = "00:05:00" # timestamp) 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% HV% MV% S') + "*" + $Message)} else {if ($ForegroundColor) {Write-Host ($(Get-Date-UFormat'% Y/%m/%d% HV% MRV% S') + "*" + $Message)-ForegroundColor $ForegroundColor} Else {Write-Host ($(Get-Date-UFormat'% Y/%m/%d% HVR% MGV% S') + "*" + $Message)} # Get metric nameswitch ($Metric) {Memory {$MetricName = "\ Memory\% Committed Bytes In Use"} CPU {$MetricName = "\ Processor Information (_ Total)\% Processor Time"} default {#} # Find the vm if vmname parameter specifiedtry {$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 = $AlertName + "_ vm.Name _ "+ $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 {Error [0] .Exception.Message}

You can see that the script is very simple. Here is an example of how to run the script. For example, if you want to create an alert for all VM under the resource group mxytest whose CPU10 is greater than 80 within minutes, you can email abc@abc.com as follows

.\ Create-AzureAlert.ps1-RGName mxytest-MailAddress "abc@abc.com"-Metric CPU-Operation GreaterThan-Threshold 80-TimeAggregationOperator Average-WindowSize "00:10:00"

After the creation, you can see the corresponding content in alert China.

Get-AzureRmAlertRule-ResourceGroupName mxytest-WarningAction SilentlyContinue

Information can also be obtained through PowerShell

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report