How to fetch the CloudWatch metrics data for EC2 instances

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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

回答1:

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



回答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.



回答3:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!