Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch the CloudWatch metrics data for EC2 instances

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);
    }
like image 668
Sparkplug Avatar asked May 17 '13 07:05

Sparkplug


People also ask

How do I view EC2 instances in CloudWatch?

Click on the log group name to see the log streams. Each log stream uses the EC2 instance ID, so you know which EC2 instance logged the data: To search the logs, click the Search Log Group button.

Which AWS service collects metrics from running EC2 instances?

Amazon CloudWatch is a monitoring service for AWS cloud resources and the applications you run on AWS. You can use Amazon CloudWatch to collect and track metrics, collect and monitor log files, and set alarms.


2 Answers

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:

  • Getting Started with the AWS SDK for Android .

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

like image 80
Tom Avatar answered Sep 22 '22 21:09

Tom


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.

like image 20
user3316561 Avatar answered Sep 23 '22 21:09

user3316561