Skip to main content

Apply Azure monitoring metrics using PowerShell

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

Popular posts from this blog

Microsoft-Windows-User Profiles Service / Id: 1511

Windows cannot find the local profile and is logging you on with a temporary profile. Changes you make to this profile will be lost when you log off. Cause: This event can be caused by the following issues:   Your local profile is corrupt. You no longer have adequate privileges to the profile folders. User profile was deleted manually or by other means. A profile that is manually deleted does not remove the Security Identifier (SID) from the profile list in the registry. If the SID is present, Windows will try to load the profile by using the ProfileImagePath that points to a nonexistent path. Therefore, the profile cannot be loaded. Occasionally, Windows might not read your user profile correctly, for example, if your antivirus software is scanning your computer while you try to log on. Resolutions 1) Sign out and restart the computer Try restarting your computer and signing on with your user account again to resolve the issue. 2) Delete the error SID and ...

Useful Open Manage Server Administrator(omreport) commands for Dell server

The Open Manage command line interface allows you to check the state of your hardware and do hardware settings. Below commands are quick reference for Dell server, you can always use "omhelp omreport" commands to show help manual of "omreport". 1. System component properties. System logs omreport system alertlog  --View alert log. omreport system esmlog    --View hardware (Embedded System Management) log. omreport system cmdlog    --View command log. OS omreport system operatingsystem Summary of all system components(Including major components versions) omreport system summary Version report for all updateable components. (Including BIOS,OS and controller versions) omreport system version 2. Chassis component properties. Power (Check power peak stats and threshold) omreport chassis pwrmonitoring Check power capacity omreport chassis pwrmanagement Power health status omreport chassi...