使用 CloudWatch 警报 - AWS SDK for Java 1.x

自2024年7月31日起, AWS SDK for Java 1.x已进入维护模式,并将于2025年12月31日end-of-support上线。我们建议您迁移到AWS SDK for Java 2.x以继续接收新功能、可用性改进和安全更新。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 CloudWatch 警报

创建警报

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

导入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.ComparisonOperator; import com.amazonaws.services.cloudwatch.model.Dimension; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmResult; import com.amazonaws.services.cloudwatch.model.StandardUnit; import com.amazonaws.services.cloudwatch.model.Statistic;

代码

final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); Dimension dimension = new Dimension() .withName("InstanceId") .withValue(instanceId); PutMetricAlarmRequest request = new PutMetricAlarmRequest() .withAlarmName(alarmName) .withComparisonOperator( ComparisonOperator.GreaterThanThreshold) .withEvaluationPeriods(1) .withMetricName("CPUUtilization") .withNamespace("{AWS}/EC2") .withPeriod(60) .withStatistic(Statistic.Average) .withThreshold(70.0) .withActionsEnabled(false) .withAlarmDescription( "Alarm when server CPU utilization exceeds 70%") .withUnit(StandardUnit.Seconds) .withDimensions(dimension); PutMetricAlarmResult response = cw.putMetricAlarm(request);

列出警报

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

导入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult; import com.amazonaws.services.cloudwatch.model.MetricAlarm;

代码

final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; DescribeAlarmsRequest request = new DescribeAlarmsRequest(); while(!done) { DescribeAlarmsResult response = cw.describeAlarms(request); for(MetricAlarm alarm : response.getMetricAlarms()) { System.out.printf("Retrieved alarm %s", alarm.getAlarmName()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } }

警报列表可以通过在 describeAlarms 返回的 DescribeAlarmsResult 中调用 getMetricAlarms 获得。

结果可以分页。要检索下一批结果,请在原始请求对象中使用 DescribeAlarmsResult 对象的 getNextToken 方法的返回值调用 setNextToken,并将已修改的请求对象传回对 describeAlarms 的另一个调用。

注意

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

删除警报

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

导入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest; import com.amazonaws.services.cloudwatch.model.DeleteAlarmsResult;

代码

final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); DeleteAlarmsRequest request = new DeleteAlarmsRequest() .withAlarmNames(alarm_name); DeleteAlarmsResult response = cw.deleteAlarms(request);

更多信息