4

我想为我的EC2实例获取Cloudmetrics数据,以便使用这些数据绘制图形并将其显示在我的Android设备上。我怎么做?有没有相同的示例程序或教程?如何获取EC2实例的CloudWatch指标数据

在此先感谢。

这是我在做什么:

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

我不确定它会导致问题的症结,但是您的代码将无法正常工作。维名称区分大小写,因此您应该将名称设置为“InstanceId”而不是“instanceid”。 – Eric

回答

3

由于您已为问题与安卓我假设你想获取你的EC2实例CloudWatch的度量在Android的应用程序。 所以这可能是你一个很好的起点:

您需要:

  1. 下载AWS SDK for Android
  2. 创建您的访问密钥用于AWS(通过IAM)
  3. 从CloudWatch的
  4. 阅读 aws-android-sdk-VERSION-cloudwatch.jar
  5. 启动文档使用所获取的数据

Regards

汤姆

+0

有没有相同的示例程序或教程? –

+0

适用于Android的AWS开发工具包包含示例。它们可能并不完全适合您的使用情况,但您至少可以获得正确的方向。 – Tom

0

我想你只与读取数据并绘制图表击中。

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进行排序并绘制图形。

+0

我已经从cloudwatch获取了值..现在需要绘制图形。哪个java图形库要使用? – Anand

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

相关问题