如何使用Azure Monitor创建警报处理规则- Java SDK

efzxgjgh  于 2023-06-30  发布在  Java
关注(0)|答案(1)|浏览(139)

我试图使用Java中的Azure Monitor SDK创建警报处理规则,但我在找到正确的方法时遇到了麻烦。**我想以编程方式创建警报处理规则 * 我已经在Java项目中设置了Azure Management SDK,并使用Azure Active Directory凭据进行身份验证。**但是,我找不到SDK中用于创建警报处理规则的直接方法或类。
我已经查看了Microsoft文档,但找不到此场景的具体细节或代码示例。
https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-processing-rules?tabs=portal

kjthegm6

kjthegm61#

使用Azure Monitor创建警报处理规则- Java SDK
您可以使用下面的代码使用Azure Monitor java-SDK创建警报处理规则。

代码:

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();
        }
    }
}

输出:

Alert Processing rule is created.....

入口:

参考:

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

相关问题