使用Java检索AWS EC2s CPUUtilization

时间:2016-01-24 22:31:46

标签: java amazon-web-services javafx amazon-s3 amazon-ec2

我正在尝试获取特定区域中EC2的所有平均CPU使用率。我尝试了以下方法:

  • 请求每个请求一个实例统计信息,但这种方法很有效。
  • 我尝试从请求中删除维度,但我得到了一个空响应,尽管cli会返回所有可用的实例(Statistics for up to 35 instances over a span of 24 hours)。
  • 我尝试为每个请求设置10个维度,但我得到了这个例外:

    2016-01-24 23:13:30.925 java[20057:31669898] GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT java.lang.RuntimeException: Requested texture dimensions (62206x154) require dimensions (0x154) that exceed maximum texture size (16384)
     at com.sun.prism.es2.ES2RTTexture.create(ES2RTTexture.java:220)
     at com.sun.prism.es2.ES2ResourceFactory.createRTTexture(ES2ResourceFactory.java:158)
     at com.sun.prism.es2.ES2SwapChain.createGraphics(ES2SwapChain.java:210)
     at com.sun.prism.es2.ES2SwapChain.createGraphics(ES2SwapChain.java:40)
     at com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:87)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
     at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
     at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
     at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
     at java.lang.Thread.run(Thread.java:745)
    

代码:

 private List<Datapoint> monitorInstance(AmazonCloudWatchClient cloudWatchClient, List<Dimension> dimensions) {
     List<Datapoint> datapoints = new ArrayList<>();
     try {
         int dimensionsLastIndex = 0;
         for (int dimensionsIndex = 0; dimensionsIndex < dimensions.size(); dimensionsIndex = dimensionsLastIndex + 1) {
             dimensionsLastIndex = (dimensionsIndex + 10) <= dimensions.size() ? dimensionsIndex + 9 : dimensions.size() - 1;

             long offsetInMilliseconds = 1000 * 60 * 60 * 24;
             GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
                     .withStartTime(new Date(new Date().getTime() - offsetInMilliseconds))
                     .withNamespace("AWS/EC2")
                     .withPeriod(60 * 60)
                     .withDimensions(dimensions.subList(dimensionsIndex, dimensionsLastIndex))
                     .withMetricName("CPUUtilization")
                     .withStatistics("Average")
                     .withEndTime(new Date());
             GetMetricStatisticsResult getMetricStatisticsResult = cloudWatchClient.getMetricStatistics(request);

             datapoints.addAll(getMetricStatisticsResult.getDatapoints());
         }
         return datapoints;
     } catch (AmazonServiceException e) {
         error(e.getMessage(), stackTraceToString(e));

     }
     return new ArrayList<>();
 }

虽然我想修复此异常,但我想知道是否有一种简单的方法可以在一个请求中获取所有实例的统计信息。

0 个答案:

没有答案