我想获取EC2实例的Cloudmetrics数据,以便我可以使用这些数据绘制图形并将其显示在我的Android设备上。我怎么做?是否有相同的示例程序或教程?
提前致谢。
这就是我在做的事情:
private static void findCloudWatchData() {
AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(new BasicAWSCredentials(AccessKey, SecretKey));
cloudWatch.setEndpoint("monitoring.us-east-1.amazonaws.com");
long offsetInMilliseconds = 1000 * 60 * 60 * 24;
Dimension instanceDimension = new Dimension();
instanceDimension.setName("instanceid");
instanceDimension.setValue(instanceid);
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withStartTime(new Date(new Date().getTime() - offsetInMilliseconds))
.withNamespace("AWS/EC2")
.withPeriod(60 * 60)
.withMetricName("CPUUtilization")
.withStatistics("Average")
.withDimensions(Arrays.asList(instanceDimension))
.withEndTime(new Date());
GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch.getMetricStatistics(request);
}
答案 0 :(得分:3)
正如您使用 android 标记了您的问题,我假设您要在 Android-App 中为您的EC2实例获取CloudWatch-Metrics。 所以这对你来说可能是一个很好的起点:
你需要:
此致
汤姆
答案 1 :(得分:1)
我想你只是阅读数据和绘制图表而感到震惊。
private static void findCloudWatchData() {
LinkedHashMap<Date,Double> map=new HashMap<Date,Double>();
AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(new BasicAWSCredentials(AccessKey, SecretKey));
cloudWatch.setEndpoint("monitoring.us-east-1.amazonaws.com");
long offsetInMilliseconds = 1000 * 60 * 60 * 24;
Dimension instanceDimension = new Dimension();
instanceDimension.setName("instanceid");
instanceDimension.setValue(instanceid);
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withStartTime(new Date(new Date().getTime() - offsetInMilliseconds))
.withNamespace("AWS/EC2")
.withPeriod(60 * 60)
.withMetricName("CPUUtilization")
.withStatistics("Average")
.withDimensions(Arrays.asList(instanceDimension))
.withEndTime(new Date());
GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch.getMetricStatistics(request);
}
//To read the Data
for (Datapoint dp : result.getDatapoints()) {
map.put(dp.getTimeStamp(), dp.getAverage()); //or getMaximum() or whatever Statistics you are interested in. You can also maintain a list of the statistics you are interested in. Ex: request.setStatistics(list)
}
请注意,数据点不是有序的。对HashMap进行排序并绘制图形。
答案 2 :(得分:0)
我发现AWS/Billing
指标仅在一个地区“生活” - us-east-1 。
此外,如果您尝试从CloudWatch获取超过1440个数据点,AWS CLI(aws cloudwatch get-metric-statistics
)将会出错。
如果遇到它,请设置较大的--period
。
与您在Android上使用Java实现的类似,我使用Python / matplotlib为OS Windows编写了EC2_Metrics_Plotter。