Search in sources :

Example 1 with AWSEC2ProviderMetadata

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();
        }
    }
}
Also used : AWSEC2ProviderMetadata(org.jclouds.aws.ec2.AWSEC2ProviderMetadata) CloudWatchAsyncClient(org.jclouds.cloudwatch.CloudWatchAsyncClient) DecimalFormat(java.text.DecimalFormat) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata) Datapoint(org.jclouds.cloudwatch.domain.Datapoint) AWSCloudWatchProviderMetadata(org.jclouds.aws.cloudwatch.AWSCloudWatchProviderMetadata) CloudWatchClient(org.jclouds.cloudwatch.CloudWatchClient) MetricClient(org.jclouds.cloudwatch.features.MetricClient)

Aggregations

DecimalFormat (java.text.DecimalFormat)1 AWSCloudWatchProviderMetadata (org.jclouds.aws.cloudwatch.AWSCloudWatchProviderMetadata)1 AWSEC2ProviderMetadata (org.jclouds.aws.ec2.AWSEC2ProviderMetadata)1 CloudWatchAsyncClient (org.jclouds.cloudwatch.CloudWatchAsyncClient)1 CloudWatchClient (org.jclouds.cloudwatch.CloudWatchClient)1 Datapoint (org.jclouds.cloudwatch.domain.Datapoint)1 MetricClient (org.jclouds.cloudwatch.features.MetricClient)1 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)1 ComputeMetadata (org.jclouds.compute.domain.ComputeMetadata)1