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

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

使用 CloudWatch 警报

创建警报

要基于CloudWatch指标创建警报,请使用已填充警报条件的 AmazonCloudWatchClientPutMetricAlarmRequestputMetricAlarm 调用 方法。

导入

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警报,请使用 AmazonCloudWatchClientDescribeAlarmsRequestdescribeAlarms 调用 方法,您可以使用该方法为结果设置选项。

导入

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 获得。

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

注意

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

删除警报

要删除CloudWatch警报,请使用 AmazonCloudWatchClientDeleteAlarmsRequestdeleteAlarms 调用 的 方法,并提供要删除的警报的一个或多个名称。

导入

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

更多信息

  • 中的创建 Amazon CloudWatch 警报Amazon CloudWatch User Guide

  • 中的 PutMetricAlarmAmazon CloudWatch API Reference

  • 中的 DescribeAlarmsAmazon CloudWatch API Reference

  • 中的 DeleteAlarmsAmazon CloudWatch API Reference