5

I want to fetch the Cloudmetrics data for my EC2 instance so that I can draw graphs using those data and display it on my android device. How do I do that? Is there any sample program or tutorial for the same?

Thanks in advance.

This is what I am doing:

private static void findCloudWatchData()  {

    AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(new BasicAWSCredentials(AccessKey, SecretKey));
    cloudWatch.setEndpoint("monitoring.us-east-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)
            .withMetricName("CPUUtilization")
            .withStatistics("Average")
            .withDimensions(Arrays.asList(instanceDimension))
            .withEndTime(new Date());

    GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch.getMetricStatistics(request);
    }
4

3 回答 3

3

As you have tagged your question with android I assume that you want to fetch CloudWatch-Metrics for your EC2 Instances in an Android-App. So this might be a good starting point for you:

You need to:

  1. download AWS SDK for Android
  2. create your access keys for AWS (via IAM)
  3. read the documentation for aws-android-sdk-VERSION-cloudwatch.jar
  4. start using the fetched data from CloudWatch

Regards

Tom

于 2013-05-19T11:52:51.850 回答
2

I suppose you are struck only with reading the data and plotting the graph.

private static void findCloudWatchData()  {

LinkedHashMap<Date,Double> map=new HashMap<Date,Double>();
AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(new BasicAWSCredentials(AccessKey,     SecretKey));
cloudWatch.setEndpoint("monitoring.us-east-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)
        .withMetricName("CPUUtilization")
        .withStatistics("Average")
        .withDimensions(Arrays.asList(instanceDimension))
        .withEndTime(new Date());

GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch.getMetricStatistics(request);
}

//To read the Data
for (Datapoint dp : result.getDatapoints()) {
    map.put(dp.getTimeStamp(), dp.getAverage());   //or getMaximum() or whatever Statistics you are interested in. You can also maintain a list of the statistics you are interested in. Ex: request.setStatistics(list) 
}

Note that the datapoints are NOT in order. Sort the HashMap and plot the graph.

于 2014-09-10T12:04:09.973 回答
0

I found that AWS/Billing metrics "live" only in one region - us-east-1.

Also, AWS CLI (aws cloudwatch get-metric-statistics) will errorr out if you try to grab more than 1440 data points from CloudWatch. If you encounter it set larger --period.

Similar to what you are trying to achieve using Java on Android I wrote EC2_Metrics_Plotter for OS Windows using Python/matplotlib.

于 2016-05-12T18:03:26.537 回答