How to get CloudWatch metric data for EC2 instances

I want to get Cloudmetrics data for my EC2 instance to draw graphs using this data and display it on my Android device. How can I do it? Is there any sample program or tutorial for the same?

Thanks in advance.

This is what I do:

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
source share
3 answers

As you noted your question using android, I assume that you want to get CloudWatch-Metrics for your EC2 instances in an Android application. So this could be a good starting point for you:

You need:

Hello

Tom

+3
source

I assume that you are only struck by reading data and plotting.

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

Please note that the data is out of order. Sort the HashMap and plot the chart.

0
source

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

In addition, AWS CLI ( aws cloudwatch get-metric-statistics ) will be an error if you try to capture more than 1440 data points from CloudWatch. If you encounter this, install the larger --period .

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

0
source

Source: https://habr.com/ru/post/1481325/


All Articles