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

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

使用 CloudWatch 警报

创建警报

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

导入

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 警报,请使用您可用于设置结果选项的 AmazonCloudWatchClientdescribeAlarmsDescribeAlarmsRequest 调用 的 方法。

导入

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; } }

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

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

注意

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

删除警报

要删除 CloudWatch 警报,请使用包含要删除的警报的一个或多个名称的 AmazonCloudWatchClientdeleteAlarmsDeleteAlarmsRequest调用 的 方法。

导入

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);

更多信息