Creating Alerts Using Azure PowerShell

Azure alerts have been around for a while now and most of us are already aware of it. So, without wasting too much time in explaining what Azure alerts are, we will simply dive deep into the action.

Just to keep it short, Azure alert is a mechanism using which service administrators / users can get heads up about happenings of their Azure resources which basically helps them to take corrective required actions and monitor effectively.

We will create an Azure storage account which will be our Azure resource. Then, we will try to create an alert for it, which will trigger an email to me whenever total blob requests exceeds five. (Note, this is just for demo purposes; real world scenarios could be different depending on the need.)

Now, in order to do it using PowerShell, of course the pre-requisite is to have Azure PowerShell installed on your machine and since I have created the storage account using ARM mode, so it is mandatory that Azure PowerShell should contain required modules for Azure ARM mode. If you do not have the Azure PowerShell installed, go ahead and visit this link How to install and configure Azure PowerShell

Now, let’s launch Azure PS session and log in to our subscription, using the following commands: 

  1. Login-AzureRmAccount  
  2.   
  3. Select-AzureRmSubscription -SubscriptionName "your_subscription_name"   

Once we are in the subscription, let’s see the details of Azure PS command and its parameters which can create alerts for us.

Add-AzureRmMetricAlertRule

Now, the parameters it expects are as shown below.

  • Name –Name of your alert.
  • Description – Description of the alert.
  • Resource group – Name of the resource group containing target resource on which alert is being created.
  • Location – Location of the resource for which alert is being added.
  • MetricName of the monitoring metric. This value will vary depending upon the resource for which alert is being created. For us, this is “TotalRequests”. You can get the entire list of available metrics for any resource using command Get-AzureRmMetricDefinition
  • OperatorThis is the conditional operator required for configuring the condition before triggering an alert. E.g. in our scenario, If TotalReqeusts goes beyond five then alert will be triggered. So operator value becomes “GreaterThan”. Accepted value for this parameter are GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
  • WindowSizeThe window size over which Azure will analyze selected metric for triggering an alert. This parameter accepts input in timespan format.
  • TimeAggregationOperatorThe time aggregation operator which will be applied to the selected window size to evaluate the condition. Accepted values for this parameter are Average, Last, Maximum, Minimum, and Total.
  • TargetResourceIdThe resource if of Azure resource for which alert is being created.
  • DisableRule - Switch to mark a rule disabled.
  • ThresholdThreshold value beyond for rule evaluation.

All the required parameters to create an Azure alert are shown above. However, you might be wondering to whom the alert should be sent if the rule is triggered. And, how can we specify it in command?

There is one last parameter called “Actions” but it puzzled me a lot and took a while to understand.

  • ActionsThis parameter expects the object of type Microsoft.Azure.Management.Insights.Modes.RuleAction which leads to confusion. The simple solution to initialize this param is simple. All you need to do is to take help of New-AzureRmAlertRuleEmail.
    Here is how:

Suppose, we wish to send alert mail to other users including service administrators. Then, I will need to specify the email addresses of those users. Let's create a PS variable which will host those mail ids for you.

  1. $mailTo = New-Object "System.Collections.Generic.List[String]"  
  2. $mailTo.Add(“[email protected]”)  
  3. $mailTo.Add(“[email protected]”)  

Let’s use New-AzureRmAlertRuleEmail to assign this collection to CustomEmails property and catch the output in PS variable, called $action.

  1. $action = New-AzureRmAlertRuleEmail -CustomEmails $mailTo  

Now, in this action variable, you can control if you want to send out alert mail to service administrators or not by simply initializing a flag named SendToServiceOwners. So, let’s do that.

  1. $action.SendToServiceOwners = $true  

And, we have now initialized our action variable which we will be assigning to Action property of our initial command to create alert rule.

  1. Add-AzureRmMetricAlertRule -Location “west us” -MetricName “TotalRequests” -Name “TotalRequestsRule” -Operator GreaterThan -ResourceGroup “Bhushan-Test-RG” -TargetResourceId “/subscriptions/xxxxx-xxxx-475f-xxx-xxxxxxxx/resourceGroups/bhushan-test-rg/providers/Microsoft.Storage/storageAccounts/bhushantest/services/blob” -Threshold “5” -TimeAggregationOperator Average -WindowSize “02:00:00” -Actions $action -Description “This rule sends out alert if total requests are exceeded beyond 5”   

The above command takes a few seconds to create your alert rule. Once completed, you can see the same by logging into Azure portal.

alert rule

If the threshold gets exceeded and alert condition is fulfilled, then expect an alert notification in the inbox. This might look something like the following:

notification



This is how you can create Azure alert rule using PowerShell and monitor your Azure resources closely.

Up Next
    Ebook Download
    View all
    Learn
    View all