private double monitorInstance(AWSCredentials credential, String instanceId) {
try {
AmazonCloudWatchClient cw = new AmazonCloudWatchClient(credential) ;
long offsetInMilliseconds = 1000 * 60 * 60 * 24;
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withStartTime(new Date(new Date().getTime() - offsetInMilliseconds))
.withNamespace("AWS/EC2")
.withPeriod(60 * 60)
.withDimensions(new Dimension().withName("InstanceId").withValue(instanceId))
.withMetricName("CPUUtilization")
.withStatistics("Average", "Maximum")
.withEndTime(new Date());
GetMetricStatisticsResult getMetricStatisticsResult = cw.getMetricStatistics(request);
double avgCPUUtilization = 0;
List dataPoint = getMetricStatisticsResult.getDatapoints();
for (Object aDataPoint : dataPoint) {
Datapoint dp = (Datapoint) aDataPoint;
avgCPUUtilization = dp.getAverage();
log.info(instanceId + " instance's average CPU utilization : " + dp.getAverage());
}
return avgCPUUtilization;
} catch (AmazonServiceException ase) {
log.severe("Caught an AmazonServiceException, which means the request was made "
+ "to Amazon EC2, but was rejected with an error response for some reason.");
log.severe("Error Message: " + ase.getMessage());
log.severe("HTTP Status Code: " + ase.getStatusCode());
log.severe("AWS Error Code: " + ase.getErrorCode());
log.severe("Error Type: " + ase.getErrorType());
log.severe("Request ID: " + ase.getRequestId());
}
return 0;
}
Thursday, March 29, 2012
Monitor CPU Utilization of a Amazon EC2 instance using Amazon CloudWatch
Following code snippet show how to monitor CPU Utilization of a Amazon EC2 instance using Amazon Cloud Watch. In order to monitor the instance, Cloud Watch Monitoring should be enabled for the running instance.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment