1

我正在尝试获取 CloudWatch 指标,但出现以下错误:

Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
    at $Proxy85.getMetricStatisticsInRegion(Unknown Source)
    at GetStats.main(GetStats.java:73)
Caused by: java.util.concurrent.ExecutionException: task submitted from the following trace
    at org.jclouds.concurrent.config.ExecutorServiceModule$DescribedFuture.ensureCauseHasSubmissionTrace(ExecutorServiceModule.java:272)
    at org.jclouds.concurrent.config.ExecutorServiceModule$DescribedFuture.get(ExecutorServiceModule.java:256)
    at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:69)
    at com.google.common.util.concurrent.Futures$ChainingListenableFuture.get(Futures.java:661)
    at org.jclouds.concurrent.ExceptionParsingListenableFuture.get(ExceptionParsingListenableFuture.java:76)
    at org.jclouds.concurrent.internal.SyncProxy.invoke(SyncProxy.java:159)
    ... 2 more

重要代码片段如下:

    ComputeServiceContext EC2context =
            new ComputeServiceContextFactory().createContext("aws-ec2",
            accesskeyid,
            secretkey,
            ImmutableSet.<Module> of(new Log4JLoggingModule(),
            new SshjSshClientModule()));

    ComputeService ec2 = EC2context.getComputeService();

    for (ComputeMetadata c : ec2.listNodes()){
        NodeMetadata w = ec2.getNodeMetadata(c.getId());
        System.out.println(String.format("---node: %s(%s) status: %s---", 
                w.getId(), w.getName(),w.getState()));

        RestContext<CloudWatchClient, CloudWatchAsyncClient> cloudWatchContext =
                       new RestContextFactory().createContext("aws-cloudwatch",
                                                               accesskeyid, 
                                                               secretkey);
        String region = w.getLocation().getParent().getId();


        Date startday = new Date();
        Date today = new Date();
        Calendar calendar;
        calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.DATE, -1);
        startday = calendar.getTime();
        System.out.println("Today : " + today.toString());
        System.out.println("Startday: " + startday.toString());
        System.out.println("--going to fetch Average CPU--");

        CloudWatchClient client = cloudWatchContext.getApi();

        Set<Datapoint> datapoints = client.getMetricStatisticsInRegion(
                    region, 
                    "CPUUtilization",
                    "AWS/EC2",
                    startday, 
                    today, 
                    60, 
                     Statistics.AVERAGE,
                    GetMetricStatisticsOptions.Builder.unit(Unit.PERCENT));
        System.out.println(String.format("---datapoint for %s---",c.getId()));
        System.out.print(datapoints);
        System.out.println("------end------");

    }
4

1 回答 1

0

cloudwatch 的最终版本作为不同的语法。请查看cloudwatch-basics示例并使用 jclouds 1.5.3+ 版本,看看您是否仍然遇到问题。

于 2012-11-16T20:46:32.523 回答