英文:
How Create Azure Monitor Metrics Alert using Java SDK
问题
我正在进行一个Java项目,需要使用Azure Java SDK以编程方式创建Azure Monitor指标警报。
我想要能够根据特定的指标和阈值设置警报。例如,当虚拟机的CPU使用率超过某个百分比时,我想要创建一个警报。
是否可以提供一个示例代码片段或指导我如何使用Azure Java SDK实现这一目标?非常感谢任何帮助或建议。
英文:
I am working on a Java project and need assistance with creating an Azure Monitor metrics alert programmatically using the Azure Java SDK.
I would like to be able to set up an alert based on a specific metric and threshold. For example, I want to create an alert when the CPU usage of a virtual machine exceeds a certain percentage.
Could someone provide me with an example code snippet or guide me on how to achieve this using the Azure Java SDK? Any help or suggestions would be greatly appreciated.
答案1
得分: 1
当虚拟机的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");
// 打印选择的订阅
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..
Portal:
以上代码已成功执行并为虚拟机创建了警报规则。
参考:
Monitor-java-metric-alerts-on-critical-performance · GitHub
英文:
> Create an alert when the CPU usage of a virtual machine exceeds a certain percentage.
You can use the below code to create an alert rule using Azure java sdk.
Code:
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..");
}
}
Output:
Selected subscription: xxxxxxxx
Alert rule created..
Portal:
The above code was executed and created an alert rule successfully for the virtual machine.
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论