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);
}
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.
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.
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:
Regards
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With