use of org.jclouds.aws.ec2.AWSEC2ProviderMetadata in project legacy-jclouds-examples by jclouds.
the class MainApp method main.
public static void main(String[] args) {
if (args.length < PARAMETERS) {
throw new IllegalArgumentException(INVALID_SYNTAX);
}
// Arguments
String accessKeyId = args[0];
String secretKey = args[1];
ComputeServiceContext awsEC2Context = null;
RestContext<CloudWatchClient, CloudWatchAsyncClient> cloudWatchContext = null;
try {
cloudWatchContext = ContextBuilder.newBuilder(new AWSCloudWatchProviderMetadata()).credentials(accessKeyId, secretKey).build();
awsEC2Context = ContextBuilder.newBuilder(new AWSEC2ProviderMetadata()).credentials(accessKeyId, secretKey).build(ComputeServiceContext.class);
// Get all nodes
Set<? extends ComputeMetadata> allNodes = awsEC2Context.getComputeService().listNodes();
for (ComputeMetadata node : allNodes) {
String nodeId = node.getProviderId();
String region = getRegion(node.getLocation());
MetricClient metricClient = cloudWatchContext.getApi().getMetricClientForRegion(region);
int metricsCount = getMetricsCountForInstance(cloudWatchContext.getApi(), region, nodeId);
double[] cpuUtilization = getCPUUtilizationStatsForInstanceOverTheLast24Hours(metricClient, nodeId);
String cpuUtilizationHeader = " CPU utilization statistics: ";
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(nodeId + " CloudWatch Metrics (Past 24 hours)");
System.out.println(" Total metrics stored: " + metricsCount);
if (cpuUtilization == null) {
System.out.println(cpuUtilizationHeader + "Unable to compute as there are no CPU utilization " + "metrics stored.");
} else {
System.out.println(cpuUtilizationHeader + df.format(cpuUtilization[0]) + "% (avg), " + df.format(cpuUtilization[1]) + "% (max), " + df.format(cpuUtilization[2]) + "% (min)");
}
}
} finally {
if (awsEC2Context != null) {
awsEC2Context.close();
}
if (cloudWatchContext != null) {
cloudWatchContext.close();
}
}
}
Aggregations