Domanda

first time i'm using aws api in java to get the cloud watch statistics for my ec2-instance. i googled about this and i found some code snippet. here it is

AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(
                new BasicAWSCredentials(AccessKey, SecretKey));
        cloudWatch.setEndpoint("ec2-<my-static-ip>.compute-1.amazonaws.com");
        long offsetInMilliseconds = 1000 * 60 * 60 * 24;
        Dimension instanceDimension = new Dimension();
        instanceDimension.setName("Instanceid");
        instanceDimension.setValue(InstanceId);
        GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
                .withStartTime(
                        new Date(new Date().getTime()
                                - offsetInMilliseconds))
                .withNamespace("AWS/EC2")
                .withPeriod(60 * 60)
                .withDimensions(
                        new Dimension().withName("InstanceId").withValue(
                                InstanceId))
                .withMetricName("CPUUtilization")
                .withStatistics("Average", "Maximum")
                .withEndTime(new Date());

        GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch
                .getMetricStatistics(request);
        double avgCPUUtilization = 0;
        List dataPoint = getMetricStatisticsResult.getDatapoints();
        for (Object aDataPoint : dataPoint) {
            Datapoint dp = (Datapoint) aDataPoint;
            avgCPUUtilization = dp.getAverage();
            System.out.println(InstanceId
                    + " instance's average CPU utilization : "
                    + dp.getAverage());
        }
    } catch (AmazonServiceException ase) {
        System.out
                .println("Caught an AmazonServiceException, which means the request was made  "
                        + "to Amazon EC2, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());

    }

so, using this code i tried to get statistics, but first time it throws error saying

com.amazonaws.AmazonClientException: Unable to execute HTTP request:Connection to https://ec2-<my-static-ip>.compute-1.amazonaws.com refused

then i thought it was sending https requests. so i enabled ssl on my instance and tried, then i'm getting below exception.

 com.amazonaws.AmazonClientException: Unable to execute HTTP request: peer not authenticated

i was using OpenJDK in my instance, so i thought that may causing the problem. then i removed openjdk and installed Oracle JDK 1.7. but still same problem.

My questions are,

1) how can i send only HTTP (instead of HTTPS) requests to get statistics?

2)how to get rid of this problem, so that i can get my results?

But please don't ask me to read any docs, because i messed up by searching in net, blogs,forums, docs... etc. then i end up here. so, please just provide me solution or tell me where i'm going wrong.

Can anybody please help me out this issue.

thank you in Advance.

È stato utile?

Soluzione

Got Solution.

1) removed setting end point for AmazonCloudWatchClient.

2) problem with the AWS credentials (Access key ID, Secret key).So, i created another set of credentials and gave CloudWatchFullAccess policy for the user.

Now it is working like Charm... :-)

Thanks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top