我正在尝试获取 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------");
}