Amazon Cloudwatch服务不返回指标信息

时间:2015-03-30 11:20:27

标签: java amazon-web-services amazon-ec2 amazon-cloudwatch

我正在编写一个java代码,用于使用Amazon EC2 metrics检索Amazon Cloudwatch。 以下是代码:

        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());

虽然我可以在AWS控制台中看到指标数据,但上面的回报为0。 我想澄清几点:

1)我是否需要设置类似cloudWatch.setEndpoint(....)?的端点 2)设置与格式等相关的开始/结束时间是否有问题?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

通过设置cloudWatch.setEndpoint(....)

解决了该问题