Azure Monitor - Apply Azure monitoring metrics using PowerShell
#Creating an action group as an object
$action = [Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup]::New("/subscriptions/SubscriptionIDUpdatehere/resourceGroups/alert/providers/Microsoft.Insights/actiongroups/alertaction")
#Setting up condition as an object
$condition1 = New-AzMetricAlertRuleV2Criteria `
-MetricName "Percentage CPU" `
-MetricNameSpace "Microsoft.Compute/virtualMachines" `
-TimeAggregation Average `
-Operator GreaterThan `
-Threshold 96
$condition2 = New-AzMetricAlertRuleV2Criteria `
-MetricName "Available Memory Bytes" `
-MetricNameSpace "Microsoft.Compute/virtualMachines" `
-TimeAggregation Average `
-Operator LessThan `
-Threshold 1000000000
$condition3 = New-AzMetricAlertRuleV2Criteria `
-MetricName "Data Disk IOPS Consumed Percentage" `
-MetricNameSpace "Microsoft.Compute/virtualMachines" `
-TimeAggregation Average `
-Operator GreaterThan `
-Threshold 95
$condition4 = New-AzMetricAlertRuleV2Criteria `
-MetricName "OS Disk IOPS Consumed Percentage" `
-MetricNameSpace "Microsoft.Compute/virtualMachines" `
-TimeAggregation Average `
-Operator GreaterThan `
-Threshold 95
$condition5 = New-AzMetricAlertRuleV2Criteria `
-MetricName "VmAvailabilityMetric" `
-MetricNameSpace "Microsoft.Compute/virtualMachines" `
-TimeAggregation Average `
-Operator LessThan `
-Threshold 1
$subscriptionID = "Your Subscription ID"
Set-AzContext -Subscription $subscriptionID
$vms = Get-AzVm
foreach ($vm in $vms){
$VMname = ($vm.Name)
$ClientName = "Your Client Name"
$resourceGroupName = az vm list --query "[?name=='$VMname'].resourceGroup" --output tsv
$Location = az vm show --resource-group $resourceGroupName --name $vmName --query 'location' --output tsv
#creating an alert rule for percentage CPU
Add-AzMetricAlertRuleV2 `
-Name "$ClientName - Virtual machines - $VMname - PercentageCPU - Critical" `
-ResourceGroupName $resourceGroupName `
-WindowSize 0:30 `
-Frequency 0:15 `
-TargetResourceScope "/subscriptions/$subscriptionID/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachines/$VMname" `
-TargetResourceType "Microsoft.Compute/virtualMachines" `
-TargetResourceRegion $Location `
-Description "Action will be triggered when CPU usage is greater than Threshold" `
-Severity 0 `
-ActionGroup $action `
-Condition $condition1
#creating an alert rule for Available Memory bytesaction
Add-AzMetricAlertRuleV2 `
-Name "$ClientName - Virtual machines - $VMname - Available Memory Bytes - Critical" `
-ResourceGroupName $resourceGroupName `
-WindowSize 1:0 `
-Frequency 0:30 `
-TargetResourceScope "/subscriptions/$subscriptionID/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachines/$VMname" `
-TargetResourceType "Microsoft.Compute/virtualMachines" `
-TargetResourceRegion $Location `
-Description "Action will be triggered when Available Memory Bytes is less than Threshold" `
-Severity 0 `
-ActionGroup $action `
-Condition $condition2
#creating an alert rule for Data Disk IOPS Consumed Percentage
Add-AzMetricAlertRuleV2 `
-Name "$ClientName - Virtual machines - $VMname - Data Disk IOPS Consumed Percentage - Critical" `
-ResourceGroupName $resourceGroupName `
-WindowSize 0:30 `
-Frequency 0:30 `
-TargetResourceScope "/subscriptions/$subscriptionID/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachines/$VMname" `
-TargetResourceType "Microsoft.Compute/virtualMachines" `
-TargetResourceRegion $Location `
-Description "Action will be triggered when Data Disk IOPS usage is Greater than Threshold" `
-Severity 0 `
-ActionGroup $action `
-Condition $condition3
#creating an alert rule for Data Disk IOPS Consumed Percentage
Add-AzMetricAlertRuleV2 `
-Name "$ClientName - Virtual machines - $VMname - OS Disk IOPS Consumed Percentage - Critical" `
-ResourceGroupName $resourceGroupName `
-WindowSize 0:30 `
-Frequency 0:30 `
-TargetResourceScope "/subscriptions/$subscriptionID/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachines/$VMname" `
-TargetResourceType "Microsoft.Compute/virtualMachines" `
-TargetResourceRegion $Location `
-Description "Action will be triggered when OS Disk IOPS usage is Greater than Threshold" `
-Severity 0 `
-ActionGroup $action `
-Condition $condition4
#creating an alert rule
Add-AzMetricAlertRuleV2 `
-Name "$ClientName - Virtual machines - $VMname - VM Availability - Critical" `
-ResourceGroupName $resourceGroupName `
-WindowSize 0:15 `
-Frequency 0:15 `
-TargetResourceScope "/subscriptions/$subscriptionID/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachines/$VMname" `
-TargetResourceType "Microsoft.Compute/virtualMachines" `
-TargetResourceRegion $Location `
-Description "Action will be triggered when VM Availability is less than Threshold." `
-Severity 0 `
-ActionGroup $action `
-Condition $condition5
}
Comments
Post a Comment