Skip to content

PowerShell script and ARM templates to create new-style alerts in Azure Monitor for Windows VMs

License

Notifications You must be signed in to change notification settings

ssemyan/CreateAzureMonitorVmAlerts

Repository files navigation

Create Azure Monitor VM Alerts

Note: This project uses the new style of alert in Azure Monitor. For classic style alerts, refer to https://github.com/ssemyan/CreateClassicAzureVmAlerts

Note 2: This template only works for Windows VMs

This PowerShell script and ARM templates will create alerts in Azure Monitor for each VM using that email specified addresses when:

  1. The memory usage of the VM exceeds 80% for 5 minutes
  2. The CPU usage of the VM exceeds 80% for 5 minutes
  3. The network in of the VM falls below 15K for 5 minutes

These scripts follow the methods for alerting described here:

The PowerShell script will set these alerts for every VM in the specified resource groups. This requires the following:

  1. Azure PowerShell Module Note: if using the newer version the script will set the AzureRm alias via Enable-AzureRmAlias.
  2. VM Guest Diagnostics extension to be installed with Azure Monitor as a sink. Learn more about this here: https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/diagnostics-windows
  3. Managed Service Identity to be enabled for the VM so it can send metrics to Azure Monitor. Learn more about this here: https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/

You can enable a Managed Service Identity for a VM using the following PowerShell commands:

$rg = 'resource_group_name'
$vm = Get-AzVm -ResourceGroupName $rg -Name myvm
Update-AzVM -ResourceGroupName $rg -VM $vm -AssignIdentity:$SystemAssigned

To remove the Managed Service Identity:

$rg = 'resource_group_name'
$vm = Get-AzVm -ResourceGroupName $rg -Name myvm
Update-AzVM -ResourceGroupName $rg -VM $vm -IdentityType None

To install the Windows VM Extension while creating the alerts, set the value of $addExtension to $TRUE and update the values for $existingdiagnosticsStorageAccountName and $existingdiagnosticsStorageResourceGroup

To only install the extension and not create alerts, set the value of $addAlerts to $FALSE

To use this script edit the details in CreateAlertsOnAllVmsInResourceGroups.ps1:

First update the list of resource groups to search:

$resourceGroupsToProcess = @('my_group', 'my_group_2')

Alternatively, if you want to run against all resource groups uncomment out this line:

#$resourceGroupsToProcess = @()

Then update the email(s) to send alerts to (comma-separated if more than one):

$sendToEmails = 'myemail@company.com,myemail2@company.com'

Finally, run the following command in PowerShell.

.\CreateAlertsOnAllVmsInResourceGroups.ps1

About

PowerShell script and ARM templates to create new-style alerts in Azure Monitor for Windows VMs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published