关闭

CloudWatch Examples

标签: AWSCloudWatch
441人阅读 评论(0) 收藏 举报
分类:

原文连接:《AWS SDK for Java——Developer Guide》


译文↓


这部分提供了使用Java的AWS开发工具包编写CloudWatch程序的示例。

CloudWatch实时监控AWS资源以及运行在AWS上的应用。您可以使用CloudWatch收集和跟踪指标,这些指标是用来测量资源和应用程序的可变因素。CloudWatch警报根据您定义的规则发送通知或自动更改正在监视的资源。

更多信息,请参阅CloudWatch用户指南

注意

这些示例只包括演示每种技术所需的代码。完整的代码请参见GitHub。您可以下载一个单独的源文件或者在本地克隆版本库,以获得所有示例来构建和运行。


从CloudWatch获取指标

列举指标

要列举CloudWatch指标,需创建ListMetricsRequest并调用AmazonCloudWatchClientlistMetrics方法。您可以使用ListMetricsRequest按命名空间、指标名称或者维度来筛选返回的指标。

注意

AWS服务发布的指标和维度列表可以在Amazon CloudWatch用户指南的Amazon CloudWatch指标和维度参考中找到。


导入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.ListMetricsRequest;
import com.amazonaws.services.cloudwatch.model.ListMetricsResult;
import com.amazonaws.services.cloudwatch.model.Metric;


代码

final AmazonCloudWatch cw =
 AmazonCloudWatchClientBuilder.defaultClient();
boolean done = false;
while(!done) {
 ListMetricsRequest request = new ListMetricsRequest()
 .withMetricName(name)
 .withNamespace(namespace);
 ListMetricsResult response = cw.listMetrics(request);
 for(Metric metric : response.getMetrics()) {
 System.out.printf(
 "Retrieved metric %s", metric.getMetricName());
 }
 request.setNextToken(response.getNextToken());
 if(response.getNextToken() == null) {
 done = true;
 }
}

指标通过调用getMetrics方法返回到ListMetricsResult中。结果可能被分页。要检索下一批结果,请使用ListMetricsResult对象的getNextToken方法的返回值在原始请求对象上调用setNextToken,然后将修改过的请求对象传回给另一个调用listMetrics

更多信息

·请参阅Amazon CloudWatch API Reference的ListMetrics


发布自定义指标数据

许多AWS服务在以"AWS/"开头的命名空间中发布自己的指标。您可以发布自定义指标数据使用您自己的命名空间(只要不是以"AWS/"开头就可以)。

发布自定义指标数据

要发布自定义指标数据,请使用PutMetricDataRequest调用AmazonCloudWatchClientputMetricData方法。PutMetricDataRequest必须包含要用于的自定义命名空间的数据以及有关MetricDatum对象中数据点本身的信息。

注意

您不能指定命名空间以"AWS/"开头。以"AWS/"开头的命名空间保留供Amazon Web Services产品使用。


导入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.MetricDatum;
import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest;
import com.amazonaws.services.cloudwatch.model.PutMetricDataResult;
import com.amazonaws.services.cloudwatch.model.StandardUnit;


代码

final AmazonCloudWatch cw =
 AmazonCloudWatchClientBuilder.defaultClient();
Dimension dimension = new Dimension()
 .withName("UNIQUE_PAGES")
 .withValue("URLS");
MetricDatum datum = new MetricDatum()
 .withMetricName("PAGES_VISITED")
 .withUnit(StandardUnit.None)
 .withValue(data_point)
 .withDimensions(dimension);
PutMetricDataRequest request = new PutMetricDataRequest()
 .withNamespace("SITE/TRAFFIC")
 .withMetricData(datum);
PutMetricDataResult response = cw.putMetricData(request);


更多信息

·请参阅Amazon CloudWatch用户指南的使用Amazon CloudWatch指标

·请参阅Amazon CloudWatch用户指南的AWS命名空间

·请参阅Amazon CloudWatch API Reference的PutMetricData


