How to enable App Service Proactive Tools via PowerShell
Published Jun 04 2023 08:34 PM 8,283 Views
Microsoft

Azure App Service provides a set of proactive tools that help you monitor and maintain the health of your application. These tools include "Auto-Heal", "Proactive CPU Monitoring", and "Crash Monitoring". In this document, we will guide you through the process of enabling them using PowerShell scripts, so you can easily automate them in your deployment pipeline.

Since each tool has its own configuration settings, we will discuss them individually to provide you with a clear understanding of how to enable and configure them for your app.

 

1. Auto-Heal

Auto-Heal is a feature in Azure App Service that automatically detects and resolves certain application failures. When Auto-Heal is triggered, it can take actions such as restarting the app, recycling the app pool, or sending an email notification.

Auto-Heal will save the config data in ARM definition.

  1. First you could define the Auto-Heal from UI, e.g. below screenshot and save the settings

111.png

  1. Access https://resources.azure.com/  and navigate to your resource, and from config section, you will see the detailed autoHealRules definition

222.png

  1. Transform the JSON content to PowerShell object, use below scripts to update the resource.
    Please note transform json array to powershell object is [] -> @(),
    transform json object to powershell object is {"key":"value","key2":"value2"} -> @{key="value"; key2="value2"}
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"    # Your App's SubscriptionId
$webapp = "<WebAppName>"                        # Your App's Name
$rg = "<ResourceGroupName>"                      # Your App's Resource Group

$autohealRules = @{
    triggers=@{
    privateBytesInKB=888888;
    requests=@{count=1000;timeInterval="00:02:39"}
    statusCodesRange=@(@{
       statusCodes= "400-502";
       path="";
       count=200;
      timeInterval="00:01:00"
    })
    };
    actions=@{actionType="Recycle";minProcessExecutionTime="00:01:39"};
}

$PropertiesObject = @{
    autoHealEnabled = $true;
    autoHealRules = $autohealRules
}

Set-AzResource -Properties $PropertiesObject -ResourceGroupName $rg -ResourceType Microsoft.Web/sites/config -ResourceName "$webapp/web" -ApiVersion 2018-02-01 -Force
  1. Please note that if you have defined a Custom Action to collect dump files, you need to configure the app setting 'WEBSITE_DAAS_STORAGE_CONNECTIONSTRING' first so that the dumps can be saved to the specified location.

    Below is an example of how to define a Custom Action for collecting dumps. Information on how to set the app setting 'WEBSITE_DAAS_STORAGE_CONNECTIONSTRING' is covered in section 3.

333.png

 

$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"    # Your App's SubscriptionId
$webapp = "<WebAppName>"                        # Your App's Name
$rg = "<ResourceGroupName>"                      # Your App's Resource Group

$autohealRules = @{
    triggers=@{
    privateBytesInKB=0;
    statusCodesRange=@(@{
       statusCodes= "500-502";
       path="";
       count=200;
       timeInterval="00:01:00"
    })
    };
    actions=@{
      actionType="CustomAction";
      customAction=@{
        exe="d:\home\data\DaaS\bin\DaasConsole.exe";
        parameters="-CollectKillAnalyze ""Memory Dump"""
      }
      minProcessExecutionTime="00:01:39"
    };
}

$PropertiesObject = @{
    autoHealEnabled = $true;
    autoHealRules = $autohealRules
}

Set-AzResource -Properties $PropertiesObject -ResourceGroupName $rg -ResourceType Microsoft.Web/sites/config -ResourceName "$webapp/web" -ApiVersion 2018-02-01 -Force

2. Proactive CPU Monitoring

Proactive CPU Monitoring is a feature in Azure App Service that helps you monitor and optimize CPU usage for your app. When Proactive CPU Monitoring detects high CPU usage, it can take actions such as scaling in your app or sending an email notification.
Proactive CPU Monitoring will set the settings under extensions/daas/, so you could use the API to set the configuration.

$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"    # Your App's SubscriptionId
$webapp = "<WebAppName>"                        # Your App's Name
$rg = "<ResourceGroupName>"                      # Your App's Resource Group

$path =  "/subscriptions/$subscriptionId/resourceGroups/$rg/providers/Microsoft.Web/sites/$webapp/extensions/daas/api/cpumonitoring?api-Version=2015-08-01"
$monitoringSettings = @{
    Mode="CollectAndKill";
    MonitorDuration=15;
    ThresholdSeconds=60;
    CpuThreshold=90;
    MonitorScmProcesses=$true;
    MaxActions=10;
    MaximumNumberOfHours=480;
}

$payload = $monitoringSettings | ConvertTo-Json
Invoke-AzRestMethod -Path $path -Method "POST" -Payload $payload

Once you set the config, you could navigate to UI and verify the settings.

444.png

3. Crash Monitoring

Crash Monitoring is a feature in Azure App Service that helps you detect and diagnose application crashes. When Crash Monitoring is enabled, it logs detailed error information and failed requests, which you can use to investigate and troubleshoot app issues.
Crash Monitoring will save the data to App Settings.

  1. First you could still set the Crash Monitoring configuration from UI

555.png

  1. Save the configuration and navigate to App Service Configuration Page, you will see the App Settings confgiured for Crash Monitoring

666.png

  1. Refer the settings and use below scripts to set App Configuration
    You could create a Storage Account firstly or using existing Stourage Account, please make sure the SAS connection string is correct.
# Get app configuration
$app = "<WebAppName>"
$rg = "<ResourceGroupName>"
$webapp=Get-AzWebApp -ResourceGroupName $rg -Name $app

# Copy app settings to a new Hashtable
$appSettings = @{}
ForEach ($item in $webapp.SiteConfig.AppSettings) {
    $appSettings[$item.Name] = $item.Value
}

# Add or edit one or more app settings
$appSettings['WEBSITE_CRASHMONITORING_ENABLED'] = 'True'
$appSettings['WEBSITE_CRASHMONITORING_SETTINGS'] = '{"StartTimeUtc":"2023-05-16T00:36:00.000Z","MaxHours":360,"MaxDumpCount":2,"ExceptionFilter":"-f E053534F -f C00000FD.STACK_OVERFLOW"}'
$appSettings['WEBSITE_CRASHMONITORING_USE_DEBUGDIAG']='True'
$appSettings['WEBSITE_DAAS_STORAGE_CONNECTIONSTRING']='DefaultEndpointsProtocol=https;AccountName=windowtwpr7p;AccountKey=xxxxxxxxxxxxxxxxx;EndpointSuffix=core.windows.net'

# Save changes
Set-AzWebApp -ResourceGroupName $rg -Name $app -AppSettings $appSettings

4. References

Co-Authors
Version history
Last update:
‎Jun 04 2023 10:56 PM
Updated by: