gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:13:02 26 4
gpt4 key购买 nike

我正在开发一个 Java 项目,需要使用 Azure Java SDK 以编程方式创建 Azure Monitor 指标警报方面的帮助。

我希望能够根据特定指标和阈值设置警报。例如,我想在虚拟机的 CPU 使用率超过一定百分比时创建警报。

有人可以为我提供示例代码片段或指导我如何使用 Azure Java SDK 实现此目的吗?任何帮助或建议将不胜感激。

最佳答案

Create an alert when the CPU usage of a virtual machine exceeds a certain percentage.

您可以使用以下代码通过 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..

enter image description here

门户:

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

enter image description here

引用:

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

关于azure - 如何使用 Java SDK 创建 Azure Monitor 指标警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76556662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com