Search code examples
azureazure-resource-managerazure-monitoringazure-sdk-for-javaazure-java-tools

How to Create an Alert Processing Rule using the Azure Monitor - Java SDK


I'm trying to create an Alert Processing Rule using the Azure Monitor SDK in Java, but I'm having trouble finding the right approach. I want to programmatically create an Alert Processing Rule I have already set up the Azure Management SDK in my Java project and have authentication working using the Azure Active Directory credentials. However, I couldn't find a direct method or class for creating Alert Processing Rules in the SDK.

I have already checked the Microsoft documentation, but I couldn't find specific details or code examples for this scenarios.

https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-processing-rules?tabs=portal


Solution

  • Create an Alert Processing Rule using the Azure Monitor - Java SDK

    You can use the below code to create an alert processing rule using the Azure Monitor java-SDK.

    Code:

    import com.azure.resourcemanager.alertsmanagement.models.AddActionGroups;
    import com.azure.resourcemanager.alertsmanagement.models.AlertProcessingRule;
    import com.azure.resourcemanager.alertsmanagement.models.AlertProcessingRuleProperties;
    import com.azure.core.credential.TokenCredential;
    import com.azure.core.management.AzureEnvironment;
    import com.azure.identity.DefaultAzureCredentialBuilder;
    import com.azure.core.management.profile.AzureProfile;
    import com.azure.resourcemanager.alertsmanagement.AlertsManagementManager;
    
    import java.util.Arrays;
    
    
    public final class App {
    
        public static boolean runSample(com.azure.resourcemanager.alertsmanagement.AlertsManagementManager azureResourceManager) {
    
                AlertProcessingRule ma = azureResourceManager
                        .alertProcessingRules()
                        .define("AddActionGroupsBySeverity")
                        .withRegion("Global")
                        .withExistingResourceGroup("v-venkat-mindtree")
                        .withProperties(
                    new AlertProcessingRuleProperties()
                        .withScopes(Arrays.asList("/subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.Compute/virtualMachines/vm678"))
                        .withActions(
                            Arrays
                                .asList(
                                    new AddActionGroups()
                                        .withActionGroupIds(
                                            Arrays
                                                .asList(
                                                    "/subscriptions/xxx/resourceGroups/xxx/providers/microsoft.insights/actiongroups/actiongrp326"))))
                        .withDescription("Add ActionGroup1 to all alerts in the subscription")
                        .withEnabled(true))
                .create();
                return true;
            }
        public static void main(String[] args) {
            try {
    
                final AzureProfile profile = new AzureProfile("<YOUR_TENANT_ID>", "<YOUR_SUBSCRIPTION_ID>", AzureEnvironment.AZURE);
                final TokenCredential credential = new DefaultAzureCredentialBuilder()
                    .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                    .build();
    
                AlertsManagementManager azureResourceManager = AlertsManagementManager
                    .authenticate(credential, profile);
    
               System.out.println("Alert Processing rule is created.....");
    
                runSample(azureResourceManager);
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }
    

    Output:

    Alert Processing rule is created.....
    

    enter image description here

    Portal:

    enter image description here

    Reference:

    Azure-sdk-for-java/sdk/alertsmanagement· GitHub