如何使用Java SDK创建Azure Monitor指标警报
使用Azure Java SDK创建Azure Monitor指标警报(虚拟机CPU使用率示例)
1. 添加依赖
先在Maven项目中引入Azure Monitor Alerts SDK和身份验证相关依赖:
<dependencies> <!-- Azure Monitor Alerts SDK --> <dependency> <groupId>com.azure.resourcemanager</groupId> <artifactId>azure-resourcemanager-monitor</artifactId> <version>2.34.0</version> </dependency> <!-- Azure Identity 用于身份认证 --> <dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.12.0</version> </dependency> </dependencies>
2. 核心实现代码
以下是创建虚拟机CPU使用率超阈值警报的完整代码:
import com.azure.core.credential.DefaultAzureCredential; import com.azure.resourcemanager.monitor.MonitorManager; import com.azure.resourcemanager.monitor.models.*; import java.time.Duration; import java.util.Collections; public class VmCpuAlertCreator { public static void main(String[] args) { // 替换为你的实际参数 String subscriptionId = "your-subscription-id"; String resourceGroupName = "your-vm-resource-group"; String vmResourceId = "/subscriptions/your-subscription-id/resourceGroups/your-vm-resource-group/providers/Microsoft.Compute/virtualMachines/your-vm-name"; String alertRuleName = "VM-CPU-Over-80-Alert"; double cpuThreshold = 80.0; String vmRegion = "eastus"; // 替换为虚拟机所在区域 // 初始化Monitor管理器 MonitorManager monitorManager = MonitorManager.authenticate(new DefaultAzureCredential(), subscriptionId); // 定义CPU使用率的警报条件 MetricAlertSingleCondition cpuCondition = new MetricAlertSingleCondition() .withOperator(ComparisonOperationType.GREATER_THAN) .withThreshold(cpuThreshold) .withTimeAggregation(TimeAggregationType.AVERAGE) .withMetricTrigger(new MetricTrigger() .withMetricName("Percentage CPU") .withNamespace("Microsoft.Compute/virtualMachines") .withResourceLocation(vmRegion) .withOperator(ComparisonOperationType.GREATER_THAN) .withThreshold(cpuThreshold) .withTimeAggregation(TimeAggregationType.AVERAGE) .withTimeWindow(Duration.ofMinutes(5)) .withTimeAggregation(TimeAggregationType.AVERAGE)); // 构建警报规则配置 MetricAlertResource alertRule = new MetricAlertResource() .withDescription("虚拟机CPU使用率超过" + cpuThreshold + "%时触发警报") .withSeverity(AlertSeverityLevel.S2) // S0到S4,级别越高越紧急 .withEnabled(true) .withScopes(Collections.singletonList(vmResourceId)) .withEvaluationFrequency(Duration.ofMinutes(1)) // 每分钟检查一次 .withWindowSize(Duration.ofMinutes(5)) // 基于过去5分钟的统计数据评估 .withCriteria(new MetricAlertMultipleResourceMultipleMetricCriteria() .withAllOf(Collections.singletonList(cpuThreshold))); // 如果需要绑定动作组(邮件/短信通知等),取消下面注释并替换动作组ID //.withActions(Collections.singletonList(new MetricAlertAction().withActionGroupId("/subscriptions/your-subscription-id/resourceGroups/your-rg/providers/microsoft.insights/actionGroups/your-action-group"))); // 创建警报规则 monitorManager.metricAlerts().define(alertRuleName) .withExistingResourceGroup(resourceGroupName) .withAlertRule(alertRule) .create(); System.out.println("CPU警报规则创建完成"); } }
3. 关键配置说明
- 认证逻辑:
DefaultAzureCredential会自动从环境变量、Azure CLI、托管身份等渠道获取凭据,适配开发和生产环境。 - 指标参数:
MetricName固定为Percentage CPU,Namespace对应虚拟机资源的命名空间Microsoft.Compute/virtualMachines。 - 评估规则:
timeWindow是统计指标的时间范围,evaluationFrequency是检查频率,可根据业务需求调整。 - 动作组:若需要触发通知,需提前在Azure门户创建动作组,再在代码中传入其资源ID。
注意事项
- 运行代码的身份(本地开发账户/生产环境托管身份)必须拥有
Microsoft.Insights/metricAlerts/write权限。 - 务必替换代码中所有占位符为实际资源信息。
内容的提问来源于stack exchange,提问作者lpdevlop
相关产品推荐
相关产品推荐

