使用 AWS Java SDK 获取 CloudWatch 指标?
Fetching CloudWatch metrics using the AWS Java SDK?
我正在尝试使用 CloudWatch API:
从 EC2 实例中获取 CPU 统计信息
我有以下代码,但它返回了一个空结果,即使实例 ID、AWS 访问和秘密密钥是正确的。
我可以在 CloudWatch UI 上看到实例的 CPU 实用程序,但似乎无法在下面找到它?
我使用的是 AWS SDK 1.9.0 版。
非常感谢任何帮助。
import java.util.Date;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.cloudwatch.model.Datapoint;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
public class AmazonCloudWatchFetchCpuUtilTest {
public static void main(String[] args) {
final String awsAccessKey = ...;
final String awsSecretKey = ...;
final String instanceId = ...;
final AmazonCloudWatchClient client = client(awsAccessKey, awsSecretKey);
final GetMetricStatisticsRequest request = request(instanceId);
final GetMetricStatisticsResult result = result(client, request);
toStdOut(result, instanceId);
}
private static AmazonCloudWatchClient client(final String awsAccessKey, final String awsSecretKey) {
return new AmazonCloudWatchClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
}
private static GetMetricStatisticsRequest request(final String instanceId) {
final long twentyFourHrs = 1000 * 60 * 60 * 24;
final int oneHour = 60 * 60;
return new GetMetricStatisticsRequest()
.withStartTime(new Date(new Date().getTime()- twentyFourHrs))
.withNamespace("AWS/EC2")
.withPeriod(oneHour)
.withDimensions(new Dimension().withName("InstanceId").withValue(instanceId))
.withMetricName("CPUUtilization")
.withStatistics("Average", "Maximum")
.withEndTime(new Date());
}
private static GetMetricStatisticsResult result(
final AmazonCloudWatchClient client, final GetMetricStatisticsRequest request) {
return client.getMetricStatistics(request);
}
private static void toStdOut(final GetMetricStatisticsResult result, final String instanceId) {
System.out.println(result); // outputs empty result: {Label: CPUUtilization,Datapoints: []}
for (final Datapoint dataPoint : result.getDatapoints()) {
System.out.printf("%s instance's average CPU utilization : %s%n", instanceId, dataPoint.getAverage());
System.out.printf("%s instance's max CPU utilization : %s%n", instanceId, dataPoint.getMaximum());
}
}
}
我缺少客户端端点设置。现在工作。
改变了这个:
private static AmazonCloudWatchClient client(final String awsAccessKey, final String awsSecretKey) {
return new AmazonCloudWatchClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
}
对此:
private static AmazonCloudWatchClient client(final String awsAccessKey, final String awsSecretKey) {
final AmazonCloudWatchClient client = new AmazonCloudWatchClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
client.setEndpoint("http://monitoring.eu-west-1.amazonaws.com");
return client;
}
我正在尝试使用 CloudWatch API:
从 EC2 实例中获取 CPU 统计信息我有以下代码,但它返回了一个空结果,即使实例 ID、AWS 访问和秘密密钥是正确的。
我可以在 CloudWatch UI 上看到实例的 CPU 实用程序,但似乎无法在下面找到它?
我使用的是 AWS SDK 1.9.0 版。
非常感谢任何帮助。
import java.util.Date;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.cloudwatch.model.Datapoint;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
public class AmazonCloudWatchFetchCpuUtilTest {
public static void main(String[] args) {
final String awsAccessKey = ...;
final String awsSecretKey = ...;
final String instanceId = ...;
final AmazonCloudWatchClient client = client(awsAccessKey, awsSecretKey);
final GetMetricStatisticsRequest request = request(instanceId);
final GetMetricStatisticsResult result = result(client, request);
toStdOut(result, instanceId);
}
private static AmazonCloudWatchClient client(final String awsAccessKey, final String awsSecretKey) {
return new AmazonCloudWatchClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
}
private static GetMetricStatisticsRequest request(final String instanceId) {
final long twentyFourHrs = 1000 * 60 * 60 * 24;
final int oneHour = 60 * 60;
return new GetMetricStatisticsRequest()
.withStartTime(new Date(new Date().getTime()- twentyFourHrs))
.withNamespace("AWS/EC2")
.withPeriod(oneHour)
.withDimensions(new Dimension().withName("InstanceId").withValue(instanceId))
.withMetricName("CPUUtilization")
.withStatistics("Average", "Maximum")
.withEndTime(new Date());
}
private static GetMetricStatisticsResult result(
final AmazonCloudWatchClient client, final GetMetricStatisticsRequest request) {
return client.getMetricStatistics(request);
}
private static void toStdOut(final GetMetricStatisticsResult result, final String instanceId) {
System.out.println(result); // outputs empty result: {Label: CPUUtilization,Datapoints: []}
for (final Datapoint dataPoint : result.getDatapoints()) {
System.out.printf("%s instance's average CPU utilization : %s%n", instanceId, dataPoint.getAverage());
System.out.printf("%s instance's max CPU utilization : %s%n", instanceId, dataPoint.getMaximum());
}
}
}
我缺少客户端端点设置。现在工作。
改变了这个:
private static AmazonCloudWatchClient client(final String awsAccessKey, final String awsSecretKey) {
return new AmazonCloudWatchClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
}
对此:
private static AmazonCloudWatchClient client(final String awsAccessKey, final String awsSecretKey) {
final AmazonCloudWatchClient client = new AmazonCloudWatchClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
client.setEndpoint("http://monitoring.eu-west-1.amazonaws.com");
return client;
}