lundi 30 mars 2015

Amazon Cloudwatch service not returning metrics information of EC2 service

I am writing a java code for retrieving Amazon EC2 metrics using Amazon Cloudwatch. Below is the code:



AWSCredentials awsCredentials = new BasicAWSCredentials(aws_accessKey, aws_secretKey);
AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(awsCredentials);

Dimension instanceDimension = new Dimension();
instanceDimension.setName("InstanceId");
instanceDimension.setValue("i-480de11e");


GetMetricStatisticsRequest request = new GetMetricStatisticsRequest();
request.setNamespace("AWS/EC2");
request.setPeriod(60 * 5);

ArrayList<String> stats = new ArrayList<String>();
stats.add("Average");
request.setStatistics(stats);

ArrayList<Dimension> dimensions = new ArrayList<Dimension>();
dimensions.add(instanceDimension);
request.setDimensions(dimensions);
request.setMetricName("CPUUtilization");

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY, -5);
cal.add(Calendar.MINUTE, -30);
Date endTime = format.parse(cal.getTime().toString());
request.setEndTime(endTime);

cal.add(Calendar.MINUTE, -10);
Date startTime = format.parse(cal.getTime().toString());
request.setStartTime(startTime);

GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch.getMetricStatistics(request);
System.out.println(getMetricStatisticsResult.getDatapoints().size());


The above is returning zero though I can see the metrics data in AWS console. Few thing I would like to clarify:


1) Do I need to set endpoint like cloudWatch.setEndpoint(....)?

2) Could there be an issue with setting start/end time related to format etc?


Any help will be appreciated.





Aucun commentaire:

Enregistrer un commentaire