适用于 Java 的 AWS 开发工具包
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

使用 CloudWatch 警报

创建警报

根据 CloudWatch 指标创建警报,请使用已填充警报条件的 PutMetricAlarmRequest 调用 CloudWatchClientputMetricAlarm 方法。

导入

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.ComparisonOperator; import software.amazon.awssdk.services.cloudwatch.model.Dimension; import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest; import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmResponse; import software.amazon.awssdk.services.cloudwatch.model.StandardUnit; import software.amazon.awssdk.services.cloudwatch.model.Statistic;

代码

CloudWatchClient cw = CloudWatchClient.builder().build(); Dimension dimension = Dimension.builder() .name("InstanceId") .value(instanceId).build(); PutMetricAlarmRequest request = PutMetricAlarmRequest.builder() .alarmName(alarmName) .comparisonOperator( ComparisonOperator.GREATER_THAN_THRESHOLD) .evaluationPeriods(1) .metricName("CPUUtilization") .namespace("AWS/EC2") .period(60) .statistic(Statistic.AVERAGE) .threshold(70.0) .actionsEnabled(false) .alarmDescription( "Alarm when server CPU utilization exceeds 70%") .unit(StandardUnit.SECONDS) .dimensions(dimension) .build(); PutMetricAlarmResponse response = cw.putMetricAlarm(request);

请参阅 GitHub 上的完整示例

列出警报

要列出您已创建的 CloudWatch 警报,请使用您用来设置结果选项的 DescribeAlarmsRequest 调用 CloudWatchClientdescribeAlarms 方法。

导入

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse; import software.amazon.awssdk.services.cloudwatch.model.MetricAlarm;

代码

CloudWatchClient cw = CloudWatchClient.builder().build(); boolean done = false; String new_token = null; while(!done) { DescribeAlarmsResponse response; if (new_token == null) { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder().build(); response = cw.describeAlarms(request); } else { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder() .nextToken(new_token) .build(); response = cw.describeAlarms(request); } for(MetricAlarm alarm : response.metricAlarms()) { System.out.printf("Retrieved alarm %s", alarm.alarmName()); } if(response.nextToken() == null) { done = true; } else { new_token = response.nextToken(); } }

警报列表可以通过在 describeAlarms 返回的 DescribeAlarmsResponse 上调用 MetricAlarms 获得。

结果可以分页。要检索下一批结果,请对响应对象调用 nextToken 并使用该令牌值构建新的请求对象。然后使用新请求再次调用 describeAlarms 方法。

注意

您还可以使用 CloudWatchClientdescribeAlarmsForMetric 方法检索特定指标的警报。它的使用类似于 describeAlarms

请参阅 GitHub 上的完整示例

删除警报

要删除 CloudWatch 警报,请使用 DeleteAlarmsRequest (包含您要删除的一个或更多警报名称) 调用 CloudWatchClientdeleteAlarms 方法。

导入

import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest; import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsResponse;

代码

CloudWatchClient cw = CloudWatchClient.builder().build(); DeleteAlarmsRequest request = DeleteAlarmsRequest.builder() .alarmNames(alarm_name).build(); DeleteAlarmsResponse response = cw.deleteAlarms(request); System.out.printf("Successfully deleted alarm %s", alarm_name);

请参阅 GitHub 上的完整示例

更多信息