可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
- download AWS SDK for Android
- create your access keys for AWS (via IAM)
- read the documentation for aws-android-sdk-VERSION-cloudwatch.jar
- 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.