Azure实践之如何批量为资源组虚拟机创建alert
发表于:2025-02-21 作者:千家信息网编辑
千家信息网最后更新 2025年02月21日,通过上一篇的简介,相信各位对于简单的创建alert,以及Azure monitor使用以及大概有个印象了。基础的使用总是非常简单的,这里再分享一个常用的alert使用方法实际工作中,不管是日常运维还是
千家信息网最后更新 2025年02月21日Azure实践之如何批量为资源组虚拟机创建alert
通过上一篇的简介,相信各位对于简单的创建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 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 = $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获取到信息
资源
方式
可以通过
实际
性能
内容
名称
方法
瓶颈
脚本
还是
邮件
工作
运行
最大
代码
使用方法
例子
信息
内存
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全宣传画
最新ssr服务器分享2018
计算机网络技术第三版考试题
为什么spss要拆分数据库
潍坊联想服务器代理哪家服务好
软件开发人员用什么系统
御芝林软件开发怎么样
长沙系统软件开发工程师
河北手机软件开发销售价格
批量导出批量导入数据库表
中山数字软件开发价钱
一年级最简单的网络安全画
游戏服务器没有用户名怎么办
厦门网络安全观后感
打印服务器无法打印机
什么服务器可以跑酷
软件开发费计算方法
魔兽最平衡的服务器
捷锐网络技术是什么公司
服务器两边通吗
外国数据库
车家互联网科技有限公司
三级网络技术培训视频教程
比弗数据库连接失败
驻马店市网络安全宣传周
中兴基带软件开发如何
服务器安全狗怎么加
神奇宝贝手机版服务器下载
服务器最低内存
hp服务器硬盘未经过验证