This article is contributed. See the original author and article here.

 


Background/Scenario


Azure Alerts can be used to proactively notify you when important conditions are found in your monitoring data.  After setting up either metric alerts or log alerts for your workloads, specifically IaaS workloads, there may be times when you need to disable those alerts during a maintenance window. 


 


Depending on the size of your environment and the number of alerts you’ve created, it might be quite a chore to go through each one to disable/enable.


The following will demonstrate how to setup an Azure Automation Runbook to quickly set the status of our IaaS Alerts to either Enabled or Disabled via a webhook.  The webhook will allow us to execute the Azure Automation Runbook from anywhere, like an on-premises workstation, to set the alert status.  The runbook will also take advantage of Azure Resource Graph as a mechanism to search for alerts across all of the available subscriptions.


 


Requirements



 


Configuration/Setup


 


Step 1: Create a metric alert(s) for your IaaS Server(s) based on CPU Usage


If you already have an alert(s) defined with the server name in the alert rule, skip this step.



  1. Navigate to Alerts

  2. New alert rule

  3. Select resource

    • Select a virtual machine




RobertLightner_0-1604163327289.png


 



  1. Select a condition based on Percentage CPU


RobertLightner_1-1604163327295.png


 



  • Set the threshold value and leave the other options with their default value


RobertLightner_2-1604163327301.png


 



  1. Select or create an Action Group (required)

  2. Fill in the remaining Alert rule details and include the server name in the Alert rule name


RobertLightner_3-1604163327303.png


 


 


(For Step 2, chose either 2a or 2b for creating/deploying an Automation Account)


Step 2a: Create an Automation Account – ARM Template Method



  1. Deploying this ARM template [GitHub] will include the following:

    1. Azure Automation Account

    2. Import of PowerShell Modules (Az.Accounts, Az.Monitor, Az.ResourceGraph)

    3. Runbook (SetAzAlertsStatus-Webhook)

    4. Creation of the Automation Run As account is not supported when you’re using an ARM template.



  2. Create a Run As account in Azure portal

    1. Grant the run as account, at a minimum, the ability to manage Alerts. By default, the AAA run as account is granted contributor rights at the subscription it’s deployed into. In production, granting access to the AAA run as account at a Management Group is recommended.




 


Step 2b: Create an Automation Account – Manually Method



  1. Create an Azure Automation Account (AAA)

  2. Grant the AAA run as account, at a minimum, the ability to manage Alerts. By default, the AAA run as account is granted contributor rights at the subscription it’s deployed into. In production, granting access to the AAA run as account at a Management Group is recommended.

  3. Import PowerShell Gallery modules (Az.Accounts, Az.Monitor, Az.ResourceGraph) into the AAA

    1. Under Shared Resources, select Modules.

    2. Select Browse gallery, and then search the Gallery for a module.

    3. Select the module to import, and select Import.

    4. Select OK to start the import process.



  4. Create an Azure Automation runbook (PowerShell Runbook)

    1. In Create an Azure Automation runbook article, step #6, copy SetAzAlertsStatus-Webhook.ps1 from GitHub and paste it into the runbook.




 


Step 3: Create a Webhook for your Runbook



  1. Create a webhook for your Runbook.

    1. From the Runbooks page in the Azure portal, click the runbook that the webhook starts to view the runbook details. Ensure that the runbook Status field is set to Published.

    2. Click Webhook at the top of the page to open the Add Webhook page.

    3. Click Create new webhook to open the Create Webhook page.

    4. Fill in the Name and Expiration Date fields for the webhook and specify if it should be enabled. See Webhook properties for more information about these properties.

    5. Click the copy icon or press Ctrl+C to copy the URL of the webhook. Then record it in a safe place.
      RobertLightner_4-1604163327305.png

       



      1.       Please save your webhook URL. Once you create the webhook, you cannot retrieve the URL again.



    6. Click Parameters, leave it blank, press OK.
      RobertLightner_5-1604163327306.png

       



    7. Click Create to create the webhook.




 


Step 4: Test your Automation Account Runbook via webhook



  1. Download the PowerShell script SetAzAlertsStatus-Webhook-Wrapper.ps1 and save it to your computer.

  2. Edit the script and update line 32 with your webhook URL:

    1. $uri = “<runbook webhook URL you saved earlier>”



  3. Execute the PowerShell script from your local computer.


RobertLightner_6-1604163327308.png


 


 


Conclusion


With an Alert naming convention that includes your server name, this method works very well for quickly enabling or disabling Azure alerts.


I hope you have found this article helpful and thank you for taking the time to read this post.


 


References



 


Disclaimer


The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.