使用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来调用AmazonCloudWatch的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;
while(!done) {
 DescribeAlarmsRequest request = new DescribeAlarmsRequest();
 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类似。


删除警报

要删除警报,使用设置了一个或多个想要删除的警报名称的DeleteAlarmsRequest来调用AmazonCloudWatchClientdeleteAlarms方法。

导入

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;

代码

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


更多信息

·请参阅Amazon CloudWatch用户指南的创建Amazon CloudWatch警报

·请参阅Amazon CloudWatch API Reference的PutMetricAlarm

·请参阅Amazon CloudWatch API Reference的DescribeAlarms

·请参阅Amazon CloudWatch API Reference的DeleteAlarms


在CloudWatch中使用警报操作

使用CloudWatch警报操作,您可以创建执行操作的警报,如自动停止,终止,重新引导或恢复Amazon EC2实例。

注意

在创建警报时,可以通过PutMetricAlarmRequest的setAlarmActions方法向警报添加警报操作。

启动警报操作

为CloudWatch警报启动警报操作,请使用设置了一个或多个想要启动操作的警报名称的EnableAlarmActionsRequest来调用AmazonCloudWatchClient的enableAlarmActions方法。

导入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.EnableAlarmActionsRequest;
import com.amazonaws.services.cloudwatch.model.EnableAlarmActionsResult;

代码

final AmazonCloudWatch cw =
 AmazonCloudWatchClientBuilder.defaultClient();
EnableAlarmActionsRequest request = new EnableAlarmActionsRequest()
 .withAlarmNames(alarm);
EnableAlarmActionsResult response = cw.enableAlarmActions(request);

禁用警报操作

为CloudWatch警报禁用警报操作,请使用设置了一个或多个想要禁用操作的警报名称的DisableAlarmActionsRequest来调用AmazonCloudWatchClient的disableAlarmActions方法。

导入

import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.DisableAlarmActionsRequest;
import com.amazonaws.services.cloudwatch.model.DisableAlarmActionsResult;


代码

final AmazonCloudWatch cw =
 AmazonCloudWatchClientBuilder.defaultClient();
DisableAlarmActionsRequest request = new DisableAlarmActionsRequest()
 .withAlarmNames(alarmName);
DisableAlarmActionsResult response = cw.disableAlarmActions(request);

更多信息

·请参阅Amazon CloudWatch用户指南的创建警报以停止、终止、重启或恢复实例

·请参阅Amazon CloudWatch API Reference的PutMetricAlarm

·请参阅Amazon CloudWatch API Reference的EnableAlarmActions

·请参阅Amazon CloudWatch API Reference的DisableAlarmActions


向CloudWatch发送事件

CloudWatch Events提供了一个接近实时的系统事件流,描述了AWS资源对Amazon EC2实例,Lambda函数,Kinesis流,Amazon ECS任务,Step Functions状态机,Amazon SNS主题,Amazon SQS队列或内置目标。您可以使用简单的规则匹配事件并将其路由到一个或多个目标函数或流。

添加事件

要添加自定义CloudWatch事件,请使用包含一个或多个提供有关每个事件的详细信息的PutEventsRequestEntry对象的PutEventsRequest对象来调用AmazonCloudWatchEventsClient的putEvents方法。您可以为入口指定多个参数,例如事件的来源和类型,与事件关联的资源等。

注意

您可以为每个putEvents调用指定至多10个事件。

入口

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents;
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder;
import com.amazonaws.services.cloudwatchevents.model.PutEventsRequest;
import com.amazonaws.services.cloudwatchevents.model.PutEventsRequestEntry;
import com.amazonaws.services.cloudwatchevents.model.PutEventsResult;


代码

final AmazonCloudWatchEvents cwe =
 AmazonCloudWatchEventsClientBuilder.defaultClient();
final String EVENT_DETAILS =
 "{ \"key1\": \"value1\", \"key2\": \"value2\" }";
PutEventsRequestEntry request_entry = new PutEventsRequestEntry()
 .withDetail(EVENT_DETAILS)
 .withDetailType("sampleSubmitted")
 .withResources(resource_arn)
 .withSource("aws-sdk-java-cloudwatch-example");
PutEventsRequest request = new PutEventsRequest()
 .withEntries(request_entry);
PutEventsResult response = cwe.putEvents(request);

添加规则

要创建或更新规则,请使用PutRuleRequest调用AmazonCloudWatchEventsClient的putRule方法,其中包含规则名称和可选参数(如事件模式,与规则关联的IAM角色以及描述规则运行频率的调度表达式)。

导入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents;
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder;
import com.amazonaws.services.cloudwatchevents.model.PutRuleRequest;
import com.amazonaws.services.cloudwatchevents.model.PutRuleResult;
import com.amazonaws.services.cloudwatchevents.model.RuleState;

代码

final AmazonCloudWatchEvents cwe =
 AmazonCloudWatchEventsClientBuilder.defaultClient();
PutRuleRequest request = new PutRuleRequest()
 .withName(rule_name)
 .withRoleArn(role_arn)
 .withScheduleExpression("rate(5 minutes)")
 .withState(RuleState.ENABLED);
PutRuleResult response = cwe.putRule(request);

添加目标

目标是触发规则时调用的资源。 示例目标包括Amazon EC2实例,Lambda函数,Kinesis流,Amazon ECS任务,Step Functions状态机和内置目标。

要将目标添加到规则,请使用包含要更新规则的PutTargetsRequest和要添加到规则的目标列表来调用AmazonCloudWatchEventsClient的putTargets方法。

导入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents;
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder;
import com.amazonaws.services.cloudwatchevents.model.PutTargetsRequest;
import com.amazonaws.services.cloudwatchevents.model.PutTargetsResult;
import com.amazonaws.services.cloudwatchevents.model.Target;

代码

final AmazonCloudWatchEvents cwe =
 AmazonCloudWatchEventsClientBuilder.defaultClient();
Target target = new Target()
 .withArn(function_arn)
 .withId(target_id);
PutTargetsRequest request = new PutTargetsRequest()
 .withTargets(target)
 .withRule(rule_name);
PutTargetsResult response = cwe.putTargets(request);

更多信息

·Amazon CloudWatch事件用户指南的使用PutEvents添加事件

·Amazon CloudWatch事件用户指南的规则的计划表达式

·Amazon CloudWatch事件用户指南的每个支持服务的 CloudWatch Events 事件示例

·Amazon CloudWatch事件用户指南的事件与事件模式

·Amazon CloudWatch Events API Reference的PutEvents

·Amazon CloudWatch Events API Reference的PutTargets

·Amazon CloudWatch Events API Reference的PutRule

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2682次
    • 积分:200
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:0篇
    • 译文:5篇
    • 评论:0条