使用 CloudWatch 警报 - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用 CloudWatch 警报

创建警报

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

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.Dimension; import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest; import software.amazon.awssdk.services.cloudwatch.model.ComparisonOperator; import software.amazon.awssdk.services.cloudwatch.model.Statistic; import software.amazon.awssdk.services.cloudwatch.model.StandardUnit; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;

代码

public static void putMetricAlarm(CloudWatchClient cw, String alarmName, String instanceId) { try { 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(); cw.putMetricAlarm(request); System.out.printf( "Successfully created alarm with name %s", alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

请参阅上的完整示例 GitHub。

列出警报

要列出您创建 CloudWatchClient的CloudWatch警报,请使用可以用来设置结果选项的's describeAlarms 方法。DescribeAlarmsRequest

导入

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

代码

public static void desCWAlarms( CloudWatchClient cw) { try { boolean done = false; String newToken = null; while(!done) { DescribeAlarmsResponse response; if (newToken == null) { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder().build(); response = cw.describeAlarms(request); } else { DescribeAlarmsRequest request = DescribeAlarmsRequest.builder() .nextToken(newToken) .build(); response = cw.describeAlarms(request); } for(MetricAlarm alarm : response.metricAlarms()) { System.out.printf("\n Retrieved alarm %s", alarm.alarmName()); } if(response.nextToken() == null) { done = true; } else { newToken = response.nextToken(); } } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.printf("Done"); }

可以通过调用MetricAlarms返回的来获取警报列表describeAlarmsDescribeAlarmsResponse

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

注意

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

请参阅上的完整示例 GitHub。

删除警报

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

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest;

代码

public static void deleteCWAlarm(CloudWatchClient cw, String alarmName) { try { DeleteAlarmsRequest request = DeleteAlarmsRequest.builder() .alarmNames(alarmName) .build(); cw.deleteAlarms(request); System.out.printf("Successfully deleted alarm %s", alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

请参阅上的完整示例 GitHub。

更多信息