如何使用Java SDK创建Azure Monitor指标警报

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

我正在处理一个Java项目,需要使用Azure Java SDK以编程方式创建Azure Monitor指标警报的帮助。
我希望能够根据特定指标和阈值设置警报。例如,我想在虚拟机的CPU使用率超过特定百分比时创建警报。
有人可以提供一个示例代码片段或指导我如何使用Azure Java SDK实现这一点吗?任何帮助或建议将不胜感激。

ojsjcaue

ojsjcaue1#

当虚拟机的CPU使用率超过特定百分比时创建警报。
您可以使用以下代码使用Azure java sdk创建警报规则。

代码:

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.monitor.models.MetricAlert;
import com.azure.resourcemanager.monitor.models.MetricAlertRuleCondition;
import com.azure.resourcemanager.monitor.models.MetricAlertRuleTimeAggregation;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.AzureResourceManager;

import java.time.Duration;

public final class App {

    public static boolean runSample(com.azure.resourcemanager.AzureResourceManager azureResourceManager) {

            String rgName="your-resourcegrp";
            MetricAlert ma = azureResourceManager.alertRules().metricAlerts().define("Critical performance alert")
                    .withExistingResourceGroup(rgName)
                    .withTargetResource("/subscriptions/xxxxx/resourceGroups/xxxproviders/Microsoft.Compute/virtualMachines/vm678")
                    .withPeriod(Duration.ofMinutes(5))
                    .withFrequency(Duration.ofMinutes(1))
                    .withAlertDetails(3, "This alert rule is for U5 - Single resource-multiple criteria - with dimensions - with star")
                    .withActionGroups("/subscriptions/xxxxxx/resourceGroups/xxxx/providers/microsoft.insights/actiongroups/actiongroup1")
                    .defineAlertCriteria("Metric1")
                        .withMetricName("Percentage CPU")
                        .withCondition(MetricAlertRuleTimeAggregation.TOTAL, MetricAlertRuleCondition.GREATER_THAN, 80)
                        .attach()
                    .create();
            return true;
        }
   /**
     * Main entry point.
     * @param args the parameters
     */
    public static void main(String[] args) {
        try {

            final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
            final TokenCredential credential = new DefaultAzureCredentialBuilder()
                .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                .build();

            AzureResourceManager azureResourceManager = AzureResourceManager
                .configure()
                .withLogLevel(HttpLogDetailLevel.BASIC)
                .authenticate(credential, profile)
                .withSubscription("your-subscription-id");

            // Print selected subscription
            System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());

            runSample(azureResourceManager);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        System.out.println("Alert rule created..");
    }
}

输出:

Selected subscription: xxxxxxxx
Alert rule created..

入口:

已执行上述代码并成功为虚拟机创建了警报规则。

参考:

Monitor-java-metric-alerts-on-critical-performance · GitHub

相关问题