How to create Alerts for Azure Automation Runbook Output?

Murali R 245 Reputation points
2023-10-23T09:37:44.7033333+00:00

Hi Team,

Iam currently working on creating alerts for Azure Application Gateway Backend health using Automation account

I have created the below powershell script and its giving the exact error on which server is unhealthy.

$subscription = "xxxxxxx"
$identity = "xxxxxx"
$null = Disable-AzContextAutosave -Scope Process # Ensures you do not inherit an AzContext in your runbook
$AzureContext = (Connect-AzAccount -Identity -AccountId $identity).context  # Connect to Azure with user-assigned managed identity
$connectionResult = Set-AzContext -Subscription $subscription -DefaultProfile $AzureContextxxxxx/resourceGroups/xxxxx/providers/Microsoft.Network/applicationGateways/xxxxx/backendhealth?api-version=2023-05-01" 
$token = (Get-AzAccessToken).token
$headers = @{'Authorization' = "Bearer $token"}
$result = Invoke-WebRequest -Method POST -Uri $url -Headers $headers -UseBasicParsing
$Backendhealth= $result.Headers.Location
Start-Sleep -Seconds 5
$L= Invoke-WebRequest -Uri $Backendhealth -Headers $headers -UseBasicParsing
$body = $L.Content | ConvertFrom-Json
$serverlist = $body.backendAddressPools.backendHttpSettingsCollection.servers
Foreach ($server in $serverlist)
{
$healthStaus = $server.health
if ("Healthy" -ne $healthStaus)
{
$server
}
}

Is it possible to send an email trigger to the concerned application team saying that their Backend is Unhealthy. For example if we have 50+ applications in one application gateway, that means i need to send the alert to the 50+ application team email whenever their backend is Unhealthy.

Could some one guide me on this.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,645 questions
Azure Application Gateway
Azure Application Gateway
An Azure service that provides a platform-managed, scalable, and highly available application delivery controller as a service.
1,213 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-10-23T20:28:51.94+00:00

    Hi @Murali R

    I suggest going the REST API route and creating an alert. You can use the following API endpoint to create an alert rule:

    POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/metricAlerts/{ruleName}?api-version=2018-03-01-preview
    

    Replace {subscriptionId} with the subscription ID, {resourceGroupName} with the name of the resource group containing the Application Gateway, and {ruleName} with a unique name for the alert rule. For the request body, specify the details of the alert, including the metric to monitor, threshold, and action to take. Here's an example request body:

    {
      "location": "global",
      "properties": {
        "description": "Alert rule for Application Gateway health status",
        "severity": 2,
        "enabled": true,
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Monitor.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Monitor.Models.RuleMetricDataSource",
            "resourceUri": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/applicationGateways/{applicationGatewayName}",
            "metricNamespace": "Microsoft.Network/applicationGateways/backendHealth",
            "metricName": "health",
            "aggregation": {
              "odata.type": "Microsoft.Azure.Management.Monitor.Models.RuleMetricAggregation",
              "operator": "Count",
              "threshold": 1
            }
          },
          "operator": "GreaterThan",
          "threshold": 0,
          "windowSize": "PT5M"
        },
        "actions": [
          {
            "odata.type": "Microsoft.Azure.Management.Monitor.Models.RuleEmailAction",
            "sendToServiceOwners": true,
            "customEmails": []
          }
        ]
      }
    }
    

    In this example, the alert rule is configured to monitor the health status of the Application Gateway and trigger an email notification when the health status is unhealthy. Make any necessary modifications to meet your specific requirements.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.