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:
- The memory usage of the VM exceeds 80% for 5 minutes
- The CPU usage of the VM exceeds 80% for 5 minutes
- The network in of the VM falls below 15K for 5 minutes
These scripts follow the methods for alerting described here:
- Azure Monitor Alerts
- Sending guest OS metrics to Azure Monitor
- Create a metric alert with a Resource Manager template
The PowerShell script will set these alerts for every VM in the specified resource groups. This requires the following:
- Azure PowerShell Module Note: if using the newer version the script will set the AzureRm alias via Enable-AzureRmAlias.
- 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
- 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